Skip to content

Commit

Permalink
add mpg123 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco149 committed Jul 20, 2017
1 parent c981e8f commit f8ba201
Show file tree
Hide file tree
Showing 10 changed files with 595 additions and 11 deletions.
5 changes: 3 additions & 2 deletions SDL_mixer.h
Expand Up @@ -123,6 +123,7 @@ typedef enum {
MUS_OGG,
MUS_MP3,
MUS_MP3_MAD,
MUS_MP3_MPG,
MUS_FLAC,
MUS_MODPLUG
} Mix_MusicType;
Expand Down Expand Up @@ -590,8 +591,8 @@ extern DECLSPEC int SDLCALL Mix_PausedMusic(void);
/* Set the current position in the music stream.
This returns 0 if successful, or -1 if it failed or isn't implemented.
This function is only implemented for MOD music formats (set pattern
order number) and for OGG, FLAC, MP3_MAD, and MODPLUG music (set
position in seconds), at the moment.
order number) and for OGG, FLAC, MP3_MAD, MP3_MPG and MODPLUG music
(set position in seconds), at the moment.
*/
extern DECLSPEC int SDLCALL Mix_SetMusicPosition(double position);

Expand Down
24 changes: 23 additions & 1 deletion configure.in
Expand Up @@ -643,7 +643,29 @@ if test x$enable_music_mp3 = xyes -a x$enable_music_mp3_mad_gpl = xyes; then
fi
fi

if test x$have_smpeg = xyes -o x$have_libmad = xyes; then
AC_ARG_ENABLE(music-mp3-mpg123,
AC_HELP_STRING([--enable-music-mp3-mpg123], [enable MP3 music via libmpg123 [[default=no]]]),
[], [enable_music_mp3_mpg123=no])
if test x$enable_music_mp3 = xyes -a x$enable_music_mp3_mpg123 = xyes; then
AC_MSG_CHECKING(for libmpg123 headers)
have_libmpg123=no
AC_TRY_COMPILE([
#include "mpg123.h"
],[
],[
have_libmpg123=yes
])
AC_MSG_RESULT($have_libmpg123)
if test x$have_libmpg123 = xyes; then
SOURCES="$SOURCES $srcdir/music_mpg.c"
EXTRA_CFLAGS="$EXTRA_CFLAGS -DMP3_MPG_MUSIC"
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lmpg123"
else
AC_MSG_WARN([*** Unable to find mpg123 library (https://www.mpg123.de)])
fi
fi

if test x$have_smpeg = xyes -o x$have_libmad = xyes -o x$have_libmpg123; then
SOURCES="$SOURCES $srcdir/*_mp3.c"
else
AC_MSG_WARN([MP3 support disabled])
Expand Down
19 changes: 19 additions & 0 deletions dynamic_mp3.c
Expand Up @@ -178,4 +178,23 @@ void Mix_QuitMP3()
}
#endif /* MP3_DYNAMIC */

#elif defined(MP3_MPG_MUSIC)
#include "mpg123.h"

int Mix_InitMP3()
{
int result;

result = mpg123_init();
if (result != MPG123_OK) {
return 1;
}

return 0;
}

void Mix_QuitMP3() {
mpg123_exit();
}

#endif /* MP3_MUSIC */
4 changes: 4 additions & 0 deletions dynamic_mp3.h
Expand Up @@ -48,6 +48,10 @@ typedef struct {

extern smpeg_loader smpeg;

#elif defined(MP3_MPG_MUSIC)

#include "mpg123.h"

#endif /* MUSIC_MP3 */

extern int Mix_InitMP3();
Expand Down
33 changes: 32 additions & 1 deletion load_mp3.c
Expand Up @@ -23,7 +23,7 @@

/* $Id$ */

#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)

#include "SDL_mixer.h"

Expand All @@ -33,6 +33,8 @@
#include "dynamic_mp3.h"
#elif defined(MP3_MAD_MUSIC)
#include "music_mad.h"
#elif defined(MP3_MPG_MUSIC)
#include "music_mpg.h"
#endif

SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
Expand All @@ -44,6 +46,8 @@ SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec,
SMPEG_Info info;
#elif defined(MP3_MAD_MUSIC)
mad_data *mp3_mad;
#elif defined(MP3_MPG_MUSIC)
mpg_data *mp3_mpg;
#endif
long samplesize;
int read_len;
Expand Down Expand Up @@ -75,6 +79,9 @@ SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec,
#elif defined(MP3_MAD_MUSIC)
mp3_mad = mad_openFileRW(src, spec, freesrc);
err = (mp3_mad == NULL);
#elif defined(MP3_MPG_MUSIC)
mp3_mpg = mpg_new_rw(src, spec, freesrc);
err = (mp3_mpg == NULL);
#endif
}

Expand Down Expand Up @@ -116,6 +123,18 @@ SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec,

mad_stop(mp3_mad);

#elif defined(MP3_MPG_MUSIC)

mpg_start(mp3_mpg);

/* read once for audio length */
while ((read_len = mpg_get_samples(mp3_mpg, *audio_buf, chunk_len)) > 0)
{
*audio_len += read_len;
}

mpg_stop(mp3_mpg);

#endif

err = (read_len < 0);
Expand Down Expand Up @@ -146,6 +165,11 @@ SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec,
mad_start(mp3_mad);
err = (*audio_len != mad_getSamples(mp3_mad, *audio_buf, *audio_len));
mad_stop(mp3_mad);
#elif defined(MP3_MPG_MUSIC)
mpg_seek(mp3_mpg, 0);
mpg_start(mp3_mpg);
err = (*audio_len != mpg_get_samples(mp3_mpg, *audio_buf, *audio_len));
mpg_stop(mp3_mpg);
#endif
}
}
Expand All @@ -171,6 +195,13 @@ SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec,
/* Deleting the MP3 closed the source if desired */
freesrc = SDL_FALSE;
}
#elif defined(MP3_MPG_MUSIC)
if (mp3_mpg)
{
mpg_delete(mp3_mpg); mp3_mpg = NULL;
/* Deleting the MP3 closed the source if desired */
freesrc = SDL_FALSE;
}
#endif

