Skip to content

Commit

Permalink
Isaac Burns added support for loading MP3 files as sound chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Sep 16, 2013
1 parent 7cc4d24 commit 7c9dbb0
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,7 @@
2.0.1:
Isaac Burns - Sun Sep 15 21:50:27 PDT 2013
* Added support for loading MP3 files as sound chunks

2.0.0:
Sam Lantinga - Sun Jun 9 14:45:30 PDT 2013
* Made libmodplug the default MOD player as it is now in the public domain
Expand Down
2 changes: 1 addition & 1 deletion Makefile.in
Expand Up @@ -39,7 +39,7 @@ VERSION_OBJECTS = @VERSION_OBJECTS@
PLAYWAVE_OBJECTS = @PLAYWAVE_OBJECTS@
PLAYMUS_OBJECTS = @PLAYMUS_OBJECTS@

DIST = *.txt Android.mk Makefile.in SDL2_mixer.pc.in SDL_mixer.h SDL2_mixer.spec SDL2_mixer.spec.in debian VisualC Xcode Xcode-iOS acinclude autogen.sh build-scripts configure configure.in dynamic_flac.c dynamic_flac.h dynamic_fluidsynth.c dynamic_fluidsynth.h dynamic_modplug.c dynamic_modplug.h dynamic_mod.c dynamic_mod.h dynamic_mp3.c dynamic_mp3.h dynamic_ogg.c dynamic_ogg.h effect_position.c effect_stereoreverse.c effects_internal.c effects_internal.h fluidsynth.c fluidsynth.h external gcc-fat.sh libmikmod-3.1.12.zip load_aiff.c load_aiff.h load_flac.c load_flac.h load_ogg.c load_ogg.h load_voc.c load_voc.h mixer.c music.c music_cmd.c music_cmd.h music_flac.c music_flac.h music_mad.c music_mad.h music_mod.c music_mod.h music_modplug.c music_modplug.h music_ogg.c music_ogg.h native_midi playmus.c playwave.c timidity wavestream.c wavestream.h version.rc
DIST = *.txt Android.mk Makefile.in SDL2_mixer.pc.in SDL_mixer.h SDL2_mixer.spec SDL2_mixer.spec.in debian VisualC Xcode Xcode-iOS acinclude autogen.sh build-scripts configure configure.in dynamic_flac.c dynamic_flac.h dynamic_fluidsynth.c dynamic_fluidsynth.h dynamic_modplug.c dynamic_modplug.h dynamic_mod.c dynamic_mod.h dynamic_mp3.c dynamic_mp3.h dynamic_ogg.c dynamic_ogg.h effect_position.c effect_stereoreverse.c effects_internal.c effects_internal.h fluidsynth.c fluidsynth.h external gcc-fat.sh libmikmod-3.1.12.zip load_aiff.c load_aiff.h load_flac.c load_flac.h load_mp3.c load_mp3.h load_ogg.c load_ogg.h load_voc.c load_voc.h mixer.c music.c music_cmd.c music_cmd.h music_flac.c music_flac.h music_mad.c music_mad.h music_mod.c music_mod.h music_modplug.c music_modplug.h music_ogg.c music_ogg.h native_midi playmus.c playwave.c timidity wavestream.c wavestream.h version.rc

