Commit 5f718b61 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Use mp3 loader in WavData.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent a7ae7b8b
...@@ -36,6 +36,8 @@ mp3_try_load (const string& filename, WavData& wav_data) ...@@ -36,6 +36,8 @@ mp3_try_load (const string& filename, WavData& wav_data)
err = mpg123_open (mh, filename.c_str()); err = mpg123_open (mh, filename.c_str());
assert (err == 0); assert (err == 0);
err = mpg123_getformat (mh, &rate, &channels, &encoding); err = mpg123_getformat (mh, &rate, &channels, &encoding);
if (err != 0)
return false;
assert (err == 0); assert (err == 0);
printf ("# %d\n", err); printf ("# %d\n", err);
printf ("# %ld %d %d\n", rate, channels, encoding); printf ("# %ld %d %d\n", rate, channels, encoding);
...@@ -54,7 +56,6 @@ mp3_try_load (const string& filename, WavData& wav_data) ...@@ -54,7 +56,6 @@ mp3_try_load (const string& filename, WavData& wav_data)
{ {
err = mpg123_read( mh, &buffer[0], buffer.size(), &done ); err = mpg123_read( mh, &buffer[0], buffer.size(), &done );
assert (err == 0 || err == MPG123_DONE); assert (err == 0 || err == MPG123_DONE);
printf ("# done=%zd err=%d\n", done, err);
float *f = reinterpret_cast<float *> (&buffer[0]); float *f = reinterpret_cast<float *> (&buffer[0]);
for (int i = 0; i < buffer.size() / 4; i++) for (int i = 0; i < buffer.size() / 4; i++)
......
...@@ -4,7 +4,9 @@ int ...@@ -4,7 +4,9 @@ int
main (int argc, char **argv) main (int argc, char **argv)
{ {
WavData wd; WavData wd;
if (argc >= 2 && mp3_try_load (argv[1], wd)) if (argc >= 2)
{
if (mp3_try_load (argv[1], wd))
{ {
int sec = wd.n_values() / wd.sample_rate(); int sec = wd.n_values() / wd.sample_rate();
...@@ -15,4 +17,9 @@ main (int argc, char **argv) ...@@ -15,4 +17,9 @@ main (int argc, char **argv)
printf ("saved wav: %s\n", argv[2]); printf ("saved wav: %s\n", argv[2]);
} }
} }
else
{
printf ("mp3 try load %s failed\n", argv[1]);
}
}
} }
#include "wavdata.hh" #include "wavdata.hh"
#include "mp3.hh"
#include <math.h> #include <math.h>
#include <sndfile.h> #include <sndfile.h>
...@@ -34,6 +35,13 @@ WavData::load (const string& filename) ...@@ -34,6 +35,13 @@ WavData::load (const string& filename)
int error = sf_error (sndfile); int error = sf_error (sndfile);
if (error) if (error)
{
if (mp3_try_load (filename, *this))
{
// ok, if its an mp3, take it
return true;
}
else
{ {
m_error_blurb = sf_strerror (sndfile); m_error_blurb = sf_strerror (sndfile);
if (sndfile) if (sndfile)
...@@ -41,6 +49,7 @@ WavData::load (const string& filename) ...@@ -41,6 +49,7 @@ WavData::load (const string& filename)
return false; return false;
} }
}
vector<int> isamples (sfinfo.frames * sfinfo.channels); vector<int> isamples (sfinfo.frames * sfinfo.channels);
sf_count_t count = sf_readf_int (sndfile, &isamples[0], sfinfo.frames); sf_count_t count = sf_readf_int (sndfile, &isamples[0], sfinfo.frames);
......
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