Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
dev/StereoAudioRecorder.js now supports timeSlice/ondataavailable.
  • Loading branch information
muaz-khan committed Dec 13, 2017
1 parent 6c4ee3a commit 72d3499
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 684 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ bower install recordrtc
You can even link specific [releases](https://github.com/muaz-khan/RecordRTC/releases):

```html
<!-- use 5.4.5 or any other version -->
<script src="https://github.com/muaz-khan/RecordRTC/releases/download/5.4.5/RecordRTC.js"></script>
<!-- use 5.4.6 or any other version -->
<script src="https://github.com/muaz-khan/RecordRTC/releases/download/5.4.6/RecordRTC.js"></script>
```

## How to capture stream?
Expand Down Expand Up @@ -487,7 +487,7 @@ recorder.stopRecording(function() {
});
```

You can event use `getState` method:
You can even use `getState` method:

```javascript
alert('Current recorder status: ' + recorder.getState());
Expand Down
67 changes: 64 additions & 3 deletions RecordRTC.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

// Last time updated: 2017-12-05 7:27:40 AM UTC
// Last time updated: 2017-12-13 5:07:29 AM UTC

// ________________
// RecordRTC v5.4.6
Expand Down Expand Up @@ -2543,6 +2543,10 @@ function StereoAudioRecorder(mediaStream, config) {

isAudioProcessStarted = isPaused = false;
recording = true;

if (typeof config.timeSlice !== 'undefined') {
looper();
}
};

function mergeLeftRightBuffers(config, callback) {
Expand Down Expand Up @@ -2975,6 +2979,12 @@ function StereoAudioRecorder(mediaStream, config) {
self.desiredSampRate = desiredSampRate;
self.sampleRate = sampleRate;
self.recordingLength = recordingLength;

intervalsBasedBuffers = {
left: [],
right: [],
recordingLength: 0
};
}

function clearRecordedDataCB() {
Expand Down Expand Up @@ -3042,17 +3052,28 @@ function StereoAudioRecorder(mediaStream, config) {
var left = e.inputBuffer.getChannelData(0);

// we clone the samples
leftchannel.push(new Float32Array(left));
var chLeft = new Float32Array(left);
leftchannel.push(chLeft);

if (numberOfAudioChannels === 2) {
var right = e.inputBuffer.getChannelData(1);
rightchannel.push(new Float32Array(right));
var chRight = new Float32Array(right);
rightchannel.push(chRight);
}

recordingLength += bufferSize;

// export raw PCM
self.recordingLength = recordingLength;

if (typeof config.timeSlice !== 'undefined') {
intervalsBasedBuffers.recordingLength += bufferSize;
intervalsBasedBuffers.left.push(chLeft);

if (numberOfAudioChannels === 2) {
intervalsBasedBuffers.right.push(chRight);
}
}
}

jsAudioNode.onaudioprocess = onAudioProcessDataAvailable;
Expand All @@ -3067,6 +3088,46 @@ function StereoAudioRecorder(mediaStream, config) {
this.desiredSampRate = desiredSampRate;
this.sampleRate = sampleRate;
self.recordingLength = recordingLength;

// helper for intervals based blobs
var intervalsBasedBuffers = {
left: [],
right: [],
recordingLength: 0
};

// this looper is used to support intervals based blobs (via timeSlice+ondataavailable)
function looper() {
if (!recording || typeof config.ondataavailable !== 'function' || typeof config.timeSlice === 'undefined') {
return;
}

if (intervalsBasedBuffers.left.length) {
mergeLeftRightBuffers({
desiredSampRate: desiredSampRate,
sampleRate: sampleRate,
numberOfAudioChannels: numberOfAudioChannels,
internalInterleavedLength: intervalsBasedBuffers.recordingLength,
leftBuffers: intervalsBasedBuffers.left,
rightBuffers: numberOfAudioChannels === 1 ? [] : intervalsBasedBuffers.right
}, function(buffer, view) {
var blob = new Blob([view], {
type: 'audio/wav'
});
config.ondataavailable(blob);

setTimeout(looper, config.timeSlice);
});

intervalsBasedBuffers = {
left: [],
right: [],
recordingLength: 0
};
} else {
setTimeout(looper, config.timeSlice);
}
}
}

if (typeof RecordRTC !== 'undefined') {
Expand Down
8 changes: 4 additions & 4 deletions RecordRTC.min.js

Large diffs are not rendered by default.

65 changes: 63 additions & 2 deletions dev/StereoAudioRecorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ function StereoAudioRecorder(mediaStream, config) {

isAudioProcessStarted = isPaused = false;
recording = true;

if (typeof config.timeSlice !== 'undefined') {
looper();
}
};

function mergeLeftRightBuffers(config, callback) {
Expand Down Expand Up @@ -545,6 +549,12 @@ function StereoAudioRecorder(mediaStream, config) {
self.desiredSampRate = desiredSampRate;
self.sampleRate = sampleRate;
self.recordingLength = recordingLength;

intervalsBasedBuffers = {
left: [],
right: [],
recordingLength: 0
};
}

function clearRecordedDataCB() {
Expand Down Expand Up @@ -612,17 +622,28 @@ function StereoAudioRecorder(mediaStream, config) {
var left = e.inputBuffer.getChannelData(0);

// we clone the samples
leftchannel.push(new Float32Array(left));
var chLeft = new Float32Array(left);
leftchannel.push(chLeft);

if (numberOfAudioChannels === 2) {
var right = e.inputBuffer.getChannelData(1);
rightchannel.push(new Float32Array(right));
var chRight = new Float32Array(right);
rightchannel.push(chRight);
}

recordingLength += bufferSize;

// export raw PCM
self.recordingLength = recordingLength;

if (typeof config.timeSlice !== 'undefined') {
intervalsBasedBuffers.recordingLength += bufferSize;
intervalsBasedBuffers.left.push(chLeft);

if (numberOfAudioChannels === 2) {
intervalsBasedBuffers.right.push(chRight);
}
}
}

jsAudioNode.onaudioprocess = onAudioProcessDataAvailable;
Expand All @@ -637,6 +658,46 @@ function StereoAudioRecorder(mediaStream, config) {
this.desiredSampRate = desiredSampRate;
this.sampleRate = sampleRate;
self.recordingLength = recordingLength;

// helper for intervals based blobs
var intervalsBasedBuffers = {
left: [],
right: [],
recordingLength: 0
};

// this looper is used to support intervals based blobs (via timeSlice+ondataavailable)
function looper() {
if (!recording || typeof config.ondataavailable !== 'function' || typeof config.timeSlice === 'undefined') {
return;
}

if (intervalsBasedBuffers.left.length) {
mergeLeftRightBuffers({
desiredSampRate: desiredSampRate,
sampleRate: sampleRate,
numberOfAudioChannels: numberOfAudioChannels,
internalInterleavedLength: intervalsBasedBuffers.recordingLength,
leftBuffers: intervalsBasedBuffers.left,
rightBuffers: numberOfAudioChannels === 1 ? [] : intervalsBasedBuffers.right
}, function(buffer, view) {
var blob = new Blob([view], {
type: 'audio/wav'
});
config.ondataavailable(blob);

setTimeout(looper, config.timeSlice);
});

intervalsBasedBuffers = {
left: [],
right: [],
recordingLength: 0
};
} else {
setTimeout(looper, config.timeSlice);
}
}
}

if (typeof RecordRTC !== 'undefined') {
Expand Down
Loading

0 comments on commit 72d3499

Please sign in to comment.