Skip to content

Commit

Permalink
Fixed tiny bug in dev/CanvasRecorder.js, updated audio-recording.html…
Browse files Browse the repository at this point in the history
  • Loading branch information
muaz-khan committed Nov 11, 2017
1 parent ab6300a commit b353e13
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 79 deletions.
74 changes: 43 additions & 31 deletions RecordRTC.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

// Last time updated: 2017-10-17 2:23:06 PM UTC
// Last time updated: 2017-11-11 6:28:47 AM UTC

// ________________
// RecordRTC v5.4.5
// RecordRTC v5.4.6

// Open-Sourced: https://github.com/muaz-khan/RecordRTC

Expand Down Expand Up @@ -739,6 +739,11 @@ function RecordRTC(mediaStream, config) {
setState('destroyed');
returnObject = self = null;

if (Storage.AudioContextConstructor) {
Storage.AudioContextConstructor.close();
Storage.AudioContextConstructor = null;
}

if (!disableLogs) {
console.warn('RecordRTC is destroyed.');
}
Expand Down Expand Up @@ -1681,7 +1686,8 @@ if (typeof navigator !== 'undefined' && typeof navigator.getUserMedia === 'undef

var isEdge = navigator.userAgent.indexOf('Edge') !== -1 && (!!navigator.msSaveBlob || !!navigator.msSaveOrOpenBlob);
var isOpera = !!window.opera || navigator.userAgent.indexOf('OPR/') !== -1;
var isChrome = (!isOpera && !isEdge && !!navigator.webkitGetUserMedia) || isElectron();
var isSafari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
var isChrome = (!isOpera && !isEdge && !!navigator.webkitGetUserMedia) || isElectron() || isSafari;

var MediaStream = window.MediaStream;

Expand Down Expand Up @@ -2468,6 +2474,10 @@ function StereoAudioRecorder(mediaStream, config) {
numberOfAudioChannels = 1;
}

if (!numberOfAudioChannels || numberOfAudioChannels < 1) {
numberOfAudioChannels = 2;
}

if (!config.disableLogs) {
console.log('StereoAudioRecorder is set to record number of channels: ', numberOfAudioChannels);
}
Expand Down Expand Up @@ -2510,16 +2520,7 @@ function StereoAudioRecorder(mediaStream, config) {
throw 'Please make sure MediaStream is active.';
}

// reset the buffers for the new recording
leftchannel.length = rightchannel.length = 0;
recordingLength = 0;

if (audioInput) {
audioInput.connect(jsAudioNode);
}

// to prevent self audio to be connected with speakers
// jsAudioNode.connect(context.destination);
resetVariables();

isAudioProcessStarted = isPaused = false;
recording = true;
Expand All @@ -2539,6 +2540,7 @@ function StereoAudioRecorder(mediaStream, config) {
if (numberOfAudioChannels === 2) {
leftBuffers = mergeBuffers(leftBuffers, internalInterleavedLength);
rightBuffers = mergeBuffers(rightBuffers, internalInterleavedLength);

if (desiredSampRate) {
leftBuffers = interpolateArray(leftBuffers, desiredSampRate, sampleRate);
rightBuffers = interpolateArray(rightBuffers, desiredSampRate, sampleRate);
Expand All @@ -2547,6 +2549,7 @@ function StereoAudioRecorder(mediaStream, config) {

if (numberOfAudioChannels === 1) {
leftBuffers = mergeBuffers(leftBuffers, internalInterleavedLength);

if (desiredSampRate) {
leftBuffers = interpolateArray(leftBuffers, desiredSampRate, sampleRate);
}
Expand All @@ -2561,21 +2564,17 @@ function StereoAudioRecorder(mediaStream, config) {
// http://stackoverflow.com/a/28977136/552182
function interpolateArray(data, newSampleRate, oldSampleRate) {
var fitCount = Math.round(data.length * (newSampleRate / oldSampleRate));
//var newData = new Array();
var newData = [];
//var springFactor = new Number((data.length - 1) / (fitCount - 1));
var springFactor = Number((data.length - 1) / (fitCount - 1));
newData[0] = data[0]; // for new allocation
newData[0] = data[0];
for (var i = 1; i < fitCount - 1; i++) {
var tmp = i * springFactor;
//var before = new Number(Math.floor(tmp)).toFixed();
//var after = new Number(Math.ceil(tmp)).toFixed();
var before = Number(Math.floor(tmp)).toFixed();
var after = Number(Math.ceil(tmp)).toFixed();
var atPoint = tmp - before;
newData[i] = linearInterpolate(data[before], data[after], atPoint);
}
newData[fitCount - 1] = data[data.length - 1]; // for new allocation
newData[fitCount - 1] = data[data.length - 1];
return newData;
}

Expand Down Expand Up @@ -2702,8 +2701,7 @@ function StereoAudioRecorder(mediaStream, config) {
});
}

if (!isChrome) {
// its Microsoft Edge
if (isEdge || isOpera || isSafari || config.noWorker) {
mergeAudioBuffers(config, function(data) {
callback(data.buffer, data.view);
});
Expand Down Expand Up @@ -2751,9 +2749,6 @@ function StereoAudioRecorder(mediaStream, config) {
// stop recording
recording = false;

// to make sure onaudioprocess stops firing
// audioInput.disconnect();

mergeLeftRightBuffers({
desiredSampRate: desiredSampRate,
sampleRate: sampleRate,
Expand Down Expand Up @@ -2800,11 +2795,11 @@ function StereoAudioRecorder(mediaStream, config) {
// recorded audio length
self.length = recordingLength;

isAudioProcessStarted = false;

if (callback) {
callback(self.blob);
}

isAudioProcessStarted = false;
});
};

Expand Down Expand Up @@ -2852,7 +2847,7 @@ function StereoAudioRecorder(mediaStream, config) {
throw 'WebAudio API has no support on this browser.';
}

// connect the stream to the gain node
// connect the stream to the script processor
audioInput.connect(jsAudioNode);

if (!config.bufferSize) {
Expand Down Expand Up @@ -2946,13 +2941,24 @@ function StereoAudioRecorder(mediaStream, config) {
clearRecordedDataCB();
};

function clearRecordedDataCB() {
leftchannel.length = rightchannel.length = 0;
function resetVariables() {
leftchannel = [];
rightchannel = [];
recordingLength = 0;
isAudioProcessStarted = false;
recording = false;
isPaused = false;
context = null;

self.leftchannel = leftchannel;
self.rightchannel = rightchannel;
self.numberOfAudioChannels = numberOfAudioChannels;
self.desiredSampRate = desiredSampRate;
self.sampleRate = sampleRate;
self.recordingLength = recordingLength;
}

function clearRecordedDataCB() {
if (jsAudioNode) {
jsAudioNode.onaudioprocess = null;
jsAudioNode.disconnect();
Expand All @@ -2963,6 +2969,8 @@ function StereoAudioRecorder(mediaStream, config) {
audioInput.disconnect();
audioInput = null;
}

resetVariables();
}

// for debugging
Expand All @@ -2987,7 +2995,10 @@ function StereoAudioRecorder(mediaStream, config) {
}

if (!recording) {
audioInput.disconnect();
if (audioInput) {
audioInput.disconnect();
audioInput = null;
}
return;
}

Expand Down Expand Up @@ -3036,6 +3047,7 @@ function StereoAudioRecorder(mediaStream, config) {
this.numberOfAudioChannels = numberOfAudioChannels;
this.desiredSampRate = desiredSampRate;
this.sampleRate = sampleRate;
self.recordingLength = recordingLength;
}

if (typeof RecordRTC !== 'undefined') {
Expand Down Expand Up @@ -3064,7 +3076,7 @@ if (typeof RecordRTC !== 'undefined') {
*/

function CanvasRecorder(htmlElement, config) {
if (typeof html2canvas === 'undefined' && htmlElement.nodeName.toLowerCase() !== 'canvas') {
if (typeof html2canvas === 'undefined') {
throw 'Please link: https://cdn.webrtc-experiment.com/screenshot.js';
}

Expand Down
10 changes: 5 additions & 5 deletions RecordRTC.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "recordrtc",
"version": "5.4.5",
"version": "5.4.6",
"authors": [
{
"name": "Muaz Khan",
Expand Down
2 changes: 1 addition & 1 deletion dev/CanvasRecorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

function CanvasRecorder(htmlElement, config) {
if (typeof html2canvas === 'undefined' && htmlElement.nodeName.toLowerCase() !== 'canvas') {
if (typeof html2canvas === 'undefined') {
throw 'Please link: https://cdn.webrtc-experiment.com/screenshot.js';
}

Expand Down
3 changes: 2 additions & 1 deletion dev/Cross-Browser-Declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ if (typeof navigator !== 'undefined' && typeof navigator.getUserMedia === 'undef

var isEdge = navigator.userAgent.indexOf('Edge') !== -1 && (!!navigator.msSaveBlob || !!navigator.msSaveOrOpenBlob);
var isOpera = !!window.opera || navigator.userAgent.indexOf('OPR/') !== -1;
var isChrome = (!isOpera && !isEdge && !!navigator.webkitGetUserMedia) || isElectron();
var isSafari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
var isChrome = (!isOpera && !isEdge && !!navigator.webkitGetUserMedia) || isElectron() || isSafari;

var MediaStream = window.MediaStream;

Expand Down
5 changes: 5 additions & 0 deletions dev/RecordRTC.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,11 @@ function RecordRTC(mediaStream, config) {
setState('destroyed');
returnObject = self = null;

if (Storage.AudioContextConstructor) {
Storage.AudioContextConstructor.close();
Storage.AudioContextConstructor = null;
}

if (!disableLogs) {
console.warn('RecordRTC is destroyed.');
}
Expand Down
Loading

0 comments on commit b353e13

Please sign in to comment.