|
| 1 | + |
| 2 | +/** |
| 3 | + * Module dependencies. |
| 4 | + */ |
| 5 | + |
| 6 | +var debug = require('debug')('vorbis:encoder'); |
| 7 | +var binding = require('./binding'); |
| 8 | +var inherits = require('util').inherits; |
| 9 | +var Transform = require('stream').Transform; |
| 10 | + |
| 11 | +// node v0.8.x compat |
| 12 | +if (!Transform) Transform = require('readable-stream/transform'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Module exports. |
| 16 | + */ |
| 17 | + |
| 18 | +module.exports = Encoder; |
| 19 | + |
| 20 | +/** |
| 21 | + * The Vorbis `Encoder` class. |
| 22 | + * Accepts PCM audio data and outputs `ogg_packet` Buffer instances. |
| 23 | + * Input must be 32-bit float samples. You may specify the number of `channels` |
| 24 | + * and the `sampleRate`. |
| 25 | + * You may also specify the "quality" which is a float number from -0.1 to 1.0 |
| 26 | + * (low to high quality). If unspecified, the default is 0.6. |
| 27 | + * |
| 28 | + * @param {Object} opts PCM audio format options |
| 29 | + * @api public |
| 30 | + */ |
| 31 | + |
| 32 | +function Encoder (opts) { |
| 33 | + if (!(this instanceof Encoder)) return new Encoder(opts); |
| 34 | + Transform.call(this, opts); |
| 35 | + |
| 36 | + this._writableState.objectMode = true; |
| 37 | + this._writableState.lowWaterMark = 0; |
| 38 | + this._writableState.highWaterMark = 0; |
| 39 | + |
| 40 | + // set to `true` after the headerout() call |
| 41 | + this._headerWritten = false; |
| 42 | + |
| 43 | + // range from -0.1 to 1.0 |
| 44 | + this._quality = 0.6; |
| 45 | + |
| 46 | + this.vi = new Buffer(binding.sizeof_vorbis_info); |
| 47 | + this.vc = new Buffer(binding.sizeof_vorbis_comment); |
| 48 | + binding.vorbis_info_init(this.vi); |
| 49 | + binding.vorbis_comment_init(this.vc); |
| 50 | + |
| 51 | + // the `vorbis_dsp_state` and `vorbis_block` stucts get allocated when the |
| 52 | + // initial 3 packets are being written |
| 53 | + this.vd = null; |
| 54 | + this.vb = null; |
| 55 | +} |
| 56 | +inherits(Encoder, Transform); |
| 57 | + |
| 58 | +/** |
| 59 | + * Adds a vorbis comment to the output ogg stream. |
| 60 | + * All calls to this function must be made *before* any PCM audio data is written |
| 61 | + * to this encoder. |
| 62 | + * |
| 63 | + * @param {String} tag key name (i.e. "ENCODER") |
| 64 | + * @param {String} content value (i.e. "my awesome script") |
| 65 | + * @api public |
| 66 | + */ |
| 67 | + |
| 68 | +Encoder.prototype.addComment = function (tag, contents) { |
| 69 | + if (this.headerWritten) { |
| 70 | + throw new Error('Can\'t add comment since "comment packet" has already been output'); |
| 71 | + } else { |
| 72 | + binding.vorbis_comment_add_tag(this.vc, tag, contents); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Transform stream callback function. |
| 78 | + * |
| 79 | + * @api private |
| 80 | + */ |
| 81 | + |
| 82 | +Encoder.prototype._transform = function (packet, output, fn) { |
| 83 | + debug('_transform(%d bytes)', packet.length); |
| 84 | + if (!this._headerWritten) { |
| 85 | + var err = this._writeHeader(output); |
| 86 | + if (err) return fn(err); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * Initializes the "analysis" data structures and creates the first 3 Vorbis |
| 92 | + * packets to be written to the output ogg stream. |
| 93 | + * |
| 94 | + * @api private |
| 95 | + */ |
| 96 | + |
| 97 | +Encoder.prototype._writeHeader = function (output) { |
| 98 | + debug('_writeHeader()'); |
| 99 | + |
| 100 | + // encoder init (only VBR currently supported) |
| 101 | + var channels = 2; |
| 102 | + var sampleRate = 44100; |
| 103 | + // TODO: async maybe? |
| 104 | + r = binding.vorbis_encode_init_vbr(this.vi, channels, sampleRate, this._quality); |
| 105 | + if (0 !== r) { |
| 106 | + return new Error(r); |
| 107 | + } |
| 108 | + |
| 109 | + // synthesis init |
| 110 | + this.vd = new Buffer(binding.sizeof_vorbis_dsp_state); |
| 111 | + this.vb = new Buffer(binding.sizeof_vorbis_block); |
| 112 | + var r = binding.vorbis_analysis_init(this.vd, this.vi); |
| 113 | + if (0 !== r) return new Error(r); |
| 114 | + r = binding.vorbis_block_init(this.vd, this.vb); |
| 115 | + if (0 !== r) return new Error(r); |
| 116 | + |
| 117 | + // create first 3 packets |
| 118 | + // TODO: async |
| 119 | + var op_header = new Buffer(binding.sizeof_ogg_packet); |
| 120 | + var op_comments = new Buffer(binding.sizeof_ogg_packet); |
| 121 | + var op_code = new Buffer(binding.sizeof_ogg_packet); |
| 122 | + r = binding.vorbis_analysis_headerout(this.vd, this.vc, op_header, op_comments, op_code); |
| 123 | + if (0 !== r) return new Error(r); |
| 124 | + |
| 125 | + output(op_header); // automatically gets placed in its own page |
| 126 | + output(op_comments); |
| 127 | + output(op_code); |
| 128 | + |
| 129 | + // imply that a page flush() call is required |
| 130 | + this.emit('flush'); |
| 131 | + |
| 132 | + this._headerWritten = true; |
| 133 | +}; |
0 commit comments