Skip to content
Permalink
Browse files
Added support for libXMP
  • Loading branch information
Wohlstand committed Mar 10, 2019
1 parent b76b602 commit e20a21d2b8ca40f1982159631f39dc6fe2c13e44
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 1 deletion.
@@ -416,6 +416,39 @@ if(USE_MIKMOD)
endif()
endif()

option(USE_XMP "Build with XMP library" ON)
if(USE_XMP)
if(USE_SYSTEM_AUDIO_LIBRARIES)
find_path(LIBXMP_INCLUDE_DIR xmp.h xmp/xmp.h)
find_library(LIBXMP_LIB NAMES xmp)
if(LIBXMP_INCLUDE_DIR AND LIBXMP_LIB)
set(FOUND_LIBXMP 1)
endif()
message("XMP: [${FOUND_LIBXMP}] ${LIBXMP_INCLUDE_DIR} ${LIBXMP_LIB}")
else()
set(FOUND_LIBXMP 1)
if(WIN32)
add_definitions(-DBUILDING_STATIC)
endif()
if(DOWNLOAD_AUDIO_CODECS_DEPENDENCY)
set(LIBXMP_LIB xmp)
else()
find_library(LIBXMP_LIB NAMES xmp
HINTS "${AUDIO_CODECS_INSTALL_PATH}/lib")
endif()
endif()
if(FOUND_LIBXMP)
message("== using libXMP ==")
add_definitions(-DMUSIC_MOD_XMP)
if(AUDIO_CODECS_REPO_PATH)
list(APPEND SDL_MIXER_INCLUDE_PATHS ${AUDIO_CODECS_PATH}/libxmp/include)
endif()
list(APPEND SDLMixerX_LINK_LIBS ${LIBXMP_LIB})
list(APPEND SDLMixerX_SOURCES
${SDLMixerX_SOURCE_DIR}/src/codecs/music_xmp.c)
endif()
endif()

option(USE_GME "Build with Game Music Emulators library" ON)
if(USE_GME)
if(USE_SYSTEM_AUDIO_LIBRARIES)
@@ -230,7 +230,7 @@ static void MODPLUG_SetVolume(void *context, int volume)
{
MODPLUG_Music *music = (MODPLUG_Music *)context;
music->volume = volume;
modplug.ModPlug_SetMasterVolume(music->file, (unsigned int)volume * 4);
modplug.ModPlug_SetMasterVolume(music->file, (unsigned int)volume * 2); /* 0-512, reduced to 0-256 to prevent clipping */
}

/* Get the volume for a modplug stream */
@@ -0,0 +1,268 @@
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
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.
*/

#ifdef MUSIC_MOD_XMP

#include "SDL_loadso.h"

#include "music_xmp.h"

#include <xmp/xmp.h>

typedef struct
{
int volume;
int play_count;
struct xmp_module_info mi;
struct xmp_frame_info fi;
xmp_context ctx;
int has_module;
SDL_AudioStream *stream;
void *buffer;
int buffer_size;
Mix_MusicMetaTags tags;
} XMP_Music;


static int XMP_Seek(void *context, double position);
static void XMP_Delete(void *context);

/* Load a modplug stream from an SDL_RWops object */
void *XMP_CreateFromRW(SDL_RWops *src, int freesrc)
{
XMP_Music *music;
void *buffer;
size_t size;

music = (XMP_Music *)SDL_calloc(1, sizeof(*music));
if (!music) {
SDL_OutOfMemory();
return NULL;
}

music->ctx = NULL;
music->has_module = SDL_FALSE;

music->volume = MIX_MAX_VOLUME;

music->stream = SDL_NewAudioStream(AUDIO_S16, 2, music_spec.freq,
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
XMP_Delete(music);
return NULL;
}

music->ctx = xmp_create_context();
if (!music->ctx) {
XMP_Delete(music);
return NULL;
}

music->buffer_size = music_spec.samples * 2 * 2;
music->buffer = SDL_malloc((size_t)music->buffer_size);
if (!music->buffer) {
XMP_Delete(music);
return NULL;
}

buffer = SDL_LoadFile_RW(src, &size, SDL_FALSE);
if (buffer) {
if (xmp_load_module_from_memory(music->ctx, buffer, (long)size) < 0) {
Mix_SetError("xmp_load_module failed");
}
SDL_free(buffer);
}

if (xmp_start_player(music->ctx, music_spec.freq, 0) != 0) {
XMP_Delete(music);
return NULL;
}

music->has_module = SDL_TRUE;

meta_tags_init(&music->tags);
xmp_get_module_info(music->ctx, &music->mi);
if (music->mi.comment) {
meta_tags_set(&music->tags, MIX_META_TITLE, music->mi.comment);
}

if (freesrc) {
SDL_RWclose(src);
}
return music;
}

/* Set the volume for a modplug stream */
static void XMP_SetVolume(void *context, int volume)
{
XMP_Music *music = (XMP_Music *)context;
music->volume = volume;
}

/* Get the volume for a modplug stream */
static int XMP_GetVolume(void *context)
{
XMP_Music *music = (XMP_Music *)context;
return music->volume;
}

/* Start playback of a given modplug stream */
static int XMP_Play(void *context, int play_count)
{
XMP_Music *music = (XMP_Music *)context;
music->play_count = play_count;
return XMP_Seek(music, 0.0);
}

