Commit cb891005 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Merge mp3 detect/init code into mp3inputstream.cc/hh.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent e52e763b
bin_PROGRAMS = audiowmark
COMMON_SRC = utils.hh utils.cc convcode.hh convcode.cc random.hh random.cc mp3.cc mp3.hh wavdata.cc wavdata.hh \
COMMON_SRC = utils.hh utils.cc convcode.hh convcode.cc random.hh random.cc wavdata.cc wavdata.hh \
audiostream.cc audiostream.hh sfinputstream.cc sfinputstream.hh stdoutwavoutputstream.cc stdoutwavoutputstream.hh \
sfoutputstream.cc sfoutputstream.hh rawinputstream.cc rawinputstream.hh rawoutputstream.cc rawoutputstream.hh \
rawconverter.cc rawconverter.hh mp3inputstream.cc mp3inputstream.hh wmcommon.cc wmcommon.hh fft.cc fft.hh
......
......@@ -24,7 +24,7 @@ AudioInputStream::create (const string& filename, Error& err)
SFInputStream *sistream = new SFInputStream();
in_stream.reset (sistream);
err = sistream->open (filename);
if (err && mp3_detect (filename))
if (err && MP3InputStream::detect (filename))
{
MP3InputStream *mistream = new MP3InputStream();
in_stream.reset (mistream);
......
......@@ -8,87 +8,4 @@
using std::vector;
using std::string;
struct ScopedMHandle
{
mpg123_handle *mh = nullptr;
bool need_close = false;
~ScopedMHandle()
{
if (mh && need_close)
mpg123_close (mh);
if (mh)
mpg123_delete (mh);
}
};
void
mp3_init()
{
static bool mpg123_init_ok = false;
if (!mpg123_init_ok)
{
int err = mpg123_init();
if (err != MPG123_OK)
{
error ("audiowmark: init mpg123 lib failed\n");
exit (1);
}
mpg123_init_ok = true;
}
}
/* there is no really simple way of detecting if something is an mp3
*
* so we try to decode a few frames; if that works without error the
* file is probably a valid mp3
*/
bool
mp3_detect (const string& filename)
{
int err = 0;
mp3_init();
mpg123_handle *mh = mpg123_new (NULL, &err);
if (err != MPG123_OK)
return false;
auto smh = ScopedMHandle { mh }; // cleanup on return
err = mpg123_param (mh, MPG123_ADD_FLAGS, MPG123_QUIET, 0);
if (err != MPG123_OK)
return false;
err = mpg123_open (mh, filename.c_str());
if (err != MPG123_OK)
return false;
smh.need_close = true;
long rate;
int channels;
int encoding;
err = mpg123_getformat (mh, &rate, &channels, &encoding);
if (err != MPG123_OK)
return false;
size_t buffer_bytes = mpg123_outblock (mh);
unsigned char buffer[buffer_bytes];
for (size_t i = 0; i < 30; i++)
{
size_t done;
err = mpg123_read (mh, buffer, buffer_bytes, &done);
if (err == MPG123_DONE)
{
return true;
}
else if (err != MPG123_OK)
{
return false;
}
}
return true;
}
......@@ -5,7 +5,5 @@
#include "wavdata.hh"
bool mp3_detect (const std::string& filename);
void mp3_init();
#endif /* AUDIOWMARK_MP3_HH */
......@@ -5,6 +5,23 @@
#include <assert.h>
using std::min;
using std::string;
static void
mp3_init()
{
static bool mpg123_init_ok = false;
if (!mpg123_init_ok)
{
int err = mpg123_init();
if (err != MPG123_OK)
{
error ("audiowmark: init mpg123 lib failed\n");
exit (1);
}
mpg123_init_ok = true;
}
}
MP3InputStream::~MP3InputStream()
{
......@@ -12,7 +29,7 @@ MP3InputStream::~MP3InputStream()
}
Error
MP3InputStream::open (const std::string& filename)
MP3InputStream::open (const string& filename)
{
int err = 0;
......@@ -165,3 +182,72 @@ MP3InputStream::n_frames() const
{
return m_n_values / m_n_channels;
}
/* there is no really simple way of detecting if something is an mp3
*
* so we try to decode a few frames; if that works without error the
* file is probably a valid mp3
*/
bool
MP3InputStream::detect (const string& filename)
{
struct ScopedMHandle
{
mpg123_handle *mh = nullptr;
bool need_close = false;
~ScopedMHandle()
{
if (mh && need_close)
mpg123_close (mh);
if (mh)
mpg123_delete (mh);
}
};
int err = 0;
mp3_init();
mpg123_handle *mh = mpg123_new (NULL, &err);
if (err != MPG123_OK)
return false;
auto smh = ScopedMHandle { mh }; // cleanup on return
err = mpg123_param (mh, MPG123_ADD_FLAGS, MPG123_QUIET, 0);
if (err != MPG123_OK)
return false;
err = mpg123_open (mh, filename.c_str());
if (err != MPG123_OK)
return false;
smh.need_close = true;
long rate;
int channels;
int encoding;
err = mpg123_getformat (mh, &rate, &channels, &encoding);
if (err != MPG123_OK)
return false;
size_t buffer_bytes = mpg123_outblock (mh);
unsigned char buffer[buffer_bytes];
for (size_t i = 0; i < 30; i++)
{
size_t done;
err = mpg123_read (mh, buffer, buffer_bytes, &done);
if (err == MPG123_DONE)
{
return true;
}
else if (err != MPG123_OK)
{
return false;
}
}
return true;
}
......@@ -35,6 +35,7 @@ public:
int n_channels() const override;
size_t n_frames() const override;
static bool detect (const std::string& filename);
};
#endif /* AUDIOWMARK_MP3_INPUT_STREAM_HH */
......
#include "mp3.hh"
#include "mp3inputstream.hh"
#include "wavdata.hh"
using std::string;
......@@ -9,7 +9,7 @@ main (int argc, char **argv)
WavData wd;
if (argc >= 2)
{
if (mp3_detect (argv[1]))
if (MP3InputStream::detect (argv[1]))
{
MP3InputStream m3i;
Error err = m3i.open (argv[1]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment