Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:54:34+00:00 2026-06-17T22:54:34+00:00

Edit: Cleaned up the code and the player (on Github) a little so it’s

  • 0

Edit: Cleaned up the code and the player (on Github) a little so it’s easier to set the frequency

I’m trying to synthesize strings using the Karplus Strong string synthesis algorithm, but I can’t get the string to tune properly. Does anyone have any idea?

As linked above, the code is on Github: https://github.com/achalddave/Audio-API-Frequency-Generator (the relevant bits are in strings.js).

Wiki has the following diagram:

Karplus Strong String Synthesis diagram

So essentially, I generate the noise, which then gets output and sent to a delay filter simultaneously. The delay filter is connected to a low-pass filter, which is then mixed with the output. According to Wikipedia, the delay should be of N samples, where N is the sampling frequency divided by the fundamental frequency (N = f_s/f_0).

Excerpts from my code:

Generating the noise (bufferSize is 2048, but that shouldn’t matter too 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); // random noise from -1 to 1
}

Create a delay node

var delayNode = context.createDelayNode();

We need to delay by f_s/f_0 samples. However, the delay node takes the delay in seconds, so we need to divide that by the 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 the lowpass filter (the frequency cutoff, as far as I can tell, shouldn’t affect the frequency, and is more a matter of whether the string “sounds” 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 the delay node (destination = context.destination and was defined earlier):

bufferSource.connect(destination);
bufferSource.connect(delayNode);

Connect the delay to the lowpass filter:

delayNode.connect(lowpassFilter);

Connect the lowpass to the output and back to the delay*:

lowpassFilter.connect(destination);
lowpassFilter.connect(delayNode);

Does anyone have any ideas? I can’t figure out whether the issue is my code, my interpretation of the algorithm, my understanding of the API, or (though this is least likely) an issue with the API itself.


*Note that on Github, there’s actually a Gain Node between the lowpass and the output, but this doesn’t really make a big difference in the output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:54:35+00:00Added an answer on June 17, 2026 at 10:54 pm

    Here’s what I think is the problem. I don’t think the DelayNode implementation is designed to handle such tight feedback loops. For a 441 Hz tone, for example, that’s only 100 samples of delay, and the DelayNode implementation probably processes its input in blocks of 128 or more. (The delayTime attribute is “k-rate”, meaning changes to it are only processed in blocks of 128 samples. That doesn’t prove my point, but it hints at it.) So the feedback comes in too late, or only partially, or something.

    EDIT/UPDATE: As I state in a comment below, the actual problem is that a DelayNode in a cycle adds 128 sample frames between output and input, so that the observed delay is 128 / sampleRate seconds longer than specified.

    My advice (and what I’ve begun to do) is to implement the whole Karplus-Strong including your own delay line in a JavaScriptNode (now known as a ScriptProcessorNode). It’s not hard and I’ll post my code once I get rid of an annoying bug that can’t possibly exist but somehow does.

    Incidentally, the tone you (and I) get with a delayTime of 1/440 (which is supposed to be an A) seems to be a G, two semitones below where it should be. Doubling the frequency raises it to a B, four semitones higher. (I could be off by an octave or two – kind of hard to tell.) Probably one could figure out what’s going on (mathematically) from a couple more data points like this, but I won’t bother.

    EDIT: Here’s my code, certified bug-free.

    var context = new webkitAudioContext();
    
    var frequency = 440;
    var impulse = 0.001 * context.sampleRate;
    
    var node = context.createJavaScriptNode(4096, 0, 1);
    var N = Math.round(context.sampleRate / frequency);
    var y = new Float32Array(N);
    var n = 0;
    node.onaudioprocess = function (e) {
      var output = e.outputBuffer.getChannelData(0);
      for (var i = 0; i < e.outputBuffer.length; ++i) {
        var xn = (--impulse >= 0) ? Math.random()-0.5 : 0;
        output[i] = y[n] = xn + (y[n] + y[(n + 1) % N]) / 2;
        if (++n >= N) n = 0;
      }
    }
    
    node.connect(context.destination);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below referred code to edit a csv using Python. Functions called
EDIT: iam using ajax to load text in my content that is why onload
I have cleaned and shortened a snippet of my code for a multiuser game
jqGrid add forms contains autocomplete boxes using code below. If new row is added
[EDIT: The HTML is invalid and needs to be cleaned up after being spit
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: I was an idiot. I simply had an image that was vertically long,
EDIT: See my answer below--> I am wanting to have a view that when
Edit (updated question) I have a simple C program: // it is not important
Edit : Note that, as Daniel and latkin noted in an answer and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.