Skip to content

Commit b6f14b4

Browse files
committed
Added [email protected], Merged & Closed #85 (PCM RAW Audio)
Returning Blob rather than ArrayBuffer. You can use FileReader API to get original buffer.
1 parent 7f5bcd5 commit b6f14b4

8 files changed

+71
-11
lines changed

AudioStreamRecorder/StereoAudioRecorderHelper.js

+25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function StereoAudioRecorderHelper(mediaStream, root) {
2323
var volume;
2424
var audioInput;
2525
var sampleRate = root.sampleRate || deviceSampleRate;
26+
27+
var mimeType = root.mimeType || 'audio/wav';
28+
var isPCM = mimeType.indexOf('audio/pcm') > -1;
29+
2630
var context;
2731

2832
var numChannels = root.audioChannels || 2;
@@ -66,6 +70,17 @@ function StereoAudioRecorderHelper(mediaStream, root) {
6670
interleaved = interleave(leftBuffer, rightBuffer);
6771
}
6872

73+
if (isPCM) {
74+
// our final binary blob
75+
var blob = new Blob([convertoFloat32ToInt16(interleaved)], {
76+
type: 'audio/pcm'
77+
});
78+
79+
console.debug('audio recorded blob size:', bytesToSize(blob.size));
80+
root.ondataavailable(blob);
81+
return;
82+
}
83+
6984
// we create our wav file
7085
var buffer = new ArrayBuffer(44 + interleaved.length * 2);
7186
var view = new DataView(buffer);
@@ -151,6 +166,16 @@ function StereoAudioRecorderHelper(mediaStream, root) {
151166
}
152167
}
153168

169+
function convertoFloat32ToInt16(buffer) {
170+
var l = buffer.length;
171+
var buf = new Int16Array(l)
172+
173+
while (l--) {
174+
buf[l] = buffer[l] * 0xFFFF; //convert to 16 bit
175+
}
176+
return buf.buffer
177+
}
178+
154179
// creates the audio context
155180
var context = ObjectStore.AudioContextConstructor;
156181

MediaStreamRecorder.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Last time updated: 2016-07-03 8:17:15 AM UTC
1+
// Last time updated: 2016-07-03 8:51:35 AM UTC
22

33
// links:
44
// Open-Sourced: https://github.com/streamproc/MediaStreamRecorder
@@ -51,7 +51,8 @@ function MediaStreamRecorder(mediaStream) {
5151
}
5252

5353
// audio/wav is supported only via StereoAudioRecorder
54-
if (this.mimeType === 'audio/wav') {
54+
// audio/pcm (int16) is supported only via StereoAudioRecorder
55+
if (this.mimeType === 'audio/wav' || this.mimeType === 'audio/pcm') {
5556
Recorder = StereoAudioRecorder;
5657
}
5758

@@ -988,6 +989,10 @@ function StereoAudioRecorderHelper(mediaStream, root) {
988989
var volume;
989990
var audioInput;
990991
var sampleRate = root.sampleRate || deviceSampleRate;
992+
993+
var mimeType = root.mimeType || 'audio/wav';
994+
var isPCM = mimeType.indexOf('audio/pcm') > -1;
995+
991996
var context;
992997

993998
var numChannels = root.audioChannels || 2;
@@ -1031,6 +1036,17 @@ function StereoAudioRecorderHelper(mediaStream, root) {
10311036
interleaved = interleave(leftBuffer, rightBuffer);
10321037
}
10331038

1039+
if (isPCM) {
1040+
// our final binary blob
1041+
var blob = new Blob([convertoFloat32ToInt16(interleaved)], {
1042+
type: 'audio/pcm'
1043+
});
1044+
1045+
console.debug('audio recorded blob size:', bytesToSize(blob.size));
1046+
root.ondataavailable(blob);
1047+
return;
1048+
}
1049+
10341050
// we create our wav file
10351051
var buffer = new ArrayBuffer(44 + interleaved.length * 2);
10361052
var view = new DataView(buffer);
@@ -1116,6 +1132,16 @@ function StereoAudioRecorderHelper(mediaStream, root) {
11161132
}
11171133
}
11181134

1135+
function convertoFloat32ToInt16(buffer) {
1136+
var l = buffer.length;
1137+
var buf = new Int16Array(l)
1138+
1139+
while (l--) {
1140+
buf[l] = buffer[l] * 0xFFFF; //convert to 16 bit
1141+
}
1142+
return buf.buffer
1143+
}
1144+
11191145
// creates the audio context
11201146
var context = ObjectStore.AudioContextConstructor;
11211147

MediaStreamRecorder.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Then link single/standalone "MediaStreamRecorder.js" file:
7878
<script src="https://www.webrtc-experiment.com/MediaStreamRecorder.js"> </script>
7979

8080
<!-- or link specific release -->
81-
<script src="https://github.com/streamproc/MediaStreamRecorder/releases/download/1.3.3/MediaStreamRecorder.js"></script>
81+
<script src="https://github.com/streamproc/MediaStreamRecorder/releases/download/1.3.4/MediaStreamRecorder.js"></script>
8282
```
8383

8484
## Record audio+video
@@ -406,6 +406,7 @@ videoRecorder.mimeType = 'video/mp4';
406406
audioRecorder.mimeType = 'audio/webm'; // MediaRecorderWrapper
407407
audioRecorder.mimeType = 'audio/ogg'; // MediaRecorderWrapper
408408
audioRecorder.mimeType = 'audio/wav'; // StereoAudioRecorder
409+
audioRecorder.mimeType = 'audio/pcm'; // StereoAudioRecorder
409410

410411
// gif:
411412
gifRecorder.mimeType = 'image/gif'; // GifRecorder

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "msr",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"authors": [
55
{
66
"name": "Muaz Khan",

common/MediaStreamRecorder.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ function MediaStreamRecorder(mediaStream) {
2727
}
2828

2929
// audio/wav is supported only via StereoAudioRecorder
30-
if (this.mimeType === 'audio/wav') {
30+
// audio/pcm (int16) is supported only via StereoAudioRecorder
31+
if (this.mimeType === 'audio/wav' || this.mimeType === 'audio/pcm') {
3132
Recorder = StereoAudioRecorder;
3233
}
3334

demos/audio-recorder.html

+9-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ <h1>
7676
<select id="audio-recorderType" style="font-size:22px;vertical-align: middle;margin-right: 5px;">
7777
<option>[Best Available Recorder]</option>
7878
<option>MediaRecorder API</option>
79-
<option>WebAudio API</option>
79+
<option>WebAudio API (WAV)</option>
80+
<option>WebAudio API (PCM)</option>
8081
</select>
8182
<br>
8283

@@ -167,8 +168,14 @@ <h1>
167168
mediaRecorder.recorderType = MediaRecorderWrapper;
168169
}
169170

170-
if (recorderType === 'WebAudio API') {
171+
if (recorderType === 'WebAudio API (WAV)') {
171172
mediaRecorder.recorderType = StereoAudioRecorder;
173+
mediaRecorder.mimeType = 'audio/wav';
174+
}
175+
176+
if (recorderType === 'WebAudio API (PCM)') {
177+
mediaRecorder.recorderType = StereoAudioRecorder;
178+
mediaRecorder.mimeType = 'audio/pcm';
172179
}
173180

174181
// don't force any mimeType; use above "recorderType" instead.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "msr",
33
"preferGlobal": false,
4-
"version": "1.3.3",
4+
"version": "1.3.4",
55
"author": {
66
"name": "Muaz Khan",
77
"email": "[email protected]",

0 commit comments

Comments
 (0)