UNPKG

sono

Version:

A simple yet powerful JavaScript library for working with Web Audio

133 lines (117 loc) 2.83 kB
"use strict"; exports.__esModule = true; exports.default = MicrophoneSource; function MicrophoneSource(stream, context) { var ended = false, paused = false, cuedAt = 0, playing = false, sourceNode = null, // MicrophoneSourceNode startedAt = 0; function createSourceNode() { if (!sourceNode && context) { sourceNode = context.createMediaStreamSource(stream); // HACK: stops moz garbage collection killing the stream // see https://support.mozilla.org/en-US/questions/984179 if (navigator.mozGetUserMedia) { window.mozHack = sourceNode; } } return sourceNode; } /* * Controls */ function play(delay) { delay = delay ? context.currentTime + delay : 0; createSourceNode(); sourceNode.start(delay); startedAt = context.currentTime - cuedAt; ended = false; playing = true; paused = false; cuedAt = 0; } function stop() { if (sourceNode) { try { sourceNode.stop(0); } catch (e) {} sourceNode = null; } ended = true; paused = false; cuedAt = 0; playing = false; startedAt = 0; } function pause() { var elapsed = context.currentTime - startedAt; stop(); cuedAt = elapsed; playing = false; paused = true; } /* * Destroy */ function destroy() { stop(); context = null; sourceNode = null; stream = null; window.mozHack = null; } /* * Api */ var api = { play: play, pause: pause, stop: stop, destroy: destroy, duration: 0, progress: 0 }; /* * Getters & Setters */ Object.defineProperties(api, { currentTime: { get: function get() { if (cuedAt) { return cuedAt; } if (startedAt) { return context.currentTime - startedAt; } return 0; }, set: function set(value) { cuedAt = value; } }, ended: { get: function get() { return ended; } }, paused: { get: function get() { return paused; } }, playing: { get: function get() { return playing; } }, sourceNode: { get: function get() { return createSourceNode(); } } }); return Object.freeze(api); }