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.

158 lines
5.1KB

  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. """----------------------------------------------------------------------------
  32. LFO and envelope increments.
  33. ----------------------------------------------------------------------------"""
  34. lookup_tables = []
  35. lookup_tables_32 = []
  36. sample_rate = 48000
  37. min_frequency = 1.0 / 32.0 # Hertz
  38. max_frequency = 160.0 # Hertz
  39. excursion = 1 << 32
  40. num_values = 257
  41. min_increment = excursion * min_frequency / sample_rate
  42. max_increment = excursion * max_frequency / sample_rate
  43. rates = numpy.linspace(numpy.log(min_increment),
  44. numpy.log(max_increment), num_values)
  45. lookup_tables_32.append(
  46. ('lfo_increments', numpy.exp(rates).astype(int))
  47. )
  48. # Create lookup table for envelope times (x^0.25).
  49. max_time = 8.0 # seconds
  50. min_time = 0.0005
  51. gamma = 0.175
  52. min_increment = excursion / (max_time * sample_rate)
  53. max_increment = excursion / (min_time * sample_rate)
  54. rates = numpy.linspace(numpy.power(max_increment, -gamma),
  55. numpy.power(min_increment, -gamma), num_values)
  56. values = numpy.power(rates, -1/gamma).astype(int)
  57. lookup_tables_32.append(
  58. ('env_increments', values)
  59. )
  60. # Create table for pitch.
  61. a4_midi = 69
  62. a4_pitch = 440.0
  63. highest_octave = 116
  64. notes = numpy.arange(
  65. highest_octave * 128.0,
  66. (highest_octave + 12) * 128.0 + 16,
  67. 16)
  68. pitches = a4_pitch * 2 ** ((notes - a4_midi * 128) / (128 * 12))
  69. increments = excursion / sample_rate * pitches
  70. lookup_tables_32.append(
  71. ('oscillator_increments', increments.astype(int)))
  72. """----------------------------------------------------------------------------
  73. Pulse delay values.
  74. ----------------------------------------------------------------------------"""
  75. sample_rate = 48000 / 8
  76. min_delay = 0.001
  77. max_delay = 10.0
  78. gamma = 0.3
  79. times = numpy.linspace(numpy.power(min_delay, gamma),
  80. numpy.power(max_delay, gamma), num_values)
  81. times = numpy.power(times, 1 / gamma)
  82. lookup_tables.append(
  83. ('delay_times', (times * sample_rate).astype(int))
  84. )
  85. """----------------------------------------------------------------------------
  86. Gravity factors.
  87. ----------------------------------------------------------------------------"""
  88. sample_rate = 48000
  89. min_delay = 0.015
  90. max_delay = 2.0
  91. gamma = 0.2
  92. times = numpy.linspace(numpy.power(min_delay, gamma),
  93. numpy.power(max_delay, gamma), num_values)
  94. times = numpy.power(times, 1 / gamma)
  95. lookup_tables.append(
  96. ('gravity', ((times * sample_rate / (2 * 65536.0)) ** -2).astype(int))
  97. )
  98. """----------------------------------------------------------------------------
  99. Envelope curves
  100. -----------------------------------------------------------------------------"""
  101. env_linear = numpy.arange(0, 257.0) / 256.0
  102. env_linear[-1] = env_linear[-2]
  103. env_quartic = env_linear ** 3.32
  104. env_expo = 1.0 - numpy.exp(-4 * env_linear)
  105. lookup_tables.append(('env_linear', env_linear / env_linear.max() * 65535.0))
  106. lookup_tables.append(('env_expo', env_expo / env_expo.max() * 65535.0))
  107. lookup_tables.append(('env_quartic', env_quartic / env_quartic.max() * 65535.0))
  108. raised_cosine = 0.5 - numpy.cos(env_linear * numpy.pi) / 2
  109. lookup_tables.append(('raised_cosine', raised_cosine * 65535.0))
  110. """----------------------------------------------------------------------------
  111. SVF coefficients
  112. ----------------------------------------------------------------------------"""
  113. cutoff = 440.0 * 2 ** ((numpy.arange(0, 257) - 69) / 12.0)
  114. f = cutoff / sample_rate
  115. f[f > 1 / 8.0] = 1 / 8.0
  116. f = 2 * numpy.sin(numpy.pi * f)
  117. resonance = numpy.arange(0, 257) / 257.0
  118. damp = numpy.minimum(2 * (1 - resonance ** 0.25),
  119. numpy.minimum(2, 2 / f - f * 0.5))
  120. lookup_tables.append(
  121. ('svf_cutoff', f * 32767.0)
  122. )
  123. lookup_tables.append(
  124. ('svf_damp', damp * 32767.0)
  125. )
  126. lookup_tables.append(
  127. ('svf_scale', ((damp / 2) ** 0.5) * 32767.0)
  128. )