-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEncoderFactory.js
114 lines (93 loc) · 3.84 KB
/
EncoderFactory.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
module.exports = function ($log, $q, pcmencoder) {
var Service = {};
var encoder;
var deferredPrepare;
var deferredInit;
var deferredLoad;
var deferredFlush;
var workerOnMessage = function(e) {
//$log.log('EncoderFactory.js :: workerOnMessage cmd:' + e.data.cmd);
switch(e.data.cmd) {
case 'prepareComplete':
deferredPrepare.resolve();
break;
case 'initComplete':
deferredInit.resolve();
break;
case 'loadComplete':
deferredLoad.resolve();
break;
case 'flushComplete':
if(e.data.outputAudio) {
var blob = new Blob([e.data.outputAudio], { type: 'audio/' + e.data.outputFormat });
deferredFlush.resolve({
'format':e.data.outputFormat,
'codec':e.data.outputCodec,
'sampleRate':e.data.outputSampleRate,
'channels':e.data.outputChannels,
'blob':blob
});
} else {
deferredFlush.reject('EncoderFactory.js :: no data received');
}
encoder.postMessage({'cmd':'dispose'});
break;
case 'disposeComplete':
$log.log('EncoderFactory.js :: workerOnMessage cmd:' + e.data.cmd);
break;
default:
$log.error('EncoderFactory.js :: unknown command ' + e.data.cmd);
}
};
var workerOnError = function(e) {
if(e.filename && e.lineno && e.message) {
$log.error('EncoderFactory.js :: listener error ' + e.filename + ' line:' + e.lineno + ' ' + e.message);
} else {
$log.error('EncoderFactory.js :: listener error ' + e);
}
};
//TODO: figure out a better way to make this reference through browserify to get the javascript properly loaded as a webworker
// https://github.com/substack/webworkify
// https://github.com/shama/workerify
// https://github.com/fabiosantoscode/require-emscripten
Service.prepare = function () {
//$log.log('EncoderFactory.js :: prepare');
deferredPrepare = $q.defer();
encoder = new Worker('/js/encoder.js'); //TODO: get wasm build working
//encoder = new Worker('/js/encoder.wasm');
encoder.onmessage = workerOnMessage;
encoder.onerror = workerOnError;
return deferredPrepare.promise;
};
Service.init = function (inputFormat, inputCodec, inputSampleRate, inputChannels, outputFormat, outputCodec, outputSampleRate, outputChannels, outputBitRate, maxSeconds) {
//$log.log('EncoderFactory.js :: init');
deferredInit = $q.defer();
encoder.postMessage({
'cmd':'init',
'inputFormat': inputFormat,
'inputCodec': inputCodec,
'inputSampleRate': inputSampleRate,
'inputChannels': inputChannels,
'outputFormat': outputFormat,
'outputCodec': outputCodec,
'outputSampleRate': outputSampleRate,
'outputChannels': outputChannels,
'outputBitRate': outputBitRate,
'maxSeconds': maxSeconds
});
return deferredInit.promise;
};
Service.load = function(inputAudio) {
//$log.log('EncoderFactory.load');
if(inputAudio !== undefined) {
deferredLoad = $q.defer();
encoder.postMessage({'cmd':'load', 'inputAudio':inputAudio}, [inputAudio]);
return deferredLoad.promise;
} else {
deferredFlush = $q.defer();
encoder.postMessage({'cmd':'load'});
return deferredFlush.promise;
}
};
return Service;
};