From 3cfadc89df6ced41178e5e3360de4553ff6a74f7 Mon Sep 17 00:00:00 2001 From: Benjamin Eidelman Date: Tue, 13 May 2014 10:45:20 -0300 Subject: [PATCH] decoder input mp3 info --- examples/mp32wav.js | 3 ++- lib/decoder.js | 10 ++++++++++ src/node_mpg123.cc | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/examples/mp32wav.js b/examples/mp32wav.js index 2cad1a8..2729712 100644 --- a/examples/mp32wav.js +++ b/examples/mp32wav.js @@ -47,7 +47,8 @@ decoder.on('format', onFormat); input.pipe(decoder); function onFormat (format) { - console.error('MP3 format: %j', format); + console.error('MP3 info: %j', decoder.info()); + console.error('output format: %j', format); // write the decoded MP3 data into a WAV file var writer = new wav.Writer(format); diff --git a/lib/decoder.js b/lib/decoder.js index 1e5bf69..64a7410 100644 --- a/lib/decoder.js +++ b/lib/decoder.js @@ -153,3 +153,13 @@ Decoder.prototype._transform = function (chunk, encoding, done) { read(); } }; + +/** + * Calls `mpg123_info()` to get info about the input mp3 format. + */ + +Decoder.prototype.info = function () { + var info = binding.mpg123_info(this.mh); + debug('info: %j', info); + return info; +}; diff --git a/src/node_mpg123.cc b/src/node_mpg123.cc index d544803..db8f192 100644 --- a/src/node_mpg123.cc +++ b/src/node_mpg123.cc @@ -139,6 +139,30 @@ Handle node_mpg123_getformat (const Arguments& args) { return scope.Close(rtn); } +Handle node_mpg123_info (const Arguments& args) { + UNWRAP_MH; + mpg123_frameinfo info; + int ret; + Local rtn; + ret = mpg123_info(mh, &info); + if (ret == MPG123_OK) { + Local o = Object::New(); + o->Set(String::NewSymbol("version"), Number::New(info.version)); + o->Set(String::NewSymbol("layer"), Number::New(info.layer)); + o->Set(String::NewSymbol("bitRate"), Number::New(info.bitrate)); + o->Set(String::NewSymbol("sampleRate"), Number::New(info.rate)); + o->Set(String::NewSymbol("mode"), Number::New(info.mode)); + o->Set(String::NewSymbol("modeExtension"), Number::New(info.mode_ext)); + o->Set(String::NewSymbol("frameSize"), Number::New(info.framesize)); + o->Set(String::NewSymbol("emphasis"), Number::New(info.emphasis)); + o->Set(String::NewSymbol("vbr"), Number::New(info.vbr)); + o->Set(String::NewSymbol("averageBitRate"), Number::New(info.abr_rate)); + rtn = o; + } else { + rtn = Integer::New(ret); + } + return scope.Close(rtn); +} Handle node_mpg123_safe_buffer (const Arguments& args) { HandleScope scope; @@ -502,6 +526,7 @@ void InitMPG123(Handle target) { NODE_SET_METHOD(target, "mpg123_current_decoder", node_mpg123_current_decoder); NODE_SET_METHOD(target, "mpg123_supported_decoders", node_mpg123_supported_decoders); NODE_SET_METHOD(target, "mpg123_getformat", node_mpg123_getformat); + NODE_SET_METHOD(target, "mpg123_info", node_mpg123_info); NODE_SET_METHOD(target, "mpg123_safe_buffer", node_mpg123_safe_buffer); NODE_SET_METHOD(target, "mpg123_outblock", node_mpg123_outblock); NODE_SET_METHOD(target, "mpg123_framepos", node_mpg123_framepos);