You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
7.2KB

  1. #!/usr/bin/python2.5
  2. #
  3. # Copyright 2014 Olivier Gillet.
  4. #
  5. # Author: Olivier Gillet (ol.gillet@gmail.com)
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. # See http://creativecommons.org/licenses/MIT/ for more information.
  26. #
  27. # -----------------------------------------------------------------------------
  28. #
  29. # Lookup table definitions.
  30. import numpy
  31. lookup_tables = []
  32. int16_lookup_tables = []
  33. uint32_lookup_tables = []
  34. SAMPLE_RATE = 32000.0
  35. """----------------------------------------------------------------------------
  36. Sine table
  37. ----------------------------------------------------------------------------"""
  38. WAVETABLE_SIZE = 4096
  39. t = numpy.arange(0.0, WAVETABLE_SIZE + 1) / WAVETABLE_SIZE
  40. t[-1] = t[0]
  41. x = numpy.sin(2 * numpy.pi * t)
  42. lookup_tables += [('sine', x)]
  43. """----------------------------------------------------------------------------
  44. Coefficients for approximate filter, 32Hz to 16kHz ; Q = 0.5 to 500
  45. ----------------------------------------------------------------------------"""
  46. frequency = 32 * (10 ** (2.7 * numpy.arange(0, 257) / 256))
  47. frequency /= SAMPLE_RATE
  48. frequency[frequency >= 0.499] = 0.499
  49. g = numpy.tan(numpy.pi * frequency)
  50. r = 2.0
  51. h = 1.0 / (1.0 + r * g + g * g)
  52. gain = (0.42 / frequency) * (4 ** (frequency * frequency))
  53. r = 1 / (0.5 * 10 ** (3.0 * numpy.arange(0, 257) / 256))
  54. lookup_tables += [
  55. ('approx_svf_gain', gain),
  56. ('approx_svf_g', g),
  57. ('approx_svf_r', r),
  58. ('approx_svf_h', h)]
  59. """----------------------------------------------------------------------------
  60. Exponentials covering several decades in 256 steps, with safeguard
  61. ----------------------------------------------------------------------------"""
  62. x = numpy.arange(0, 257) / 256.0
  63. lookup_tables += [('4_decades', 10 ** (4 * x))]
  64. """----------------------------------------------------------------------------
  65. 3dB/V table for accent/strength control
  66. ----------------------------------------------------------------------------"""
  67. x = numpy.arange(0, 257) / 256.0
  68. coarse = 10 ** (1.5 * (x - 0.5))
  69. fine = 10 ** (x * numpy.log10(coarse[1] / coarse[0]))
  70. lookup_tables += [('accent_gain_coarse', coarse)]
  71. lookup_tables += [('accent_gain_fine', fine)]
  72. """----------------------------------------------------------------------------
  73. dB brightness table
  74. ----------------------------------------------------------------------------"""
  75. x = numpy.arange(0, 513) / 512.0
  76. x[0] = x[1]
  77. x[-1] = x[-2]
  78. brightness = (9 + numpy.log2(x)) / 9
  79. int16_lookup_tables += [('db_led_brightness', brightness * 256.0)]
  80. """----------------------------------------------------------------------------
  81. Stiffness table.
  82. ----------------------------------------------------------------------------"""
  83. geometry = numpy.arange(0, 257) / 256.0
  84. stiffness = geometry + 0
  85. for i, g in enumerate(geometry):
  86. if g < 0.25:
  87. g = 0.25 - g
  88. stiffness[i] = -g * 0.25
  89. elif g < 0.3:
  90. stiffness[i] = 0.0
  91. elif g < 0.9:
  92. g -= 0.3
  93. g /= 0.6
  94. stiffness[i] = 0.01 * 10 ** (g * 2.005) - 0.01
  95. else:
  96. g -= 0.9
  97. g /= 0.1
  98. g *= g
  99. stiffness[i] = 1.5 - numpy.cos(g * numpy.pi) / 2.0
  100. stiffness[-1] = 2.0
  101. stiffness[-2] = 2.0
  102. lookup_tables += [('stiffness', stiffness)]
  103. """----------------------------------------------------------------------------
  104. Envelope increments and curves.
  105. ----------------------------------------------------------------------------"""
  106. TABLE_SIZE = 256
  107. t = numpy.arange(0.0, TABLE_SIZE + 2) / TABLE_SIZE
  108. t[-1] = t[-2] = 1.0
  109. control_rate = SAMPLE_RATE / 16.0
  110. max_time = 8.0 # seconds
  111. min_time = 0.0005
  112. gamma = 0.175
  113. min_increment = 1.0 / (max_time * control_rate)
  114. max_increment = 1.0 / (min_time * control_rate)
  115. a = numpy.power(max_increment, -gamma)
  116. b = numpy.power(min_increment, -gamma)
  117. lookup_tables.append(
  118. ('env_increments', numpy.power(a + (b - a) * t, -1 / gamma))
  119. )
  120. env_linear = t
  121. env_quartic = t ** 3.32
  122. env_expo = 1.0 - numpy.exp(-4 * t)
  123. lookup_tables.append(('env_linear', env_linear / env_linear.max()))
  124. lookup_tables.append(('env_expo', env_expo / env_expo.max()))
  125. lookup_tables.append(('env_quartic', env_quartic / env_quartic.max()))
  126. """----------------------------------------------------------------------------
  127. MIDI to normalized frequency table.
  128. ----------------------------------------------------------------------------"""
  129. TABLE_SIZE = 256
  130. midi_note = numpy.arange(0, TABLE_SIZE) - 48
  131. frequency = 440 * 2 ** ((midi_note - 69) / 12.0)
  132. max_frequency = min(12000, SAMPLE_RATE / 2)
  133. frequency[frequency >= max_frequency] = max_frequency
  134. frequency /= SAMPLE_RATE
  135. semitone = 2 ** (numpy.arange(0, TABLE_SIZE) / 256.0 / 12.0)
  136. lookup_tables.append(('midi_to_f_high', frequency))
  137. lookup_tables.append(('midi_to_increment_high', frequency * (1 << 32)))
  138. lookup_tables.append(('midi_to_f_low', semitone))
  139. """----------------------------------------------------------------------------
  140. Quantizer for FM frequencies.
  141. ----------------------------------------------------------------------------"""
  142. fm_frequency_ratios = [ 0.5, 0.5 * 2 ** (16 / 1200.0),
  143. numpy.sqrt(2) / 2, numpy.pi / 4, 1.0, 1.0 * 2 ** (16 / 1200.0), numpy.sqrt(2),
  144. numpy.pi / 2, 7.0 / 4, 2, 2 * 2 ** (16 / 1200.0), 9.0 / 4, 11.0 / 4,
  145. 2 * numpy.sqrt(2), 3, numpy.pi, numpy.sqrt(3) * 2, 4, numpy.sqrt(2) * 3,
  146. numpy.pi * 3 / 2, 5, numpy.sqrt(2) * 4, 8]
  147. scale = []
  148. for ratio in fm_frequency_ratios:
  149. ratio = 12 * numpy.log2(ratio)
  150. scale.extend([ratio, ratio, ratio])
  151. target_size = int(2 ** numpy.ceil(numpy.log2(len(scale))))
  152. while len(scale) < target_size:
  153. gap = numpy.argmax(numpy.diff(scale))
  154. scale = scale[:gap + 1] + [(scale[gap] + scale[gap + 1]) / 2] + \
  155. scale[gap + 1:]
  156. scale.append(scale[-1])
  157. lookup_tables.append(
  158. ('fm_frequency_quantizer', scale)
  159. )
  160. """----------------------------------------------------------------------------
  161. Quantizer for FM frequencies.
  162. ----------------------------------------------------------------------------"""
  163. detune_ratios = [-24, -12, -11.95, -5.0, -0.05, 0.0, 0.05, 7.0, 12.0, 19.0, 24.0]
  164. scale = []
  165. for ratio in detune_ratios:
  166. scale.extend([ratio, ratio, ratio])
  167. target_size = int(2 ** numpy.ceil(numpy.log2(len(scale))))
  168. while len(scale) < target_size:
  169. gap = numpy.argmax(numpy.diff(scale))
  170. scale = scale[:gap + 1] + [(scale[gap] + scale[gap + 1]) / 2] + \
  171. scale[gap + 1:]
  172. scale.append(scale[-1])
  173. lookup_tables.append(
  174. ('detune_quantizer', scale)
  175. )