⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl_audio.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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
*/

#ifdef SAVE_RCSID
static char rcsid =
 "@(#) $Id: SDL_audio.c,v 1.4 2002/04/22 21:38:01 wmay Exp $";
#endif

/* Allow access to a raw mixing buffer */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "SDL.h"
#include "SDL_audio.h"
#include "SDL_timer.h"
#include "SDL_error.h"
#include "SDL_audio_c.h"
#include "SDL_audiomem.h"
#include "SDL_sysaudio.h"

/* Available audio drivers */
static AudioBootStrap *bootstrap[] = {
#ifdef OPENBSD_AUDIO_SUPPORT
	&OPENBSD_AUDIO_bootstrap,
#endif
#ifdef OSS_SUPPORT
	&DSP_bootstrap,
	&DMA_bootstrap,
#endif
#ifdef ALSA_SUPPORT
	&ALSA_bootstrap,
#endif
#ifdef SUNAUDIO_SUPPORT
	&SUNAUDIO_bootstrap,
#endif
#ifdef DMEDIA_SUPPORT
	&DMEDIA_bootstrap,
#endif
#ifdef ARTSC_SUPPORT
	&ARTSC_bootstrap,
#endif
#ifdef ESD_SUPPORT
	&ESD_bootstrap,
#endif
#ifdef NAS_SUPPORT
	&NAS_bootstrap,
#endif
#ifdef ENABLE_DIRECTX
	&DSOUND_bootstrap,
#endif
#ifdef ENABLE_WINDIB
	&WAVEOUT_bootstrap,
#endif
#ifdef __BEOS__
	&BAUDIO_bootstrap,
#endif
#if defined(macintosh) || TARGET_API_MAC_CARBON
	&SNDMGR_bootstrap,
#endif
#ifdef _AIX
	&Paud_bootstrap,
#endif
#ifdef ENABLE_AHI
	&AHI_bootstrap,
#endif
#ifdef DISKAUD_SUPPORT
	&DISKAUD_bootstrap,
#endif
	NULL
};
SDL_AudioDevice *current_audio = NULL;

/* Various local functions */
int SDL_AudioInit(const char *driver_name);
void SDL_AudioQuit(void);

#ifdef ENABLE_AHI
static int audio_configured = 0;
#endif

/* The general mixing thread function */
int SDL_RunAudio(void *audiop)
{
	SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop;
	Uint8 *stream;
	int    stream_len;
	void  *udata;
	void (*fill)(void *userdata,Uint8 *stream, int len);
	int    silence;
#ifdef ENABLE_AHI
	int started = 0;

/* AmigaOS NEEDS that the audio driver is opened in the thread that uses it! */

	D(bug("Task audio started audio struct:<%lx>...\n",audiop));

	D(bug("Before Openaudio..."));
	if(audio->OpenAudio(audio, &audio->spec)==-1)
	{
		D(bug("Open audio failed...\n"));
		return(-1);
	}
	D(bug("OpenAudio...OK\n"));
#endif

	/* Perform any thread setup */
	if ( audio->ThreadInit ) {
		audio->ThreadInit(audio);
	}
	audio->threadid = SDL_ThreadID();

	/* Set up the mixing function */
	fill  = audio->spec.callback;
	udata = audio->spec.userdata;

#ifdef ENABLE_AHI
	audio_configured = 1;

	D(bug("Audio configured... Checking for conversion\n"));
	SDL_mutexP(audio->mixer_lock);
	D(bug("Semaphore obtained...\n"));
#endif

	if ( audio->convert.needed ) {
		if ( audio->convert.src_format == AUDIO_U8 ) {
			silence = 0x80;
		} else {
			silence = 0;
		}
		stream_len = audio->convert.len;
	} else {
		silence = audio->spec.silence;
		stream_len = audio->spec.size;
	}
	stream = audio->fake_stream;

#ifdef ENABLE_AHI
	SDL_mutexV(audio->mixer_lock);
	D(bug("Entering audio loop...\n"));
#endif


	/* Loop, filling the audio buffers */
	while ( audio->enabled ) {

		/* Wait for new current buffer to finish playing */
		if ( stream == audio->fake_stream ) {
			SDL_Delay((audio->spec.samples*1000)/audio->spec.freq);
		} else {
#ifdef ENABLE_AHI
			if ( started > 1 )
#endif
			audio->WaitAudio(audio);
		}

		/* Fill the current buffer with sound */
		if ( audio->convert.needed ) {
			if ( audio->convert.buf ) {
				stream = audio->convert.buf;
			} else {
				continue;
			}
		} else {
			stream = audio->GetAudioBuf(audio);
			if ( stream == NULL ) {
				stream = audio->fake_stream;
			}
		}
		memset(stream, silence, stream_len);

		if ( ! audio->paused ) {
			SDL_mutexP(audio->mixer_lock);
			(*fill)(udata, stream, stream_len);
			SDL_mutexV(audio->mixer_lock);
		}

		/* Convert the audio if necessary */
		if ( audio->convert.needed ) {
			SDL_ConvertAudio(&audio->convert);
			stream = audio->GetAudioBuf(audio);
			if ( stream == NULL ) {
				stream = audio->fake_stream;
			}
			memcpy(stream, audio->convert.buf,
			               audio->convert.len_cvt);
		}

		/* Ready current buffer for play and change current buffer */
		if ( stream != audio->fake_stream ) {
			audio->PlayAudio(audio);
#ifdef ENABLE_AHI
/* AmigaOS don't have to wait the first time audio is played! */
			started++;
#endif
		}
	}
	/* Wait for the audio to drain.. */
	if ( audio->WaitDone ) {
		audio->WaitDone(audio);
	}

#ifdef ENABLE_AHI
	D(bug("WaitAudio...Done\n"));

	audio->CloseAudio(audio);

	D(bug("CloseAudio..Done, subtask exiting...\n"));
	audio_configured = 0;
#endif
	return(0);
}