LT_AGE = @LT_AGE@
LT_CURRENT = @LT_CURRENT@
Expand Down
8 changes: 8 additions & 0 deletions VisualC/SDL_mixer_VS2008.vcproj
Expand Up @@ -672,6 +672,14 @@
RelativePath="..\load_flac.h"
>
</File>
<File
RelativePath="..\load_mp3.c"
>
</File>
<File
RelativePath="..\load_mp3.h"
>
</File>
<File
RelativePath="..\load_ogg.c"
>
Expand Down
2 changes: 2 additions & 0 deletions VisualC/SDL_mixer_VS2010.vcxproj
Expand Up @@ -250,6 +250,7 @@
<ClCompile Include="..\fluidsynth.c" />
<ClCompile Include="..\load_aiff.c" />
<ClCompile Include="..\load_flac.c" />
<ClCompile Include="..\load_mp3.c" />
<ClCompile Include="..\load_ogg.c" />
<ClCompile Include="..\load_voc.c" />
<ClCompile Include="..\mixer.c" />
Expand All @@ -273,6 +274,7 @@
<ClInclude Include="..\fluidsynth.h" />
<ClInclude Include="..\load_aiff.h" />
<ClInclude Include="..\load_flac.h" />
<ClInclude Include="..\load_mp3.h" />
<ClInclude Include="..\load_ogg.h" />
<ClInclude Include="..\load_voc.h" />
<ClInclude Include="..\music_cmd.h" />
Expand Down
2 changes: 2 additions & 0 deletions VisualC/SDL_mixer_VS2012.vcxproj
Expand Up @@ -254,6 +254,7 @@
<ClCompile Include="..\fluidsynth.c" />
<ClCompile Include="..\load_aiff.c" />
<ClCompile Include="..\load_flac.c" />
<ClCompile Include="..\load_mp3.c" />
<ClCompile Include="..\load_ogg.c" />
<ClCompile Include="..\load_voc.c" />
<ClCompile Include="..\mixer.c" />
Expand All @@ -277,6 +278,7 @@
<ClInclude Include="..\fluidsynth.h" />
<ClInclude Include="..\load_aiff.h" />
<ClInclude Include="..\load_flac.h" />
<ClInclude Include="..\load_mp3.h" />
<ClInclude Include="..\load_ogg.h" />
<ClInclude Include="..\load_voc.h" />
<ClInclude Include="..\music_cmd.h" />
Expand Down
8 changes: 8 additions & 0 deletions Xcode-iOS/SDL_mixer.xcodeproj/project.pbxproj
Expand Up @@ -12,6 +12,8 @@
00938E55108A397A0009CF19 /* dynamic_mod.c in Sources */ = {isa = PBXBuildFile; fileRef = 00938E4B108A397A0009CF19 /* dynamic_mod.c */; };
00938E56108A397A0009CF19 /* dynamic_mp3.h in Headers */ = {isa = PBXBuildFile; fileRef = 00938E4C108A397A0009CF19 /* dynamic_mp3.h */; };
00938E57108A397A0009CF19 /* dynamic_ogg.h in Headers */ = {isa = PBXBuildFile; fileRef = 00938E4D108A397A0009CF19 /* dynamic_ogg.h */; };
04939B4A17E607250015E4E3 /* load_mp3.c in Sources */ = {isa = PBXBuildFile; fileRef = 04939B4817E607250015E4E3 /* load_mp3.c */; };
04939B4B17E607250015E4E3 /* load_mp3.h in Headers */ = {isa = PBXBuildFile; fileRef = 04939B4917E607250015E4E3 /* load_mp3.h */; };
04A8FCA60A19CAEC0046373F /* dynamic_mp3.c in Sources */ = {isa = PBXBuildFile; fileRef = 04A8FCA40A19CAEC0046373F /* dynamic_mp3.c */; };
04A8FCAB0A19CB070046373F /* dynamic_ogg.c in Sources */ = {isa = PBXBuildFile; fileRef = 04A8FCA90A19CB070046373F /* dynamic_ogg.c */; };
AA5F0F0914B6A6490036992F /* dynamic_fluidsynth.c in Sources */ = {isa = PBXBuildFile; fileRef = AA5F0F0214B6A6490036992F /* dynamic_fluidsynth.c */; };
Expand Down Expand Up @@ -150,6 +152,8 @@
00938E4B108A397A0009CF19 /* dynamic_mod.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = dynamic_mod.c; path = ../dynamic_mod.c; sourceTree = SOURCE_ROOT; };
00938E4C108A397A0009CF19 /* dynamic_mp3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = dynamic_mp3.h; path = ../dynamic_mp3.h; sourceTree = SOURCE_ROOT; };
00938E4D108A397A0009CF19 /* dynamic_ogg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = dynamic_ogg.h; path = ../dynamic_ogg.h; sourceTree = SOURCE_ROOT; };
04939B4817E607250015E4E3 /* load_mp3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = load_mp3.c; path = ../load_mp3.c; sourceTree = "<group>"; };
04939B4917E607250015E4E3 /* load_mp3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = load_mp3.h; path = ../load_mp3.h; sourceTree = "<group>"; };
04A8FCA40A19CAEC0046373F /* dynamic_mp3.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = dynamic_mp3.c; path = ../dynamic_mp3.c; sourceTree = SOURCE_ROOT; };
04A8FCA90A19CB070046373F /* dynamic_ogg.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = dynamic_ogg.c; path = ../dynamic_ogg.c; sourceTree = SOURCE_ROOT; };
1014BAEA010A4B677F000001 /* SDL_mixer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = SDL_mixer.h; path = ../SDL_mixer.h; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -349,6 +353,8 @@
F567D64801CD88A701F3E8B9 /* load_aiff.h */,
00938E2B108A37800009CF19 /* load_flac.c */,
00938E2C108A37800009CF19 /* load_flac.h */,
04939B4817E607250015E4E3 /* load_mp3.c */,
04939B4917E607250015E4E3 /* load_mp3.h */,
B2A42D0E03FAC6A400A8000A /* load_ogg.c */,
B2A42D0F03FAC6A400A8000A /* load_ogg.h */,
F567D64901CD88A701F3E8B9 /* load_voc.c */,
Expand Down Expand Up @@ -543,6 +549,7 @@
AA60219017653A9800662B9C /* modplug.h in Headers */,
AA60219617653A9800662B9C /* tables.h in Headers */,
AA60219A17653B8700662B9C /* dynamic_modplug.h in Headers */,
04939B4B17E607250015E4E3 /* load_mp3.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -692,6 +699,7 @@
AA60219417653A9800662B9C /* sndfile.cpp in Sources */,
AA60219517653A9800662B9C /* sndmix.cpp in Sources */,
AA60219917653B8700662B9C /* dynamic_modplug.c in Sources */,
04939B4A17E607250015E4E3 /* load_mp3.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
12 changes: 12 additions & 0 deletions Xcode/SDL_mixer.xcodeproj/project.pbxproj
Expand Up @@ -27,6 +27,10 @@
00938E57108A397A0009CF19 /* dynamic_ogg.h in Headers */ = {isa = PBXBuildFile; fileRef = 00938E4D108A397A0009CF19 /* dynamic_ogg.h */; };
0448E8AE108B937A00C9D3EA /* native_midi_macosx.c in Sources */ = {isa = PBXBuildFile; fileRef = 0448E8AD108B937A00C9D3EA /* native_midi_macosx.c */; };
0448E8AF108B937A00C9D3EA /* native_midi_macosx.c in Sources */ = {isa = PBXBuildFile; fileRef = 0448E8AD108B937A00C9D3EA /* native_midi_macosx.c */; };
04939B4E17E607F70015E4E3 /* load_mp3.c in Sources */ = {isa = PBXBuildFile; fileRef = 04939B4C17E607F70015E4E3 /* load_mp3.c */; };
04939B4F17E607F70015E4E3 /* load_mp3.c in Sources */ = {isa = PBXBuildFile; fileRef = 04939B4C17E607F70015E4E3 /* load_mp3.c */; };
04939B5017E607F70015E4E3 /* load_mp3.h in Headers */ = {isa = PBXBuildFile; fileRef = 04939B4D17E607F70015E4E3 /* load_mp3.h */; };
04939B5117E607F70015E4E3 /* load_mp3.h in Headers */ = {isa = PBXBuildFile; fileRef = 04939B4D17E607F70015E4E3 /* load_mp3.h */; };
04A8FCA60A19CAEC0046373F /* dynamic_mp3.c in Sources */ = {isa = PBXBuildFile; fileRef = 04A8FCA40A19CAEC0046373F /* dynamic_mp3.c */; };
04A8FCA70A19CAEC0046373F /* dynamic_mp3.c in Sources */ = {isa = PBXBuildFile; fileRef = 04A8FCA40A19CAEC0046373F /* dynamic_mp3.c */; };
04A8FCAB0A19CB070046373F /* dynamic_ogg.c in Sources */ = {isa = PBXBuildFile; fileRef = 04A8FCA90A19CB070046373F /* dynamic_ogg.c */; };
Expand Down Expand Up @@ -190,6 +194,8 @@
00938E4C108A397A0009CF19 /* dynamic_mp3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = dynamic_mp3.h; path = ../dynamic_mp3.h; sourceTree = SOURCE_ROOT; };
00938E4D108A397A0009CF19 /* dynamic_ogg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = dynamic_ogg.h; path = ../dynamic_ogg.h; sourceTree = SOURCE_ROOT; };
0448E8AD108B937A00C9D3EA /* native_midi_macosx.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = native_midi_macosx.c; sourceTree = "<group>"; };
04939B4C17E607F70015E4E3 /* load_mp3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = load_mp3.c; path = ../load_mp3.c; sourceTree = "<group>"; };
04939B4D17E607F70015E4E3 /* load_mp3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = load_mp3.h; path = ../load_mp3.h; sourceTree = "<group>"; };
04A8FCA40A19CAEC0046373F /* dynamic_mp3.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = dynamic_mp3.c; path = ../dynamic_mp3.c; sourceTree = SOURCE_ROOT; };
04A8FCA90A19CB070046373F /* dynamic_ogg.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = dynamic_ogg.c; path = ../dynamic_ogg.c; sourceTree = SOURCE_ROOT; };
1014BAEA010A4B677F000001 /* SDL_mixer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = SDL_mixer.h; path = ../SDL_mixer.h; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -352,6 +358,8 @@
F567D64801CD88A701F3E8B9 /* load_aiff.h */,
00938E2B108A37800009CF19 /* load_flac.c */,
00938E2C108A37800009CF19 /* load_flac.h */,
04939B4C17E607F70015E4E3 /* load_mp3.c */,
04939B4D17E607F70015E4E3 /* load_mp3.h */,
B2A42D0E03FAC6A400A8000A /* load_ogg.c */,
B2A42D0F03FAC6A400A8000A /* load_ogg.h */,
F567D64901CD88A701F3E8B9 /* load_voc.c */,
Expand Down Expand Up @@ -500,6 +508,7 @@
AA602142176538EB00662B9C /* dynamic_modplug.h in Headers */,
AA602146176538EB00662B9C /* fluidsynth.h in Headers */,
AA60214A176538EB00662B9C /* music_modplug.h in Headers */,
04939B5017E607F70015E4E3 /* load_mp3.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -534,6 +543,7 @@
AA602143176538EB00662B9C /* dynamic_modplug.h in Headers */,
AA602147176538EB00662B9C /* fluidsynth.h in Headers */,
AA60214B176538EB00662B9C /* music_modplug.h in Headers */,
04939B5117E607F70015E4E3 /* load_mp3.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -751,6 +761,7 @@
AA602140176538EB00662B9C /* dynamic_modplug.c in Sources */,
AA602144176538EB00662B9C /* fluidsynth.c in Sources */,
AA602148176538EB00662B9C /* music_modplug.c in Sources */,
04939B4E17E607F70015E4E3 /* load_mp3.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -791,6 +802,7 @@
AA602141176538EB00662B9C /* dynamic_modplug.c in Sources */,
AA602145176538EB00662B9C /* fluidsynth.c in Sources */,
AA602149176538EB00662B9C /* music_modplug.c in Sources */,
04939B4F17E607F70015E4E3 /* load_mp3.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
195 changes: 195 additions & 0 deletions load_mp3.c
@@ -0,0 +1,195 @@
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2013 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 is the source needed to decode an MP3 into a waveform.
*/

