Edit: cleared the code and player a bit (on Github) to make it easier to set the frequency
I am trying to synthesize strings using Karplus Strong's string synthesis algorithm , but I canโt configure the string correctly. Anyone have any ideas?
As stated above, the code is in Github: https://github.com/achalddave/Audio-API-Frequency-Generator (the corresponding bits are in strings.js ).
Wiki has the following diagram:

Thus, I generate noise, which is then output and sent to the delay filter at the same time. The delay filter is connected to a low-pass filter, which is then mixed with the output. According to Wikipedia, the delay should be N samples, where N is the sampling frequency divided by the fundamental frequency ( N = f_s/f_0 ).
Excerpts from my code:
Noise generation ( bufferSize is 2048, but this should not matter much)
var buffer = context.createBuffer(1, bufferSize, context.sampleRate); var bufferSource = context.createBufferSource(); bufferSource.buffer = buffer; var bufferData = buffer.getChannelData(0); for (var i = 0; i < delaySamples+1; i++) { bufferData[i] = 2*(Math.random()-0.5);
Create delay node
var delayNode = context.createDelayNode();
We need to delay the f_s/f_0 patterns. However, the delay node takes a delay in seconds, so we need to divide this into samples per second, and we get (f_s/f_0) / f_s , which is just 1/f_0 .
var delaySeconds = 1/(frequency); delayNode.delayTime.value = delaySeconds;
Create a low-pass filter (cutting the frequency, as far as I can tell, should not affect the frequency and depends more on whether the string is โsonicโ natural):
var lowpassFilter = context.createBiquadFilter(); lowpassFilter.type = lowpassFilter.LOWPASS; // explicitly set type lowpassFilter.frequency.value = 20000; // make things sound better
Connect the noise to the output and delay node ( destination = context.destination and was previously defined):
bufferSource.connect(destination); bufferSource.connect(delayNode);
Connect the delay to the low pass filter:
delayNode.connect(lowpassFilter);
Connect the lower limit to the output and back to the delay *:
lowpassFilter.connect(destination); lowpassFilter.connect(delayNode);
Does anyone have any ideas? I cannot understand if the problem is my code, my interpretation of the algorithm, my understanding of the API, or (although this is the least likely) the problem with the API itself.
* Please note that on Github there is actually a node gain between the bottom and the output, but this is actually not very important.