forked from TooTallNate/node-lame
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathencoder.js
304 lines (256 loc) · 7.3 KB
/
encoder.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Module dependencies.
*/
var assert = require('assert');
var binding = require('./bindings');
var inherits = require('util').inherits;
var Transform = require('readable-stream/transform');
var debug = require('debug')('lame:encoder');
/**
* Module exports.
*/
module.exports = Encoder;
/**
* Constants.
*/
var LAME_OKAY = binding.LAME_OKAY;
var LAME_BADBITRATE = binding.LAME_BADBITRATE;
var LAME_BADSAMPFREQ = binding.LAME_BADSAMPFREQ;
var LAME_INTERNALERROR = binding.LAME_INTERNALERROR;
var PCM_TYPE_SHORT_INT = binding.PCM_TYPE_SHORT_INT;
var PCM_TYPE_FLOAT = binding.PCM_TYPE_FLOAT;
var PCM_TYPE_DOUBLE = binding.PCM_TYPE_DOUBLE;
/**
* Messages for error codes returned from the lame C encoding functions.
*/
var ERRORS = {
'-1': 'output buffer too small',
'-2': 'malloc() problems',
'-3': 'lame_init_params() not called',
'-4': 'psycho acoustic problems'
};
/**
* Map of libmp3lame functions to node-lame property names.
*/
var PROPS = {
'brate': 'bitRate',
'num_channels': 'channels',
'bWriteVbrTag': 'writeVbrTag',
'in_samplerate': 'sampleRate',
'out_samplerate': 'outSampleRate'
};
/**
* The valid bit depths that lame supports encoding in.
*/
var INT_BITS = binding.sizeof_int * 8;
var SHORT_BITS = binding.sizeof_short * 8;
var FLOAT_BITS = binding.sizeof_float * 8;
var DOUBLE_BITS = binding.sizeof_double * 8;
/**
* The `Encoder` class is a Transform stream class.
* Write raw PCM data, out comes an MP3 file.
*
* @param {Object} opts PCM stream format info and stream options
* @api public
*/
function Encoder (opts) {
if (!(this instanceof Encoder)) {
return new Encoder(opts);
}
Transform.call(this, opts);
// lame malloc()s the "gfp" buffer
this.gfp = binding.lame_init();
// set default options
if (!opts) opts = {};
if (null == opts.channels) opts.channels = 2;
if (null == opts.bitDepth) opts.bitDepth = 16;
if (null == opts.sampleRate) opts.sampleRate = 44100;
if (null == opts.signed) opts.signed = opts.bitDepth != 8;
if (opts.float && opts.bitDepth == DOUBLE_BITS) {
this.inputType = PCM_TYPE_DOUBLE;
} else if (opts.float && opts.bitDepth == FLOAT_BITS) {
this.inputType = PCM_TYPE_FLOAT;
} else if (!opts.float && opts.bitDepth == SHORT_BITS) {
this.inputType = PCM_TYPE_SHORT_INT;
} else {
throw new Error('unsupported PCM format!');
}
// copy over opts to the encoder instance
Object.keys(opts).forEach(function(key){
if (key[0] != '_' && Encoder.prototype.hasOwnProperty(key)) {
debug('setting opt %j', key);
this[key] = opts[key];
}
}, this);
}
inherits(Encoder, Transform);
/**
* Default PCM format: signed 16-bit little endian integer samples.
*/
Encoder.prototype.bitDepth = 16;
/**
* Called one time at the beginning of the first `_transform()` call.
*
* @api private
*/
Encoder.prototype._init = function () {
debug('_init()');
var r = binding.lame_init_params(this.gfp);
if (LAME_OKAY !== r) {
throw new Error('error initializing params: ' + r);
}
// constant: number of 'bytes per sample'
this.blockAlign = this.bitDepth / 8 * this.channels;
};
/**
* Calls `lame_encode_buffer_interleaved()` on the given "chunk.
*
* @api private
*/
Encoder.prototype._transform = function (chunk, encoding, done) {
debug('_transform (%d bytes)', chunk.length);
var self = this;
if (!this._initCalled) {
try { this._init(); } catch (e) { return done(e); }
this._initCalled = true;
}
// first handle any _remainder
if (this._remainder) {
debug('concating remainder');
chunk = Buffer.concat([ this._remainder, chunk ]);
this._remainder = null;
}
// set any necessary _remainder (we can only send whole samples at a time)
var remainder = chunk.length % this.blockAlign;
if (remainder > 0) {
debug('setting remainder of %d bytes', remainder);
var slice = chunk.length - remainder;
this._remainder = chunk.slice(slice);
chunk = chunk.slice(0, slice);
}
assert.equal(chunk.length % this.blockAlign, 0);
var num_samples = chunk.length / this.blockAlign;
// TODO: Use better calculation logic from lame.h here
var estimated_size = 1.25 * num_samples + 7200;
var output = Buffer.alloc(estimated_size);
debug('encoding %d byte chunk with %d byte output buffer (%d samples)', chunk.length, output.length, num_samples);
binding.lame_encode_buffer(
this.gfp,
chunk,
this.inputType,
this.channels,
num_samples,
output,
0,
output.length,
cb
);
function cb (bytesWritten) {
debug('after lame_encode_buffer() (rtn: %d)', bytesWritten);
if (bytesWritten < 0) {
var err = new Error(ERRORS[bytesWritten]);
err.code = bytesWritten;
done(err);
} else if (bytesWritten > 0) {
output = output.slice(0, bytesWritten);
debug('writing %d MP3 bytes', output.length);
self.push(output);
done();
} else { // bytesWritten == 0
done();
}
}
};
/**
* Calls `lame_encode_flush_nogap()` on the thread pool.
*/
Encoder.prototype._flush = function (done) {
debug('_flush');
var self = this;
var estimated_size = 7200; // value specified in lame.h
var output = Buffer.alloc(estimated_size);
if (!this._initCalled) {
try { this._init(); } catch (e) { return done(e); }
this._initCalled = true;
}
binding.lame_encode_flush_nogap(
this.gfp,
output,
0,
output.length,
cb
);
function cb (bytesWritten) {
debug('after lame_encode_flush_nogap() (rtn: %d)', bytesWritten);
binding.lame_close(self.gfp);
self.gfp = null;
if (bytesWritten < 0) {
var err = new Error(ERRORS[bytesWritten]);
err.code = bytesWritten;
done(err);
} else if (bytesWritten > 0) {
output = output.slice(0, bytesWritten);
self.push(output);
done();
} else { // bytesWritten == 0
done();
}
}
};
/**
* Define the getter/setters for the lame encoder settings.
*/
Object.keys(binding).forEach(function (key) {
if (!/^lame_[gs]et/.test(key)) return;
var name = key.substring(9);
var prop = PROPS[name] || toCamelCase(name);
debug('processing prop %j as %j', key, prop);
var getter = 'g' == key[5];
/*
console.error({
key: key,
name: name,
prop: prop,
getter: getter
});
*/
var desc = Object.getOwnPropertyDescriptor(Encoder.prototype, prop);
if (!desc) desc = { enumerable: true, configurable: true };
if (getter) {
desc.get = function () {
debug('%s()', key);
return binding[key](this.gfp);
};
} else {
desc.set = function (v) {
debug('%s(%j)', key, v);
var r = binding[key](this.gfp, v);
if (LAME_OKAY !== r) {
throw new Error('error setting prop "' + prop + '": ' + r);
}
return r;
};
}
Object.defineProperty(Encoder.prototype, prop, desc);
});
/**
* The lame encoder only supports "signed" data types.
*/
Object.defineProperty(Encoder.prototype, 'signed', {
enumerable: true,
configurable: true,
get: function () { return true; },
set: function (v) { if (!v) throw new Error('"signed" must be `true`'); }
});
/**
* Converts a string_with_underscores to camelCase.
*
* @param {String} name The name to convert.
* @return {String} The camel case'd name.
* @api private
*/
function toCamelCase (name) {
return name.replace(/(\_[a-zA-Z])/g, function ($1) {
return $1.toUpperCase().replace('_', '');
});
}