forked from manofearth/microm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microm.js
231 lines (202 loc) · 4.79 KB
/
microm.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var adapter = require("./bower_components/webrtc-adapter/adapter.js");
var RecordRTC = require("./bower_components/recordrtc/RecordRTC.js");
var Promise = require('rsvp').Promise;
var Converter = require('./lib/converter');
var Player = require('./lib/player');
var config = require('./lib/config');
config.setup();
class Microm {
constructor() {
this.isRecording = false;
this.recordRTC = null;
this.player = null;
this.mp3 = null;
this.stream = null;
this.eventListeners = {};
this.converter = new Converter();
}
/**
* Request browser microphone access and waits for it resolution.
* If the user grant access, Microm will start recording the audio.
*
* @return {Promise}
*/
record() {
this.isRecording = true;
var media = navigator.mediaDevices.getUserMedia({audio: true})
media.then(this.startUserMedia.bind(this)).catch(this.onUserMediaError.bind(this));
return media;
}
stopRecording() {
var self = this;
this.isRecording = false;
this.stream = null;
return new Promise((resolve, reject) => {
self.recordRTC.stopRecording(() => {
self.getMp3().then((mp3) => {
self.mp3 = mp3;
self.player = new Player(mp3.url, self);
resolve(mp3);
})
});
});
}
/**
* Reproduce the player audio.
*
* @return {void}
*/
play() {
this.player.play();
}
/**
* Pauses the player.
*
* @return {void}
*/
pause() {
this.player.pause();
}
/**
* Stops recording audio if Micron is recording, if not
* just pauses the player and set's the currentTime to 0.
*
* @example
* microm.stop().then(function(mp3) {
* console.log(mp3.url, mp3.blob);
* });
*
* @return {Promise} Will be resolved with the mp3.
*/
stop() {
if (this.isRecording) {
return this.stopRecording();
}
return new Promise((resolve, reject) => {
this.player.stop();
resolve(this.mp3);
});
}
/**
* Returns all mp3 info.
* Right now we are converting the recorded data
* everytime this function it's called.
*
* @return {Promise}
*/
getMp3() {
var blob = this.recordRTC.getBlob();
// TODO: throw error if we don't have recordedData yet
return this.converter.toMp3(blob);
}
/**
* Blob enconded as Wav.
*
* @return {Blob}
*/
getWav() {
}
/**
* Link to the mp3.
* It can be used as a audio "src" value
*
* @example
* microm.getUrl();
* // Something like --> "blob:http%3A//localhost%3A8090/8b40fc63-8bb7-42e3-9622-9dcc59e5df8f"
*
* @return {String}
*/
getUrl() {
// TODO: throw error if mp3 is not ready
return this.mp3.url;
}
/**
* Blob value of the recorded data.
*
* @return {Blob}
*/
getBlob() {
// TODO: throw error if mp3 is not ready
return this.mp3.blob;
}
/**
* ArrayBuffer of the recorded data (raw binary data buffer).
*
* @return {ArrayBuffer}
*/
getBuffer() {
// TODO: throw error if mp3 is not ready
return this.mp3.buffer;
}
/**
* Base64 value of the recorded data.
*
* @example
* microm.getBase64().then(function(base64) {
* console.log(base64);
* });
*
* @return {Promise}
*/
getBase64() {
// TODO: throw error if mp3 is not ready
return this.converter.toBase64(this.getBlob());
}
/**
* Stream returned by getUserMedia. Null if not recording.
*
* @example
* console.log(microm.getStream());
*
* @return {stream}
*/
getStream() {
return this.stream;
}
/**
* Forces file download.
*
* @param {String} fileName
*
* @return {void}
*/
download(fileName = 'micro_record') {
var link = document.createElement('a');
var click = document.createEvent("Event");
link.href = this.getUrl();
link.download = `${fileName}.mp3`;
click.initEvent("click", true, true);
link.dispatchEvent(click);
}
startUserMedia(stream) {
var recordRTC = RecordRTC(stream, {type: 'audio', recorderType: RecordRTC.StereoAudioRecorder});
recordRTC.startRecording();
this.recordRTC = recordRTC;
this.isRecording = true;
this.stream = stream;
return stream;
}
onUserMediaError(e) {
console.log(e);
}
/**
* Attach an event handler function for event name
* @param {String} eventName
* @param {Function} handler
* @return {void}
*/
on(eventName, handler) {
// TODO: throw error if type of handler is not a function
this.eventListeners[eventName] = handler;
}
/**
* Remove an event handler
* @param {String} eventName
* @return {void}
*/
off(eventName) {
//TODO: Warn if there's not eventName attached
delete this.eventListeners[eventName];
}
}
module.exports = Microm;