src/audio/baudio/SDL_beaudio.cc
changeset 0 74212992fb08
child 114 dabc453ce7f7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/baudio/SDL_beaudio.cc	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,211 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* Allow access to the audio stream on BeOS */
    1.32 +
    1.33 +#include <stdlib.h>
    1.34 +#include <stdio.h>
    1.35 +#include <string.h>
    1.36 +#include <SoundPlayer.h>
    1.37 +
    1.38 +#include "SDL_BeApp.h"
    1.39 +
    1.40 +extern "C" {
    1.41 +
    1.42 +#include "SDL_audio.h"
    1.43 +#include "SDL_audio_c.h"
    1.44 +#include "SDL_sysaudio.h"
    1.45 +#include "SDL_systhread_c.h"
    1.46 +#include "SDL_beaudio.h"
    1.47 +
    1.48 +
    1.49 +/* Audio driver functions */
    1.50 +static int BE_OpenAudio(_THIS, SDL_AudioSpec *spec);
    1.51 +static void BE_WaitAudio(_THIS);
    1.52 +static void BE_PlayAudio(_THIS);
    1.53 +static Uint8 *BE_GetAudioBuf(_THIS);
    1.54 +static void BE_CloseAudio(_THIS);
    1.55 +
    1.56 +/* Audio driver bootstrap functions */
    1.57 +
    1.58 +static int Audio_Available(void)
    1.59 +{
    1.60 +	return(1);
    1.61 +}
    1.62 +
    1.63 +static void Audio_DeleteDevice(SDL_AudioDevice *device)
    1.64 +{
    1.65 +	free(device->hidden);
    1.66 +	free(device);
    1.67 +}
    1.68 +
    1.69 +static SDL_AudioDevice *Audio_CreateDevice(int devindex)
    1.70 +{
    1.71 +	SDL_AudioDevice *device;
    1.72 +
    1.73 +	/* Initialize all variables that we clean on shutdown */
    1.74 +	device = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
    1.75 +	if ( device ) {
    1.76 +		memset(device, 0, (sizeof *device));
    1.77 +		device->hidden = (struct SDL_PrivateAudioData *)
    1.78 +				malloc((sizeof *device->hidden));
    1.79 +	}
    1.80 +	if ( (device == NULL) || (device->hidden == NULL) ) {
    1.81 +		SDL_OutOfMemory();
    1.82 +		if ( device ) {
    1.83 +			free(device);
    1.84 +		}
    1.85 +		return(0);
    1.86 +	}
    1.87 +	memset(device->hidden, 0, (sizeof *device->hidden));
    1.88 +
    1.89 +	/* Set the function pointers */
    1.90 +	device->OpenAudio = BE_OpenAudio;
    1.91 +	device->WaitAudio = BE_WaitAudio;
    1.92 +	device->PlayAudio = BE_PlayAudio;
    1.93 +	device->GetAudioBuf = BE_GetAudioBuf;
    1.94 +	device->CloseAudio = BE_CloseAudio;
    1.95 +
    1.96 +	device->free = Audio_DeleteDevice;
    1.97 +
    1.98 +	return device;
    1.99 +}
   1.100 +
   1.101 +AudioBootStrap BAUDIO_bootstrap = {
   1.102 +	"baudio", "BeOS BSoundPlayer",
   1.103 +	Audio_Available, Audio_CreateDevice
   1.104 +};
   1.105 +
   1.106 +/* The BeOS callback for handling the audio buffer */
   1.107 +static void FillSound(void *device, void *stream, size_t len, 
   1.108 +					const media_raw_audio_format &format)
   1.109 +{
   1.110 +	SDL_AudioDevice *audio = (SDL_AudioDevice *)device;
   1.111 +
   1.112 +	/* Silence the buffer, since it's ours */
   1.113 +	memset(stream, audio->spec.silence, len);
   1.114 +
   1.115 +	/* Only do soemthing if audio is enabled */
   1.116 +	if ( ! audio->enabled )
   1.117 +		return;
   1.118 +
   1.119 +	if ( ! audio->paused ) {
   1.120 +		if ( audio->convert.needed ) {
   1.121 +			SDL_mutexP(audio->mixer_lock);
   1.122 +			(*audio->spec.callback)(audio->spec.userdata,
   1.123 +				(Uint8 *)audio->convert.buf,audio->convert.len);
   1.124 +			SDL_mutexV(audio->mixer_lock);
   1.125 +			SDL_ConvertAudio(&audio->convert);
   1.126 +			memcpy(stream,audio->convert.buf,audio->convert.len_cvt);
   1.127 +		} else {
   1.128 +			SDL_mutexP(audio->mixer_lock);
   1.129 +			(*audio->spec.callback)(audio->spec.userdata,
   1.130 +						(Uint8 *)stream, len);
   1.131 +			SDL_mutexV(audio->mixer_lock);
   1.132 +		}
   1.133 +	}
   1.134 +	return;
   1.135 +}
   1.136 +
   1.137 +/* Dummy functions -- we don't use thread-based audio */
   1.138 +void BE_WaitAudio(_THIS)
   1.139 +{
   1.140 +	return;
   1.141 +}
   1.142 +void BE_PlayAudio(_THIS)
   1.143 +{
   1.144 +	return;
   1.145 +}
   1.146 +Uint8 *BE_GetAudioBuf(_THIS)
   1.147 +{
   1.148 +	return(NULL);
   1.149 +}
   1.150 +
   1.151 +void BE_CloseAudio(_THIS)
   1.152 +{
   1.153 +	if ( audio_obj ) {
   1.154 +		audio_obj->Stop();
   1.155 +		delete audio_obj;
   1.156 +		audio_obj = NULL;
   1.157 +	}
   1.158 +
   1.159 +	/* Quit the Be Application, if there's nothing left to do */
   1.160 +	SDL_QuitBeApp();
   1.161 +}
   1.162 +
   1.163 +int BE_OpenAudio(_THIS, SDL_AudioSpec *spec)
   1.164 +{
   1.165 +	media_raw_audio_format format;
   1.166 +
   1.167 +	/* Initialize the Be Application, if it's not already started */
   1.168 +	if ( SDL_InitBeApp() < 0 ) {
   1.169 +		return(-1);
   1.170 +	}
   1.171 +
   1.172 +	/* Parse the audio format and fill the Be raw audio format */
   1.173 +	format.frame_rate = (float)spec->freq;
   1.174 +	format.channel_count = spec->channels;
   1.175 +	switch (spec->format&~0x1000) {
   1.176 +		case AUDIO_S8:
   1.177 +			/* Signed 8-bit audio unsupported, convert to U8 */
   1.178 +			spec->format = AUDIO_U8;
   1.179 +		case AUDIO_U8:
   1.180 +			format.format = media_raw_audio_format::B_AUDIO_UCHAR;
   1.181 +			format.byte_order = 0;
   1.182 +			break;
   1.183 +		case AUDIO_U16:
   1.184 +			/* Unsigned 16-bit audio unsupported, convert to S16 */
   1.185 +			spec->format ^= 0x8000;
   1.186 +		case AUDIO_S16:
   1.187 +			format.format = media_raw_audio_format::B_AUDIO_SHORT;
   1.188 +			if ( spec->format & 0x1000 ) {
   1.189 +				format.byte_order = 1; /* Big endian */
   1.190 +			} else {
   1.191 +				format.byte_order = 2; /* Little endian */
   1.192 +			}
   1.193 +			break;
   1.194 +	}
   1.195 +	format.buffer_size = spec->samples;
   1.196 +	
   1.197 +	/* Calculate the final parameters for this audio specification */
   1.198 +	SDL_CalculateAudioSpec(spec);
   1.199 +
   1.200 +	/* Subscribe to the audio stream (creates a new thread) */
   1.201 +	{ sigset_t omask;
   1.202 +		SDL_MaskSignals(&omask);
   1.203 +		audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
   1.204 +		                                                 NULL, _this);
   1.205 +		SDL_UnmaskSignals(&omask);
   1.206 +	}
   1.207 +	audio_obj->Start();
   1.208 +	audio_obj->SetHasData(true);
   1.209 +
   1.210 +	/* We're running! */
   1.211 +	return(1);
   1.212 +}
   1.213 +
   1.214 +};	/* Extern C */