Skip to content
Permalink
Browse files

Austen Dicken - Tue Feb 26 23:28:27 PST 2008

Ok, here is the patch I made for FLAC support.

I have tested it relatively thoroughly and currently the patch allows:
 1. Pre-loading FLAC files and playing them via LoadWAV
 2. The patch allows for FLAC support in the LoadMUS setting as well as:
     *  Pause / Resume
     *  Volume control
     *  Seeking

I also did a little benchmarking by comparing memory/cpu usage of playmus to
that of mplayer, and the results were very good.  playmus typically took about
half the RAM as mplayer, though that may be attributed to mplayer being a more
"bulky" program.  As such I would say that the two are probably about equal in
efficiency.

Also, it is important to note that, similar to the OGG support currently
built-in, my FLAC patch only supports 16 bit stereo-encoded sound.  Also, it
is only for Native FLAC (standard) and not the derivative, Ogg-FLAC.

I have tried to find a simple way to detect Ogg-FLAC files, as the only
difference between Ogg-FLAC and Native FLAC support is changing the init_
function call, but after digging a little deeper it seems that Ogg-FLAC is
basically FLAC wrapped in an Ogg transport layer, so it would be better to have
a way to read the Ogg transport layer which then reads the inner audio files
according to the proper codec.

But anyway, that's another job for another day!  For now this should provide
Native FLAC support!
  • Loading branch information
slouken committed Feb 27, 2008
1 parent c0d0164 commit 36b33c0e46976f572d8820ab524e1c4dafca08a3
Showing with 1,407 additions and 1 deletion.
  1. +2 −0 CHANGES
  2. +1 −0 SDL_mixer.h
  3. +35 −0 configure.in
  4. +176 −0 dynamic_flac.c
  5. +68 −0 dynamic_flac.h
  6. +332 −0 load_flac.c
  7. +29 −0 load_flac.h
  8. +9 −1 mixer.c
  9. +71 −0 music.c
  10. +591 −0 music_flac.c
  11. +93 −0 music_flac.h
@@ -1,4 +1,6 @@
1.2.9:
Austen Dicken - Tue Feb 26 23:28:27 PST 2008
* Added support for FLAC audio both as chunks and streaming
Tilman Sauerbeck - Tue Feb 26 03:44:47 PST 2008
* Added support for streaming WAV files with Mix_LoadMUS_RW()

@@ -103,6 +103,7 @@ typedef enum {
MUS_MOD,
MUS_MID,
MUS_OGG,
MUS_FLAC,
MUS_MP3,
MUS_MP3_MAD
} Mix_MusicType;
@@ -318,6 +318,41 @@ if test x$enable_music_ogg = xyes; then
fi
fi
fi
AC_ARG_ENABLE([music-flac],
AC_HELP_STRING([--enable-music-flac], [enable FLAC music [[default=yes]]]),
[], [enable_music_flac=yes])
AC_ARG_ENABLE([music-flac-shared],
AC_HELP_STRING([--enable-music-flac-shared],
[dynamically load FLAC support [[default=yes]]]),
[], [enable_music_flac_shared=yes])
if test x$enable_music_flac = xyes; then
AC_CHECK_HEADER([FLAC/stream_decoder.h], [have_flac_hdr=yes])
AC_CHECK_LIB([FLAC], [FLAC__stream_decoder_new], [have_flac_lib=yes])
if test x$have_flac_hdr = xyes -a x$have_flac_lib = xyes; then
case "$host" in
*-*-darwin*)
flac_lib=[`find_lib FLAC*.dylib`]
;;
*-*-cygwin* | *-*-mingw32*)
flac_lib=[`find_lib "libFLAC*.dll"`]
;;
*)
flac_lib=[`find_lib "libFLAC.so.[0-9]"`]
if test x$flac_lib = x; then
flac_lib=[`find_lib "libFLAC.so.[0-9]*"`]
fi
;;
esac
SOURCES="$SOURCES $srcdir/*_flac.c"
EXTRA_CFLAGS="$EXTRA_CFLAGS -DFLAC_MUSIC"
if test x$enable_music_flac_shared = xyes && test x$flac_lib != x; then
echo "-- dynamic libFLAC -> $flac_lib"
EXTRA_CFLAGS="$EXTRA_CFLAGS -DFLAC_DYNAMIC=\\\"$flac_lib\\\""
else
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lFLAC"
fi
fi
fi
AC_ARG_ENABLE(music-mp3,
[ --enable-music-mp3 enable MP3 music via smpeg [[default=yes]]],
, enable_music_mp3=yes)
@@ -0,0 +1,176 @@
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2004 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@libsdl.org
Implementation of the dynamic loading functionality for libFLAC.
~ Austen Dicken (admin@cvpcs.org)
*/

#ifdef FLAC_MUSIC

#include "SDL_loadso.h"

#include "dynamic_flac.h"

flac_loader flac = {
0, NULL
};

#ifdef FLAC_DYNAMIC

