make-barker-audios.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/python3
  2. """
  3. This script creates useful barker code files
  4. """
  5. import subprocess
  6. import argparse
  7. from logging import basicConfig
  8. from barker import BARKERS
  9. def get_parser():
  10. p = argparse.ArgumentParser()
  11. p.add_argument("fname")
  12. p.add_argument(
  13. "--barker-sequence-length", type=int, default=5, choices=BARKERS.keys()
  14. )
  15. p.add_argument(
  16. "--barker-freq-1", default=880, type=int, help='Known as "+1" in barker code'
  17. )
  18. p.add_argument(
  19. "--barker-freq-2", default=440, type=int, help='Known as "-1" in barker code'
  20. )
  21. p.add_argument("--duration", default=100, type=int)
  22. p.add_argument("--pipupa", action="store_true", default=False)
  23. p.add_argument(
  24. "--log-level",
  25. default="WARNING",
  26. choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
  27. )
  28. return p
  29. def compress(seq):
  30. """
  31. Make repetition more explicit
  32. >>> compress([1,1,10,100,100,100])
  33. [[2, 1], [1, 10], [3, 100]]
  34. """
  35. if not seq:
  36. return []
  37. out = [[1, seq[0]]]
  38. for elem in seq[1:]:
  39. if elem == out[-1][1]:
  40. out[-1][0] += 1
  41. else:
  42. out.append([1, elem])
  43. return out
  44. # PIPUPA_SEQ = [0, 300, 1000, 2000, 0]
  45. # DO - MIb - FA
  46. PIPUPA_SEQ = [0, 261, 1244, 2793, 0]
  47. PIPUPA_TONE_DURATION = 500 # milliseconds
  48. def main():
  49. args = get_parser().parse_args()
  50. basicConfig(level=args.log_level)
  51. barker_sequence = BARKERS[args.barker_sequence_length]
  52. barker_seq_duration = compress(barker_sequence)
  53. synth = []
  54. symbol_map = {1: str(args.barker_freq_1), -1: str(args.barker_freq_2)}
  55. for howmany, symbol in barker_seq_duration:
  56. freq = symbol_map[symbol]
  57. part = ["synth", "%.2f" % (howmany * args.duration / 1000.0),
  58. "sin", freq, ":"]
  59. synth.extend(part)
  60. if args.pipupa:
  61. for freq in PIPUPA_SEQ:
  62. part = ["synth", "%.2f" % (howmany * PIPUPA_TONE_DURATION / 1000.0),
  63. "sin", str(freq), ":"]
  64. synth.extend(part)
  65. synth.pop() # remove trailing colon
  66. cmd = ["sox", "-n", args.fname] + synth
  67. subprocess.check_call(cmd)
  68. if __name__ == "__main__":
  69. main()