static void SDL_LockAudio_Default(SDL_AudioDevice *audio)
{
	if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
		return;
	}
	SDL_mutexP(audio->mixer_lock);
}

static void SDL_UnlockAudio_Default(SDL_AudioDevice *audio)
{
	if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
		return;
	}
	SDL_mutexV(audio->mixer_lock);
}

int SDL_AudioInit(const char *driver_name)
{
	SDL_AudioDevice *audio;
	int i = 0, idx;

	/* Check to make sure we don't overwrite 'current_audio' */
	if ( current_audio != NULL ) {
		SDL_AudioQuit();
	}

	/* Select the proper audio driver */
	audio = NULL;
	idx = 0;
#ifdef unix
	if ( (driver_name == NULL) && (getenv("ESPEAKER") != NULL) ) {
		/* Ahem, we know that if ESPEAKER is set, user probably wants
		   to use ESD, but don't start it if it's not already running.
		   This probably isn't the place to do this, but... Shh! :)
		 */
		for ( i=0; bootstrap[i]; ++i ) {
			if ( strcmp(bootstrap[i]->name, "esd") == 0 ) {
				const char *esd_no_spawn;

				/* Don't start ESD if it's not running */
				esd_no_spawn = getenv("ESD_NO_SPAWN");
				if ( esd_no_spawn == NULL ) {
					putenv("ESD_NO_SPAWN=1");
				}
				if ( bootstrap[i]->available() ) {
					audio = bootstrap[i]->create(0);
					break;
				}
#ifdef linux	/* No unsetenv() on most platforms */
				if ( esd_no_spawn == NULL ) {
					unsetenv("ESD_NO_SPAWN");
				}
#endif
			}
		}
	}
#endif /* unix */
	if ( audio == NULL ) {
		if ( driver_name != NULL ) {
#if 0	/* This will be replaced with a better driver selection API */
			if ( strrchr(driver_name, ':') != NULL ) {
				idx = atoi(strrchr(driver_name, ':')+1);
			}
#endif
			for ( i=0; bootstrap[i]; ++i ) {
				if (strncmp(bootstrap[i]->name, driver_name,
				            strlen(bootstrap[i]->name)) == 0) {
					if ( bootstrap[i]->available() ) {
						audio=bootstrap[i]->create(idx);
						break;
					}
				}
			}
		} else {
			for ( i=0; bootstrap[i]; ++i ) {
				if ( bootstrap[i]->available() ) {
					audio = bootstrap[i]->create(idx);
					if ( audio != NULL ) {
						break;
					}
				}
			}
		}
		if ( audio == NULL ) {
			SDL_SetError("No available audio device");
#if 0 /* Don't fail SDL_Init() if audio isn't available.
         SDL_OpenAudio() will handle it at that point.  *sigh*
       */
			return(-1);
#endif
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -