2

I'm trying to create a complex periodic sound with long period. I want to define frequencies as accurately as I can, so I'm using step sampleRate*0.5/tableLen. But I have some issues with large wave tables. The sound becomes distorted and loses high frequencies.

Here is a minimal example with ~440 Hz sine wave. When I use table with length 8192, the resulting sine wave is quite recognizable:

https://jsfiddle.net/zxqzntf0/

var gAudioCtx = new AudioContext();
var osc = gAudioCtx.createOscillator();
var tableSize = 8192;
var real = new Float32Array(tableSize);
var imag = new Float32Array(tableSize);
var freq = 440;
var step = gAudioCtx.sampleRate*0.5/tableSize;
real[Math.floor(freq/step)] = 1;
var wave = gAudioCtx.createPeriodicWave(real, imag, {disableNormalization: true});
osc.frequency.value = step;
osc.setPeriodicWave(wave);
osc.connect(gAudioCtx.destination);
osc.start();

But when I increase my table size, I'm getting something strange. Result is not a sine wave at all!

https://jsfiddle.net/0cc75nnm/

This problem reproduces in all browsers (Chrome, Firefox, Edge), so it doesn't seem to be a browser bug. But I've found nothing about this in documentation.

Added

I found that if oscillator frequency is a whole number >= 2 Hz, I have no any artifacts in resulting sound with table size 16384. I think it is quite acceptable for my needs for now. But someday I may want to create longer periods. If someone explains me why I get sound artifacts when step is less than 2 Hz, I will accept his answer.

There is an example of a complex sound melody that I generate in JavaScript: https://jsfiddle.net/h9rfzrnL/1/

1 Answer 1

3

You're creating you periodic waves incorrectly. When filling the arrays for the periodic wave, assume the sample rate is 1. Then if you want an oscillator at a frequency of 440 Hz, set the oscillator frequency to 440 Hz.

Thus, for a sine wave, the real array should be all zeroes and the imaginary array is [0, 1]. (You're actually creating a cosine wave, but that doesn't really matter too much.)

12
  • I want 20 sines in range 400-480 Hz, 40 sines in range 800-960 Hz and so on. I can't do it if I have 440 Hz step. Single sine wave is a minimal example of my problem.
    – devoln
    Sep 20, 2017 at 17:40
  • Maybe a concrete example of the wave you want to create would help explain the issue better. Still, the basic idea is to design your wave as if the sample rate were 1 Hz. Then set the frequency of the oscillator as the base frequency of the desired signal. Sep 20, 2017 at 19:37
  • I need harmonics that are distributed in some frequency range. So how can I create oscillator with frequencies 400, 401, ..., 480, 800, 801, ..., 960, 1200, 1201, ..., 1920, ... ... ..., 12800, 12801, ..., 15360? I can't do it, if I have step 440, I need step 1 Hz and very big table. Lesser step is better.
    – devoln
    Sep 20, 2017 at 19:37
  • Where does this complex periodic signal come from? Is it really described as a bunch of frequencies? If so, it might just be easiest to create a whole bunch of oscillators at the desired frequencies. Not the best solution if you have a ton of oscillators. Sep 20, 2017 at 19:43
  • I generate this signal procedurally in my JS code. Simple sine harmonics sound too artificial, but "wide" harmonics sound warmer and more pleasant. Do you suggest to create 5000 sine oscillators for example above? It is not a solution at all.
    – devoln
    Sep 20, 2017 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.