Skip to content

How to create pink noise with the API? #258

Closed Answered by michalsek
timoisalive asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, thanks for reaching us! :)

instead of using ScriptProcessorNode you can use AudioBufferSourceNode as it is suggested in the white nose section :)

by reusing the values from the tutorial you've linked, we can create something like that:

  const createPinkNoise = () => {
    // aCtx is audio context created f.e. in useEffect hook
    const bufferSize = aCtx.sampleRate * 2; // We want at least two seconds of noise

    const output = new Array<number>(bufferSize);
    let b0, b1, b2, b3, b4, b5, b6;
    b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0.0;

    for (let i = 0; i < bufferSize; i += 1) {
      const white = Math.random() * 2 - 1;

      b0 = 0.99886 * b0 + white * 0.0555179;
      b1 =…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by michalsek
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@michalsek
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants