horn.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python
  2. # Bicycle Bell/Horn firmware
  3. # Copyright (C) 2015 Stuart Longland
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. import math
  19. import wave
  20. import struct
  21. import textwrap
  22. # Generate a horn sound
  23. F_sample = 6400 # 6.4kHz sample rate
  24. # Horn frequencies and amplitudes
  25. A_horn = 0.8
  26. FA_horn = [
  27. (485, 10**(-20.4/20)),
  28. (618, 10**(-15.4/20)),
  29. (762, 10**(-7.3/20)),
  30. (967, 10**(-13.4/20)),
  31. (1147, 10**(-15.0/20)),
  32. ]
  33. T_attack = 0.029 # Rise time, seconds
  34. T_hold = 1.000 # Hold time, seconds
  35. T_decay = 0.300 # Decay time, seconds
  36. N_samples = int(math.ceil((T_attack+T_hold+T_decay) * F_sample))
  37. T_hold_end = T_attack + T_hold
  38. duration = T_hold_end + T_decay
  39. def amplitude(t):
  40. if t < T_attack:
  41. return (1.0 + math.sin((t*math.pi)/(2*T_attack)))/2.0
  42. elif t < T_hold_end:
  43. return 1.0
  44. else:
  45. t -= T_hold_end
  46. return (1.0 + math.cos((math.pi*t)/T_decay))/2.0
  47. horn = lambda t : amplitude(t) * sum([ \
  48. a * A_horn * math.sin(2*math.pi*f*t)\
  49. for f, a in FA_horn \
  50. ])
  51. samples = []
  52. for n in range(0, int(duration*F_sample)):
  53. t = float(n)/float(F_sample)
  54. samples.append(int(127*horn(t)) + 127)
  55. assert samples[-1] >= 0, '%s <= 0' % samples[-1]
  56. assert samples[-1] <= 255, '%s > 255' % samples[-1]
  57. num_samples = len(samples)
  58. # C output
  59. with file('hornsnd.c','w') as f:
  60. f.write('#include "hornsnd.h"\n')
  61. f.write('const uint8_t horn[%s] PROGMEM = {\n' % num_samples)
  62. f.write('\n'.join(textwrap.wrap(', '.join([str(s) for s in samples]),
  63. initial_indent='\t', subsequent_indent='\t',
  64. expand_tabs=False)))
  65. f.write('\n};\n')
  66. with file('hornsnd.h','w') as f:
  67. f.write('#include <stdint.h>\n')
  68. f.write('#include <avr/pgmspace.h>\n')
  69. f.write('#define HORN_RATE (%s)\n' % F_sample)
  70. f.write('#define HORN_LOOP_OFFSET (%s)\n' % int(T_attack*F_sample))
  71. f.write('#define HORN_LOOP_SZ (%s)\n' % int(T_hold_end*F_sample))
  72. f.write('#define HORN_SZ (%s)\n' % num_samples)
  73. f.write('const uint8_t horn[%s] PROGMEM;\n' % num_samples)
  74. # For the sake of analysis, we'll dump wav audio too
  75. f = wave.open('hornsnd.wav','w')
  76. f.setnchannels(1)
  77. f.setsampwidth(1)
  78. f.setframerate(F_sample)
  79. f.setnframes(num_samples)
  80. f.setcomptype('NONE','not compressed')
  81. f.writeframes(''.join([
  82. struct.pack('B', s) for s in samples
  83. ]))
  84. f.close()