src/audio/baudio/SDL_beaudio.cc
author Sam Lantinga <slouken@lokigames.com>
Fri, 20 Jul 2001 20:07:53 +0000
changeset 114 dabc453ce7f7
parent 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Now returns an error if unable to open audio on BeOS
Fixed bugs in fullscreen/windowed mode changes, removed duplicated code.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Allow access to the audio stream on BeOS */
slouken@0
    29
slouken@0
    30
#include <stdlib.h>
slouken@0
    31
#include <stdio.h>
slouken@0
    32
#include <string.h>
slouken@0
    33
#include <SoundPlayer.h>
slouken@0
    34
slouken@0
    35
#include "SDL_BeApp.h"
slouken@0
    36
slouken@0
    37
extern "C" {
slouken@0
    38
slouken@0
    39
#include "SDL_audio.h"
slouken@0
    40
#include "SDL_audio_c.h"
slouken@0
    41
#include "SDL_sysaudio.h"
slouken@0
    42
#include "SDL_systhread_c.h"
slouken@0
    43
#include "SDL_beaudio.h"
slouken@0
    44
slouken@0
    45
slouken@0
    46
/* Audio driver functions */
slouken@0
    47
static int BE_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    48
static void BE_WaitAudio(_THIS);
slouken@0
    49
static void BE_PlayAudio(_THIS);
slouken@0
    50
static Uint8 *BE_GetAudioBuf(_THIS);
slouken@0
    51
static void BE_CloseAudio(_THIS);
slouken@0
    52
slouken@0
    53
/* Audio driver bootstrap functions */
slouken@0
    54
slouken@0
    55
static int Audio_Available(void)
slouken@0
    56
{
slouken@0
    57
	return(1);
slouken@0
    58
}
slouken@0
    59
slouken@0
    60
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    61
{
slouken@0
    62
	free(device->hidden);
slouken@0
    63
	free(device);
slouken@0
    64
}
slouken@0
    65
slouken@0
    66
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    67
{
slouken@0
    68
	SDL_AudioDevice *device;
slouken@0
    69
slouken@0
    70
	/* Initialize all variables that we clean on shutdown */
slouken@0
    71
	device = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
    72
	if ( device ) {
slouken@0
    73
		memset(device, 0, (sizeof *device));
slouken@0
    74
		device->hidden = (struct SDL_PrivateAudioData *)
slouken@0
    75
				malloc((sizeof *device->hidden));
slouken@0
    76
	}
slouken@0
    77
	if ( (device == NULL) || (device->hidden == NULL) ) {
slouken@0
    78
		SDL_OutOfMemory();
slouken@0
    79
		if ( device ) {
slouken@0
    80
			free(device);
slouken@0
    81
		}
slouken@0
    82
		return(0);
slouken@0
    83
	}
slouken@0
    84
	memset(device->hidden, 0, (sizeof *device->hidden));
slouken@0
    85
slouken@0
    86
	/* Set the function pointers */
slouken@0
    87
	device->OpenAudio = BE_OpenAudio;
slouken@0
    88
	device->WaitAudio = BE_WaitAudio;
slouken@0
    89
	device->PlayAudio = BE_PlayAudio;
slouken@0
    90
	device->GetAudioBuf = BE_GetAudioBuf;
slouken@0
    91
	device->CloseAudio = BE_CloseAudio;
slouken@0
    92
slouken@0
    93
	device->free = Audio_DeleteDevice;
slouken@0
    94
slouken@0
    95
	return device;
slouken@0
    96
}
slouken@0
    97
slouken@0
    98
AudioBootStrap BAUDIO_bootstrap = {
slouken@0
    99
	"baudio", "BeOS BSoundPlayer",
slouken@0
   100
	Audio_Available, Audio_CreateDevice
slouken@0
   101
};
slouken@0
   102
slouken@0
   103
/* The BeOS callback for handling the audio buffer */
slouken@0
   104
static void FillSound(void *device, void *stream, size_t len, 
slouken@0
   105
					const media_raw_audio_format &format)
slouken@0
   106
{
slouken@0
   107
	SDL_AudioDevice *audio = (SDL_AudioDevice *)device;
slouken@0
   108
slouken@0
   109
	/* Silence the buffer, since it's ours */
slouken@0
   110
	memset(stream, audio->spec.silence, len);
slouken@0
   111
slouken@0
   112
	/* Only do soemthing if audio is enabled */
slouken@0
   113
	if ( ! audio->enabled )
slouken@0
   114
		return;
slouken@0
   115
slouken@0
   116
	if ( ! audio->paused ) {
slouken@0
   117
		if ( audio->convert.needed ) {
slouken@0
   118
			SDL_mutexP(audio->mixer_lock);
slouken@0
   119
			(*audio->spec.callback)(audio->spec.userdata,
slouken@0
   120
				(Uint8 *)audio->convert.buf,audio->convert.len);
slouken@0
   121
			SDL_mutexV(audio->mixer_lock);
slouken@0
   122
			SDL_ConvertAudio(&audio->convert);
slouken@0
   123
			memcpy(stream,audio->convert.buf,audio->convert.len_cvt);
slouken@0
   124
		} else {
slouken@0
   125
			SDL_mutexP(audio->mixer_lock);
slouken@0
   126
			(*audio->spec.callback)(audio->spec.userdata,
slouken@0
   127
						(Uint8 *)stream, len);
slouken@0
   128
			SDL_mutexV(audio->mixer_lock);
slouken@0
   129
		}
slouken@0
   130
	}
slouken@0
   131
	return;
slouken@0
   132
}
slouken@0
   133
slouken@0
   134
/* Dummy functions -- we don't use thread-based audio */
slouken@0
   135
void BE_WaitAudio(_THIS)
slouken@0
   136
{
slouken@0
   137
	return;
slouken@0
   138
}
slouken@0
   139
void BE_PlayAudio(_THIS)
slouken@0
   140
{
slouken@0
   141
	return;
slouken@0
   142
}
slouken@0
   143
Uint8 *BE_GetAudioBuf(_THIS)
slouken@0
   144
{
slouken@0
   145
	return(NULL);
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
void BE_CloseAudio(_THIS)
slouken@0
   149
{
slouken@0
   150
	if ( audio_obj ) {
slouken@0
   151
		audio_obj->Stop();
slouken@0
   152
		delete audio_obj;
slouken@0
   153
		audio_obj = NULL;
slouken@0
   154
	}
slouken@0
   155
slouken@0
   156
	/* Quit the Be Application, if there's nothing left to do */
slouken@0
   157
	SDL_QuitBeApp();
slouken@0
   158
}
slouken@0
   159
slouken@0
   160
int BE_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   161
{
slouken@0
   162
	media_raw_audio_format format;
slouken@0
   163
slouken@0
   164
	/* Initialize the Be Application, if it's not already started */
slouken@0
   165
	if ( SDL_InitBeApp() < 0 ) {
slouken@0
   166
		return(-1);
slouken@0
   167
	}
slouken@0
   168
slouken@0
   169
	/* Parse the audio format and fill the Be raw audio format */
slouken@0
   170
	format.frame_rate = (float)spec->freq;
slouken@0
   171
	format.channel_count = spec->channels;
slouken@0
   172
	switch (spec->format&~0x1000) {
slouken@0
   173
		case AUDIO_S8:
slouken@0
   174
			/* Signed 8-bit audio unsupported, convert to U8 */
slouken@0
   175
			spec->format = AUDIO_U8;
slouken@0
   176
		case AUDIO_U8:
slouken@0
   177
			format.format = media_raw_audio_format::B_AUDIO_UCHAR;
slouken@0
   178
			format.byte_order = 0;
slouken@0
   179
			break;
slouken@0
   180
		case AUDIO_U16:
slouken@0
   181
			/* Unsigned 16-bit audio unsupported, convert to S16 */
slouken@0
   182
			spec->format ^= 0x8000;
slouken@0
   183
		case AUDIO_S16:
slouken@0
   184
			format.format = media_raw_audio_format::B_AUDIO_SHORT;
slouken@0
   185
			if ( spec->format & 0x1000 ) {
slouken@0
   186
				format.byte_order = 1; /* Big endian */
slouken@0
   187
			} else {
slouken@0
   188
				format.byte_order = 2; /* Little endian */
slouken@0
   189
			}
slouken@0
   190
			break;
slouken@0
   191
	}
slouken@0
   192
	format.buffer_size = spec->samples;
slouken@0
   193
	
slouken@0
   194
	/* Calculate the final parameters for this audio specification */
slouken@0
   195
	SDL_CalculateAudioSpec(spec);
slouken@0
   196
slouken@0
   197
	/* Subscribe to the audio stream (creates a new thread) */
slouken@0
   198
	{ sigset_t omask;
slouken@0
   199
		SDL_MaskSignals(&omask);
slouken@0
   200
		audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
slouken@0
   201
		                                                 NULL, _this);
slouken@0
   202
		SDL_UnmaskSignals(&omask);
slouken@0
   203
	}
slouken@114
   204
	if ( audio_obj->Start() == B_NO_ERROR ) {
slouken@114
   205
		audio_obj->SetHasData(true);
slouken@114
   206
	} else {
slouken@114
   207
		SDL_SetError("Unable to start Be audio");
slouken@114
   208
		return(-1);
slouken@114
   209
	}
slouken@0
   210
slouken@0
   211
	/* We're running! */
slouken@0
   212
	return(1);
slouken@0
   213
}
slouken@0
   214
slouken@0
   215
};	/* Extern C */