-
Notifications
You must be signed in to change notification settings - Fork 0
/
prg.recorder.js
266 lines (245 loc) · 6.32 KB
/
prg.recorder.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*! Copyright 2014 Rainer Rillke <lastname@wikipedia.de>
* Quintuple-licensed under
* - GPLv2 and, at your option any later version of that license,
* - LGPL 2.1 and, at your option any later version of that license,
* - Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0),
* - GNU Free Documentation License (GNU FDL aka. GFDL) 1.2
* and, at your option any later version of that license.
* - MIT (http://opensource.org/licenses/MIT)
* For detailed information confer to LICENSE in the repository's root directory.
*
* @author Rainer Rillke
*/
/**
* Pronunciation recording gadget.
* Framework for recording audio in a browser.
*
* Funded by the Wikimedia Foundation as part of an Individual Investment Grant.
* This means the Wikimedia Foundation supported creation materially
* but did not directly endorse creation of this software.
* More information about
* [Individual Investment Grants](https://meta.wikimedia.org/wiki/Grants:IEG).
*
* @class prg.Recorder
* Base recorder class.
* Each recording approach must implement prg.Recorder
* and prg.Recording and those must be shipped together.
*
* @abstract
* @requires jQuery
*/
/*global mediaWiki:false*/
( function( $, global ) {
'use strict';
/**
* Object.create polyfill
* source:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Polyfill
*/
if ( typeof Object.create !== 'function' ) {
( function() {
var F = function() {};
Object.create = function( o ) {
if ( arguments.length > 1 ) {
throw new Error( 'Second argument not supported' );
}
if ( o === null ) {
throw new Error( 'Cannot set a null [[Prototype]]' );
}
if ( typeof o !== 'object' ) {
throw new TypeError( 'Argument must be an object' );
}
F.prototype = o;
return new F();
};
} )();
}
// Make sure the namespace exists
if ( !global.prg ) {
global.prg = {
recorders: [],
compatibleRecorder: null,
version: '0.0.0.1'
};
}
global.prg.Recorder = function() {};
$.extend( global.prg.Recorder.prototype, {
/**
* Compatiblity check for the recorder type on the currently chosen client
*
* @abstract
* @static
* @return {jQuery.Promise}
*/
isCompatible: function() {
var $def = $.Deferred();
$def.reject();
return $def.promise();
},
hasMicrophoneAccess: false,
/**
* Requests access to the microphone.
* This may open a confirmation dialog.
* Must set .hasMicrophoneAccess to true when
* successful and before resolving the Deferred.
*
* @abstract
* @return {jQuery.Promise}
*/
requestMicrophoneAccess: function() {
var $def = $.Deferred();
$def.reject();
return $def.promise();
},
/**
* Create a recording and return a reference to
* that recording.
*
* @abstract
* @return {prg.Recording}
*/
getRecording: function() {
if ( !this.hasMicrophoneAccess ) {
throw new Error( 'Request access to the microphone before attempting to record!' );
}
return new global.prg.Recording();
}
} );
/**
* @class prg.Recording
* Base recording class.
* Represents a recording.
*
* @abstract
* @requires jQuery
*/
global.prg.Recording = function() {};
$.extend( global.prg.Recording.prototype, {
/**
* Starts recording.
*
* @abstract
* @return {jQuery.Promise}
*/
start: function() {
var $def = $.Deferred();
$def.reject();
return $def.promise();
},
/**
* Stops recording.
*
* @abstract
* @return {jQuery.Deferred}
*/
stopRecording: function() {
var $def = $.Deferred();
$def.reject();
return $def.promise();
},
/**
* Get a player element.
* First and only argument to the .done() callback
* of the Deferred is the HTMLElement containing the player.
*
* @abstract
* @return {jQuery.Promise}
*/
getPlayer: function() {
var $def = $.Deferred();
$def.reject();
return $def.promise();
},
/**
* Get a player element containing compressed audio.
* First and only argument to the .done() callback
* of the Deferred is the HTMLElement containing the player.
*
* @abstract
* @return {jQuery.Promise}
*/
getCompressedPlayer: function() {
var $def = $.Deferred();
$def.reject();
return $def.promise();
},
/**
* Plays the previously recorded sound.
*
* @abstract
* @return {jQuery.Promise}
*/
play: function() {
var $def = $.Deferred();
$def.reject();
return $def.promise();
},
/**
* Stops playing the previously recorded sound.
*
* @abstract
* @param {Object} player obtained through `.play()`
* @return {jQuery.Promise}
*/
stopPlay: function( player ) {
var $def = $.Deferred();
$def.reject();
return $def.promise();
},
/**
* Uploads the previously recorded sound.
* Note derived classes can either implement
* `.upload()` or `.getData()` or both.
*
* @abstract
* @method upload
* @return {jQuery.Promise}
*/
upload: null,
/**
* Get the data of the previously recorded sound.
* Note derived classes can either implement
* `.upload()` or `.getData()` or both.
* First and only argument to the .done() callback
* of the Deferred is a blob of the structure
*
* @abstract
* @method getData
* @return {jQuery.Promise}
*/
getData: null,
/**
* Get the data of the previously recorded sound.
* Note derived classes can either implement
* `.upload()` or `.getData()` and `.getCompressedData()`
* or both.
* First and only argument to the .done() callback
* of the Deferred returned by calling this function is a
* blob of the structure
*
* @abstract
* @method getCompressedData
* @return {jQuery.Promise}
*/
getCompressedData: null
} );
/**
* Returns the recorder class that fits best with the
* current hardware.
*
* @abstract
* @method prg.getCompatibleRecorder
* @return {prg.Recorder}
*/
global.prg.getCompatibleRecorder = function() {
var compatibleRecorder = null;
if ( global.prg.compatibleRecorder ) return global.prg.compatibleRecorder;
$.each( global.prg.recorders, function( i, recorder ) {
if ( recorder.prototype.isCompatible() ) {
compatibleRecorder = recorder;
return false;
}
} );
return ( global.prg.compatibleRecorder = compatibleRecorder );
};
} )( jQuery, window.mediaWiki ? mediaWiki.libs : window );