int Mix_InitFLAC() {
if ( flac.loaded == 0 ) {
flac.handle = SDL_LoadObject(FLAC_DYNAMIC);
if ( flac.handle == NULL ) {
return -1;
}
flac.FLAC__stream_decoder_new =
(FLAC__StreamDecoder *(*)())
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_new");
if ( flac.FLAC__stream_decoder_new == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_delete =
(void (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_delete");
if ( flac.FLAC__stream_decoder_delete == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_init_stream =
(FLAC__StreamDecoderInitStatus (*)(
FLAC__StreamDecoder *,
FLAC__StreamDecoderReadCallback,
FLAC__StreamDecoderSeekCallback,
FLAC__StreamDecoderTellCallback,
FLAC__StreamDecoderLengthCallback,
FLAC__StreamDecoderEofCallback,
FLAC__StreamDecoderWriteCallback,
FLAC__StreamDecoderMetadataCallback,
FLAC__StreamDecoderErrorCallback,
void *))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_init_stream");
if ( flac.FLAC__stream_decoder_init_stream == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_finish =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_finish");
if ( flac.FLAC__stream_decoder_finish == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_flush =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_flush");
if ( flac.FLAC__stream_decoder_flush == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_process_single =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle,
"FLAC__stream_decoder_process_single");
if ( flac.FLAC__stream_decoder_process_single == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_process_until_end_of_metadata =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle,
"FLAC__stream_decoder_process_until_end_of_metadata");
if ( flac.FLAC__stream_decoder_process_until_end_of_metadata == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_process_until_end_of_stream =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle,
"FLAC__stream_decoder_process_until_end_of_stream");
if ( flac.FLAC__stream_decoder_process_until_end_of_stream == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_seek_absolute =
(FLAC__bool (*)(FLAC__StreamDecoder *, FLAC__uint64))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_seek_absolute");
if ( flac.FLAC__stream_decoder_seek_absolute == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_get_state =
(FLAC__StreamDecoderState (*)(const FLAC__StreamDecoder *decoder))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_get_state");
if ( flac.FLAC__stream_decoder_get_state == NULL ) {
SDL_UnloadObject(flac.handle);
return -1;
}
}
++flac.loaded;

return 0;
}
void Mix_QuitFLAC() {
if ( flac.loaded == 0 ) {
return;
}
if ( flac.loaded == 1 ) {
SDL_UnloadObject(flac.handle);
}
--flac.loaded;
}
#else
int Mix_InitFLAC() {
if ( flac.loaded == 0 ) {
flac.FLAC__stream_decoder_new = FLAC__stream_decoder_new;
flac.FLAC__stream_decoder_delete = FLAC__stream_decoder_delete;
flac.FLAC__stream_decoder_init_stream =
FLAC__stream_decoder_init_stream;
flac.FLAC__stream_decoder_finish = FLAC__stream_decoder_finish;
flac.FLAC__stream_decoder_flush = FLAC__stream_decoder_flush;
flac.FLAC__stream_decoder_process_single =
FLAC__stream_decoder_process_single;
flac.FLAC__stream_decoder_process_until_end_of_metadata =
FLAC__stream_decoder_process_until_end_of_metadata;
flac.FLAC__stream_decoder_process_until_end_of_stream =
FLAC__stream_decoder_process_until_end_of_stream;
flac.FLAC__stream_decoder_seek_absolute =
FLAC__stream_decoder_seek_absolute;
flac.FLAC__stream_decoder_get_state =
FLAC__stream_decoder_get_state;
}
++flac.loaded;

return 0;
}
void Mix_QuitFLAC() {
if ( flac.loaded == 0 ) {
return;
}
if ( flac.loaded == 1 ) {
}
--flac.loaded;
}
#endif // FLAC_DYNAMIC

#endif // FLAC_MUSIC
@@ -0,0 +1,68 @@
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2004 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@libsdl.org
The following file defines all of the functions/objects used to dynamically
link to the libFLAC library.
~ Austen Dicken (admin@cvpcs.org)
*/

#ifdef FLAC_MUSIC

#include <FLAC/stream_decoder.h>

typedef struct {
int loaded;
void *handle;
FLAC__StreamDecoder *(*FLAC__stream_decoder_new)();
void (*FLAC__stream_decoder_delete)(FLAC__StreamDecoder *decoder);
FLAC__StreamDecoderInitStatus (*FLAC__stream_decoder_init_stream)(
FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderReadCallback read_callback,
FLAC__StreamDecoderSeekCallback seek_callback,
FLAC__StreamDecoderTellCallback tell_callback,
FLAC__StreamDecoderLengthCallback length_callback,
FLAC__StreamDecoderEofCallback eof_callback,
FLAC__StreamDecoderWriteCallback write_callback,
FLAC__StreamDecoderMetadataCallback metadata_callback,
FLAC__StreamDecoderErrorCallback error_callback,
void *client_data);
FLAC__bool (*FLAC__stream_decoder_finish)(FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_flush)(FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_process_single)(
FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_process_until_end_of_metadata)(
FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_process_until_end_of_stream)(
FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_seek_absolute)(
FLAC__StreamDecoder *decoder,
FLAC__uint64 sample);
FLAC__StreamDecoderState (*FLAC__stream_decoder_get_state)(
const FLAC__StreamDecoder *decoder);
} flac_loader;

extern flac_loader flac;

extern int Mix_InitFLAC();
extern void Mix_QuitFLAC();

#endif // FLAC_MUSIC

0 comments on commit 36b33c0

Please sign in to comment.