if (freesrc)
Expand Down
2 changes: 1 addition & 1 deletion load_mp3.h
Expand Up @@ -23,7 +23,7 @@

/* $Id$ */

#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
/* Don't call this directly; use Mix_LoadWAV_RW() for now. */
SDL_AudioSpec *Mix_LoadMP3_RW (SDL_RWops *src, int freesrc,
SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
Expand Down
8 changes: 4 additions & 4 deletions mixer.c
Expand Up @@ -188,7 +188,7 @@ int Mix_Init(int flags)
#endif
}
if (flags & MIX_INIT_MP3) {
#ifdef MP3_MUSIC
#if defined(MP3_MUSIC) || defined(MP3_MPG_MUSIC)
if ((initialized & MIX_INIT_MP3) || Mix_InitMP3() == 0) {
result |= MIX_INIT_MP3;
}
Expand Down Expand Up @@ -234,7 +234,7 @@ void Mix_Quit()
Mix_QuitMOD();
}
#endif
#ifdef MP3_MUSIC
#if defined(MP3_MUSIC) || defined(MP3_MPG_MUSIC)
if (initialized & MIX_INIT_MP3) {
Mix_QuitMP3();
}
Expand Down Expand Up @@ -506,7 +506,7 @@ int Mix_OpenAudioDevice(int frequency, Uint16 format, int nchannels, int chunksi
#ifdef FLAC_MUSIC
add_chunk_decoder("FLAC");
#endif
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
add_chunk_decoder("MP3");
#endif

Expand Down Expand Up @@ -665,7 +665,7 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
(Uint8 **)&chunk->abuf, &chunk->alen);
break;
default:
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
if (detect_mp3((Uint8*)&magic))
{
/* note: send a copy of the mixer spec */
Expand Down
57 changes: 55 additions & 2 deletions music.c
Expand Up @@ -64,11 +64,14 @@
#ifdef MP3_MAD_MUSIC
#include "music_mad.h"
#endif
#ifdef MP3_MPG_MUSIC
#include "music_mpg.h"
#endif
#ifdef FLAC_MUSIC
#include "music_flac.h"
#endif

#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
static SDL_AudioSpec used_mixer;
#endif

Expand Down Expand Up @@ -115,6 +118,9 @@ struct _Mix_Music {
#ifdef MP3_MAD_MUSIC
mad_data *mp3_mad;
#endif
#ifdef MP3_MPG_MUSIC
mpg_data *mp3_mpg;
#endif
#ifdef FLAC_MUSIC
FLAC_music *flac;
#endif
Expand Down Expand Up @@ -332,6 +338,11 @@ void music_mixer(void *udata, Uint8 *stream, int len)
case MUS_MP3_MAD:
left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
break;
#endif
#ifdef MP3_MPG_MUSIC
case MUS_MP3_MPG:
left = mpg_get_samples(music_playing->data.mp3_mpg, stream, len);
break;
#endif
default:
/* Unknown music type?? */
Expand Down Expand Up @@ -412,7 +423,7 @@ int open_music(SDL_AudioSpec *mixer)
add_music_decoder("FLAC");
}
#endif
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
/* Keep a copy of the mixer */
used_mixer = *mixer;
add_music_decoder("MP3");
Expand Down Expand Up @@ -678,6 +689,16 @@ Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *src, Mix_MusicType type, int freesrc)
Mix_SetError("Could not initialize MPEG stream.");
}
break;
#elif defined(MP3_MPG_MUSIC)
case MUS_MP3:
music->type = MUS_MP3_MPG;
music->data.mp3_mpg = mpg_new_rw(src, &used_mixer, freesrc);
if (music->data.mp3_mpg) {
music->error = 0;
} else {
Mix_SetError("Could not initialize MPEG stream.");
}
break;
#endif
#ifdef MID_MUSIC
case MUS_MID:
Expand Down Expand Up @@ -841,6 +862,11 @@ void Mix_FreeMusic(Mix_Music *music)
case MUS_MP3_MAD:
mad_closeFile(music->data.mp3_mad);
break;
#endif
#ifdef MP3_MPG_MUSIC
case MUS_MP3_MPG:
mpg_delete(music->data.mp3_mpg);
break;
#endif
default:
/* Unknown music type?? */
Expand Down Expand Up @@ -968,6 +994,11 @@ static int music_internal_play(Mix_Music *music, double position)
case MUS_MP3_MAD:
mad_start(music->data.mp3_mad);
break;
#endif
#ifdef MP3_MPG_MUSIC
case MUS_MP3_MPG:
mpg_start(music->data.mp3_mpg);
break;
#endif
default:
Mix_SetError("Can't play unknown music type");
Expand Down Expand Up @@ -1085,6 +1116,11 @@ int music_internal_position(double position)
case MUS_MP3_MAD:
mad_seek(music_playing->data.mp3_mad, position);
break;
#endif
#ifdef MP3_MPG_MUSIC
case MUS_MP3_MPG:
mpg_seek(music_playing->data.mp3_mpg, position);
break;
#endif
default:
/* TODO: Implement this for other music backends */
Expand Down Expand Up @@ -1187,6 +1223,11 @@ static void music_internal_volume(int volume)
case MUS_MP3_MAD:
mad_setVolume(music_playing->data.mp3_mad, volume);
break;
#endif
#ifdef MP3_MPG_MUSIC
case MUS_MP3_MPG:
mpg_volume(music_playing->data.mp3_mpg, volume);
break;
#endif
default:
/* Unknown music type?? */
Expand Down Expand Up @@ -1278,6 +1319,11 @@ static void music_internal_halt(void)
case MUS_MP3_MAD:
mad_stop(music_playing->data.mp3_mad);
break;
#endif
#ifdef MP3_MPG_MUSIC
case MUS_MP3_MPG:
mpg_stop(music_playing->data.mp3_mpg);
break;
#endif
default:
/* Unknown music type?? */
Expand Down Expand Up @@ -1466,6 +1512,13 @@ static int music_internal_playing()
playing = 0;
}
break;
#endif
#ifdef MP3_MPG_MUSIC
case MUS_MP3_MPG:
if (!mpg_playing(music_playing->data.mp3_mpg)) {
playing = 0;
}
break;
#endif
default:
playing = 0;
Expand Down

0 comments on commit f8ba201

Please sign in to comment.