This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.js
87 lines (70 loc) · 2.56 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var recorderApp = angular.module('recorder', [ ]);
recorderApp.controller('RecorderController', [ '$scope' , function($scope) {
$scope.stream = null;
$scope.recording = false;
$scope.encoder = null;
$scope.ws = null;
$scope.input = null;
$scope.node = null;
$scope.samplerate = 22050;
$scope.samplerates = [ 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 ];
$scope.bitrate = 64;
$scope.bitrates = [ 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 192, 224, 256, 320 ];
$scope.recordButtonStyle = "red-btn";
$scope.startRecording = function() {
if ($scope.recording)
return;
console.log('start recording');
$scope.encoder = new Worker('encoder.js');
console.log('initializing encoder with samplerate = ' + $scope.samplerate + ' and bitrate = ' + $scope.bitrate);
$scope.encoder.postMessage({ cmd: 'init', config: { samplerate: $scope.samplerate, bitrate: $scope.bitrate } });
$scope.encoder.onmessage = function(e) {
$scope.ws.send(e.data.buf);
if (e.data.cmd == 'end') {
$scope.ws.close();
$scope.ws = null;
$scope.encoder.terminate();
$scope.encoder = null;
}
};
$scope.ws = new WebSocket("ws://" + window.location.host + "/ws/audio");
$scope.ws.onopen = function() {
navigator.webkitGetUserMedia({ video: false, audio: true }, $scope.gotUserMedia, $scope.userMediaFailed);
};
};
$scope.userMediaFailed = function(code) {
console.log('grabbing microphone failed: ' + code);
};
$scope.gotUserMedia = function(localMediaStream) {
$scope.recording = true;
$scope.recordButtonStyle = '';
console.log('success grabbing microphone');
$scope.stream = localMediaStream;
var audio_context = new window.webkitAudioContext();
$scope.input = audio_context.createMediaStreamSource($scope.stream);
$scope.node = $scope.input.context.createJavaScriptNode(4096, 1, 1);
console.log('sampleRate: ' + $scope.input.context.sampleRate);
$scope.node.onaudioprocess = function(e) {
if (!$scope.recording)
return;
var channelLeft = e.inputBuffer.getChannelData(0);
$scope.encoder.postMessage({ cmd: 'encode', buf: channelLeft });
};
$scope.input.connect($scope.node);
$scope.node.connect(audio_context.destination);
$scope.$apply();
};
$scope.stopRecording = function() {
if (!$scope.recording) {
return;
}
$scope.recordButtonStyle = "red-btn";
console.log('stop recording');
$scope.stream.stop();
$scope.recording = false;
$scope.encoder.postMessage({ cmd: 'finish' });
$scope.input.disconnect();
$scope.node.disconnect();
$scope.input = $scope.node = null;
};
}]);