Exemple #1
0
/* Mixing function */
void music_mixer(void *udata, Uint8 *stream, int len)
{
	int left = 0;

	if ( music_playing && music_active ) {
		/* Handle fading */
		if ( music_playing->fading != MIX_NO_FADING ) {
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
				int volume;
				int fade_step = music_playing->fade_step;
				int fade_steps = music_playing->fade_steps;

				if ( music_playing->fading == MIX_FADING_OUT ) {
					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
				} else { /* Fading in */
					volume = (music_volume * fade_step) / fade_steps;
				}
				music_internal_volume(volume);
			} else {
				if ( music_playing->fading == MIX_FADING_OUT ) {
					music_internal_halt();
					if ( music_finished_hook ) {
						music_finished_hook();
					}
					return;
				}
				music_playing->fading = MIX_NO_FADING;
			}
		}
		
		if (music_halt_or_loop() == 0)
			return;
		
		
		switch (music_playing->type) {
#ifdef CMD_MUSIC
			case MUS_CMD:
				/* The playing is done externally */
				break;
#endif
#ifdef WAV_MUSIC
			case MUS_WAV:
				left = WAVStream_PlaySome(stream, len);
				break;
#endif
#ifdef MOD_MUSIC
			case MUS_MOD:
				left = MOD_playAudio(music_playing->data.module, stream, len);
				break;
#endif
#ifdef MID_MUSIC
#ifdef USE_TIMIDITY_MIDI
			case MUS_MID:
				if ( timidity_ok ) {
					int samples = len / samplesize;
  					Timidity_PlaySome(stream, samples);
				}
				break;
#endif
#endif
#ifdef OGG_MUSIC
			case MUS_OGG:
				
				left = OGG_playAudio(music_playing->data.ogg, stream, len);
				break;
#endif
#ifdef FLAC_MUSIC
			case MUS_FLAC:
				left = FLAC_playAudio(music_playing->data.flac, stream, len);
				break;
#endif
#ifdef MP3_MUSIC
			case MUS_MP3:
				left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
				break;
#endif
#ifdef MP3_MAD_MUSIC
			case MUS_MP3_MAD:
				left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
				break;
#endif
			default:
				/* Unknown music type?? */
				break;
		}
	}

	/* Handle seamless music looping */
	if (left > 0 && left < len && music_halt_or_loop()) {
		music_mixer(udata, stream+(len-left), left);
	}
}
Exemple #2
0
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 = 0;
	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;
}