-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynth.js
310 lines (279 loc) · 9.8 KB
/
synth.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// synth.js
// synthesiser save/load/keyboard interaction
//
// MUMT307 project - Alastair Porter
var currentSoundSample = 0;
var Synth = function(element, options) {
var defaults = {
numvoices: 0,
currentVoice: 0
};
var settings = $.extend({}, defaults, options);
var voices = {};
// Audio output object
var output;
// Sample Rate
var sampleRate = 44100;
var currentWritePosition = 0;
var prebufferSize = sampleRate / 2; // buffer 500ms
var tail = null, tailPosition;
var intervalId;
var prebufferSize = sampleRate * 0.020; // Initial buffer is 20 ms
var autoLatency = true, started = new Date().valueOf();
/**
* Convert a midi note number to a frequency (A440)
*/
function _midiToFreq(m) {
semis = midi - 69;
f = 440 * Math.pow(2, (semis/12.0));
return f;
}
// This method almost verbatim from https://wiki.mozilla.org/Audio_Data_API
// Plays the frequency 'freq'
function _playPitch() {
f = freq;
var written;
// Check if some data was not written in previous attempts.
if(tail) {
written = output.mozWriteAudio(tail.subarray(tailPosition));
currentWritePosition += written;
tailPosition += written;
if(tailPosition < tail.length) {
// Not all the data was written, saving the tail...
return; // ... and exit the function.
}
tail = null;
}
// Auto latency detection
if (autoLatency) {
prebufferSize = Math.floor(sampleRate * (new Date().valueOf() - started) / 1000);
if (output.mozCurrentSampleOffset()) { // Play position moved?
autoLatency = false;
}
}
// Check if we need to add some data to the audio output.
var currentPosition = output.mozCurrentSampleOffset();
if (currentWritePosition > currentPosition) {
currentWritePosition = currentPosition;
}
var available = currentPosition + prebufferSize - currentWritePosition;
if(available > 0) {
// Find the next power of 2 (so the FFT works)
available = (available >> 1) | available;
available = (available >> 2) | available;
available = (available >> 4) | available;
available = (available >> 8) | available;
available = (available >> 16) | available;
available += 1;
// Request some sound data from the callback function.
mode = $('#voice').voice().getSynthMode();
if (mode === "Additive") {
var soundData = makeAdditive(f, available);
} else if (mode === "FM") {
var soundData = makeFM(f, available);
}
// Paint the waveform/fft
_drawSignal(soundData, f);
written = output.mozWriteAudio(soundData);
if(written < soundData.length) {
// Not all the data was written, saving the tail.
tail = soundData;
tailPosition = written;
}
currentWritePosition += written;
}
}
// When the mouse is pressed, start filling the synth buffer
function _startPitch(f) {
autoLatency = true;
started = new Date().valueOf();
currentSoundSample = 0;
freq = f;
intervalId = setInterval(_playPitch, 100);
}
// Stop playing when mouse is lifted
function _stopPitch() {
clearInterval(intervalId);
tail = null;
// XXX: Play decay
}
// Draw a signal's waveform and its FFT
function _drawSignal(signal, frequency) {
fft = new FFT(signal.length, 44100);
fft.forward(signal);
canvas = $('#signal');
// Signal
canvas.each(function(i, c) {
c.width = c.width; // reset
context = c.getContext("2d");
scale = 20;
firsty = scale + 100 - signal[0] * scale;
context.moveTo(0, firsty);
if (frequency > 1000) {
skip = 1;
} else if (frequency > 800) {
skip = 2;
} else {
skip = 4;
}
cap = Math.min(500*4, signal.length);
for ( var i = 0; i < cap; i+=skip ) {
context.lineTo(i, scale + 100 - signal[i] * scale);
}
context.stroke();
});
// FFT
canvas = $('#fft');
canvas.each(function(i, c) {
c.width = c.width; // reset
context = c.getContext("2d");
height = 200;
firsty = height - 10 - fft.spectrum[0] * 512;
context.moveTo(0, firsty);
skip = Math.floor(fft.spectrum.length / 500);
cap = Math.min(500*skip, fft.spectrum.length)
for ( var i = 0; i < cap; i+=1 ) {
context.lineTo(i/skip, height - 10 - fft.spectrum[i] * 64);
}
context.stroke();
});
}
// Load a Synth that was stored as json
function _loadSynth(newsynth) {
for (voice in newsynth.voices) {
_addVoice(newsynth.voices[voice]);
}
settings.numvoices = newsynth.numvoices;
settings.currentVoice = newsynth.currentVoice;
_selectVoice(settings.currentVoice);
}
function _setup() {
if (!!new Audio() && !!new Audio().mozSetup) {
output = new Audio();
output.mozSetup(1, sampleRate, 1);
$("#messages").html("We're ready to go!");
} else {
$("#messages").html("Sorry, you have no audio support. Please download Firefox 4.");
}
if (localStorage["synth"] === null) {
_resetSynth();
} else {
_loadSynth($.evalJSON(localStorage["synth"]));
}
}
// Load the default set of synths
function _resetSynth() {
_removeAllVoices();
_loadSynth(synthDefaults)
}
// Add a new voice. If called with an argument, use the settings
// in that argument. Otherwise make a new voice.
function _addVoice() {
if (arguments.length > 0) {
voicename = arguments[0].name;
} else {
voicename = 'New voice '+settings.numvoices
}
$('#voicetable tbody').append(
$('<tr>').attr('id', 'voicerow'+settings.numvoices).append(
$('<td>').append($('<span>').attr('id', 'voice'+settings.numvoices).text(voicename))
).append(
$('<td>').append($('<button>').attr('id', 'editvoice'+settings.numvoices).text('Edit voice'))
).append(
$('<td>').append($('<button>').attr('id', 'selectvoice'+settings.numvoices).text('Select voice'))
).append(
$('<td>').append($('<a>').attr('href', '#').attr('id', 'deletevoice'+settings.numvoices).text('[x]'))
)
);
(function(num, loadargs) {
if (loadargs.length > 0) {
v = loadargs[0];
voices[num] = new Voice(null, {id: v.id, algorithm:v.algorithm, mode: v.mode, 'name': v['name']});
voices[num].setup(v.operators, v.envelopes);
} else {
voices[num] = new Voice(null, {id: num, algorithm:num});
voices[num].setup();
}
$('#editvoice'+num).click(function(event) {
$('#synth').synth().editVoice(num);
});
$('#deletevoice'+num).click(function(event) {
event.preventDefault();
$('#synth').synth().removeVoice(num);
});
$('#selectvoice'+num).click(function(event) {
$('#synth').synth().selectVoice(num);
});
})(settings.numvoices, arguments);
settings.numvoices += 1;
}
// Delete voices from the synth
function _removeVoice(voice) {
delete voices[voice];
$('#voicerow'+voice).remove();
}
function _removeAllVoices() {
for (v in voices) {
_removeVoice(v);
}
}
// Make a voice active in the synth
function _selectVoice(n) {
$('#voicerow'+settings.currentVoice).css('background', 'white');
settings.currentVoice = n;
$('#voicerow'+n).css('background', 'yellow');
// Copy voice into the UI
$('#voice').data('voice', voices[settings.currentVoice]);
$('#voice').voice().fillForm();
}
// Public methods
return {
setup: function() {
_setup()
},
addVoice: function() {
_addVoice();
},
removeVoice: function(n) {
_removeVoice(n);
},
selectVoice: function(n) {
_selectVoice(n);
},
editVoice: function(n) {
_selectVoice(n);
$('#voice').show();
},
updateVoice: function(vid, newname) {
voices[vid] = $('#voice').voice();
$('#voice'+vid).text(newname);
},
saveLocal: function() {
var v = {};
for (voice in voices) {
v[voice] = voices[voice].getSettings();
}
var d = $.extend({}, settings, {voices: v});
localStorage["synth"] = $.toJSON(d);
},
loadLocal: function() {
_removeAllVoices();
_loadSynth($.evalJSON(localStorage["synth"]));
},
playMidi: function(midi) {
_playPitch(_midiToFreq(midi));
},
dumpVoices: function() {
//console.debug(voices);
},
resetSynth: function() {
_resetSynth();
},
startMidi: function(midi) {
_startPitch(_midiToFreq(midi));
},
stopMidi: function() {
_stopPitch();
}
};
};