forked from itsjoesullivan/iago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (71 loc) · 2.24 KB
/
index.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
var Thread = require('thread');
var Iago = module.exports = function(object) {
this.thread = new Thread(function() {
// Import vorbis into worker
importScripts( 'https://s3.amazonaws.com/scat/lib/iago/vorbis.js' );
// Initialize
var state = Module._lexy_encoder_start(44100, 3);
// On writes, write to vorbis
thread.on('write', function(data) {
var l = data[0];
var r = data[1];
var inbuf_l = Module._malloc(l.length*l.BYTES_PER_ELEMENT);
Module.HEAPF32.set( l, inbuf_l>>2 );
var inbuf_r = Module._malloc(r.length*r.BYTES_PER_ELEMENT);
Module.HEAPF32.set( r, inbuf_r>>2 );
Module._lexy_encoder_write( state, inbuf_l, inbuf_r, l.length );
Module._free(inbuf_l);
Module._free(inbuf_r);
});
thread.on('finish', function(e) {
// Wrap up
Module._lexy_encoder_finish( state );
// Get pointer
var ptr = Module._lexy_get_buffer( state );
var oggData = new Uint8Array( Module.HEAPU8.subarray( ptr,
ptr + Module._lexy_get_buffer_length( state ) )
);
thread.send('blob', oggData);
});
});
if (object instanceof window.MediaStreamAudioSourceNode) {
this.source = object;
this.context = this.source.context;
}
this.input = this.context.createScriptProcessor(4096, 2, 2);
this.input.onaudioprocess = this.write.bind(this);
this.input.connect(this.context.destination);
if (object instanceof window.MediaStreamAudioSourceNode) {
this.source.connect( this.input );
}
};
Iago.prototype.write = function( e ) {
if (this.done) {
// No more writes"
return;
}
this.thread.send('write',
e.inputBuffer.getChannelData(0),
e.inputBuffer.getChannelData(1) );
};
Iago.prototype.getBlob = function(cb) {
if (this.blob) {
return cb( null, this.blob );
}
this.done = true;
this.thread.on('blob', function( data ) {
this.blob = new Blob( [ data ], {
type: 'audio/ogg'
});
cb( null, this.blob );
}.bind(this));
this.thread.send('finish');
};
Iago.prototype.download = function() {
// Download
var a = document.createElement('a');
a.href = window.URL.createObjectURL(this.blob);
a.download = 'test.ogg';
document.body.appendChild(a);
a.click();
};