beep.js 633 B

1234567891011121314151617181920
  1. //data:charset=utf-8,
  2. var audioCtx = new window.AudioContext;
  3. function beep(duration, frequency, volume, type, callback) {
  4. var oscillator = audioCtx.createOscillator();
  5. var gainNode = audioCtx.createGain();
  6. oscillator.connect(gainNode);
  7. gainNode.connect(audioCtx.destination);
  8. if (volume){gainNode.gain.value = volume;}
  9. if (frequency){oscillator.frequency.value = frequency;}
  10. if (type){oscillator.type = type;}
  11. if (callback){oscillator.onended = callback;}
  12. oscillator.start(audioCtx.currentTime);
  13. oscillator.stop(audioCtx.currentTime + ((duration || 500) / 1000));
  14. };
  15. beep();
  16. self.close();