1#!/usr/bin/python2.5
2#
3# Copyright 2017 Emilie Gillet.
4#
5# Author: Emilie Gillet (emilie.o.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
31import numpy
32import pylab
33
34lookup_tables = []
35lookup_tables_i16 = []
36
37sample_rate = 48000
38
39"""----------------------------------------------------------------------------
40Sine table.
41----------------------------------------------------------------------------"""
42
43size = 1024
44t = numpy.arange(0, size + size / 4 + 1) / float(size) * numpy.pi * 2
45lookup_tables.append(('sine', numpy.sin(t)))
46
47
48
49"""----------------------------------------------------------------------------
50Waveshaper for audio rate
51----------------------------------------------------------------------------"""
52
53WAVESHAPER_SIZE = 1024
54
55x = numpy.arange(0, WAVESHAPER_SIZE) / float(WAVESHAPER_SIZE)
56
57linear = x
58sin = 0.5 - 0.5 * numpy.cos(x * numpy.pi)
59tan = numpy.arctan(8 * numpy.cos(numpy.pi * x))
60tan_scale = tan.max()
61fade_crop = numpy.minimum(1.0, 4.0 - 4.0 * x)
62bump = (1.0 - numpy.cos(numpy.pi * x * 1.5)) * (1.0 - numpy.cos(numpy.pi * fade_crop)) / 4.5
63inverse_sin = numpy.arccos(1 - 2 * x) / numpy.pi
64inverse_tan = numpy.arccos(numpy.tan(tan_scale * (1.0 - 2.0 * x)) / 8.0) / numpy.pi
65inverse_tan[0] = 0
66
67def scale(x):
68  x = numpy.array(list(x) + [x[-1]])
69  # pylab.plot(x)
70  # pylab.show()
71  return list(numpy.round((x * 32767.0)).astype(int))
72
73wavetable = []
74wavetable += scale(inverse_tan)
75wavetable += scale(inverse_sin)
76wavetable += scale(linear)
77wavetable += scale(sin)
78wavetable += scale(bump)
79
80"""----------------------------------------------------------------------------
81Waveshaper for control rate
82----------------------------------------------------------------------------"""
83
84x = numpy.arange(0, WAVESHAPER_SIZE / 2) / float(WAVESHAPER_SIZE / 2)
85linear = x
86sin = (1.0 - numpy.cos(numpy.pi * x)) / 2.0
87inverse_sin = numpy.arccos(1 - 2 * x) / numpy.pi
88expo = 1.0 - numpy.exp(-5 * x)
89expo_max = expo.max()
90expo /= expo_max
91log = numpy.log(1.0 - x * expo_max) / -5.0
92
93def scale_flip(x, flip=True):
94  if flip:
95    y = x[::-1]
96  else:
97    y = 1.0 - x
98  x = numpy.array(list(x) + [1] + list(y))
99  return list(numpy.round((x * 32767.0)).astype(int))
100
101wavetable += scale_flip(log, False)
102wavetable += scale_flip(log, True)
103wavetable += scale_flip(inverse_sin, False)
104wavetable += scale_flip(linear, False)
105wavetable += scale_flip(sin, False)
106wavetable += scale_flip(expo, True)
107wavetable += scale_flip(expo, False)
108
109lookup_tables_i16.append(('wavetable', wavetable))
110
111
112
113"""----------------------------------------------------------------------------
114Post waveshaper
115----------------------------------------------------------------------------"""
116
117x = numpy.arange(0, WAVESHAPER_SIZE + 4) / (WAVESHAPER_SIZE / 2.0) - 1.0
118x[-1] = x[-2]
119sine = numpy.sin(8 * numpy.pi * x)
120window = numpy.exp(-x * x * 4) ** 2
121bipolar_fold = sine * window + numpy.arctan(3 * x) * (1 - window)
122bipolar_fold /= numpy.abs(bipolar_fold).max()
123lookup_tables.append(('bipolar_fold', bipolar_fold))
124
125x = numpy.arange(0, WAVESHAPER_SIZE + 4) / float(WAVESHAPER_SIZE)
126x[-1] = x[-3]
127x[-2] = x[-3]
128sine = numpy.sin(16 * numpy.pi * x)
129window = numpy.exp(-x * x * 4) ** 2
130unipolar_fold = (0.38 * sine + 4 * x) * window + numpy.arctan(4 * x) * (1 - window)
131unipolar_fold /= numpy.abs(unipolar_fold).max()
132lookup_tables.append(('unipolar_fold', unipolar_fold))
133