0

I want to know how I can create custom sounds (using JavaScript) and stumbled upon following post by mozilla developer: https://developer.mozilla.org/en-US/docs/Web/API/BaseAudioContext/createPeriodicWave#example

This post shows, how to create the data for a sound and then play it, but sadly I don't know how the real and imaginary parts affect the sound.

What if, for example, I want to create a piano-like sound?

It would be nice if you could help me out and explain, how exactly I can create custom sounds by using pure JavaScript and maths.

  • From a quick look at the link its using [FFT](https://stackoverflow.com/a/26355569/2521214) so You feed your complex domain array (representing frequency domain) into inverse FFT and use only its real part of output (time domain) as PCM sampled periodic sound ... I do not code in javascript so I just guess you should have some sort of API for PCM sound output ... however some data scaling/aligning/conversion is most likely required to match the sound API data format ... – Spektre Jun 28 '21 at 07:13
  • Another option is construct the signal directly from FFT result as a set of sin waves and current time... each complex value has its initial phase and amplitude and the frequency is given by the index (order of values) so it will be something like `A(t) = X0 + A0*sin(ph0+t*f0) + A1*sin(ph1+t*f1) ... ` so just compute `A(t)` for each sample you are outputting as sound – Spektre Jun 28 '21 at 09:00

0 Answers0