-
Notifications
You must be signed in to change notification settings - Fork 6
/
denver.lua
207 lines (178 loc) · 6.34 KB
/
denver.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
local denver = {
_VERSION = 'denver v1.0.2',
_DESCRIPTION = 'An audio generation module for LÖVE2D',
_URL = 'http://github.com/superzazu/denver.lua',
_LICENSE = [[
Copyright (c) 2015 Nicolas Allemand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
}
denver.rate = 44100
denver.bits = 16
denver.channel = 1
denver.base_freq = 440 -- A4 = 440
local oscillators = {}
-- returns a LOVE2D audio source with a waveform
-- examples :
-- s = denver.get({waveform='sinus', frequency=440, length=1})
-- s = denver.get{waveform='square', frequency='E#3'}
--
-- note: creates one period-sample by default; that allows user to loop the
-- sample (and to have a minimum of RAM used)
denver.get = function (args, ...)
local waveform = args.waveform or 'sinus'
local frequency = denver.noteToFrequency(args.frequency)
or args.frequency or 440
local length = args.length or 1 / frequency
-- creating an empty sample
local sound_data = love.sound.newSoundData(length * denver.rate,
denver.rate,
denver.bits,
denver.channel)
-- setting up the oscillator
if not oscillators[waveform] then
error('waveform "'.. waveform ..'"" is not supported.', 2)
end
local osc = oscillators[waveform](frequency, ...)
-- filling the sample with values
local amplitude = 0.2
for i = 0, length * denver.rate - 1 do
local sample = osc(freq, denver.rate) * amplitude
sound_data:setSample(i, sample)
end
return sound_data
end
-- you can add your own waves
denver.set = function (wave_type, osc)
oscillators[wave_type] = osc
end
-- takes a note in parameter and returns a frequency
denver.noteToFrequency = function (note_str)
if not note_str or type(note_str) ~= 'string' then
return
end
local note_semitones = {C=-9, D=-7, E=-5, F=-4, G=-2, A=0, B=2}
local semitones = note_semitones[note_str:sub(1, 1)]
if not semitones then
return
end
local octave = 4
local alteration = 0
if note_str:len() == 2 then
octave = note_str:sub(2, 2)
elseif note_str:len() == 3 then -- # or flat
if note_str:sub(2, 2) == '#' then
semitones = semitones + 1
elseif note_str:sub(2, 2) == 'b' then
semitones = semitones - 1
end
octave = note_str:sub(3, 3)
end
semitones = semitones + 12 * (octave - 4)
return denver.base_freq * math.pow(math.pow(2, 1 / 12), semitones)
-- frequency = root * (2^(1/12))^steps (steps(=semitones) can be negative)
end
-- OSCILLATORS
oscillators.sinus = function (f)
local phase = 0
return function()
phase = phase + 2 * math.pi / denver.rate
if phase >= 2 * math.pi then
phase = phase - 2 * math.pi
end
return math.sin(f * phase)
end
end
-- thanks https://github.com/zevv/worp/blob/master/lib/Dsp/Saw.lua
oscillators.sawtooth = function (f)
local dv = 2 * f / denver.rate
local v = 0
return function()
v = v + dv
if v > 1 then v = v - 2 end
return v
end
end
oscillators.square = function (f, pwm)
pwm = pwm or 0
if pwm >= 1 or pwm < 0 then
error('PWM must be between 0 and 1 (0 <= PWM < 1)', 2)
end
local saw = oscillators.sawtooth(f)
return function()
return saw() < pwm and -1 or 1
end
end
oscillators.triangle = function (f)
local dv = 1 / denver.rate
local v = 0
local a = 1 -- up or down
return function()
v = v + a * dv * 4 * f
if v > 1 or v < -1 then
a = a * -1
v = math.floor(v+.5)
end
return v
end
end
oscillators.whitenoise = function ()
return function()
return math.random() * 2 - 1
end
end
oscillators.pinknoise = function () -- http://www.musicdsp.org/files/pink.txt
local b0, b1, b2, b3, b4, b5, b6 = 0, 0, 0, 0, 0, 0, 0
return function()
local white = math.random() * 2 - 1
b0 = 0.99886 * b0 + white * 0.0555179;
b1 = 0.99332 * b1 + white * 0.0750759;
b2 = 0.96900 * b2 + white * 0.1538520;
b3 = 0.86650 * b3 + white * 0.3104856;
b4 = 0.55000 * b4 + white * 0.5329522;
b5 = -0.7616 * b5 - white * 0.0168980;
local pink = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
b6 = white * 0.115926;
return pink * 0.11 -- (roughly) compensate for gain
end
end
-- thanks http://noisehack.com/generate-noise-web-audio-api/
oscillators.brownnoise = function ()
local lastOut = 0
return function()
local white = math.random() * 2 - 1
local out = (lastOut + (0.02 * white)) / 1.02
lastOut = out
return out * 3.5 -- (roughly) compensate for gain
end
end
function denver.is_valid(osc_name)
return oscillators[osc_name] ~= nil
end
-- Denver, the last dinosaur
-- He's my friend and a whole lot more
-- Denver, the last dinosaur
-- Shows me a world I never saw before
-- Everywhere we go we don't really care
-- If people stop and stare at our pal dino.
-- Creating history thru the rock n' roll spotlight
-- We've got a friend who helps us, we can do alright
-- That's Denver, the last dinosaur
-- He's my friend and a whole lot more
-- Denver, the last dinosaur
-- Shows me a world I never saw before.
return denver