src/audio/SDL_audio.c
changeset 21 75a95f82bc1f
parent 0 74212992fb08
child 35 d3bc792e136d
equal deleted inserted replaced
20:3dc008dc229d 21:75a95f82bc1f
    74 	&SNDMGR_bootstrap,
    74 	&SNDMGR_bootstrap,
    75 #endif
    75 #endif
    76 #ifdef _AIX
    76 #ifdef _AIX
    77 	&Paud_bootstrap,
    77 	&Paud_bootstrap,
    78 #endif
    78 #endif
       
    79 #ifdef ENABLE_AHI
       
    80 	&AHI_bootstrap,
       
    81 #endif
       
    82 
    79 	NULL
    83 	NULL
    80 };
    84 };
    81 SDL_AudioDevice *current_audio = NULL;
    85 SDL_AudioDevice *current_audio = NULL;
    82 
    86 
    83 /* Various local functions */
    87 /* Various local functions */
    84 int SDL_AudioInit(const char *driver_name);
    88 int SDL_AudioInit(const char *driver_name);
    85 void SDL_AudioQuit(void);
    89 void SDL_AudioQuit(void);
    86 
    90 
       
    91 #ifdef ENABLE_AHI
       
    92 static int audio_configured = 0;
       
    93 #endif
    87 
    94 
    88 /* The general mixing thread function */
    95 /* The general mixing thread function */
    89 int SDL_RunAudio(void *audiop)
    96 int SDL_RunAudio(void *audiop)
    90 {
    97 {
    91 	SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop;
    98 	SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop;
    92 	Uint8 *stream;
    99 	Uint8 *stream;
    93 	int    stream_len;
   100 	int    stream_len;
    94 	void  *udata;
   101 	void  *udata;
    95 	void (*fill)(void *userdata,Uint8 *stream, int len);
   102 	void (*fill)(void *userdata,Uint8 *stream, int len);
    96 	int    silence;
   103 	int    silence;
       
   104 #ifdef ENABLE_AHI
       
   105 	int started = 0;
       
   106 
       
   107 /* AmigaOS NEEDS that the audio driver is opened in the thread that uses it! */
       
   108 
       
   109 	D(bug("Task audio started audio struct:<%lx>...\n",audiop));
       
   110 
       
   111 	D(bug("Before Openaudio..."));
       
   112 	if(audio->OpenAudio(audio, &audio->spec)==-1)
       
   113 	{
       
   114 		D(bug("Open audio failed...\n"));
       
   115 		return(-1);
       
   116 	}
       
   117 	D(bug("OpenAudio...OK\n"));
       
   118 #endif
    97 
   119 
    98 	/* Perform any thread setup */
   120 	/* Perform any thread setup */
    99 	if ( audio->ThreadInit ) {
   121 	if ( audio->ThreadInit ) {
   100 		audio->ThreadInit(audio);
   122 		audio->ThreadInit(audio);
   101 	}
   123 	}
   102 	audio->threadid = SDL_ThreadID();
   124 	audio->threadid = SDL_ThreadID();
   103 
   125 
   104 	/* Set up the mixing function */
   126 	/* Set up the mixing function */
   105 	fill  = audio->spec.callback;
   127 	fill  = audio->spec.callback;
   106 	udata = audio->spec.userdata;
   128 	udata = audio->spec.userdata;
       
   129 
       
   130 #ifdef ENABLE_AHI
       
   131 	audio_configured = 1;
       
   132 
       
   133 	D(bug("Audio configured... Checking for conversion\n"));
       
   134 	SDL_mutexP(audio->mixer_lock);
       
   135 	D(bug("Semaphore obtained...\n"));
       
   136 #endif
       
   137 
   107 	if ( audio->convert.needed ) {
   138 	if ( audio->convert.needed ) {
   108 		if ( audio->convert.src_format == AUDIO_U8 ) {
   139 		if ( audio->convert.src_format == AUDIO_U8 ) {
   109 			silence = 0x80;
   140 			silence = 0x80;
   110 		} else {
   141 		} else {
   111 			silence = 0;
   142 			silence = 0;
   115 		silence = audio->spec.silence;
   146 		silence = audio->spec.silence;
   116 		stream_len = audio->spec.size;
   147 		stream_len = audio->spec.size;
   117 	}
   148 	}
   118 	stream = audio->fake_stream;
   149 	stream = audio->fake_stream;
   119 
   150 
       
   151 #ifdef ENABLE_AHI
       
   152 	SDL_mutexV(audio->mixer_lock);
       
   153 	D(bug("Entering audio loop...\n"));
       
   154 #endif
       
   155 
       
   156 
   120 	/* Loop, filling the audio buffers */
   157 	/* Loop, filling the audio buffers */
   121 	while ( audio->enabled ) {
   158 	while ( audio->enabled ) {
   122 
   159 
   123 		/* Wait for new current buffer to finish playing */
   160 		/* Wait for new current buffer to finish playing */
   124 		if ( stream == audio->fake_stream ) {
   161 		if ( stream == audio->fake_stream ) {
   125 			SDL_Delay((audio->spec.samples*1000)/audio->spec.freq);
   162 			SDL_Delay((audio->spec.samples*1000)/audio->spec.freq);
   126 		} else {
   163 		} else {
       
   164 #ifdef ENABLE_AHI
       
   165 			if ( started > 1 )
       
   166 #endif
   127 			audio->WaitAudio(audio);
   167 			audio->WaitAudio(audio);
   128 		}
   168 		}
   129 
   169 
   130 		/* Fill the current buffer with sound */
   170 		/* Fill the current buffer with sound */
   131 		if ( audio->convert.needed ) {
   171 		if ( audio->convert.needed ) {
   132 			/* The buffer may not be allocated yet */
       
   133 			if ( audio->convert.buf ) {
   172 			if ( audio->convert.buf ) {
   134 				stream = audio->convert.buf;
   173 				stream = audio->convert.buf;
   135 			} else {
   174 			} else {
   136 				continue;
   175 				continue;
   137 			}
   176 			}
   161 		}
   200 		}
   162 
   201 
   163 		/* Ready current buffer for play and change current buffer */
   202 		/* Ready current buffer for play and change current buffer */
   164 		if ( stream != audio->fake_stream ) {
   203 		if ( stream != audio->fake_stream ) {
   165 			audio->PlayAudio(audio);
   204 			audio->PlayAudio(audio);
       
   205 #ifdef ENABLE_AHI
       
   206 /* AmigaOS don't have to wait the first time audio is played! */
       
   207 			started++;
       
   208 #endif
   166 		}
   209 		}
   167 	}
   210 	}
   168 	/* Wait for the audio to drain.. */
   211 	/* Wait for the audio to drain.. */
   169 	if ( audio->WaitDone ) {
   212 	if ( audio->WaitDone ) {
   170 		audio->WaitDone(audio);
   213 		audio->WaitDone(audio);
   171 	}
   214 	}
       
   215 
       
   216 #ifdef ENABLE_AHI
       
   217 	D(bug("WaitAudio...Done\n"));
       
   218 
       
   219 	audio->CloseAudio(audio);
       
   220 
       
   221 	D(bug("CloseAudio..Done, subtask exiting...\n"));
       
   222 	audio_configured = 0;
       
   223 #endif
   172 	return(0);
   224 	return(0);
   173 }
   225 }
   174 
   226 
   175 int SDL_AudioInit(const char *driver_name)
   227 int SDL_AudioInit(const char *driver_name)
   176 {
   228 {
   310 	/* Open the audio subsystem */
   362 	/* Open the audio subsystem */
   311 	memcpy(&audio->spec, desired, sizeof(audio->spec));
   363 	memcpy(&audio->spec, desired, sizeof(audio->spec));
   312 	audio->convert.needed = 0;
   364 	audio->convert.needed = 0;
   313 	audio->enabled = 1;
   365 	audio->enabled = 1;
   314 	audio->paused  = 1;
   366 	audio->paused  = 1;
       
   367 
       
   368 #ifndef ENABLE_AHI
       
   369 
       
   370 /* AmigaOS opens audio inside the main loop */
   315 	audio->opened = audio->OpenAudio(audio, &audio->spec)+1;
   371 	audio->opened = audio->OpenAudio(audio, &audio->spec)+1;
       
   372 
   316 	if ( ! audio->opened ) {
   373 	if ( ! audio->opened ) {
   317 		SDL_CloseAudio();
   374 		SDL_CloseAudio();
   318 		return(-1);
   375 		return(-1);
   319 	}
   376 	}
       
   377 #else
       
   378 	D(bug("Locking semaphore..."));
       
   379 	SDL_mutexP(audio->mixer_lock);
       
   380 
       
   381 	audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
       
   382 	D(bug("Created thread...\n"));
       
   383 
       
   384 	if ( audio->thread == NULL ) {
       
   385 		SDL_mutexV(audio->mixer_lock);
       
   386 		SDL_CloseAudio();
       
   387 		SDL_SetError("Couldn't create audio thread");
       
   388 		return(-1);
       
   389 	}
       
   390 
       
   391 	while(!audio_configured)
       
   392 		SDL_Delay(100);
       
   393 #endif
   320 
   394 
   321 	/* If the audio driver changes the buffer size, accept it */
   395 	/* If the audio driver changes the buffer size, accept it */
   322 	if ( audio->spec.samples != desired->samples ) {
   396 	if ( audio->spec.samples != desired->samples ) {
   323 		desired->samples = audio->spec.samples;
   397 		desired->samples = audio->spec.samples;
   324 		SDL_CalculateAudioSpec(desired);
   398 		SDL_CalculateAudioSpec(desired);
   363 				}
   437 				}
   364 			}
   438 			}
   365 		}
   439 		}
   366 	}
   440 	}
   367 
   441 
       
   442 #ifndef ENABLE_AHI
   368 	/* Start the audio thread if necessary */
   443 	/* Start the audio thread if necessary */
   369 	switch (audio->opened) {
   444 	switch (audio->opened) {
   370 		case  1:
   445 		case  1:
   371 			/* Start the audio thread */
   446 			/* Start the audio thread */
   372 			audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
   447 			audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
   379 
   454 
   380 		default:
   455 		default:
   381 			/* The audio is now playing */
   456 			/* The audio is now playing */
   382 			break;
   457 			break;
   383 	}
   458 	}
       
   459 #else
       
   460 	SDL_mutexV(audio->mixer_lock);
       
   461 	D(bug("SDL_OpenAudio USCITA...\n"));
       
   462 
       
   463 #endif
       
   464 
   384 	return(0);
   465 	return(0);
   385 }
   466 }
   386 
   467 
   387 SDL_audiostatus SDL_GetAudioStatus(void)
   468 SDL_audiostatus SDL_GetAudioStatus(void)
   388 {
   469 {
   455 		if ( audio->fake_stream != NULL ) {
   536 		if ( audio->fake_stream != NULL ) {
   456 			SDL_FreeAudioMem(audio->fake_stream);
   537 			SDL_FreeAudioMem(audio->fake_stream);
   457 		}
   538 		}
   458 		if ( audio->convert.needed ) {
   539 		if ( audio->convert.needed ) {
   459 			SDL_FreeAudioMem(audio->convert.buf);
   540 			SDL_FreeAudioMem(audio->convert.buf);
   460 		}
   541 
       
   542 		}
       
   543 #ifndef ENABLE_AHI
   461 		if ( audio->opened ) {
   544 		if ( audio->opened ) {
   462 			audio->CloseAudio(audio);
   545 			audio->CloseAudio(audio);
   463 			audio->opened = 0;
   546 			audio->opened = 0;
   464 		}
   547 		}
   465 
   548 #endif
   466 		/* Free the driver data */
   549 		/* Free the driver data */
   467 		audio->free(audio);
   550 		audio->free(audio);
   468 		current_audio = NULL;
   551 		current_audio = NULL;
   469 	}
   552 	}
   470 }
   553 }