12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #!/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=880, type=int, help='Known as "+1" in barker code'
- )
- p.add_argument(
- "--barker-freq-2", default=440, type=int, help='Known as "-1" in barker code'
- )
- p.add_argument("--duration", default=100, type=int)
- p.add_argument("--pipupa", action="store_true", default=False)
- 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
- # PIPUPA_SEQ = [0, 300, 1000, 2000, 0]
- # DO - MIb - FA
- PIPUPA_SEQ = [0, 261, 1244, 2793, 0]
- PIPUPA_TONE_DURATION = 500 # milliseconds
- 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)
- if args.pipupa:
- for freq in PIPUPA_SEQ:
- part = ["synth", "%.2f" % (howmany * PIPUPA_TONE_DURATION / 1000.0),
- "sin", str(freq), ":"]
- synth.extend(part)
- synth.pop() # remove trailing colon
- cmd = ["sox", "-n", args.fname] + synth
- subprocess.check_call(cmd)
- if __name__ == "__main__":
- main()
|