load_mp3.c
changeset 777 92882ef2ab81
parent 776 07b8e286bc03
child 778 05c93f7af403
     1.1 --- a/load_mp3.c	Mon Oct 16 02:16:47 2017 -0700
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,226 +0,0 @@
     1.4 -/*
     1.5 -  SDL_mixer:  An audio mixer library based on the SDL library
     1.6 -  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     1.7 -
     1.8 -  This software is provided 'as-is', without any express or implied
     1.9 -  warranty.  In no event will the authors be held liable for any damages
    1.10 -  arising from the use of this software.
    1.11 -
    1.12 -  Permission is granted to anyone to use this software for any purpose,
    1.13 -  including commercial applications, and to alter it and redistribute it
    1.14 -  freely, subject to the following restrictions:
    1.15 -
    1.16 -  1. The origin of this software must not be misrepresented; you must not
    1.17 -	 claim that you wrote the original software. If you use this software
    1.18 -	 in a product, an acknowledgment in the product documentation would be
    1.19 -	 appreciated but is not required.
    1.20 -  2. Altered source versions must be plainly marked as such, and must not be
    1.21 -	 misrepresented as being the original software.
    1.22 -  3. This notice may not be removed or altered from any source distribution.
    1.23 -
    1.24 -  This is the source needed to decode an MP3 into a waveform.
    1.25 -*/
    1.26 -
    1.27 -/* $Id$ */
    1.28 -
    1.29 -#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
    1.30 -
    1.31 -#include "SDL_mixer.h"
    1.32 -
    1.33 -#include "load_mp3.h"
    1.34 -
    1.35 -#if defined(MP3_MUSIC)
    1.36 -#include "dynamic_mp3.h"
    1.37 -#elif defined(MP3_MAD_MUSIC)
    1.38 -#include "music_mad.h"
    1.39 -#elif defined(MP3_MPG_MUSIC)
    1.40 -#include "music_mpg.h"
    1.41 -#endif
    1.42 -
    1.43 -SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
    1.44 -{
    1.45 -	/* note: spec is initialized to mixer spec */
    1.46 -
    1.47 -#if defined(MP3_MUSIC)
    1.48 -	SMPEG* mp3;
    1.49 -	SMPEG_Info info;
    1.50 -#elif defined(MP3_MAD_MUSIC)
    1.51 -	mad_data *mp3_mad;
    1.52 -#elif defined(MP3_MPG_MUSIC)
    1.53 -	mpg_data *mp3_mpg;
    1.54 -#endif
    1.55 -	long samplesize;
    1.56 -	int read_len;
    1.57 -	const Uint32 chunk_len = 4096;
    1.58 -	int err = 0;
    1.59 -
    1.60 -	if ((!src) || (!spec) || (!audio_buf) || (!audio_len))
    1.61 -	{
    1.62 -		return NULL;
    1.63 -	}
    1.64 -
    1.65 -	if (!err)
    1.66 -	{
    1.67 -		*audio_len = 0;
    1.68 -		*audio_buf = (Uint8*) SDL_malloc(chunk_len);
    1.69 -		err = (*audio_buf == NULL);
    1.70 -	}
    1.71 -
    1.72 -	if (!err)
    1.73 -	{
    1.74 -		err = ((Mix_Init(MIX_INIT_MP3) & MIX_INIT_MP3) == 0);
    1.75 -	}
    1.76 -
    1.77 -	if (!err)
    1.78 -	{
    1.79 -#if defined(MP3_MUSIC)
    1.80 -		mp3 = smpeg.SMPEG_new_rwops(src, &info, freesrc, 0);
    1.81 -		err = (mp3 == NULL);
    1.82 -#elif defined(MP3_MAD_MUSIC)
    1.83 -        mp3_mad = mad_openFileRW(src, spec, freesrc);
    1.84 -		err = (mp3_mad == NULL);
    1.85 -#elif defined(MP3_MPG_MUSIC)
    1.86 -		mp3_mpg = mpg_new_rw(src, spec, freesrc);
    1.87 -        err = (mp3_mpg == NULL);
    1.88 -#endif
    1.89 -	}
    1.90 -
    1.91 -#if defined(MP3_MUSIC)
    1.92 -	if (!err)
    1.93 -	{
    1.94 -		err = !info.has_audio;
    1.95 -	}
    1.96 -#endif
    1.97 -
    1.98 -	if (!err)
    1.99 -	{
   1.100 -#if defined(MP3_MUSIC)
   1.101 -
   1.102 -		smpeg.SMPEG_actualSpec(mp3, spec);
   1.103 -
   1.104 -		smpeg.SMPEG_enableaudio(mp3, 1);
   1.105 -		smpeg.SMPEG_enablevideo(mp3, 0);
   1.106 -
   1.107 -		smpeg.SMPEG_play(mp3);
   1.108 -
   1.109 -		/* read once for audio length */
   1.110 -		while ((read_len = smpeg.SMPEG_playAudio(mp3, *audio_buf, chunk_len)) > 0)
   1.111 -		{
   1.112 -			*audio_len += read_len;
   1.113 -		}
   1.114 -
   1.115 -		smpeg.SMPEG_stop(mp3);
   1.116 -
   1.117 -#elif defined(MP3_MAD_MUSIC)
   1.118 -
   1.119 -        mad_start(mp3_mad);
   1.120 -
   1.121 -		/* read once for audio length */
   1.122 -		while ((read_len = mad_getSamples(mp3_mad, *audio_buf, chunk_len)) > 0)
   1.123 -		{
   1.124 -			*audio_len += read_len;
   1.125 -		}
   1.126 -
   1.127 -		mad_stop(mp3_mad);
   1.128 -
   1.129 -#elif defined(MP3_MPG_MUSIC)
   1.130 -
   1.131 -		mpg_start(mp3_mpg);
   1.132 -
   1.133 -		/* read once for audio length */
   1.134 -		while ((read_len = mpg_get_samples(mp3_mpg, *audio_buf, chunk_len)) > 0)
   1.135 -		{
   1.136 -			*audio_len += read_len;
   1.137 -		}
   1.138 -
   1.139 -		mpg_stop(mp3_mpg);
   1.140 -
   1.141 -#endif
   1.142 -
   1.143 -		err = (read_len < 0);
   1.144 -	}
   1.145 -
   1.146 -	if (!err)
   1.147 -	{
   1.148 -		/* reallocate, if needed */
   1.149 -		if ((*audio_len > 0) && (*audio_len != chunk_len))
   1.150 -		{
   1.151 -			*audio_buf = (Uint8*) SDL_realloc(*audio_buf, *audio_len);
   1.152 -			err = (*audio_buf == NULL);
   1.153 -		}
   1.154 -	}
   1.155 -
   1.156 -	if (!err)
   1.157 -	{
   1.158 -		/* read again for audio buffer, if needed */
   1.159 -		if (*audio_len > chunk_len)
   1.160 -		{
   1.161 -#if defined(MP3_MUSIC)
   1.162 -			smpeg.SMPEG_rewind(mp3);
   1.163 -			smpeg.SMPEG_play(mp3);
   1.164 -			err = (*audio_len != smpeg.SMPEG_playAudio(mp3, *audio_buf, *audio_len));
   1.165 -			smpeg.SMPEG_stop(mp3);
   1.166 -#elif defined(MP3_MAD_MUSIC)
   1.167 -			mad_seek(mp3_mad, 0);
   1.168 -			mad_start(mp3_mad);
   1.169 -			err = (*audio_len != mad_getSamples(mp3_mad, *audio_buf, *audio_len));
   1.170 -			mad_stop(mp3_mad);
   1.171 -#elif defined(MP3_MPG_MUSIC)
   1.172 -			mpg_seek(mp3_mpg, 0);
   1.173 -			mpg_start(mp3_mpg);
   1.174 -			err = (*audio_len != mpg_get_samples(mp3_mpg, *audio_buf, *audio_len));
   1.175 -			mpg_stop(mp3_mpg);
   1.176 -#endif
   1.177 -		}
   1.178 -	}
   1.179 -
   1.180 -	if (!err)
   1.181 -	{
   1.182 -		/* Don't return a buffer that isn't a multiple of samplesize */
   1.183 -		samplesize = ((spec->format & 0xFF)/8)*spec->channels;
   1.184 -		*audio_len &= ~(samplesize-1);
   1.185 -	}
   1.186 -
   1.187 -#if defined(MP3_MUSIC)
   1.188 -	if (mp3)
   1.189 -	{
   1.190 -		smpeg.SMPEG_delete(mp3); mp3 = NULL;
   1.191 -		/* Deleting the MP3 closed the source if desired */
   1.192 -		freesrc = SDL_FALSE;
   1.193 -	}
   1.194 -#elif defined(MP3_MAD_MUSIC)
   1.195 -	if (mp3_mad)
   1.196 -	{
   1.197 -		mad_closeFile(mp3_mad); mp3_mad = NULL;
   1.198 -		/* Deleting the MP3 closed the source if desired */
   1.199 -		freesrc = SDL_FALSE;
   1.200 -	}
   1.201 -#elif defined(MP3_MPG_MUSIC)
   1.202 -	if (mp3_mpg)
   1.203 -	{
   1.204 -		mpg_delete(mp3_mpg); mp3_mpg = NULL;
   1.205 -		/* Deleting the MP3 closed the source if desired */
   1.206 -		freesrc = SDL_FALSE;
   1.207 -	}
   1.208 -#endif
   1.209 -
   1.210 -	if (freesrc)
   1.211 -	{
   1.212 -		SDL_RWclose(src); src = NULL;
   1.213 -	}
   1.214 -
   1.215 -	/* handle error */
   1.216 -	if (err)
   1.217 -	{
   1.218 -		if (*audio_buf != NULL)
   1.219 -		{
   1.220 -			SDL_free(*audio_buf); *audio_buf = NULL;
   1.221 -		}
   1.222 -		*audio_len = 0;
   1.223 -		spec = NULL;
   1.224 -	}
   1.225 -
   1.226 -	return spec;
   1.227 -}
   1.228 -
   1.229 -#endif