mirror of
https://github.com/love2d/love.git
synced 2026-08-12 00:20:52 +02:00
Merge pull request #5 from MikuAuahDark/replace-mpg123
Replace Mpg123 with dr_mp3
This commit is contained in:
+1
-1
@@ -97,7 +97,7 @@ LOCAL_SRC_FILES := \
|
||||
))
|
||||
|
||||
LOCAL_CXXFLAGS := -std=c++11
|
||||
LOCAL_SHARED_LIBRARIES := libopenal libmpg123
|
||||
LOCAL_SHARED_LIBRARIES := libopenal
|
||||
LOCAL_STATIC_LIBRARIES := libvorbis libogg libtheora libmodplug libfreetype libluajit SDL2_static
|
||||
|
||||
# $(info liblove: include dirs $(LOCAL_C_INCLUDES))
|
||||
|
||||
+13
-37
@@ -49,7 +49,6 @@ else()
|
||||
endif()
|
||||
|
||||
option(LOVE_JIT "Use LuaJIT" TRUE)
|
||||
option(LOVE_MPG123 "Use mpg123" TRUE)
|
||||
|
||||
if(LOVE_JIT)
|
||||
if(APPLE)
|
||||
@@ -60,10 +59,6 @@ else()
|
||||
message(STATUS "LuaJIT: Disabled")
|
||||
endif()
|
||||
|
||||
if(NOT LOVE_MPG123)
|
||||
add_definitions(-DLOVE_NOMPG123)
|
||||
endif()
|
||||
|
||||
message(STATUS "Target platform: ${LOVE_TARGET_PLATFORM}")
|
||||
|
||||
if(POLICY CMP0072)
|
||||
@@ -118,17 +113,6 @@ if(MEGA)
|
||||
${MEGA_OPENAL}
|
||||
)
|
||||
|
||||
if(LOVE_MPG123)
|
||||
set(LOVE_LINK_LIBRARIES
|
||||
${LOVE_LINK_LIBRARIES}
|
||||
${MEGA_MPEG123}
|
||||
)
|
||||
set(LOVE_MOVE_DLLS
|
||||
${LOVE_MOVE_DLLS}
|
||||
${MEGA_MPEG123}
|
||||
)
|
||||
endif()
|
||||
|
||||
if(LOVE_JIT)
|
||||
set(LOVE_LUA_LIBRARY ${MEGA_LUAJIT_LIB})
|
||||
# LOVE_EXTRA_DLLS are non-runtime DLLs which should be bundled with the
|
||||
@@ -209,18 +193,6 @@ Please see http://bitbucket.org/rude/megasource
|
||||
${ZLIB_LIBRARY}
|
||||
)
|
||||
|
||||
if(LOVE_MPG123)
|
||||
find_package(MPG123 REQUIRED)
|
||||
set(LOVE_LINK_LIBRARIES
|
||||
${LOVE_LINK_LIBRARIES}
|
||||
${MPG123_LIBRARY}
|
||||
)
|
||||
set(LOVE_INCLUDE_DIRS
|
||||
${LOVE_INCLUDE_DIRS}
|
||||
${MPG123_INCLUDE_DIR}
|
||||
)
|
||||
endif()
|
||||
|
||||
if(LOVE_JIT)
|
||||
find_package(LuaJIT REQUIRED)
|
||||
set(LOVE_LUA_LIBRARY ${LUAJIT_LIBRARY})
|
||||
@@ -911,6 +883,8 @@ set(LOVE_SRC_MODULE_SOUND_LULLABY
|
||||
src/modules/sound/lullaby/GmeDecoder.h
|
||||
src/modules/sound/lullaby/ModPlugDecoder.cpp
|
||||
src/modules/sound/lullaby/ModPlugDecoder.h
|
||||
src/modules/sound/lullaby/MP3Decoder.h
|
||||
src/modules/sound/lullaby/MP3Decoder.cpp
|
||||
src/modules/sound/lullaby/Sound.cpp
|
||||
src/modules/sound/lullaby/Sound.h
|
||||
src/modules/sound/lullaby/VorbisDecoder.cpp
|
||||
@@ -919,14 +893,6 @@ set(LOVE_SRC_MODULE_SOUND_LULLABY
|
||||
src/modules/sound/lullaby/WaveDecoder.h
|
||||
)
|
||||
|
||||
if(LOVE_MPG123)
|
||||
set(LOVE_SRC_MODULE_SOUND_LULLABY
|
||||
${LOVE_SRC_MODULE_SOUND_LULLABY}
|
||||
src/modules/sound/lullaby/Mpg123Decoder.cpp
|
||||
src/modules/sound/lullaby/Mpg123Decoder.h
|
||||
)
|
||||
endif()
|
||||
|
||||
set(LOVE_SRC_MODULE_SOUND
|
||||
${LOVE_SRC_MODULE_SOUND_ROOT}
|
||||
${LOVE_SRC_MODULE_SOUND_LULLABY}
|
||||
@@ -1242,11 +1208,21 @@ add_library(love_3p_ddsparse ${LOVE_SRC_3P_DDSPARSE})
|
||||
#
|
||||
|
||||
set(LOVE_SRC_3P_DRFLAC
|
||||
src/libraries/dr_flac/dr_flac.h
|
||||
src/libraries/dr/dr_flac.h
|
||||
)
|
||||
|
||||
# dr_flac has no implementation files of its own.
|
||||
|
||||
#
|
||||
# dr_mp3
|
||||
#
|
||||
|
||||
set(LOVE_SRC_3P_DRMP3
|
||||
src/libraries/dr/dr_mp3.h
|
||||
)
|
||||
|
||||
# dr_mp3 has no implementation files of its own.
|
||||
|
||||
#
|
||||
# enet
|
||||
#
|
||||
|
||||
@@ -42,7 +42,6 @@ Section "LOVE" MainProg
|
||||
File "${LOVEBINDIR}\DevIL.dll"
|
||||
File "${LOVEBINDIR}\SDL.dll"
|
||||
File "${LOVEBINDIR}\OpenAL32.dll"
|
||||
File "${LOVEBINDIR}\libmpg123.dll"
|
||||
File "${LOVEBINDIR}\gme.dll"
|
||||
# File "${LOVEBINDIR}\lua51.dll"
|
||||
File "${LOVEICODIR}\love.ico"
|
||||
@@ -91,7 +90,6 @@ Section "Uninstall"
|
||||
Delete $INSTDIR\"SDL.dll"
|
||||
Delete $INSTDIR\"love.exe"
|
||||
Delete $INSTDIR\"OpenAL32.dll"
|
||||
Delete $INSTDIR\"libmpg123.dll"
|
||||
Delete $INSTDIR\"gme.dll"
|
||||
# Delete $INSTDIR\"lua51.dll"
|
||||
Delete $INSTDIR\"game.ico"
|
||||
|
||||
+7
-15
@@ -75,24 +75,16 @@ This distribution contains code from the following projects (full license text b
|
||||
- xxHash source repository : https://github.com/Cyan4973/xxHash
|
||||
|
||||
- dr_flac
|
||||
Website: https://github.com/mackron/dr_libs
|
||||
Source download: https://github.com/mackron/dr_libs/blob/41bc0e8/dr_flac.h
|
||||
Website: https://mackron.github.io/dr_libs/
|
||||
Source download: https://github.com/mackron/dr_libs/blob/d705d31/dr_flac.h
|
||||
License: MIT/Expat
|
||||
Copyright 2018 David Reid
|
||||
|
||||
- libmpg123
|
||||
Website: http://www.mpg123.de/
|
||||
Source download: http://sourceforge.net/projects/mpg123/files/latest/download
|
||||
License: LGPL 2.1
|
||||
Copyright (c) 1995-2013 by Michael Hipp and others, free software under the terms of the LGPL v2.1
|
||||
Detailed information from the debian project:
|
||||
Copyright 1995-2016 by the mpg123 project
|
||||
Copyright 2009-2011 by Malcolm Boczek
|
||||
Copyright 2008 Christian Weisgerber <naddy@openbsd.org>
|
||||
Copyright 2006-2007 by Zuxy Meng
|
||||
Copyright 2000-2002 David Olofson
|
||||
Copyright 1998 Fabrice Bellard
|
||||
Copyright 1997 Mikko Tommila
|
||||
- dr_mp3
|
||||
Website: https://mackron.github.io/dr_libs/
|
||||
Source download: https://github.com/mackron/dr_libs/blob/47fdc9d/dr_mp3.h
|
||||
License: MIT/Expat
|
||||
Copyright 2018 David Reid
|
||||
|
||||
- OpenAL Soft
|
||||
Website: http://kcat.strangesoft.net/openal.html
|
||||
|
||||
@@ -55,7 +55,6 @@ with_clean_luaversion=`printf ${with_luaversion} | sed 's/\.//g'`
|
||||
m4_include([configure-modules-pre.ac])
|
||||
|
||||
# Other features that can be enabled/disabled
|
||||
AC_ARG_ENABLE([mpg123], AC_HELP_STRING([--disable-mpg123], [Disable mp3 support, for patent-free builds]), [], [enable_mpg123=yes])
|
||||
AC_ARG_ENABLE([gme], AC_HELP_STRING([--enable-gme], [Enable GME support, for more chiptuney goodness]), [], [enable_gme=no])
|
||||
|
||||
# Dependencies we always use
|
||||
@@ -71,12 +70,9 @@ AS_VAR_IF([enable_module_font], [yes], [ACLOVE_DEP_FREETYPE2], [])
|
||||
AS_VAR_IF([enable_module_sound], [yes], [
|
||||
ACLOVE_DEP_LIBMODPLUG
|
||||
ACLOVE_DEP_VORBISFILE
|
||||
], [enable_mpg123=no])
|
||||
], [])
|
||||
AS_VAR_IF([enable_module_video], [yes], [ACLOVE_DEP_THEORA], [])
|
||||
AS_VAR_IF([enable_gme], [yes], [ACLOVE_DEP_GME], [])
|
||||
AS_VAR_IF([enable_mpg123], [no],
|
||||
AC_DEFINE([LOVE_NOMPG123], [], [Build without mpg123]),
|
||||
[ACLOVE_DEP_MPG123])
|
||||
|
||||
# Add flags for optional libraries
|
||||
AC_ARG_ENABLE([library-enet], [ --disable-library-enet Turn off library enet], [], [enable_library_enet=yes])
|
||||
@@ -117,7 +113,6 @@ AS_VAR_IF([enable_exe], [no], [], #else
|
||||
AC_DEFINE([LOVE_BUILD_EXE], [], [Skip building launcher]))
|
||||
|
||||
AM_CONDITIONAL([LOVE_BUILD_EXE], [test "x$enable_exe" != xno])
|
||||
AM_CONDITIONAL([LOVE_NOMPG123], [test "x$enable_mpg123" = xno])
|
||||
AM_CONDITIONAL([LOVE_TARGET_OSX], [test "x$enable_osx" != xno])
|
||||
|
||||
# Automatic script file rebuilding
|
||||
|
||||
@@ -11,7 +11,6 @@ Build-Depends: debhelper (>= 9),
|
||||
luajit,
|
||||
libluajit-5.1-dev,
|
||||
libmodplug-dev,
|
||||
libmpg123-dev,
|
||||
libopenal-dev,
|
||||
libphysfs-dev,
|
||||
libsdl2-dev (>= 2.0.1),
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
\./libraries/luasocket/libluasocket/wsocket.*
|
||||
\./love\.cpp
|
||||
\./modules/sound/lullaby/Mpg123Decoder\.cpp
|
||||
\./modules/sound/lullaby/Mpg123Decoder\.h
|
||||
\./libraries/glslang/glslang/OSDependent/Windows
|
||||
|
||||
@@ -45,14 +45,6 @@ genmodules()
|
||||
printf "${FILES:0:${#FILES}-2}\n\n"
|
||||
modulelist+=("$module")
|
||||
fi
|
||||
|
||||
if [[ "$module" = "sound" ]]; then
|
||||
printf "if !LOVE_NOMPG123\n"
|
||||
printf "liblove_module_$module += \\\\\n"
|
||||
printf "\t./modules/sound/lullaby/Mpg123Decoder.cpp \\\\\n"
|
||||
printf "\t./modules/sound/lullaby/Mpg123Decoder.h\n"
|
||||
printf "endif\n\n"
|
||||
fi
|
||||
done
|
||||
|
||||
cd ../libraries
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
LÖVE is an *awesome* framework you can use to make 2D games in Lua. It's free, open-source, and works on Windows, Mac OS X, Linux, Android, and iOS.
|
||||
|
||||
[](https://ci.appveyor.com/project/AlexSzpakowski/love)
|
||||

|
||||
|
||||
Documentation
|
||||
-------------
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
|
||||
@@ -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
|
||||
+14
-38
@@ -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
|
||||
@@ -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
|
||||
@@ -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>(),
|
||||
|
||||
@@ -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.
|
||||
**/
|
||||
|
||||
Reference in New Issue
Block a user