Commit fb503349 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Cleanup old mp3 loader.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent b0b7371e
......@@ -92,92 +92,3 @@ mp3_detect (const string& filename)
}
return true;
}
string
mp3_load (const string& filename, WavData& wav_data)
{
int err = 0;
mp3_init();
mpg123_handle *mh = mpg123_new (NULL, &err);
if (err != MPG123_OK)
return "mpg123_new failed";
auto smh = ScopedMHandle { mh }; // cleanup on return
err = mpg123_param (mh, MPG123_ADD_FLAGS, MPG123_QUIET, 0);
if (err != MPG123_OK)
return "setting quiet mode failed";
// allow arbitary amount of data for resync */
err = mpg123_param (mh, MPG123_RESYNC_LIMIT, -1, 0);
if (err != MPG123_OK)
return "setting resync limit parameter failed";
// force floating point output
{
const long *rates;
size_t rate_count;
mpg123_format_none (mh);
mpg123_rates (&rates, &rate_count);
for (size_t i = 0; i < rate_count; i++)
{
err = mpg123_format (mh, rates[i], MPG123_MONO|MPG123_STEREO, MPG123_ENC_FLOAT_32);
if (err != MPG123_OK)
return mpg123_strerror (mh);
}
}
err = mpg123_open (mh, filename.c_str());
if (err != MPG123_OK)
return mpg123_strerror (mh);
smh.need_close = true;
long rate;
int channels;
int encoding;
err = mpg123_getformat (mh, &rate, &channels, &encoding);
if (err != MPG123_OK)
return mpg123_strerror (mh);
/* ensure that the format will not change */
mpg123_format_none (mh);
mpg123_format (mh, rate, channels, encoding);
size_t buffer_bytes = mpg123_outblock (mh);
assert (buffer_bytes % sizeof (float) == 0);
vector<float> buffer (buffer_bytes / sizeof (float));
vector<float> samples;
while (true)
{
size_t done = 0;
err = mpg123_read (mh, reinterpret_cast<unsigned char *> (&buffer[0]), buffer_bytes, &done);
if (err == MPG123_OK)
{
const size_t n_values = done / sizeof (float);
samples.insert (samples.end(), buffer.begin(), buffer.begin() + n_values);
}
else if (err == MPG123_DONE)
{
wav_data = WavData (samples, channels, rate, 24);
return ""; /* success */
}
else if (err == MPG123_NEED_MORE)
{
// some mp3s have this error before reaching eof -> harmless
}
else
{
return mpg123_strerror (mh);
}
}
}
......@@ -6,7 +6,6 @@
#include "wavdata.hh"
bool mp3_detect (const std::string& filename);
std::string mp3_load (const std::string& filename, WavData& wav_data);
void mp3_init();
#endif /* AUDIOWMARK_MP3_HH */
......@@ -54,7 +54,9 @@ MP3InputStream::open (const std::string& filename)
m_need_close = true;
/* scan headers to get best possible length estimate */
mpg123_scan (m_handle);
err = mpg123_scan (m_handle);
if (err != MPG123_OK)
return Error (mpg123_strerror (m_handle));
long rate;
int channels;
......
#include "mp3.hh"
#include "mp3inputstream.hh"
using std::string;
......@@ -10,8 +11,16 @@ main (int argc, char **argv)
{
if (mp3_detect (argv[1]))
{
string error = mp3_load (argv[1], wd);
if (error == "")
MP3InputStream m3i;
Error err = m3i.open (argv[1]);
if (err)
{
printf ("mp3 open %s failed: %s\n", argv[1], err.message());
return 1;
}
err = wd.load (&m3i);
if (!err)
{
int sec = wd.n_values() / wd.n_channels() / wd.sample_rate();
......@@ -24,7 +33,7 @@ main (int argc, char **argv)
}
else
{
printf ("mp3 load %s failed: %s\n", argv[1], error.c_str());
printf ("mp3 load %s failed: %s\n", argv[1], err.message());
return 1;
}
}
......
......@@ -44,10 +44,16 @@ WavData::load (const string& filename)
else if (err)
return err;
return load (in_stream.get());
}
Error
WavData::load (AudioInputStream *in_stream)
{
vector<float> m_buffer;
while (true)
{
err = in_stream->read_frames (m_buffer, 1024);
Error err = in_stream->read_frames (m_buffer, 1024);
if (err)
return err;
......
......@@ -5,6 +5,7 @@
#include <vector>
#include "utils.hh"
#include "audiostream.hh"
class WavData
{
......@@ -17,6 +18,7 @@ public:
WavData();
WavData (const std::vector<float>& samples, int n_channels, int sample_rate, int bit_depth);
Error load (AudioInputStream *in_stream);
Error load (const std::string& filename);
Error save (const std::string& filename);
......
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