/* $Id$ */

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

#include "SDL_mixer.h"

#include "load_mp3.h"

#if defined(MP3_MUSIC)
#include "dynamic_mp3.h"
#elif defined(MP3_MAD_MUSIC)
#include "music_mad.h"
#endif

SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
{
/* note: spec is initialized to mixer spec */

#if defined(MP3_MUSIC)
SMPEG* mp3;
SMPEG_Info info;
#elif defined(MP3_MAD_MUSIC)
mad_data *mp3_mad;
#endif
long samplesize;
int read_len;
const Uint32 chunk_len = 4096;
int err = 0;

if ((!src) || (!spec) || (!audio_buf) || (!audio_len))
{
return NULL;
}

if (!err)
{
*audio_len = 0;
*audio_buf = (Uint8*) SDL_malloc(chunk_len);
err = (*audio_buf == NULL);
}

if (!err)
{
err = ((Mix_Init(MIX_INIT_MP3) & MIX_INIT_MP3) == 0);
}

if (!err)
{
#if defined(MP3_MUSIC)
mp3 = smpeg.SMPEG_new_rwops(src, &info, freesrc, 0);
err = (mp3 == NULL);
#elif defined(MP3_MAD_MUSIC)
mp3_mad = mad_openFileRW(src, spec, freesrc);
err = (mp3_mad == NULL);
#endif
}

#if defined(MP3_MUSIC)
if (!err)
{
err = !info.has_audio;
}
#endif

if (!err)
{
#if defined(MP3_MUSIC)

smpeg.SMPEG_actualSpec(mp3, spec);

smpeg.SMPEG_enableaudio(mp3, 1);
smpeg.SMPEG_enablevideo(mp3, 0);

smpeg.SMPEG_play(mp3);

/* read once for audio length */
while ((read_len = smpeg.SMPEG_playAudio(mp3, *audio_buf, chunk_len)) > 0)
{
*audio_len += read_len;
}

smpeg.SMPEG_stop(mp3);

#elif defined(MP3_MAD_MUSIC)

mad_start(mp3_mad);

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

mad_stop(mp3_mad);

#endif

err = (read_len < 0);
}

if (!err)
{
/* reallocate, if needed */
if ((*audio_len > 0) && (*audio_len != chunk_len))
{
*audio_buf = (Uint8*) SDL_realloc(*audio_buf, *audio_len);
err = (*audio_buf == NULL);
}
}

if (!err)
{
/* read again for audio buffer, if needed */
if (*audio_len > chunk_len)
{
#if defined(MP3_MUSIC)
smpeg.SMPEG_rewind(mp3);
smpeg.SMPEG_play(mp3);
err = (*audio_len != smpeg.SMPEG_playAudio(mp3, *audio_buf, *audio_len));
smpeg.SMPEG_stop(mp3);
#elif defined(MP3_MAD_MUSIC)
mad_seek(mp3_mad, 0);
mad_start(mp3_mad);
err = (*audio_len != mad_getSamples(mp3_mad, *audio_buf, *audio_len));
mad_stop(mp3_mad);
#endif
}
}

if (!err)
{
/* Don't return a buffer that isn't a multiple of samplesize */
samplesize = ((spec->format & 0xFF)/8)*spec->channels;
*audio_len &= ~(samplesize-1);
}

#if defined(MP3_MUSIC)
if (mp3)
{
smpeg.SMPEG_delete(mp3); mp3 = NULL;
/* Deleting the MP3 closed the source if desired */
freesrc = SDL_FALSE;
}
#elif defined(MP3_MAD_MUSIC)
if (mp3_mad)
{
mad_closeFile(mp3_mad); mp3_mad = NULL;
/* Deleting the MP3 closed the source if desired */
freesrc = SDL_FALSE;
}
#endif

if (freesrc)
{
SDL_RWclose(src); src = NULL;
}

/* handle error */
if (err)
{
if (*audio_buf != NULL)
{
SDL_free(*audio_buf); *audio_buf = NULL;
}
*audio_len = 0;
spec = NULL;
}

return spec;
}

#endif
30 changes: 30 additions & 0 deletions load_mp3.h
@@ -0,0 +1,30 @@
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2013 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 is the source needed to decode an MP3 into a waveform.
*/

/* $Id$ */

#ifdef MP3_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);
#endif

0 comments on commit 7c9dbb0

Please sign in to comment.