2017-04-17 10:27:19 +00:00
|
|
|
/*
|
|
|
|
** mpg123_decoder.cpp
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2008-2010 Chris Robinson
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
2015-04-25 10:25:10 +00:00
|
|
|
|
2019-09-29 10:48:12 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <stdio.h>
|
2019-09-29 23:34:16 +00:00
|
|
|
#include "mpg123_decoder.h"
|
2017-04-17 15:05:09 +00:00
|
|
|
#include "i_module.h"
|
2014-06-19 13:34:40 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MPG123
|
2017-04-17 15:05:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
FModule MPG123Module{"MPG123"};
|
|
|
|
|
|
|
|
#include "mpgload.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define MPG123LIB "libmpg123-0.dll"
|
|
|
|
#elif defined(__APPLE__)
|
2017-04-17 16:21:57 +00:00
|
|
|
#define MPG123LIB "libmpg123.0.dylib"
|
2017-04-17 15:05:09 +00:00
|
|
|
#else
|
2017-05-03 13:56:22 +00:00
|
|
|
#define MPG123LIB "libmpg123.so.0"
|
2017-04-17 15:05:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bool IsMPG123Present()
|
|
|
|
{
|
|
|
|
#if !defined DYN_MPG123
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
static bool cached_result = false;
|
|
|
|
static bool done = false;
|
|
|
|
|
|
|
|
if (!done)
|
|
|
|
{
|
|
|
|
done = true;
|
2019-09-29 10:48:12 +00:00
|
|
|
auto abspath = module_progdir + "/" MPG123LIB;
|
|
|
|
cached_result = MPG123Module.Load({abspath.c_str(), MPG123LIB});
|
2017-04-17 15:05:09 +00:00
|
|
|
}
|
|
|
|
return cached_result;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 13:34:40 +00:00
|
|
|
static bool inited = false;
|
|
|
|
|
|
|
|
|
2014-06-20 03:14:16 +00:00
|
|
|
off_t MPG123Decoder::file_lseek(void *handle, off_t offset, int whence)
|
|
|
|
{
|
2018-03-10 17:45:11 +00:00
|
|
|
auto &reader = reinterpret_cast<MPG123Decoder*>(handle)->Reader;
|
2014-06-20 03:14:16 +00:00
|
|
|
|
2017-01-17 09:26:51 +00:00
|
|
|
if(whence == SEEK_CUR)
|
2014-06-20 03:14:16 +00:00
|
|
|
{
|
2019-09-29 10:48:12 +00:00
|
|
|
if(offset < 0 && reader->tell()+offset < 0)
|
2014-06-20 03:14:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-06-25 11:25:36 +00:00
|
|
|
else if(whence == SEEK_END)
|
2014-06-19 13:34:40 +00:00
|
|
|
{
|
2019-09-29 10:48:12 +00:00
|
|
|
if(offset < 0 && reader->filelength() + offset < 0)
|
2014-06-19 13:34:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-09-29 10:48:12 +00:00
|
|
|
if(reader->seek(offset, whence) != 0)
|
2014-06-25 11:25:36 +00:00
|
|
|
return -1;
|
2019-09-29 10:48:12 +00:00
|
|
|
return (off_t)reader->tell();
|
2014-06-19 13:34:40 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 11:25:36 +00:00
|
|
|
ssize_t MPG123Decoder::file_read(void *handle, void *buffer, size_t bytes)
|
2014-06-19 13:34:40 +00:00
|
|
|
{
|
2018-03-10 17:45:11 +00:00
|
|
|
auto &reader = reinterpret_cast<MPG123Decoder*>(handle)->Reader;
|
2019-09-29 10:48:12 +00:00
|
|
|
return (ssize_t)reader->read(buffer, (long)bytes);
|
2014-06-19 13:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MPG123Decoder::~MPG123Decoder()
|
|
|
|
{
|
|
|
|
if(MPG123)
|
|
|
|
{
|
|
|
|
mpg123_close(MPG123);
|
|
|
|
mpg123_delete(MPG123);
|
|
|
|
MPG123 = 0;
|
|
|
|
}
|
2019-09-29 10:48:12 +00:00
|
|
|
if (Reader) Reader->close();
|
|
|
|
Reader = nullptr;
|
2014-06-19 13:34:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 10:48:12 +00:00
|
|
|
bool MPG123Decoder::open(MusicIO::FileInterface *reader)
|
2014-06-19 13:34:40 +00:00
|
|
|
{
|
|
|
|
if(!inited)
|
|
|
|
{
|
2017-04-17 15:05:09 +00:00
|
|
|
if (!IsMPG123Present()) return false;
|
|
|
|
if(mpg123_init() != MPG123_OK) return false;
|
|
|
|
inited = true;
|
2014-06-19 13:34:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 10:48:12 +00:00
|
|
|
Reader = reader;
|
2014-06-20 03:14:16 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
MPG123 = mpg123_new(NULL, NULL);
|
|
|
|
if(mpg123_replace_reader_handle(MPG123, file_read, file_lseek, NULL) == MPG123_OK &&
|
|
|
|
mpg123_open_handle(MPG123, this) == MPG123_OK)
|
|
|
|
{
|
|
|
|
int enc, channels;
|
|
|
|
long srate;
|
|
|
|
|
|
|
|
if(mpg123_getformat(MPG123, &srate, &channels, &enc) == MPG123_OK)
|
|
|
|
{
|
|
|
|
if((channels == 1 || channels == 2) && srate > 0 &&
|
|
|
|
mpg123_format_none(MPG123) == MPG123_OK &&
|
|
|
|
mpg123_format(MPG123, srate, channels, MPG123_ENC_SIGNED_16) == MPG123_OK)
|
|
|
|
{
|
|
|
|
// All OK
|
|
|
|
Done = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mpg123_close(MPG123);
|
|
|
|
}
|
|
|
|
mpg123_delete(MPG123);
|
|
|
|
MPG123 = 0;
|
|
|
|
}
|
|
|
|
|
2019-09-29 10:48:12 +00:00
|
|
|
Reader = nullptr; // need to give it back.
|
2018-03-10 19:33:49 +00:00
|
|
|
return false;
|
2014-06-20 03:14:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 13:34:40 +00:00
|
|
|
void MPG123Decoder::getInfo(int *samplerate, ChannelConfig *chans, SampleType *type)
|
|
|
|
{
|
|
|
|
int enc = 0, channels = 0;
|
|
|
|
long srate = 0;
|
|
|
|
|
|
|
|
mpg123_getformat(MPG123, &srate, &channels, &enc);
|
|
|
|
|
|
|
|
*samplerate = srate;
|
|
|
|
|
|
|
|
if(channels == 2)
|
|
|
|
*chans = ChannelConfig_Stereo;
|
|
|
|
else
|
|
|
|
*chans = ChannelConfig_Mono;
|
|
|
|
|
|
|
|
*type = SampleType_Int16;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MPG123Decoder::read(char *buffer, size_t bytes)
|
|
|
|
{
|
|
|
|
size_t amt = 0;
|
|
|
|
while(!Done && bytes > 0)
|
|
|
|
{
|
|
|
|
size_t got = 0;
|
|
|
|
int ret = mpg123_read(MPG123, (unsigned char*)buffer, bytes, &got);
|
|
|
|
|
|
|
|
bytes -= got;
|
|
|
|
buffer += got;
|
|
|
|
amt += got;
|
|
|
|
|
|
|
|
if(ret == MPG123_NEW_FORMAT || ret == MPG123_DONE || got == 0)
|
|
|
|
{
|
|
|
|
Done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return amt;
|
|
|
|
}
|
|
|
|
|
2017-04-25 19:30:11 +00:00
|
|
|
bool MPG123Decoder::seek(size_t ms_offset, bool ms, bool mayrestart)
|
2014-06-19 13:34:40 +00:00
|
|
|
{
|
2017-04-25 19:30:11 +00:00
|
|
|
int enc, channels;
|
|
|
|
long srate;
|
2014-06-19 13:34:40 +00:00
|
|
|
|
2017-04-25 19:30:11 +00:00
|
|
|
if (!mayrestart || ms_offset > 0)
|
|
|
|
{
|
|
|
|
if (mpg123_getformat(MPG123, &srate, &channels, &enc) == MPG123_OK)
|
|
|
|
{
|
|
|
|
size_t smp_offset = ms ? (size_t)((double)ms_offset / 1000. * srate) : ms_offset;
|
|
|
|
if (mpg123_seek(MPG123, (off_t)smp_offset, SEEK_SET) >= 0)
|
|
|
|
{
|
|
|
|
Done = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Restart the song instead of rewinding. A rewind seems to cause distortion when done repeatedly.
|
|
|
|
// offset is intentionally ignored here.
|
|
|
|
if (MPG123)
|
|
|
|
{
|
|
|
|
mpg123_close(MPG123);
|
|
|
|
mpg123_delete(MPG123);
|
|
|
|
MPG123 = 0;
|
|
|
|
}
|
2019-09-29 10:48:12 +00:00
|
|
|
Reader->seek(0, SEEK_SET);
|
2018-03-10 17:45:11 +00:00
|
|
|
// Do not call open with our own reader variable, that would be catastrophic.
|
|
|
|
auto reader = std::move(Reader);
|
|
|
|
return open(reader);
|
2017-04-25 19:30:11 +00:00
|
|
|
}
|
2014-06-19 13:34:40 +00:00
|
|
|
}
|
|
|
|
size_t MPG123Decoder::getSampleOffset()
|
|
|
|
{
|
|
|
|
return mpg123_tell(MPG123);
|
|
|
|
}
|
|
|
|
|
2014-06-26 04:14:35 +00:00
|
|
|
size_t MPG123Decoder::getSampleLength()
|
|
|
|
{
|
|
|
|
off_t len = mpg123_length(MPG123);
|
|
|
|
return (len > 0) ? len : 0;
|
|
|
|
}
|
|
|
|
|
2014-06-19 13:34:40 +00:00
|
|
|
#endif
|