-
Notifications
You must be signed in to change notification settings - Fork 1
/
karplusstrong.js
201 lines (168 loc) · 5.38 KB
/
karplusstrong.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
/*
* Karplus Strong synthesizer class.
*
* Generates polyphonic audio with a simple plucked string simulation, which gives a guitar like sound.
*
* Usage example: see simple.html
*
* created by Piers Titus van der Torren, 2010 - http://www.toverlamp.org/static/wickisynth/
*
* Changelog
* 2010-09: first version
* 2010-10: added sustain.
* 2014-11: Converted to Web Audio API
*/
KSPlayer = function KSPlayer(context) {
this.sampleRate = context.sampleRate;
this.releaseVolume = 0.0001;
this.damp = 0.9;
this.damp2= 1.0;
this.noiseDamp = 0.5;
this.sustain = false;
// create buffers for resonators (for 16 voices)
this.buffers = [];
for (var i=0;i<16;i++) {
this.buffers[i] = new Float32Array(2048);
}
this.polyphonyHolder = [];
// Note playing states
this.PLAYING = 0;
this.RELEASING = 1;
this.context = context;
this.node = context.createScriptProcessor(512, 1, 1);
var that = this;
this.node.onaudioprocess = function(e) { that.generate(e) };
};
KSPlayer.prototype.setSustain = function(sustain) {
switch(sustain) {
case true:
case 'on':
this.sustain = true;
break;
case false:
case 'off':
this.sustain = false;
break;
case 'switch':
this.sustain = !this.sustain;
break;
}
}
KSPlayer.prototype.generate = function(e) {
var output = e.outputBuffer.getChannelData(0);
var periodIndex = 0;
var previous = 0.0;
var sub = 0.0;
var damp = this.damp;
for (var i = 0; i < output.length; i++) {
output[i] = 0.0; // Initial signal
if(this.polyphonyHolder.length>0) {
for(var j = 0; j < this.polyphonyHolder.length; j++){
var note = this.polyphonyHolder[j];
if(note.phase == this.RELEASING && !this.sustain){
note.releaseVolume -= this.releaseVolume;
if(note.releaseVolume < 0.0){
note.releaseVolume = 0.0;
}
}
note.periodIndex += note.inc;
if( note.periodIndex >= note.periodN ) // wrap around delay-line
{
note.periodIndex -= note.periodN;
note.feedNoise = false;
}
periodIndex = Math.floor(note.periodIndex);
sub = note.periodIndex - periodIndex;
if (sub < note.inc) // generate a new sample if needed
{
damp = this.damp;
if( note.feedNoise )
{
//var damp;
if (note.periodIndex > note.periodN / 2) {
//damp = note.periodIndex/note.periodN;
note.period[ periodIndex ] = 1/this.noiseDamp * (Math.random() - Math.random()); // feed noise between -1 and +1
damp *= this.noiseDamp;
} else {
//damp = 1-(note.periodIndex/note.periodN);
// Experiment to make sound more periodic (without this there was a small high frequency component left)
note.period[ periodIndex ] = note.period[ note.periodN - periodIndex ];
}
//note.period[ periodIndex ] = 1/this.noiseDamp * (Math.random() - Math.random()); // feed noise between -1 and +1
}
note.previous = note.current;
note.current = (note.current + ( note.period[ periodIndex ] - note.current ) * damp) * this.damp2; // 1 pole lowpass (removing energy from the system)
note.period[ periodIndex ] = note.current; // write feedback
}
// linear interpolation (since Karplus Strong originally only supports integer sampleRate/frequency factors)
output[i] += (sub * note.current + (1-sub) * note.previous) * note.volume * note.releaseVolume;
//output[i] += note.current * note.volume * note.releaseVolume;
}
}
}
// Remove finished notes
for(var j = 0; j < this.polyphonyHolder.length; j++){
var note = this.polyphonyHolder[j];
if (note.releaseVolume == 0.0) {
this.buffers.push(note.period);
this.polyphonyHolder.splice(j,1);
}
}
};
KSPlayer.prototype.play = function(noteStopId, frequency, volume) {
// fact is the generating rate over playing rate.
// To remove high overtones from low notes generate
// a higher note and play it lower
// (and it uses less buffer then)
var fact = 1;
if (frequency < 200)
{
fact = frequency/205 + 0.05;
}
var periodN = fact*this.sampleRate/frequency;
// if the note is already playing reactivate it
for(var j=0; j<this.polyphonyHolder.length; j++){
if(this.polyphonyHolder[j].noteStopId == noteStopId){
this.polyphonyHolder[j].phase = this.PLAYING;
this.polyphonyHolder[j].feedNoise = true;
this.polyphonyHolder[j].PeriodIndex = 0;
this.polyphonyHolder[j].periodN = Math.floor(periodN),
this.polyphonyHolder[j].inc = fact*Math.floor(periodN)/periodN,
this.polyphonyHolder[j].releaseVolume = 1.0;
this.polyphonyHolder[j].volume = volume;
return;
}
}
// now play the note if it fits in the buffer and
// there is a buffer available.
if (periodN < 2048 && this.buffers.length > 0) {
var buffer = this.buffers.pop();
this.polyphonyHolder.push({
noteStopId: noteStopId,
periodIndex: 0.0,
periodN: Math.floor(periodN),
period: buffer, //new Float32Array(periodN),
feedNoise: true,
previous: 0.0,
current: 0.0,
inc: fact*Math.floor(periodN)/periodN,
volume: volume,
phase: this.PLAYING,
releaseVolume: 1.0
});
}
};
KSPlayer.prototype.stop = function(noteStopId) {
for(var j=0; j<this.polyphonyHolder.length; j++){
if(this.polyphonyHolder[j].noteStopId == noteStopId){
this.polyphonyHolder[j].phase = this.RELEASING;
break;
}
}
};
KSPlayer.prototype.start = function() {
this.node.connect(this.context.destination);
}
KSPlayer.prototype.pause = function() {
this.node.disconnect();
}