/* Play some of a stream previously started with modplug_play() */
static int XMP_GetSome(void *context, void *data, int bytes, SDL_bool *done)
{
XMP_Music *music = (XMP_Music *)context;
int filled, amount, ret;

filled = SDL_AudioStreamGet(music->stream, data, bytes);
if (filled != 0) {
return filled;
}

if (!music->play_count) {
/* All done */
*done = SDL_TRUE;
return 0;
}

ret = xmp_play_buffer(music->ctx, music->buffer, music->buffer_size, 0);
amount = music->buffer_size;

if (ret == 0) {
if (SDL_AudioStreamPut(music->stream, music->buffer, amount) < 0) {
return -1;
}
} else {
if (music->play_count == 1) {
music->play_count = 0;
SDL_AudioStreamFlush(music->stream);
} else {
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (XMP_Play(music, play_count) < 0) {
return -1;
}
}
}
return 0;
}
static int XMP_GetAudio(void *context, void *data, int bytes)
{
XMP_Music *music = (XMP_Music *)context;
return music_pcm_getaudio(context, data, bytes, music->volume, XMP_GetSome);
}

/* Jump (seek) to a given position */
static int XMP_Seek(void *context, double position)
{
XMP_Music *music = (XMP_Music *)context;
char dummy[2];
xmp_seek_time(music->ctx, (int)(position*1000));
xmp_play_buffer(music->ctx, dummy, 2, 0);
return 0;
}

static double XMP_Tell(void *context)
{
XMP_Music *music = (XMP_Music *)context;
xmp_get_frame_info(music->ctx, &music->fi);
return ((double)music->fi.time) / 1000.0;
}

static double XMP_Length(void *context)
{
XMP_Music *music = (XMP_Music *)context;
xmp_get_frame_info(music->ctx, &music->fi);
return ((double)music->fi.total_time) / 1000.0;
}

static const char* XMP_GetMetaTag(void *context, Mix_MusicMetaTag tag_type)
{
XMP_Music *music = (XMP_Music *)context;
return meta_tags_get(&music->tags, tag_type);
}

/* Close the given modplug stream */
static void XMP_Delete(void *context)
{
XMP_Music *music = (XMP_Music *)context;
meta_tags_clear(&music->tags);
if (music->ctx) {
if (music->has_module) {
xmp_stop_module(music->ctx);
xmp_release_module(music->ctx);
}
xmp_free_context(music->ctx);
}
if (music->stream) {
SDL_FreeAudioStream(music->stream);
}
if (music->buffer) {
SDL_free(music->buffer);
}
SDL_free(music);
}

Mix_MusicInterface Mix_MusicInterface_LIBXMP =
{
"LIBXMP",
MIX_MUSIC_LIBXMP,
MUS_MOD,
SDL_FALSE,
SDL_FALSE,

NULL, /* Load */
NULL, /* Open */
XMP_CreateFromRW,
NULL, /* CreateFromRWex [MIXER-X]*/
NULL, /* CreateFromFile */
NULL, /* CreateFromFileEx [MIXER-X]*/
XMP_SetVolume,
XMP_GetVolume, /* GetVolume [MIXER-X]*/
XMP_Play,
NULL, /* IsPlaying */
XMP_GetAudio,
XMP_Seek,
XMP_Tell, /* Tell [MIXER-X]*/
XMP_Length, /* FullLength [MIXER-X]*/
NULL, /* LoopStart [MIXER-X]*/
NULL, /* LoopEnd [MIXER-X]*/
NULL, /* LoopLength [MIXER-X]*/
XMP_GetMetaTag, /* GetMetaTag [MIXER-X]*/
NULL, /* Pause */
NULL, /* Resume */
NULL, /* Stop */
XMP_Delete,
NULL, /* Close */
NULL, /* Unload */
};

#endif /* MUSIC_MOD_XMP */

/* vi: set ts=4 sw=4 expandtab: */
@@ -0,0 +1,28 @@
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
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.
*/

/* This file supports playing MOD files with libXMP */

#include "music.h"

extern Mix_MusicInterface Mix_MusicInterface_LIBXMP;

/* vi: set ts=4 sw=4 expandtab: */
@@ -32,6 +32,7 @@
#include "music_wav.h"
#include "music_mikmod.h"
#include "music_modplug.h"
#include "music_xmp.h"
#include "music_nativemidi.h"
#include "music_fluidsynth.h"
#include "music_timidity.h"
@@ -219,6 +220,9 @@ static Mix_MusicInterface *s_music_interfaces[] =
#ifdef MUSIC_MOD_MIKMOD
&Mix_MusicInterface_MIKMOD,
#endif
#ifdef MUSIC_MOD_XMP
&Mix_MusicInterface_LIBXMP,
#endif
#ifdef MUSIC_MID_FLUIDSYNTH
&Mix_MusicInterface_FLUIDSYNTH,
#endif
@@ -51,6 +51,7 @@ typedef enum
MIX_MUSIC_GME,/*MIXER-X*/
MIX_MUSIC_ADLMIDI,/*MIXER-X*/
MIX_MUSIC_OPNMIDI,/*MIXER-X*/
MIX_MUSIC_LIBXMP,/*MIXER-X*/
MIX_MUSIC_LAST
} Mix_MusicAPI;

0 comments on commit e20a21d

Please sign in to comment.