From 6b7c665baaf6023ee1cd935a953711fe65fff73b Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 28 Dec 2010 02:20:11 +0100 Subject: Added mpg123 decoder. --- SConstruct | 4 +++- decoder.h | 11 ++++++++++ decoders/mpg123_decoder.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++ decoders/mpg123_decoder.h | 18 ++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 decoder.h create mode 100644 decoders/mpg123_decoder.cpp create mode 100644 decoders/mpg123_decoder.h diff --git a/SConstruct b/SConstruct index 07e2a04..f29694c 100644 --- a/SConstruct +++ b/SConstruct @@ -13,6 +13,8 @@ if GetOption('release'): else: env.Append(CCFLAGS = ['-Wall', '-g']) -env.Program('audistd', Glob('*.cpp')) +env.ParseConfig('pkg-config --cflags --libs libmpg123') + +env.Program('audistd', Glob('*.cpp') + Glob('decoders/*.cpp')) # vim: syn=python diff --git a/decoder.h b/decoder.h new file mode 100644 index 0000000..7e76088 --- /dev/null +++ b/decoder.h @@ -0,0 +1,11 @@ +#ifndef DECODER_H +#define DECODER_H + +#include + +class DecoderBase { + public: + virtual size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0; +}; + +#endif diff --git a/decoders/mpg123_decoder.cpp b/decoders/mpg123_decoder.cpp new file mode 100644 index 0000000..41aef40 --- /dev/null +++ b/decoders/mpg123_decoder.cpp @@ -0,0 +1,52 @@ +#include "mpg123_decoder.h" + +#include + +#include +#include + +DecoderMpg123::DecoderMpg123() { + mpg123_init(); + + int error; + handle = mpg123_new("generic", &error); + if(error) { + throw std::runtime_error(mpg123_plain_strerror(error)); + } + + error = mpg123_open_feed(handle); + if(error) { + throw std::runtime_error(mpg123_plain_strerror(error)); + } + + mpg123_format_none(handle); + mpg123_format(handle, 44100, 2, MPG123_ENC_SIGNED_16); +} + +DecoderMpg123::~DecoderMpg123() { + mpg123_close(handle); + mpg123_delete(handle); +} + +size_t DecoderMpg123::decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) { + if(mpg123_feed(handle, input, input_size) != MPG123_OK) { + throw std::runtime_error(mpg123_strerror(handle)); + } + + size_t output_written; + int error = mpg123_read(handle, output, output_size, &output_written); + + if(error == MPG123_NEW_FORMAT) { + long rate; + int channels, enc; + mpg123_getformat(handle, &rate, &channels, &enc); + std::cout << boost::format("mpg123: New format: %li Hz, %i channels, encoding value %i") % rate % channels % enc << std::endl; + error = mpg123_read(handle, output, output_size, &output_written); + } + + if(error != MPG123_OK && error != MPG123_DONE && error != MPG123_NEED_MORE) { + throw std::runtime_error(mpg123_plain_strerror(error)); + } + + return output_written; +} diff --git a/decoders/mpg123_decoder.h b/decoders/mpg123_decoder.h new file mode 100644 index 0000000..46f9e4c --- /dev/null +++ b/decoders/mpg123_decoder.h @@ -0,0 +1,18 @@ +#ifndef DECODER_MPG123_H +#define DECODER_MPG123_H + +#include "decoder.h" + +#include + +class DecoderMpg123 : public DecoderBase { + private: + mpg123_handle *handle; + + public: + DecoderMpg123(); + ~DecoderMpg123(); + size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size); +}; + +#endif -- cgit v1.2.3