Merge pull request #5 from MikuAuahDark/replace-mpg123

Replace Mpg123 with dr_mp3
This commit is contained in:
Alex Szpakowski
2020-02-16 18:34:07 -04:00
committed by GitHub
17 changed files with 4397 additions and 420 deletions
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -25,7 +25,7 @@
#include "common/Data.h"
#include "sound/Decoder.h"
#include "dr_flac/dr_flac.h"
#include "dr/dr_flac.h"
#include <string.h>
namespace love
+144
View File
@@ -0,0 +1,144 @@
/**
* Copyright (c) 2006-2020 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#define DR_MP3_IMPLEMENTATION
#define DR_MP3_NO_STDIO
#include "MP3Decoder.h"
namespace love
{
namespace sound
{
namespace lullaby
{
MP3Decoder::MP3Decoder(Data *data, int bufferSize)
: Decoder(data, bufferSize)
{
// initialize mp3 handle
if(drmp3_init_memory(&mp3, data->getData(), data->getSize(), nullptr, nullptr) == 0)
throw love::Exception("Could not read mp3 data.");
sampleRate = mp3.sampleRate;
// calculate duration
drmp3_uint64 pcmCount, mp3FrameCount;
if (!drmp3_get_mp3_and_pcm_frame_count(&mp3, &mp3FrameCount, &pcmCount))
{
drmp3_uninit(&mp3);
throw love::Exception("Could not calculate duration.");
}
duration = ((double) pcmCount) / ((double) mp3.sampleRate);
// create seek table
uint32_t mp3FrameInt = mp3FrameCount;
seekTable.resize(mp3FrameCount, {0ULL, 0ULL, 0, 0});
if (!drmp3_calculate_seek_points(&mp3, &mp3FrameInt, seekTable.data()))
{
drmp3_uninit(&mp3);
throw love::Exception("Could not calculate seek table");
}
mp3FrameInt = mp3FrameInt > mp3FrameCount ? mp3FrameCount : mp3FrameInt;
// bind seek table
if (!drmp3_bind_seek_table(&mp3, mp3FrameInt, seekTable.data()))
{
drmp3_uninit(&mp3);
throw love::Exception("Could not bind seek table");
}
}
MP3Decoder::~MP3Decoder()
{
drmp3_uninit(&mp3);
}
bool MP3Decoder::accepts(const std::string &ext)
{
static const std::string supported[] =
{
"mp3", ""
};
for (int i = 0; !(supported[i].empty()); i++)
{
if (supported[i].compare(ext) == 0)
return true;
}
return false;
}
love::sound::Decoder *MP3Decoder::clone()
{
return new MP3Decoder(data, bufferSize);
}
int MP3Decoder::decode()
{
// bufferSize is in char
int maxRead = bufferSize / sizeof(int16_t) / mp3.channels;
int read = (int) drmp3_read_pcm_frames_s16(&mp3, maxRead, (drmp3_int16 *) buffer);
if (read < maxRead)
eof = true;
return read * sizeof(int16_t) * mp3.channels;
}
bool MP3Decoder::seek(double s)
{
drmp3_uint64 targetSample = s * mp3.sampleRate;
drmp3_bool32 success = drmp3_seek_to_pcm_frame(&mp3, targetSample);
if (success)
eof = false;
return success;
}
bool MP3Decoder::rewind()
{
return seek(0.0);
}
bool MP3Decoder::isSeekable()
{
return true;
}
int MP3Decoder::getChannelCount() const
{
return mp3.channels;
}
int MP3Decoder::getBitDepth() const
{
return 16;
}
double MP3Decoder::getDuration()
{
return duration;
}
} // lullaby
} // sound
} // love
@@ -18,21 +18,17 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_SOUND_LULLABY_LIBMPG123_DECODER_H
#define LOVE_SOUND_LULLABY_LIBMPG123_DECODER_H
#ifndef LOVE_SOUND_LULLABY_MP3_DECODER_H
#define LOVE_SOUND_LULLABY_MP3_DECODER_H
// LOVE
#include "common/Data.h"
#include "sound/Decoder.h"
#ifndef LOVE_NOMPG123
// dr_mp3
#include "dr/dr_mp3.h"
// libmpg123
#ifdef LOVE_APPLE_USE_FRAMEWORKS
#include <mpg123/mpg123.h>
#else
#include <mpg123.h>
#endif
#include <vector>
namespace love
{
@@ -41,29 +37,14 @@ namespace sound
namespace lullaby
{
struct DecoderFile
{
unsigned char *data;
size_t size;
size_t offset;
DecoderFile(Data *d)
: data((unsigned char *) d->getData())
, size(d->getSize())
, offset(0)
{}
};
class Mpg123Decoder : public Decoder
class MP3Decoder: public love::sound::Decoder
{
public:
Mpg123Decoder(Data *data, int bufferSize);
virtual ~Mpg123Decoder();
MP3Decoder(Data *data, int bufsize);
virtual ~MP3Decoder();
static bool accepts(const std::string &ext);
static void quit();
love::sound::Decoder *clone();
int decode();
bool seek(double s);
@@ -75,21 +56,16 @@ public:
private:
DecoderFile decoder_file;
mpg123_handle *handle;
static bool inited;
int channels;
// MP3 handle
drmp3 mp3;
// Used for fast seeking
std::vector<drmp3_seek_point> seekTable;
double duration;
}; // Decoder
}; // MP3Decoder
} // lullaby
} // sound
} // love
#endif // LOVE_NOMPG123
#endif // LOVE_SOUND_LULLABY_LIBMPG123_DECODER_H
#endif
-298
View File
@@ -1,298 +0,0 @@
/**
* Copyright (c) 2006-2020 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Mpg123Decoder.h"
#include "common/Exception.h"
#include <iostream>
#ifndef LOVE_NOMPG123
namespace love
{
namespace sound
{
namespace lullaby
{
/**
* mpg123 callbacks for seekable streams.
**/
static ssize_t read_callback(void *udata, void *buffer, size_t count)
{
DecoderFile *file = (DecoderFile *) udata;
// Calculates how much data is still left and takes that value or
// the buffer size, whichever is lower, as the number of bytes to write.
size_t countLeft = file->size - file->offset;
size_t countWrite = countLeft < count ? countLeft : count;
if (countWrite > 0)
{
memcpy(buffer, file->data + file->offset, countWrite);
file->offset += countWrite;
}
// Returns the number of written bytes. 0 means EOF.
return countWrite;
}
static off_t seek_callback(void *udata, off_t offset, int whence)
{
DecoderFile *file = (DecoderFile *) udata;
switch (whence)
{
case SEEK_SET:
// Negative values are invalid at this point.
if (offset < 0)
return -1;
// Prevents the offset from going over EOF.
if (file->size > (size_t) offset)
file->offset = offset;
else
file->offset = file->size;
break;
case SEEK_END:
// Offset is set to EOF. Offset calculation is just like SEEK_CUR.
file->offset = file->size;
case SEEK_CUR:
// Prevents the offset from going over EOF or below 0.
if (offset > 0)
{
if (file->size > file->offset + (size_t) offset)
file->offset = file->offset + offset;
else
file->offset = file->size;
}
else if (offset < 0)
{
if (file->offset >= (size_t) (-offset))
file->offset = file->offset - (size_t) (-offset);
else
file->offset = 0;
}
break;
default:
return -1;
};
return file->offset;
}
static void cleanup_callback(void *)
{
// Cleanup is done by the Decoder class.
}
bool Mpg123Decoder::inited = false;
Mpg123Decoder::Mpg123Decoder(Data *data, int bufferSize)
: Decoder(data, bufferSize)
, decoder_file(data)
, handle(0)
, channels(MPG123_STEREO)
, duration(-2.0)
{
int ret = 0;
if (!inited)
{
ret = mpg123_init();
if (ret != MPG123_OK)
throw love::Exception("Could not initialize mpg123.");
inited = (ret == MPG123_OK);
}
// Intialize the handle.
handle = mpg123_new(nullptr, nullptr);
if (handle == nullptr)
throw love::Exception("Could not create decoder.");
// Suppressing all mpg123 messages.
mpg123_param(handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0);
try
{
ret = mpg123_replace_reader_handle(handle, &read_callback, &seek_callback, &cleanup_callback);
if (ret != MPG123_OK)
throw love::Exception("Could not set decoder callbacks.");
ret = mpg123_open_handle(handle, &decoder_file);
if (ret != MPG123_OK)
throw love::Exception("Could not open decoder.");
// mpg123_getformat should be able to tell us the properties of the stream's first frame.
long rate = 0;
ret = mpg123_getformat(handle, &rate, &channels, nullptr);
if (ret == MPG123_ERR)
throw love::Exception("Could not get stream information.");
// I forgot what this was about.
if (channels == 0)
channels = 2;
// Force signed 16-bit output.
mpg123_param(handle, MPG123_FLAGS, (channels == 2 ? MPG123_FORCE_STEREO : MPG123_MONO_MIX), 0);
mpg123_format_none(handle);
mpg123_format(handle, rate, channels, MPG123_ENC_SIGNED_16);
sampleRate = (int) rate;
// Force a read, so we can determine if it's actually an mp3
struct mpg123_frameinfo info;
ret = mpg123_info(handle, &info);
if (ret != MPG123_OK)
throw love::Exception("Could not read mp3 data.");
}
catch (love::Exception &)
{
mpg123_delete(handle);
throw;
}
}
Mpg123Decoder::~Mpg123Decoder()
{
mpg123_delete(handle);
}
bool Mpg123Decoder::accepts(const std::string &ext)
{
static const std::string supported[] =
{
"mp3", ""
};
for (int i = 0; !(supported[i].empty()); i++)
{
if (supported[i].compare(ext) == 0)
return true;
}
return false;
}
void Mpg123Decoder::quit()
{
if (inited)
mpg123_exit();
}
love::sound::Decoder *Mpg123Decoder::clone()
{
return new Mpg123Decoder(data.get(), bufferSize);
}
int Mpg123Decoder::decode()
{
int size = 0;
while (size < bufferSize && !eof)
{
size_t numbytes = 0;
int res = mpg123_read(handle, (unsigned char *) buffer + size, bufferSize - size, &numbytes);
switch (res)
{
case MPG123_NEED_MORE:
case MPG123_NEW_FORMAT:
case MPG123_OK:
size += numbytes;
continue;
case MPG123_DONE:
size += numbytes;
eof = true;
default:
return size;
}
}
return size;
}
bool Mpg123Decoder::seek(double s)
{
off_t offset = (off_t) (s * (double) sampleRate);
if (offset < 0)
return false;
if (mpg123_seek(handle, offset, SEEK_SET) >= 0)
{
eof = false;
return true;
}
else
return false;
}
bool Mpg123Decoder::rewind()
{
eof = false;
if (mpg123_seek(handle, 0, SEEK_SET) >= 0)
return true;
else
return false;
}
bool Mpg123Decoder::isSeekable()
{
return true;
}
int Mpg123Decoder::getChannelCount() const
{
return channels;
}
int Mpg123Decoder::getBitDepth() const
{
return 16;
}
double Mpg123Decoder::getDuration()
{
// Only calculate the duration if we haven't done so already.
if (duration == -2.0)
{
mpg123_scan(handle);
off_t length = mpg123_length(handle);
if (length == MPG123_ERR || length < 0)
duration = -1.0;
else
duration = (double) length / (double) sampleRate;
}
return duration;
}
} // lullaby
} // sound
} // love
#endif // LOVE_NOMPG123
+2 -10
View File
@@ -30,10 +30,7 @@
#include "GmeDecoder.h"
#include "WaveDecoder.h"
#include "FLACDecoder.h"
#ifndef LOVE_NOMPG123
# include "Mpg123Decoder.h"
#endif // LOVE_NOMPG123
#include "MP3Decoder.h"
#ifdef LOVE_SUPPORT_COREAUDIO
# include "CoreAudioDecoder.h"
@@ -73,9 +70,6 @@ Sound::Sound()
Sound::~Sound()
{
#ifndef LOVE_NOMPG123
Mpg123Decoder::quit();
#endif // LOVE_NOMPG123
}
const char *Sound::getName() const
@@ -92,9 +86,7 @@ sound::Decoder *Sound::newDecoder(love::filesystem::FileData *data, int bufferSi
#ifndef LOVE_NO_MODPLUG
DecoderImplFor<ModPlugDecoder>(),
#endif // LOVE_NO_MODPLUG
#ifndef LOVE_NOMPG123
DecoderImplFor<Mpg123Decoder>(),
#endif // LOVE_NOMPG123
DecoderImplFor<MP3Decoder>(),
DecoderImplFor<VorbisDecoder>(),
#ifdef LOVE_SUPPORT_GME
DecoderImplFor<GmeDecoder>(),
+1 -1
View File
@@ -37,7 +37,7 @@ namespace lullaby
/**
* The love.sound.lullaby module is the custom sound decoder module for LOVE. Instead
* of using an intermediate library like SDL_sound, it interfaces with relevant libraries
* directly (libmpg123, libmodplug, libFLAC, etc).
* directly (libvorbis, dr_mp3, dr_flac, etc).
*
* It was Mike that came up with the name Lullaby, which we both instantly recognized as awesome.
**/