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.

242 lines
8.3KB

  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. # Waveform definitions.
  30. import numpy
  31. waveforms = []
  32. """----------------------------------------------------------------------------
  33. Sine wave
  34. ----------------------------------------------------------------------------"""
  35. WAVETABLE_SIZE=1024
  36. x = numpy.arange(0, WAVETABLE_SIZE + 1) / float(WAVETABLE_SIZE)
  37. x[-1] = x[0]
  38. sine = numpy.sin(2 * numpy.pi * x)
  39. waveforms.append(('sine1024', (32767 * sine).astype(int)))
  40. WAVETABLE_SIZE=128
  41. x = numpy.arange(0, WAVETABLE_SIZE + 1) / float(WAVETABLE_SIZE)
  42. x[-1] = x[0]
  43. sine = numpy.sin(2 * numpy.pi * x)
  44. waveforms.append(('sine128', (32767 * sine).astype(int)))
  45. WAVETABLE_SIZE=64
  46. x = numpy.arange(0, WAVETABLE_SIZE + 1) / float(WAVETABLE_SIZE)
  47. x[-1] = x[0]
  48. sine = numpy.sin(2 * numpy.pi * x)
  49. waveforms.append(('sine64', (32767 * sine).astype(int)))
  50. WAVETABLE_SIZE=16
  51. x = numpy.arange(0, WAVETABLE_SIZE + 1) / float(WAVETABLE_SIZE)
  52. x[-1] = x[0]
  53. sine = numpy.sin(2 * numpy.pi * x)
  54. waveforms.append(('sine16', (32767 * sine).astype(int)))
  55. """----------------------------------------------------------------------------
  56. Band-limited waveforms
  57. ----------------------------------------------------------------------------"""
  58. SAMPLE_RATE = 48000.0
  59. WAVETABLE_SIZE = 1024
  60. def dither(x, order=0, type=numpy.int16):
  61. for i in xrange(order):
  62. x = numpy.hstack((numpy.zeros(1,), numpy.cumsum(x)))
  63. x = numpy.round(x)
  64. for i in xrange(order):
  65. x = numpy.diff(x)
  66. if any(x < numpy.iinfo(type).min) or any(x > numpy.iinfo(type).max):
  67. print 'Clipping occurred!'
  68. x[x < numpy.iinfo(type).min] = numpy.iinfo(type).min
  69. x[x > numpy.iinfo(type).max] = numpy.iinfo(type).max
  70. return x.astype(type)
  71. def scale(array, min=-32766, max=32766, center=True, dither_level=2):
  72. if center:
  73. array -= array.mean()
  74. mx = numpy.abs(array).max()
  75. array = (array + mx) / (2 * mx)
  76. array = array * (max - min) + min
  77. return dither(array, order=dither_level)
  78. # Band limited waveforms.
  79. num_zones = 20
  80. bl_parabola_tables = []
  81. wrap = numpy.arange(WAVETABLE_SIZE + 1) + WAVETABLE_SIZE / 2
  82. wrap = numpy.fmod(wrap, WAVETABLE_SIZE)
  83. quadrature = numpy.arange(WAVETABLE_SIZE + 1) + WAVETABLE_SIZE / 4
  84. quadrature = numpy.fmod(quadrature, WAVETABLE_SIZE)
  85. fill = numpy.arange(WAVETABLE_SIZE + 1)
  86. fill = numpy.fmod(fill, WAVETABLE_SIZE)
  87. ref_f0_energy = None
  88. for zone in range(num_zones):
  89. f0 = 440.0 * 2.0 ** ((8 + 8 * zone - 69) / 12.0)
  90. f0 = min(f0, SAMPLE_RATE / 2.0)
  91. period = SAMPLE_RATE / f0
  92. m = 2 * numpy.floor(period / 2) + 1.0
  93. i = numpy.arange(-WAVETABLE_SIZE / 2, WAVETABLE_SIZE / 2) / \
  94. float(WAVETABLE_SIZE)
  95. pulse = numpy.sin(numpy.pi * i * m) / (m * numpy.sin(numpy.pi * i) + 1e-9)
  96. pulse[WAVETABLE_SIZE / 2] = 1.0
  97. pulse = pulse[fill]
  98. square = numpy.cumsum(pulse - pulse[wrap])
  99. triangle = -numpy.cumsum(square[::-1] - square.mean()) / WAVETABLE_SIZE
  100. saw = -numpy.cumsum(pulse[wrap] - pulse.mean())
  101. parabola = numpy.cumsum(saw - saw.mean())
  102. scaled_parabola = scale(parabola[quadrature])
  103. f0_energy = numpy.abs(numpy.fft.rfft(scaled_parabola)[1])
  104. if ref_f0_energy is None:
  105. ref_f0_energy = f0_energy
  106. scaled_parabola = scaled_parabola / f0_energy * ref_f0_energy
  107. scaled_parabola *= min(1.0, 32767 / scaled_parabola.max())
  108. bl_parabola_tables.append(
  109. ('bandlimited_parabola_%d' % zone, scaled_parabola))
  110. waveforms.extend(bl_parabola_tables)
  111. """----------------------------------------------------------------------------
  112. Waveshaper for audio rate
  113. ----------------------------------------------------------------------------"""
  114. WAVESHAPER_SIZE = 1024
  115. x = numpy.arange(0, WAVESHAPER_SIZE + 1) / float(WAVESHAPER_SIZE)
  116. linear = x
  117. sin = (1.0 - numpy.cos(numpy.pi * x)) / 2.0
  118. tan = numpy.arctan(8 * numpy.cos(numpy.pi * x))
  119. scale = tan.max()
  120. tan = (1.0 - tan / scale) / 2.0
  121. inverse_sin = numpy.arccos(1 - 2 * x) / numpy.pi
  122. inverse_tan = numpy.arccos(numpy.tan(scale * (1.0 - 2.0 * x)) / 8.0) / numpy.pi
  123. def audio_rate_flip(x):
  124. x = numpy.array(list(-x[WAVESHAPER_SIZE:0:-1]) + list(x))
  125. return numpy.round((x * 32767.0)).astype(int)
  126. audio_rate_tables = []
  127. audio_rate_tables.append(('inverse_tan_audio', audio_rate_flip(inverse_tan)))
  128. audio_rate_tables.append(('inverse_sin_audio', audio_rate_flip(inverse_sin)))
  129. audio_rate_tables.append(('linear_audio', audio_rate_flip(linear)))
  130. audio_rate_tables.append(('sin_audio', audio_rate_flip(sin)))
  131. audio_rate_tables.append(('tan_audio', audio_rate_flip(tan)))
  132. waveforms.extend(audio_rate_tables)
  133. """----------------------------------------------------------------------------
  134. Waveshaper for control rate
  135. ----------------------------------------------------------------------------"""
  136. WAVESHAPER_SIZE = 512
  137. x = numpy.arange(0, WAVESHAPER_SIZE + 1) / float(WAVESHAPER_SIZE)
  138. linear = x
  139. sin = (1.0 - numpy.cos(numpy.pi * x)) / 2.0
  140. inverse_sin = numpy.arccos(1 - 2 * x) / numpy.pi
  141. inverse_sin = (((inverse_sin*2-1) ** 3)+1)*0.5 # for more contrast
  142. expo = 1.0 - numpy.exp(-3 * x)
  143. expo_max = expo.max()
  144. expo = 1.0 - (1.0 - expo) ** 2 # for more contrast
  145. expo /= expo.max()
  146. expo_flipped = 1.0 - numpy.exp(-3 * (1 - x))
  147. expo_flipped = 1.0 - (1.0 - expo_flipped) ** 2 # for more contrast
  148. expo_flipped /= expo_flipped.max()
  149. log = numpy.log(1.0 - x * expo_max) / -3.0
  150. log -= log.min()
  151. log /= log.max()
  152. log = log ** 2 # for more contrast
  153. log_flipped = numpy.log(1.0 - (1 - x) * expo_max) / -3.0
  154. log_flipped -= log_flipped.min()
  155. log_flipped /= log_flipped.max()
  156. log_flipped = log_flipped ** 2 # for more contrast
  157. def control_rate_flip(x, y):
  158. x = numpy.array(list(x) + list(y[1:]))
  159. return numpy.round((x * 32767.0)).astype(int)
  160. control_rate_tables = []
  161. control_rate_tables.append(
  162. ('reversed_control', control_rate_flip(log, 1.0 - log)))
  163. control_rate_tables.append(
  164. ('spiky_exp_control', control_rate_flip(log, log_flipped)))
  165. control_rate_tables.append(
  166. ('spiky_control', control_rate_flip(inverse_sin, 1.0 - inverse_sin)))
  167. control_rate_tables.append(
  168. ('linear_control', control_rate_flip(linear, 1.0 - linear)))
  169. control_rate_tables.append(
  170. ('bump_control', control_rate_flip(sin, 1.0 - sin)))
  171. control_rate_tables.append(
  172. ('bump_exp_control', control_rate_flip(expo, expo_flipped)))
  173. control_rate_tables.append(
  174. ('normal_control', control_rate_flip(expo, 1.0 - expo)))
  175. waveforms.extend(control_rate_tables)
  176. """----------------------------------------------------------------------------
  177. Post waveshaper
  178. ----------------------------------------------------------------------------"""
  179. WAVESHAPER_SIZE = 1024
  180. x = numpy.arange(0, WAVESHAPER_SIZE + 1) / (WAVESHAPER_SIZE / 2.0) - 1.0
  181. x[-1] = x[-2]
  182. sine = numpy.sin(8 * numpy.pi * x)
  183. window = numpy.exp(-x * x * 4) ** 2
  184. bipolar_fold = sine * window + numpy.arctan(3 * x) * (1 - window)
  185. bipolar_fold /= numpy.abs(bipolar_fold).max()
  186. waveforms.append(('bipolar_fold', numpy.round(32767 * bipolar_fold)))
  187. x = numpy.arange(0, WAVESHAPER_SIZE + 1) / float(WAVESHAPER_SIZE)
  188. x[-1] = x[-2]
  189. sine = numpy.sin(8 * numpy.pi * x)
  190. window = numpy.exp(-x * x * 4) ** 2
  191. unipolar_fold = (0.5 * sine + 2 * x) * window + numpy.arctan(4 * x) * (1 - window)
  192. unipolar_fold /= numpy.abs(unipolar_fold).max()
  193. waveforms.append(('unipolar_fold', numpy.round(32767 * unipolar_fold)))