|
|
|
@ -12,103 +12,104 @@ static void fail(const std::string &msg)
|
|
|
|
|
namespace MWSound
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
void MpgSnd_Decoder::Open(const std::string &fname)
|
|
|
|
|
void MpgSnd_Decoder::open(const std::string &fname)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
close();
|
|
|
|
|
|
|
|
|
|
SF_INFO info;
|
|
|
|
|
sndFile = sf_open(fname.c_str(), SFM_READ, &info);
|
|
|
|
|
if(sndFile)
|
|
|
|
|
mSndFile = sf_open(fname.c_str(), SFM_READ, &info);
|
|
|
|
|
if(mSndFile)
|
|
|
|
|
{
|
|
|
|
|
if(info.channels == 1)
|
|
|
|
|
chanConfig = MonoChannels;
|
|
|
|
|
mChanConfig = MonoChannels;
|
|
|
|
|
else if(info.channels == 2)
|
|
|
|
|
chanConfig = StereoChannels;
|
|
|
|
|
mChanConfig = StereoChannels;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sf_close(sndFile);
|
|
|
|
|
sndFile = NULL;
|
|
|
|
|
sf_close(mSndFile);
|
|
|
|
|
mSndFile = NULL;
|
|
|
|
|
fail("Unsupported channel count in "+fname);
|
|
|
|
|
}
|
|
|
|
|
sampleRate = info.samplerate;
|
|
|
|
|
mSampleRate = info.samplerate;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mpgFile = mpg123_new(NULL, NULL);
|
|
|
|
|
if(mpgFile && mpg123_open(mpgFile, fname.c_str()) == MPG123_OK)
|
|
|
|
|
mMpgFile = mpg123_new(NULL, NULL);
|
|
|
|
|
if(mMpgFile && mpg123_open(mMpgFile, fname.c_str()) == MPG123_OK)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int encoding, channels;
|
|
|
|
|
long rate;
|
|
|
|
|
if(mpg123_getformat(mpgFile, &rate, &channels, &encoding) != MPG123_OK)
|
|
|
|
|
if(mpg123_getformat(mMpgFile, &rate, &channels, &encoding) != MPG123_OK)
|
|
|
|
|
fail("Failed to get audio format");
|
|
|
|
|
if(encoding != MPG123_ENC_SIGNED_16)
|
|
|
|
|
fail("Unsupported encoding in "+fname);
|
|
|
|
|
if(channels != 1 && channels != 2)
|
|
|
|
|
fail("Unsupported channel count in "+fname);
|
|
|
|
|
chanConfig = ((channels==2)?StereoChannels:MonoChannels);
|
|
|
|
|
sampleRate = rate;
|
|
|
|
|
mChanConfig = ((channels==2)?StereoChannels:MonoChannels);
|
|
|
|
|
mSampleRate = rate;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch(std::exception &e)
|
|
|
|
|
{
|
|
|
|
|
mpg123_close(mpgFile);
|
|
|
|
|
mpg123_delete(mpgFile);
|
|
|
|
|
mpg123_close(mMpgFile);
|
|
|
|
|
mpg123_delete(mMpgFile);
|
|
|
|
|
mMpgFile = NULL;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
mpg123_close(mpgFile);
|
|
|
|
|
mpg123_close(mMpgFile);
|
|
|
|
|
}
|
|
|
|
|
if(mpgFile)
|
|
|
|
|
mpg123_delete(mpgFile);
|
|
|
|
|
mpgFile = NULL;
|
|
|
|
|
if(mMpgFile)
|
|
|
|
|
mpg123_delete(mMpgFile);
|
|
|
|
|
mMpgFile = NULL;
|
|
|
|
|
|
|
|
|
|
fail("Unsupported file type: "+fname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MpgSnd_Decoder::Close()
|
|
|
|
|
void MpgSnd_Decoder::close()
|
|
|
|
|
{
|
|
|
|
|
if(sndFile)
|
|
|
|
|
sf_close(sndFile);
|
|
|
|
|
sndFile = NULL;
|
|
|
|
|
if(mSndFile)
|
|
|
|
|
sf_close(mSndFile);
|
|
|
|
|
mSndFile = NULL;
|
|
|
|
|
|
|
|
|
|
if(mpgFile)
|
|
|
|
|
if(mMpgFile)
|
|
|
|
|
{
|
|
|
|
|
mpg123_close(mpgFile);
|
|
|
|
|
mpg123_delete(mpgFile);
|
|
|
|
|
mpgFile = NULL;
|
|
|
|
|
mpg123_close(mMpgFile);
|
|
|
|
|
mpg123_delete(mMpgFile);
|
|
|
|
|
mMpgFile = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MpgSnd_Decoder::GetInfo(int *samplerate, ChannelConfig *chans, SampleType *type)
|
|
|
|
|
void MpgSnd_Decoder::getInfo(int *samplerate, ChannelConfig *chans, SampleType *type)
|
|
|
|
|
{
|
|
|
|
|
if(!sndFile && !mpgFile)
|
|
|
|
|
if(!mSndFile && !mMpgFile)
|
|
|
|
|
fail("No open file");
|
|
|
|
|
|
|
|
|
|
*samplerate = sampleRate;
|
|
|
|
|
*chans = chanConfig;
|
|
|
|
|
*samplerate = mSampleRate;
|
|
|
|
|
*chans = mChanConfig;
|
|
|
|
|
*type = Int16Sample;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t MpgSnd_Decoder::Read(char *buffer, size_t bytes)
|
|
|
|
|
size_t MpgSnd_Decoder::read(char *buffer, size_t bytes)
|
|
|
|
|
{
|
|
|
|
|
size_t got = 0;
|
|
|
|
|
|
|
|
|
|
if(sndFile)
|
|
|
|
|
if(mSndFile)
|
|
|
|
|
{
|
|
|
|
|
got = sf_read_short(sndFile, (short*)buffer, bytes/2)*2;
|
|
|
|
|
got = sf_read_short(mSndFile, (short*)buffer, bytes/2)*2;
|
|
|
|
|
}
|
|
|
|
|
else if(mpgFile)
|
|
|
|
|
else if(mMpgFile)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
err = mpg123_read(mpgFile, (unsigned char*)buffer, bytes, &got);
|
|
|
|
|
err = mpg123_read(mMpgFile, (unsigned char*)buffer, bytes, &got);
|
|
|
|
|
if(err != MPG123_OK && err != MPG123_DONE)
|
|
|
|
|
fail("Failed to read from file");
|
|
|
|
|
}
|
|
|
|
|
return got;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MpgSnd_Decoder::MpgSnd_Decoder() : sndFile(NULL), mpgFile(NULL)
|
|
|
|
|
MpgSnd_Decoder::MpgSnd_Decoder() : mSndFile(NULL), mMpgFile(NULL)
|
|
|
|
|
{
|
|
|
|
|
static bool initdone = false;
|
|
|
|
|
if(!initdone)
|
|
|
|
@ -118,7 +119,7 @@ MpgSnd_Decoder::MpgSnd_Decoder() : sndFile(NULL), mpgFile(NULL)
|
|
|
|
|
|
|
|
|
|
MpgSnd_Decoder::~MpgSnd_Decoder()
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|