Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Web Audio - Slowed down audio playback cuts off early

I'm working on a web-based music sequencer/tracker, and I've noticed that in my sample playback routine, audio contexts seem to exist only for the duration of of a sample, and that the Web Audio API doesn't seem to adjust playback duration when I pitchshift a sample. For instance, if I shift a note down an octave, the routine only plays the first half of the sound before cutting off. More intense pitch downshifts result in even less of the sound playing, and while I'm not sure I can confirm this, I suspect that speeding up the audio results in relatively long periods of silence before the sound exits the buffer.

Here's my audio playback routine at the moment. So far, a lot more work has gone into making sure other functions send the right data to this than into extending the functionality of this routine.

function playSound(buffer, pitch, dspEffect, dspValue, volume) {

  var source = audioEngine.createBufferSource();  
  source.buffer = buffer;
  source.playbackRate.value = pitch; 

  // Volume adjustment is handled before other DSP effects are added.
  var volumeAdjustment = audioEngine.createGain();
  source.connect(volumeAdjustment);
  // Very basic error trapping in case of bad volume input.
  if(volume >= 0 && volume <= 1) { 
    volumeAdjustment.gain.value = volume; 
  } else { 
    volumeAdjustment.gain.value = 0.6; 
  }

  switch(dspEffect){
    case 'lowpass':
      var createLowPass = audioEngine.createBiquadFilter();
      volumeAdjustment.connect(createLowPass);
      createLowPass.connect(audioEngine.destination);
      createLowPass.type = 'lowpass';
      createLowPass.frequency.value = dspValue;
      break;
    // There are a couple of other optional DSP effects, 
    // but so far they all operate in about the same fashion.
  }
  source.start();
}

My intent is for samples to play back fully no matter how much pitch shifting is applied, and to limit the amount of pitch shifting allowed if necessary. I've found that appending several seconds of silence to a sound works around this issue, but it's cumbersome due to the large amount of sounds I would need to process, and I'd prefer a code-based solution.

EDIT: Of the browsers I can test this in, this only appears to be an issue in Google Chrome. Samples play back fully in Firefox, Internet Explorer does not yet support the Web Audio API, and I do not have ready access to Safari or Opera. This definitely changes the nature of the help I'm looking for.

like image 774
Jessica Kagan Avatar asked Jun 17 '15 19:06

Jessica Kagan


1 Answers

I've found that appending several seconds of silence to a sound works around this issue, but it's cumbersome due to the large amount of sounds I would need to process, and I'd prefer a code-based solution.

You could upload a sound file that is just several seconds of silence and append it to the actual audio file. Here is an SO answer that shows how to do this...

like image 162
MoralCode Avatar answered Nov 07 '22 22:11

MoralCode