Skip to content

Commit

Permalink
Add music (Javanaise#41)
Browse files Browse the repository at this point in the history
* Libretro: Adding music, doesnt work

* Fix invalid pointer

* Fix audio_mixer_mix_mod

* Fix libretro playing music doesn't work

* Update common.cpp

* Update Makefile.common

* Update common.cpp
  • Loading branch information
DevilFrost authored and frranck committed Nov 19, 2018
1 parent 2ea24c0 commit 51cf69a
Show file tree
Hide file tree
Showing 9 changed files with 83,421 additions and 51,728 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,6 @@ CC = CL.exe
CXX = CL.exe
LD = lib.exe
LOAD_FROM_FILES := 1
CFLAGS += -DLOAD_FROM_FILES
LDFLAGS += -lminizip
export INCLUDE := $(XDK)/xbox/include
export LIB := $(XDK)/xbox/lib
PATH := $(call unixcygpath,$(XDK)/xbox/bin/vc71):$(PATH)
Expand Down Expand Up @@ -303,6 +301,11 @@ endif

LDFLAGS += $(LIBM)

ifneq ($(LOAD_FROM_FILES),)
CFLAGS += -DLOAD_FROM_FILES
LDFLAGS += -lminizip
endif

ifneq ($(DEBUG),)
CFLAGS += -g -pg -DDEBUG
LDFLAGS += -g -pg
Expand All @@ -312,7 +315,7 @@ CFLAGS += -O3
endif
endif

CFLAGS += -DMRBOOM -D_FORTIFY_SOURCE=0 -DPLATFORM=\"$(shell uname)\" -DGIT_VERSION=\"$(GIT_VERSION)\"
CFLAGS += -DMRBOOM -DHAVE_IBXM -D_FORTIFY_SOURCE=0 -DPLATFORM=\"$(shell uname)\" -DGIT_VERSION=\"$(GIT_VERSION)\"

SDL2LIBS := -lSDL2 -lSDL2_mixer -lminizip -lmodplug

Expand Down
57 changes: 28 additions & 29 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ LIBRETRO_COMM_DIR := $(CORE_DIR)/libretro-common
AI_DIR := $(CORE_DIR)/ai
SOURCES_C := $(CORE_DIR)/mrboom.c

INCFLAGS := -I$(LIBRETRO_COMM_DIR)/include \
INCFLAGS := -I$(LIBRETRO_COMM_DIR)/include -I$(LIBRETRO_COMM_DIR)\
-I$(AI_DIR) \
-I$(CORE_DIR)

Expand All @@ -26,34 +26,33 @@ endif
ifeq ($(STATIC_LINKING), 1)
else
SOURCES_C += $(LIBRETRO_COMM_DIR)/file/file_path.c \
$(LIBRETRO_COMM_DIR)/compat/compat_strcasestr.c \
$(LIBRETRO_COMM_DIR)/compat/compat_posix_string.c \
$(LIBRETRO_COMM_DIR)/compat/fopen_utf8.c \
$(LIBRETRO_COMM_DIR)/string/stdstring.c \
$(LIBRETRO_COMM_DIR)/compat/compat_strl.c \
$(LIBRETRO_COMM_DIR)/streams/memory_stream.c \
$(LIBRETRO_COMM_DIR)/streams/file_stream.c \
$(LIBRETRO_COMM_DIR)/vfs/vfs_implementation.c \
$(LIBRETRO_COMM_DIR)/lists/string_list.c \
$(LIBRETRO_COMM_DIR)/memmap/memalign.c \
$(LIBRETRO_COMM_DIR)/encodings/encoding_utf.c \
$(LIBRETRO_COMM_DIR)/net/net_http.c \
$(LIBRETRO_COMM_DIR)/net/net_compat.c \
$(LIBRETRO_COMM_DIR)/net/net_socket.c
endif

ifneq ($(LOAD_FROM_FILES),)
SOURCES_C += $(LIBRETRO_COMM_DIR)/formats/wav/rwav.c \
$(LIBRETRO_COMM_DIR)/audio/audio_mix.c \
$(LIBRETRO_COMM_DIR)/file/config_file.c \
$(LIBRETRO_COMM_DIR)/file/config_file_userdata.c \
$(LIBRETRO_COMM_DIR)/audio/resampler/audio_resampler.c \
$(LIBRETRO_COMM_DIR)/features/features_cpu.c \
$(LIBRETRO_COMM_DIR)/audio/conversion/float_to_s16.c \
$(LIBRETRO_COMM_DIR)/audio/conversion/s16_to_float.c \
$(LIBRETRO_COMM_DIR)/audio/resampler/drivers/sinc_resampler.c \
$(LIBRETRO_COMM_DIR)/audio/resampler/drivers/nearest_resampler.c \
$(LIBRETRO_COMM_DIR)/audio/resampler/drivers/null_resampler.c
$(LIBRETRO_COMM_DIR)/compat/compat_strcasestr.c \
$(LIBRETRO_COMM_DIR)/compat/compat_posix_string.c \
$(LIBRETRO_COMM_DIR)/compat/fopen_utf8.c \
$(LIBRETRO_COMM_DIR)/string/stdstring.c \
$(LIBRETRO_COMM_DIR)/compat/compat_strl.c \
$(LIBRETRO_COMM_DIR)/streams/file_stream.c \
$(LIBRETRO_COMM_DIR)/streams/memory_stream.c \
$(LIBRETRO_COMM_DIR)/lists/string_list.c \
$(LIBRETRO_COMM_DIR)/memmap/memalign.c \
$(LIBRETRO_COMM_DIR)/encodings/encoding_utf.c \
$(LIBRETRO_COMM_DIR)/vfs/vfs_implementation.c \
$(LIBRETRO_COMM_DIR)/net/net_http.c \
$(LIBRETRO_COMM_DIR)/net/net_compat.c \
$(LIBRETRO_COMM_DIR)/net/net_socket.c \
$(LIBRETRO_COMM_DIR)/audio/audio_mixer.c \
$(LIBRETRO_COMM_DIR)/audio/resampler/audio_resampler.c \
$(LIBRETRO_COMM_DIR)/file/config_file_userdata.c \
$(LIBRETRO_COMM_DIR)/file/config_file.c \
$(LIBRETRO_COMM_DIR)/features/features_cpu.c \
$(LIBRETRO_COMM_DIR)/audio/resampler/drivers/nearest_resampler.c \
$(LIBRETRO_COMM_DIR)/audio/resampler/drivers/sinc_resampler.c \
$(LIBRETRO_COMM_DIR)/audio/resampler/drivers/null_resampler.c \
$(LIBRETRO_COMM_DIR)/formats/wav/rwav.c \
$(LIBRETRO_COMM_DIR)/audio/audio_mix.c \
$(LIBRETRO_COMM_DIR)/audio/conversion/s16_to_float.c \
$(LIBRETRO_COMM_DIR)/audio/conversion/float_to_s16.c \
$(LIBRETRO_COMM_DIR)/ibxm/ibxm.c
endif

ifneq ($(STATETESTS),)
Expand Down
112 changes: 97 additions & 15 deletions common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ extern "C" {
#pragma GCC diagnostic ignored "-Woverlength-strings"
#pragma GCC diagnostic ignored "-Warray-bounds"


#define NB_CHIPTUNES 8

#ifdef __LIBRETRO__

#include <audio/audio_mixer.h>
#include <audio/conversion/float_to_s16.h>
static float *fbuf = NULL;
static int16_t *ibuf = NULL;
static audio_mixer_sound_t *musics[NB_CHIPTUNES];
#ifndef LOAD_FROM_FILES
#include "retro_data.h"
#include "retro_music_data.h"
#endif

#include "retro.hpp"
Expand All @@ -72,16 +77,8 @@ static size_t frames_left[NB_WAV];
#define CLAMP_I16(x) (x > INT16_MAX ? INT16_MAX : x < INT16_MIN ? INT16_MIN : x)
#endif


#ifdef __LIBSDL2__
#define LOAD_FROM_FILES
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
static Mix_Chunk * wave[NB_WAV];
#define NB_CHIPTUNES 8
static Mix_Music *musics[NB_CHIPTUNES];
static int musics_index = 0;
bool music = true;
bool music = true;
static int musics_index = 0;
const char *musics_filenames[NB_CHIPTUNES] = {
"deadfeelings.XM", // Carter (for menu + replay)
"chiptune.MOD", // 4-mat
Expand All @@ -92,6 +89,13 @@ const char *musics_filenames[NB_CHIPTUNES] = {
"external.XM", // Quazar
"ESTRAYK-Drop.MOD" // Estrayk
};
#ifdef __LIBSDL2__
#define LOAD_FROM_FILES
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
static Mix_Chunk *wave[NB_WAV];
static Mix_Music *musics[NB_CHIPTUNES];

#define DEFAULT_VOLUME MIX_MAX_VOLUME / 2
#define MATKAMIE_VOLUME MIX_MAX_VOLUME
#define LOWER_VOLUME MIX_MAX_VOLUME / 3
Expand Down Expand Up @@ -142,7 +146,6 @@ int walkingToCell[nb_dyna];
int rom_unzip(const char *path, const char *extraction_directory)
{
path_mkdir(extraction_directory);

unzFile *zipfile = (unzFile *)unzOpen(path);
if (zipfile == NULL)
{
Expand Down Expand Up @@ -306,6 +309,11 @@ bool mrboom_init()
}
strcpy((char *)&m.iff_file_name, "mrboom.dat");
m.taille_exe_gonfle = 0;
#ifdef __LIBRETRO__
fbuf = (float *)malloc(num_samples_per_frame * 2 * sizeof(float));
ibuf = (int16_t *)malloc(num_samples_per_frame * 2 * sizeof(int16_t));
audio_mixer_init(SAMPLE_RATE);
#endif
#ifdef __LIBSDL2__
/* Initialize SDL. */
if (SDL_Init(SDL_INIT_AUDIO) < 0)
Expand Down Expand Up @@ -414,6 +422,36 @@ bool mrboom_init()
}
unlink(filePath);
}
#endif
#ifdef __LIBRETRO__
#ifdef HAVE_IBXM
#ifdef LOAD_FROM_FILES
for (int i = 0; i < NB_CHIPTUNES; i++)
{
sprintf(filePath, "%s/%s", extractPath, musics_filenames[i]);
int64_t len = 0;
void * buf = NULL;
if (!filestream_read_file(filePath, &buf, &len))
{
log_error("Could not load %s\n", filePath);
musics[i] = NULL;
}
else
{
musics[i] = audio_mixer_load_mod(buf, len);
}
}
#else
musics[0] = audio_mixer_load_mod(rom_deadfeelings_XM, rom_deadfeelings_XM_len);
musics[1] = audio_mixer_load_mod(rom_chiptune_MOD, rom_chiptune_MOD_len);
musics[2] = audio_mixer_load_mod(rom_matkamie_MOD, rom_matkamie_MOD_len);
musics[3] = audio_mixer_load_mod(rom_jester_chipmunks_MOD, rom_jester_chipmunks_MOD_len);
musics[4] = audio_mixer_load_mod(rom_unreeeal_superhero_3_looping_version_XM, rom_unreeeal_superhero_3_looping_version_XM_len);
musics[5] = audio_mixer_load_mod(rom_anar11_MOD, rom_anar11_MOD_len);
musics[6] = audio_mixer_load_mod(rom_external_XM, rom_external_XM_len);
musics[7] = audio_mixer_load_mod(rom_ESTRAYK_Drop_MOD, rom_ESTRAYK_Drop_MOD_len);
#endif
#endif
#endif

ignoreForAbitFlag[0] = 30;
Expand Down Expand Up @@ -467,6 +505,16 @@ bool mrboom_init()

void mrboom_deinit()
{
#ifdef __LIBRETRO__
for (int i = 0; i < NB_CHIPTUNES; i++)
{
#ifdef LOAD_FROM_FILES
audio_mixer_destroy(musics[i]);
#else
free(musics[i]);
#endif
}
#endif
#ifdef LOAD_FROM_FILES
/* free WAV */
for (int i = 0; i < NB_WAV; i++)
Expand All @@ -479,6 +527,11 @@ void mrboom_deinit()
#endif
}
#endif
#ifdef __LIBRETRO__
free(fbuf);
free(ibuf);
audio_mixer_done();
#endif
#ifndef NO_NETWORK
if (network_init_done)
{
Expand Down Expand Up @@ -591,6 +644,10 @@ void mrboom_sound(void)
fxSound(isGamePaused, 19)
fxSound(isGameUnPaused, 5)
fxSound(playerGotDisease, 20)

#ifdef __LIBRETRO__
static audio_mixer_voice_t* voice = NULL;
#endif

static int last_voice = 0;
for (int i = 0; i < NB_WAV; i++)
Expand Down Expand Up @@ -674,7 +731,6 @@ void mrboom_sound(void)
log_error("Wrong sample id %d or NULL.\n", a1);
}
}
#ifdef __LIBSDL2__
if (music)
{
static int currentLevel = -2;
Expand All @@ -686,12 +742,23 @@ void mrboom_sound(void)
{
index = 0;
}
#ifdef __LIBSDL2__
Mix_VolumeMusic(musics_volume[index]);
log_debug("Playing %s volume:%d\n", musics_filenames[index], Mix_VolumeMusic(-1));
if (Mix_PlayMusic(musics[index], -1) == -1)
{
log_error("error playing music %d\n", musics[0]);
}
#else
//audio_mixer_voice_t* audio_mixer_play(audio_mixer_sound_t* sound,
// bool repeat, float volume, audio_mixer_stop_cb_t stop_cb);
if (voice)
{
audio_mixer_stop(voice);
}
voice = audio_mixer_play(musics[index], true, 1, NULL); //stop_cb);
#endif

if (index)
{
musics_index = (musics_index + 1) % (NB_CHIPTUNES);
Expand All @@ -702,9 +769,14 @@ void mrboom_sound(void)
}
}
}
#endif
}

#ifdef __LIBRETRO__
void stop_cb(audio_mixer_sound_t *sound, unsigned reason)
{
}
#endif

static void mrboom_reset_special_keys()
{
db *keys = m.total_t;
Expand Down Expand Up @@ -908,6 +980,16 @@ void audio_callback(void)
}
}
}

memset(fbuf, 0, num_samples_per_frame * 2 * sizeof(float));
audio_mixer_mix(fbuf, num_samples_per_frame, 1, false);
convert_float_to_s16(ibuf, fbuf, num_samples_per_frame * 2);

for (i = 0; i < num_samples_per_frame; i++)
{
frame_sample_buf[i * 2] = CLAMP_I16(frame_sample_buf[i * 2] + ibuf[i * 2]);
frame_sample_buf[(i * 2) + 1] = CLAMP_I16(frame_sample_buf[(i * 2) + 1] + ibuf[(i * 2) + 1]);
}


audio_batch_cb(frame_sample_buf, num_samples_per_frame);
Expand Down
Loading

0 comments on commit 51cf69a

Please sign in to comment.