Browse Source

sox script to create tons of sample files

boyska 2 years ago
parent
commit
077ba3b060
3 changed files with 105 additions and 0 deletions
  1. 1 0
      barker/.gitignore
  2. 33 0
      barker/create-audios.sh
  3. 71 0
      barker/make-barker-audios.py

+ 1 - 0
barker/.gitignore

@@ -0,0 +1 @@
+*.wav

+ 33 - 0
barker/create-audios.sh

@@ -0,0 +1,33 @@
+#!/bin/sh
+
+lengths='2 3 4 5 7 11 13'
+for length in $lengths
+do
+    basedir="audio/barker-$(printf %02d $length)"
+    mkdir -p "$basedir"
+    base="$basedir/barker"
+    dest="$base.wav"
+    python3 make-barker-audios.py --barker-seq $length "$dest"
+    for shiftMS in 100 1000 2000
+    do
+        shiftS="$(echo "scale=2; $shiftMS / 1000" | bc)"
+        shifted="$base-shift${shiftMS}.wav"
+        sox "$dest" "$shifted" pad "${shiftS}@0"
+        for attenuation in 2 3 4 5
+        do
+            attenuated="${shifted%.wav}-att${attenuation}.wav"
+            attRatio="$(echo "scale=2; 1 / $attenuation" | bc)"
+            sox "$shifted" "$attenuated" vol "0$attRatio"
+            for noisekind in whitenoise pinknoise brownnoise
+            do
+                for noiselevel in $(seq 1 9)
+                do
+                    noised="${attenuated%.wav}-${noisekind}${noiselevel}.wav"
+                    sox -m "$attenuated" \
+                        <(sox "$attenuated" -p synth "$noisekind" vol "0.${noiselevel}") \
+                        "$noised"
+                done
+            done
+        done
+    done
+done

+ 71 - 0
barker/make-barker-audios.py

@@ -0,0 +1,71 @@
+#!/usr/bin/python3
+"""
+This script creates useful barker code files
+"""
+
+import subprocess
+import argparse
+from logging import basicConfig
+
+from barker import BARKERS
+
+
+def get_parser():
+    p = argparse.ArgumentParser()
+    p.add_argument("fname")
+    p.add_argument(
+        "--barker-sequence-length", type=int, default=5, choices=BARKERS.keys()
+    )
+    p.add_argument(
+        "--barker-freq-1", default=1000, type=int, help='Known as "+1" in barker code'
+    )
+    p.add_argument(
+        "--barker-freq-2", default=500, type=int, help='Known as "-1" in barker code'
+    )
+    p.add_argument("--duration", default=100, type=int)
+
+    p.add_argument(
+        "--log-level",
+        default="WARNING",
+        choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
+    )
+    return p
+
+
+def compress(seq):
+    """
+    Make repetition more explicit
+
+    >>> compress([1,1,10,100,100,100])
+    [[2, 1], [1, 10], [3, 100]]
+    """
+    if not seq:
+        return []
+    out = [[1, seq[0]]]
+    for elem in seq[1:]:
+        if elem == out[-1][1]:
+            out[-1][0] += 1
+        else:
+            out.append([1, elem])
+
+    return out
+
+
+def main():
+    args = get_parser().parse_args()
+    basicConfig(level=args.log_level)
+    barker_sequence = BARKERS[args.barker_sequence_length]
+    barker_seq_duration = compress(barker_sequence)
+    synth = []
+    symbol_map = {1: str(args.barker_freq_1), -1: str(args.barker_freq_2)}
+    for howmany, symbol in barker_seq_duration:
+        freq = symbol_map[symbol]
+        part = ["synth", "%.2f" % (howmany * args.duration / 1000.0), "sin", freq, ":"]
+        synth.extend(part)
+    synth.pop()  # remove trailing colon
+    cmd = ["sox", "-n", args.fname] + synth
+    subprocess.check_call(cmd)
+
+
+if __name__ == "__main__":
+    main()