Skip to content

Commit f93a63f

Browse files
committed
audio input created as single instance
1 parent 3c595dd commit f93a63f

File tree

3 files changed

+304
-250
lines changed

3 files changed

+304
-250
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Recorder from "recorderjs";
2+
3+
class AudioInput {
4+
5+
constructor() {
6+
this.startUserMedia = this.startUserMedia.bind(this);
7+
8+
// webkit shim
9+
navigator.getUserMedia = navigator.getUserMedia ||
10+
navigator.webkitGetUserMedia ||
11+
navigator.mozGetUserMedia;
12+
13+
if (navigator.getUserMedia) {
14+
navigator.getUserMedia(
15+
{"audio": {
16+
"mandatory": {
17+
"googEchoCancellation": "false",
18+
"googAutoGainControl": "false",
19+
"googNoiseSuppression": "false",
20+
"googHighpassFilter": "false"
21+
},
22+
"optional": []
23+
}},
24+
this.startUserMedia, function(e) {
25+
console.log('No live audio input: ' + e);
26+
});
27+
} else {
28+
console.log("getUserMedia not supported");
29+
}
30+
}
31+
32+
startUserMedia(stream) {
33+
let audioContext = new AudioContext;
34+
let input = audioContext.createMediaStreamSource(stream);
35+
console.log('Media stream created.');
36+
37+
this.recorder = new Recorder(input);
38+
console.log('Recorder initialised.');
39+
}
40+
41+
record() {
42+
this.recorder && this.recorder.record();
43+
console.log('Start recording...');
44+
}
45+
46+
stopRecording(handleSpeech) {
47+
this.recorder && this.recorder.stop();
48+
this.recorder.exportWAV(handleSpeech);
49+
this.recorder.clear();
50+
console.log('Stop recording...');
51+
}
52+
}
53+
54+
export let audioInput = new AudioInput();

app/assets/javascripts/record/VoiceRecorder.jsx

+3-42
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,19 @@
11
import React from "react";
2-
import Recorder from "recorderjs";
2+
import { audioInput } from "./AudioInput.jsx"
33

44
export default class VoiceRecorder extends React.Component {
55
constructor(props){
66
super(props);
7-
this.startUserMedia = this.startUserMedia.bind(this);
87
this.startRecording = this.startRecording.bind(this);
98
this.stopRecording = this.stopRecording.bind(this);
109
}
1110

12-
componentDidMount(){
13-
// webkit shim
14-
navigator.getUserMedia = navigator.getUserMedia ||
15-
navigator.webkitGetUserMedia ||
16-
navigator.mozGetUserMedia;
17-
18-
if (navigator.getUserMedia) {
19-
navigator.getUserMedia(
20-
{"audio": {
21-
"mandatory": {
22-
"googEchoCancellation": "false",
23-
"googAutoGainControl": "false",
24-
"googNoiseSuppression": "false",
25-
"googHighpassFilter": "false"
26-
},
27-
"optional": []
28-
}},
29-
this.startUserMedia, function(e) {
30-
console.log('No live audio input: ' + e);
31-
});
32-
} else {
33-
console.log("getUserMedia not supported");
34-
}
35-
}
36-
37-
startUserMedia(stream) {
38-
let audioContext = new AudioContext;
39-
let input = audioContext.createMediaStreamSource(stream);
40-
console.log('Media stream created.');
41-
42-
this.recorder = new Recorder(input);
43-
console.log('Recorder initialised.');
44-
}
45-
4611
startRecording() {
47-
this.recorder && this.recorder.record();
48-
console.log('Start recording...');
12+
audioInput.record();
4913
}
5014

5115
stopRecording(){
52-
console.log('Stop recording...');
53-
this.recorder && this.recorder.stop();
54-
this.recorder.exportWAV(this.props.handleSpeech);
55-
this.recorder.clear();
16+
audioInput.stopRecording(this.props.handleSpeech);
5617
}
5718

5819
render(){

0 commit comments

Comments
 (0)