summaryrefslogtreecommitdiff
path: root/animism-align/cli/commands
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2020-06-23 23:18:07 +0200
committerJules Laplace <julescarbon@gmail.com>2020-06-23 23:18:07 +0200
commit3cf70771cb45cc16ec33ffe44e7a1a4799d8f395 (patch)
tree55f0edb53141d5f043b486d722f507bfd94abdea /animism-align/cli/commands
parent014816dc724c1be60b7dd28d4e608c89b4ed451c (diff)
adding web app base
Diffstat (limited to 'animism-align/cli/commands')
-rw-r--r--animism-align/cli/commands/peaks/parse.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/animism-align/cli/commands/peaks/parse.py b/animism-align/cli/commands/peaks/parse.py
new file mode 100644
index 0000000..dc9a0a2
--- /dev/null
+++ b/animism-align/cli/commands/peaks/parse.py
@@ -0,0 +1,39 @@
+"""
+Extract peaks from an MP3 file.
+"""
+
+import click
+
+from app.site.builder import build_site, build_file
+
+@click.command()
+@click.option('-i', '--input', 'fp_in', required=False,
+ help='Input file')
+@click.pass_context
+def cli(ctx, fp_in):
+ """Extract peaks from an MP3 file.
+ """
+ import os
+ import math
+ import librosa
+ import numpy
+ import json
+
+ from app.settings import app_cfg
+
+ print(f"Loading {fp_in}")
+ y, sr = librosa.load(fp_in, sr=None)
+
+ sr_10 = math.floor(sr / 10)
+ steps = math.floor(y.shape[0] / sr_10)
+
+ peaks = numpy.ndarray(steps * 2)
+
+ for i in range(steps):
+ offset = i * sr_10
+ slice = y[offset:offset + sr_10]
+ peaks[i * 2] = float('%.3f' % slice.min())
+ peaks[i * 2 + 1] = float('%.3f' % slice.max())
+
+ with open(os.path.join(app_cfg.DIR_DATA_STORE, 'peaks.json'), 'w') as fp_out:
+ json.dump(peaks.tolist(), fp_out, separators=(',', ':'))