mohayonao / web-audio-engine

Pure JS implementation of the Web Audio API
243 stars 32 forks source link

large latency #61

Closed belm0 closed 6 years ago

belm0 commented 6 years ago

I'm observing 100's of milliseconds of latency in the audio output (seen on OS X). It's not sufficient for real-time use.

Example program (tone played on any key press):

const Speaker = require("speaker");
const AudioContext = new require("web-audio-engine").StreamAudioContext;
const context = new AudioContext({
  //sampleRate: 44100,
  //numberOfChannels: 2,
  //blockSize: 128,
  //bitDepth: 16,
  //float: true,
});
context.pipe(new Speaker());
context.resume();

var keypress = require('keypress');
keypress(process.stdin);

function ticktack(e) {
  var t0 = context.currentTime;
  var t1 = t0 + e.args.duration;
  var osc = context.createOscillator();
  var amp = context.createGain();

  osc.frequency.value = e.args.frequency;
  osc.start(t0);
  osc.stop(t1);
  osc.connect(amp);

  amp.gain.setValueAtTime(0.5 * e.args.amp, t0);
  amp.gain.exponentialRampToValueAtTime(1e-6, t1);
  amp.connect(context.destination);
}

process.stdin.on('keypress', function (ch, key) {
  ticktack({args: { frequency: 880, amp: 1.0, duration: 1.00 }});
  if (key && key.ctrl && key.name == 'c') {
    process.exit(0);
  }
});
process.stdin.setRawMode(true);
process.stdin.resume();
belm0 commented 6 years ago

appears to be a node-speaker issue:

https://github.com/TooTallNate/node-speaker/issues/13

belm0 commented 6 years ago

Can be addressed per project by installing the low-latency branch of node-speaker as follows:

npm install git://github.com/colinbdclark/node-speaker.git#mpg123-lower-latency