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.

91 lines
3.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. sample_rate = 48000.0
  32. lookup_tables = []
  33. lookup_tables_32 = []
  34. excursion = float(1 << 32)
  35. a4_midi = 69
  36. a4_pitch = 440.0
  37. notes = numpy.arange(0 * 128.0, 12 * 128.0 + 16, 16) / 128.0
  38. pitches = a4_pitch * 2 ** ((notes - a4_midi) / 12)
  39. increments = excursion / sample_rate * pitches
  40. lookup_tables_32.append(
  41. ('increments', increments.astype(int)))
  42. """----------------------------------------------------------------------------
  43. Filter coefficients
  44. ----------------------------------------------------------------------------"""
  45. cutoff = 440.0 * 2 ** ((numpy.arange(-256, 257) - 69) / 12.0)
  46. f = cutoff / sample_rate
  47. f[f > 0.5] = 0.5
  48. f = 2 * numpy.pi * f
  49. f = 1 - numpy.exp(-numpy.arccosh(2 - numpy.cos(f)))
  50. lookup_tables_32.append(
  51. ('cutoff', numpy.maximum(1, f * 32767.0 * 65536.0))
  52. )
  53. """----------------------------------------------------------------------------
  54. Attenuverter curve
  55. ----------------------------------------------------------------------------"""
  56. ATTENUVERTER_CURVE_SIZE = 256
  57. x = numpy.arange(0, ATTENUVERTER_CURVE_SIZE + 1) / float(ATTENUVERTER_CURVE_SIZE)
  58. x[-1] = x[-2]
  59. x = x * 1.05 - 0.05
  60. x = (x + numpy.abs(x)) / 2.0
  61. x_cosine = (1.0 - numpy.cos((x ** 1.5) * numpy.pi)) / 2.0
  62. x_power = x ** 2.0
  63. attenuverter_curve = (x_cosine + 0.5 * x_power) / 1.5
  64. lookup_tables.append(
  65. ('attenuverter_curve', numpy.round(65535 * attenuverter_curve)))
  66. """----------------------------------------------------------------------------
  67. Slope compression table (give more resolution in the edges)
  68. ----------------------------------------------------------------------------"""
  69. SLOPE_COMPRESSION_SIZE = 256
  70. x = numpy.arange(0, SLOPE_COMPRESSION_SIZE + 1) / (SLOPE_COMPRESSION_SIZE / 2.0)
  71. x -= 1.0
  72. sine = numpy.sin(x * numpy.pi / 2)
  73. lookup_tables.append(('slope_compression', numpy.round(32767.5 * (sine + 1.0))))