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.

154 lines
4.8KB

  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 = 31089
  37. excursion = 1 << 32
  38. num_values = 257
  39. # Create lookup table for envelope times (x^0.25).
  40. max_time = 12.0 # seconds
  41. min_time = 0.0005
  42. gamma = 0.25
  43. min_increment = excursion / (max_time * sample_rate)
  44. max_increment = excursion / (min_time * sample_rate)
  45. rates = numpy.linspace(numpy.power(max_increment, -gamma),
  46. numpy.power(min_increment, -gamma), num_values)
  47. values = numpy.power(rates, -1/gamma).astype(int)
  48. lookup_tables_32.append(
  49. ('env_increments', values)
  50. )
  51. """----------------------------------------------------------------------------
  52. Envelope curves
  53. -----------------------------------------------------------------------------"""
  54. env_linear = numpy.arange(0, 257.0) / 256.0
  55. env_linear[-1] = env_linear[-2]
  56. env_quartic = env_linear ** 3.32
  57. env_expo = 1.0 - numpy.exp(-4 * env_linear)
  58. lookup_tables.append(('env_linear', env_linear / env_linear.max() * 65535.0))
  59. lookup_tables.append(('env_expo', env_expo / env_expo.max() * 65535.0))
  60. lookup_tables.append(('env_quartic', env_quartic / env_quartic.max() * 65535.0))
  61. lookup_tables.append(('square_root', (env_linear ** 0.5) * 65535.0))
  62. """----------------------------------------------------------------------------
  63. SVF coefficients
  64. ----------------------------------------------------------------------------"""
  65. cutoff = 440.0 * 2 ** ((numpy.arange(0, 257) - 69) / 12.0)
  66. f = cutoff / sample_rate
  67. f[f > 1 / 8.0] = 1 / 8.0
  68. f = 2 * numpy.sin(numpy.pi * f)
  69. resonance = numpy.arange(0, 257) / 257.0
  70. damp = numpy.minimum(2 * (1 - resonance ** 0.25),
  71. numpy.minimum(2, 2 / f - f * 0.5))
  72. lookup_tables.append(
  73. ('svf_cutoff', f * 32767.0)
  74. )
  75. lookup_tables.append(
  76. ('svf_damp', damp * 32767.0)
  77. )
  78. """----------------------------------------------------------------------------
  79. Vactrol attack/decay time
  80. ----------------------------------------------------------------------------"""
  81. vactrol_time = 0.001 * 10 ** (numpy.arange(0, 128 * 5) / 128.0)
  82. vactrol_time[0] = 0.0001
  83. vactrol_time[1] = 0.0002
  84. vactrol_time[2] = 0.0005
  85. vactrol_time[3] = 0.001
  86. filter_coefficients = 1.0 - numpy.exp(-1 / (vactrol_time * sample_rate))
  87. lookup_tables_32.append(
  88. ('lp_coefficients', excursion / 2 * filter_coefficients)
  89. )
  90. """----------------------------------------------------------------------------
  91. 2164 gain
  92. ----------------------------------------------------------------------------"""
  93. gains = (numpy.arange(0, 257) / 256.0) * 3.3
  94. gains =10 ** (-1.5 * gains)
  95. lookup_tables.append(
  96. ('2164_gain', gains * 32767.0)
  97. )
  98. """----------------------------------------------------------------------------
  99. Compressor tables
  100. ----------------------------------------------------------------------------"""
  101. t = (numpy.arange(0, 257) / 256.0)
  102. lookup_tables_32 += [
  103. ('exp2', 65536.0 * 2 ** t),
  104. ('log2', 65536.0 * numpy.log2(256 + 256 * t))]
  105. lookup_tables += [
  106. ('compressor_ratio', 256 / (24 * t * t + 1)),
  107. ('soft_knee', (t ** 3.0) * 65535.0)
  108. ]
  109. """----------------------------------------------------------------------------
  110. Rate control
  111. ----------------------------------------------------------------------------"""
  112. t = numpy.arange(0, 257) / 256.0
  113. t /= (20 / 100.0 / 3.3)
  114. f = 2.0 ** t
  115. f /= f[-1]
  116. f *= 0.02 * (1 << 24)
  117. lookup_tables_32.append(('lorenz_rate', f))