src/audio/SDL_audio.c
changeset 21 75a95f82bc1f
parent 0 74212992fb08
child 35 d3bc792e136d
     1.1 --- a/src/audio/SDL_audio.c	Thu May 10 20:10:54 2001 +0000
     1.2 +++ b/src/audio/SDL_audio.c	Thu May 10 20:13:29 2001 +0000
     1.3 @@ -76,6 +76,10 @@
     1.4  #ifdef _AIX
     1.5  	&Paud_bootstrap,
     1.6  #endif
     1.7 +#ifdef ENABLE_AHI
     1.8 +	&AHI_bootstrap,
     1.9 +#endif
    1.10 +
    1.11  	NULL
    1.12  };
    1.13  SDL_AudioDevice *current_audio = NULL;
    1.14 @@ -84,6 +88,9 @@
    1.15  int SDL_AudioInit(const char *driver_name);
    1.16  void SDL_AudioQuit(void);
    1.17  
    1.18 +#ifdef ENABLE_AHI
    1.19 +static int audio_configured = 0;
    1.20 +#endif
    1.21  
    1.22  /* The general mixing thread function */
    1.23  int SDL_RunAudio(void *audiop)
    1.24 @@ -94,6 +101,21 @@
    1.25  	void  *udata;
    1.26  	void (*fill)(void *userdata,Uint8 *stream, int len);
    1.27  	int    silence;
    1.28 +#ifdef ENABLE_AHI
    1.29 +	int started = 0;
    1.30 +
    1.31 +/* AmigaOS NEEDS that the audio driver is opened in the thread that uses it! */
    1.32 +
    1.33 +	D(bug("Task audio started audio struct:<%lx>...\n",audiop));
    1.34 +
    1.35 +	D(bug("Before Openaudio..."));
    1.36 +	if(audio->OpenAudio(audio, &audio->spec)==-1)
    1.37 +	{
    1.38 +		D(bug("Open audio failed...\n"));
    1.39 +		return(-1);
    1.40 +	}
    1.41 +	D(bug("OpenAudio...OK\n"));
    1.42 +#endif
    1.43  
    1.44  	/* Perform any thread setup */
    1.45  	if ( audio->ThreadInit ) {
    1.46 @@ -104,6 +126,15 @@
    1.47  	/* Set up the mixing function */
    1.48  	fill  = audio->spec.callback;
    1.49  	udata = audio->spec.userdata;
    1.50 +
    1.51 +#ifdef ENABLE_AHI
    1.52 +	audio_configured = 1;
    1.53 +
    1.54 +	D(bug("Audio configured... Checking for conversion\n"));
    1.55 +	SDL_mutexP(audio->mixer_lock);
    1.56 +	D(bug("Semaphore obtained...\n"));
    1.57 +#endif
    1.58 +
    1.59  	if ( audio->convert.needed ) {
    1.60  		if ( audio->convert.src_format == AUDIO_U8 ) {
    1.61  			silence = 0x80;
    1.62 @@ -117,6 +148,12 @@
    1.63  	}
    1.64  	stream = audio->fake_stream;
    1.65  
    1.66 +#ifdef ENABLE_AHI
    1.67 +	SDL_mutexV(audio->mixer_lock);
    1.68 +	D(bug("Entering audio loop...\n"));
    1.69 +#endif
    1.70 +
    1.71 +
    1.72  	/* Loop, filling the audio buffers */
    1.73  	while ( audio->enabled ) {
    1.74  
    1.75 @@ -124,12 +161,14 @@
    1.76  		if ( stream == audio->fake_stream ) {
    1.77  			SDL_Delay((audio->spec.samples*1000)/audio->spec.freq);
    1.78  		} else {
    1.79 +#ifdef ENABLE_AHI
    1.80 +			if ( started > 1 )
    1.81 +#endif
    1.82  			audio->WaitAudio(audio);
    1.83  		}
    1.84  
    1.85  		/* Fill the current buffer with sound */
    1.86  		if ( audio->convert.needed ) {
    1.87 -			/* The buffer may not be allocated yet */
    1.88  			if ( audio->convert.buf ) {
    1.89  				stream = audio->convert.buf;
    1.90  			} else {
    1.91 @@ -163,12 +202,25 @@
    1.92  		/* Ready current buffer for play and change current buffer */
    1.93  		if ( stream != audio->fake_stream ) {
    1.94  			audio->PlayAudio(audio);
    1.95 +#ifdef ENABLE_AHI
    1.96 +/* AmigaOS don't have to wait the first time audio is played! */
    1.97 +			started++;
    1.98 +#endif
    1.99  		}
   1.100  	}
   1.101  	/* Wait for the audio to drain.. */
   1.102  	if ( audio->WaitDone ) {
   1.103  		audio->WaitDone(audio);
   1.104  	}
   1.105 +
   1.106 +#ifdef ENABLE_AHI
   1.107 +	D(bug("WaitAudio...Done\n"));
   1.108 +
   1.109 +	audio->CloseAudio(audio);
   1.110 +
   1.111 +	D(bug("CloseAudio..Done, subtask exiting...\n"));
   1.112 +	audio_configured = 0;
   1.113 +#endif
   1.114  	return(0);
   1.115  }
   1.116  
   1.117 @@ -312,11 +364,33 @@
   1.118  	audio->convert.needed = 0;
   1.119  	audio->enabled = 1;
   1.120  	audio->paused  = 1;
   1.121 +
   1.122 +#ifndef ENABLE_AHI
   1.123 +
   1.124 +/* AmigaOS opens audio inside the main loop */
   1.125  	audio->opened = audio->OpenAudio(audio, &audio->spec)+1;
   1.126 +
   1.127  	if ( ! audio->opened ) {
   1.128  		SDL_CloseAudio();
   1.129  		return(-1);
   1.130  	}
   1.131 +#else
   1.132 +	D(bug("Locking semaphore..."));
   1.133 +	SDL_mutexP(audio->mixer_lock);
   1.134 +
   1.135 +	audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
   1.136 +	D(bug("Created thread...\n"));
   1.137 +
   1.138 +	if ( audio->thread == NULL ) {
   1.139 +		SDL_mutexV(audio->mixer_lock);
   1.140 +		SDL_CloseAudio();
   1.141 +		SDL_SetError("Couldn't create audio thread");
   1.142 +		return(-1);
   1.143 +	}
   1.144 +
   1.145 +	while(!audio_configured)
   1.146 +		SDL_Delay(100);
   1.147 +#endif
   1.148  
   1.149  	/* If the audio driver changes the buffer size, accept it */
   1.150  	if ( audio->spec.samples != desired->samples ) {
   1.151 @@ -365,6 +439,7 @@
   1.152  		}
   1.153  	}
   1.154  
   1.155 +#ifndef ENABLE_AHI
   1.156  	/* Start the audio thread if necessary */
   1.157  	switch (audio->opened) {
   1.158  		case  1:
   1.159 @@ -381,6 +456,12 @@
   1.160  			/* The audio is now playing */
   1.161  			break;
   1.162  	}
   1.163 +#else
   1.164 +	SDL_mutexV(audio->mixer_lock);
   1.165 +	D(bug("SDL_OpenAudio USCITA...\n"));
   1.166 +
   1.167 +#endif
   1.168 +
   1.169  	return(0);
   1.170  }
   1.171  
   1.172 @@ -457,12 +538,14 @@
   1.173  		}
   1.174  		if ( audio->convert.needed ) {
   1.175  			SDL_FreeAudioMem(audio->convert.buf);
   1.176 +
   1.177  		}
   1.178 +#ifndef ENABLE_AHI
   1.179  		if ( audio->opened ) {
   1.180  			audio->CloseAudio(audio);
   1.181  			audio->opened = 0;
   1.182  		}
   1.183 -
   1.184 +#endif
   1.185  		/* Free the driver data */
   1.186  		audio->free(audio);
   1.187  		current_audio = NULL;