audio-stream
Advanced tools
Comparing version 0.0.1 to 0.1.0
90
index.js
@@ -5,4 +5,9 @@ var stream = require('stream'); | ||
var extend = require('xtend'); | ||
var debug = require('debug')('audio-stream'); | ||
var capture = require('./capture'); | ||
var BIT_DEPTH = 32; | ||
var SAMPLE_RATE = 44100; | ||
var HIGH_WATER_MARK = Math.pow(2, 14) * 16; | ||
@@ -14,3 +19,3 @@ var AudioContext = window.AudioContext || window.webkitAudioContext; | ||
if(!(this instanceof AudioStream)) return new AudioStream(media, options); | ||
stream.Readable.call(this); | ||
stream.Readable.call(this, { highWaterMark: HIGH_WATER_MARK }); | ||
@@ -28,8 +33,17 @@ options = extend({ | ||
self.duration = 0; | ||
self.samples = 0; | ||
this.duration = 0; | ||
this.samples = 0; | ||
this._destroyed = false; | ||
this._suspend = noop; | ||
this._restart = noop; | ||
this._stop = noop; | ||
this._record = once(function() { | ||
if(capture.ended(media)) { | ||
debug('ended before data'); | ||
self._emitHeader(SAMPLE_RATE, channels); | ||
return self.push(null); | ||
} | ||
var context = options.context || new AudioContext(); | ||
@@ -42,5 +56,7 @@ var source = (media instanceof Audio) ? | ||
var that = capture(media, processor); | ||
gain.gain.value = options.volume; | ||
var onaudioprocess = processor.onaudioprocess = function(e) { | ||
that.on('data', function(e) { | ||
var input = e.inputBuffer; | ||
@@ -51,5 +67,2 @@ var numberOfChannels = input.numberOfChannels; | ||
self.duration += input.duration; | ||
self.samples += numberOfSamples; | ||
for(var i = 0; i < numberOfChannels; i++) { | ||
@@ -64,8 +77,27 @@ var channel = input.getChannelData(i); | ||
self.duration += input.duration; | ||
self.samples += numberOfSamples; | ||
self.push(data); | ||
}); | ||
that.on('end', function() { | ||
self._stop(); | ||
self.push(null); | ||
}); | ||
self._suspend = function() { | ||
debug('suspend'); | ||
that.suspend(); | ||
}; | ||
self._restart = function() { | ||
debug('restart'); | ||
that.restart(); | ||
}; | ||
self._stop = function() { | ||
processor.onaudioprocess = null; | ||
debug('stop'); | ||
that.destroy(); | ||
processor.disconnect(); | ||
@@ -77,8 +109,12 @@ gain.disconnect(); | ||
self.on('end', function() { | ||
debug('end'); | ||
}); | ||
self.on('pause', function() { | ||
processor.onaudioprocess = null; | ||
debug('pause'); | ||
}); | ||
self.on('resume', function() { | ||
processor.onaudioprocess = onaudioprocess; | ||
debug('resume'); | ||
}); | ||
@@ -90,12 +126,3 @@ | ||
var sampleRate = context.sampleRate; | ||
self.emit('header', { | ||
audioFormat: 3, | ||
channels: channels, | ||
sampleRate: sampleRate, | ||
byteRate: sampleRate * channels * bytesPerSample, | ||
blockAlign: channels * bytesPerSample, | ||
bitDepth: BIT_DEPTH | ||
}); | ||
self._emitHeader(context.sampleRate, channels); | ||
}); | ||
@@ -106,3 +133,13 @@ }; | ||
AudioStream.prototype.suspend = function() { | ||
this._suspend(); | ||
}; | ||
AudioStream.prototype.restart = function() { | ||
this._restart(); | ||
}; | ||
AudioStream.prototype.destroy = function(err) { | ||
debug('destroy', err); | ||
if(this._destroyed) return; | ||
@@ -120,2 +157,15 @@ this._destroyed = true; | ||
AudioStream.prototype._emitHeader = function(sampleRate, channels) { | ||
var bytesPerSample = BIT_DEPTH / 8; | ||
this.emit('header', { | ||
audioFormat: 3, | ||
channels: channels, | ||
sampleRate: sampleRate, | ||
byteRate: sampleRate * channels * bytesPerSample, | ||
blockAlign: channels * bytesPerSample, | ||
bitDepth: BIT_DEPTH | ||
}); | ||
}; | ||
module.exports = AudioStream; |
{ | ||
"name": "audio-stream", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Stream raw audio data from a MediaStream", | ||
@@ -30,2 +30,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"debug": "^2.2.0", | ||
"once": "^1.3.2", | ||
@@ -32,0 +33,0 @@ "xtend": "^4.0.0" |
@@ -25,4 +25,4 @@ # audio-stream | ||
stream.on('header', function() { | ||
// Emit wave header proeprties | ||
stream.on('header', function(header) { | ||
// Wave header properties | ||
}); | ||
@@ -33,2 +33,10 @@ | ||
}); | ||
stream.on('end', function() { | ||
// End is emitted when media stream has ended | ||
}); | ||
setTimeout(function() { | ||
mediaStream.stop(); | ||
}, 5000); | ||
}, function() { | ||
@@ -40,1 +48,28 @@ console.log('Failed to get media'); | ||
The constructor accepts number of channels and microphone volume as options. | ||
#### `stream.destroy([err])` | ||
Destroy the audio stream, releasing all associated resources. The media stream is not closed. | ||
#### `stream.suspend()` | ||
Suspend audio data capturing. | ||
#### `stream.restart()` | ||
Restart audio data capturing. | ||
# Limitations | ||
Currently Chrome lacks supports for capturing a remote stream sent using a peer connection. But works in Firefox. | ||
```javascript | ||
// This only works in Firefox at the moment | ||
peerConnection.onaddstream = function(e) { | ||
var stream = audio(e.stream); | ||
stream.on('data', function(data) { | ||
}); | ||
}; | ||
``` |
9099
5
217
73
3
+ Addeddebug@^2.2.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)