-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
audioRecorder.ts
136 lines (111 loc) · 3.25 KB
/
audioRecorder.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2016-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as log from '../logging/log';
import { WebAudioRecorderClass } from '../window.d';
export class RecorderClass {
private context?: AudioContext;
private input?: GainNode;
private recorder?: WebAudioRecorderClass;
private source?: MediaStreamAudioSourceNode;
private blob?: Blob;
private resolve?: (blob: Blob) => void;
clear(): void {
this.blob = undefined;
this.resolve = undefined;
if (this.source) {
this.source.disconnect();
this.source = undefined;
}
if (this.recorder) {
if (this.recorder.isRecording()) {
this.recorder.cancelRecording();
}
// Reach in and terminate the web worker used by WebAudioRecorder, otherwise
// it gets leaked due to a reference cycle with its onmessage listener
this.recorder.worker.terminate();
this.recorder = undefined;
}
this.input = undefined;
if (this.context) {
this.context.close();
this.context = undefined;
}
}
async start(): Promise<void> {
this.clear();
this.context = new AudioContext();
this.input = this.context.createGain();
this.recorder = new window.WebAudioRecorder(this.input, {
encoding: 'mp3',
workerDir: 'js/', // must end with slash
options: {
timeLimit: 360, // one minute more than our UI-imposed limit
},
});
this.recorder.onComplete = this.onComplete.bind(this);
this.recorder.onError = this.onError.bind(this);
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
if (!this.context || !this.input) {
this.onError(
this.recorder,
new Error('Recorder/getUserMedia/stream: Missing context or input!')
);
return;
}
this.source = this.context.createMediaStreamSource(stream);
this.source.connect(this.input);
} catch (err) {
log.error(
'Recorder.onGetUserMediaError:',
err && err.stack ? err.stack : err
);
this.clear();
throw err;
}
if (this.recorder) {
this.recorder.startRecording();
}
}
async stop(): Promise<Blob | undefined> {
if (!this.recorder) {
log.warn('Recorder/stop: Called with no recorder');
return;
}
if (this.blob) {
return this.blob;
}
const promise = new Promise<Blob>(resolve => {
this.resolve = resolve;
});
this.recorder.finishRecording();
return promise;
}
onComplete(_recorder: WebAudioRecorderClass, blob: Blob): void {
this.blob = blob;
this.resolve?.(blob);
}
onError(_recorder: WebAudioRecorderClass, error: Error): void {
if (!this.recorder) {
log.warn('Recorder/onError: Called with no recorder');
return;
}
this.clear();
if (error && error.name === 'NotAllowedError') {
log.warn('Recorder/onError: Microphone permission missing');
window.showPermissionsPopup();
} else {
log.error(
'Recorder/onError:',
error && error.stack ? error.stack : error
);
}
}
getBlob(): Blob {
if (!this.blob) {
throw new Error('no blob found');
}
return this.blob;
}
}
export const recorder = new RecorderClass();