src/audio/baudio/SDL_beaudio.cc
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:47:46 +0000
changeset 1403 376665398b25
parent 1367 e440d5c488c1
child 1662 782fd950bd46
child 1895 c121d94672cb
child 3851 405a192b68e7
permissions -rw-r--r--
Catch the C++ and Objective C sources too...
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1403
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Allow access to the audio stream on BeOS */
slouken@0
    25
slouken@0
    26
#include <SoundPlayer.h>
slouken@0
    27
slouken@1367
    28
#include "../../main/beos/SDL_BeApp.h"
slouken@0
    29
slouken@0
    30
extern "C" {
slouken@0
    31
slouken@0
    32
#include "SDL_audio.h"
slouken@1361
    33
#include "../SDL_audio_c.h"
slouken@1361
    34
#include "../SDL_sysaudio.h"
slouken@1367
    35
#include "../../thread/beos/SDL_systhread_c.h"
slouken@0
    36
#include "SDL_beaudio.h"
slouken@0
    37
slouken@0
    38
slouken@0
    39
/* Audio driver functions */
slouken@0
    40
static int BE_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    41
static void BE_WaitAudio(_THIS);
slouken@0
    42
static void BE_PlayAudio(_THIS);
slouken@0
    43
static Uint8 *BE_GetAudioBuf(_THIS);
slouken@0
    44
static void BE_CloseAudio(_THIS);
slouken@0
    45
slouken@0
    46
/* Audio driver bootstrap functions */
slouken@0
    47
slouken@0
    48
static int Audio_Available(void)
slouken@0
    49
{
slouken@0
    50
	return(1);
slouken@0
    51
}
slouken@0
    52
slouken@0
    53
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    54
{
slouken@1336
    55
	SDL_free(device->hidden);
slouken@1336
    56
	SDL_free(device);
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    60
{
slouken@0
    61
	SDL_AudioDevice *device;
slouken@0
    62
slouken@0
    63
	/* Initialize all variables that we clean on shutdown */
slouken@1336
    64
	device = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
slouken@0
    65
	if ( device ) {
slouken@1336
    66
		SDL_memset(device, 0, (sizeof *device));
slouken@0
    67
		device->hidden = (struct SDL_PrivateAudioData *)
slouken@1336
    68
				SDL_malloc((sizeof *device->hidden));
slouken@0
    69
	}
slouken@0
    70
	if ( (device == NULL) || (device->hidden == NULL) ) {
slouken@0
    71
		SDL_OutOfMemory();
slouken@0
    72
		if ( device ) {
slouken@1336
    73
			SDL_free(device);
slouken@0
    74
		}
slouken@0
    75
		return(0);
slouken@0
    76
	}
slouken@1336
    77
	SDL_memset(device->hidden, 0, (sizeof *device->hidden));
slouken@0
    78
slouken@0
    79
	/* Set the function pointers */
slouken@0
    80
	device->OpenAudio = BE_OpenAudio;
slouken@0
    81
	device->WaitAudio = BE_WaitAudio;
slouken@0
    82
	device->PlayAudio = BE_PlayAudio;
slouken@0
    83
	device->GetAudioBuf = BE_GetAudioBuf;
slouken@0
    84
	device->CloseAudio = BE_CloseAudio;
slouken@0
    85
slouken@0
    86
	device->free = Audio_DeleteDevice;
slouken@0
    87
slouken@0
    88
	return device;
slouken@0
    89
}
slouken@0
    90
slouken@0
    91
AudioBootStrap BAUDIO_bootstrap = {
slouken@0
    92
	"baudio", "BeOS BSoundPlayer",
slouken@0
    93
	Audio_Available, Audio_CreateDevice
slouken@0
    94
};
slouken@0
    95
slouken@0
    96
/* The BeOS callback for handling the audio buffer */
slouken@0
    97
static void FillSound(void *device, void *stream, size_t len, 
slouken@0
    98
					const media_raw_audio_format &format)
slouken@0
    99
{
slouken@0
   100
	SDL_AudioDevice *audio = (SDL_AudioDevice *)device;
slouken@0
   101
slouken@0
   102
	/* Silence the buffer, since it's ours */
slouken@1336
   103
	SDL_memset(stream, audio->spec.silence, len);
slouken@0
   104
slouken@0
   105
	/* Only do soemthing if audio is enabled */
slouken@0
   106
	if ( ! audio->enabled )
slouken@0
   107
		return;
slouken@0
   108
slouken@0
   109
	if ( ! audio->paused ) {
slouken@0
   110
		if ( audio->convert.needed ) {
slouken@0
   111
			SDL_mutexP(audio->mixer_lock);
slouken@0
   112
			(*audio->spec.callback)(audio->spec.userdata,
slouken@0
   113
				(Uint8 *)audio->convert.buf,audio->convert.len);
slouken@0
   114
			SDL_mutexV(audio->mixer_lock);
slouken@0
   115
			SDL_ConvertAudio(&audio->convert);
slouken@1336
   116
			SDL_memcpy(stream,audio->convert.buf,audio->convert.len_cvt);
slouken@0
   117
		} else {
slouken@0
   118
			SDL_mutexP(audio->mixer_lock);
slouken@0
   119
			(*audio->spec.callback)(audio->spec.userdata,
slouken@0
   120
						(Uint8 *)stream, len);
slouken@0
   121
			SDL_mutexV(audio->mixer_lock);
slouken@0
   122
		}
slouken@0
   123
	}
slouken@0
   124
	return;
slouken@0
   125
}
slouken@0
   126
slouken@0
   127
/* Dummy functions -- we don't use thread-based audio */
slouken@0
   128
void BE_WaitAudio(_THIS)
slouken@0
   129
{
slouken@0
   130
	return;
slouken@0
   131
}
slouken@0
   132
void BE_PlayAudio(_THIS)
slouken@0
   133
{
slouken@0
   134
	return;
slouken@0
   135
}
slouken@0
   136
Uint8 *BE_GetAudioBuf(_THIS)
slouken@0
   137
{
slouken@0
   138
	return(NULL);
slouken@0
   139
}
slouken@0
   140
slouken@0
   141
void BE_CloseAudio(_THIS)
slouken@0
   142
{
slouken@0
   143
	if ( audio_obj ) {
slouken@0
   144
		audio_obj->Stop();
slouken@0
   145
		delete audio_obj;
slouken@0
   146
		audio_obj = NULL;
slouken@0
   147
	}
slouken@0
   148
slouken@0
   149
	/* Quit the Be Application, if there's nothing left to do */
slouken@0
   150
	SDL_QuitBeApp();
slouken@0
   151
}
slouken@0
   152
slouken@0
   153
int BE_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   154
{
slouken@0
   155
	media_raw_audio_format format;
slouken@0
   156
slouken@0
   157
	/* Initialize the Be Application, if it's not already started */
slouken@0
   158
	if ( SDL_InitBeApp() < 0 ) {
slouken@0
   159
		return(-1);
slouken@0
   160
	}
slouken@0
   161
slouken@0
   162
	/* Parse the audio format and fill the Be raw audio format */
slouken@0
   163
	format.frame_rate = (float)spec->freq;
slouken@0
   164
	format.channel_count = spec->channels;
slouken@0
   165
	switch (spec->format&~0x1000) {
slouken@0
   166
		case AUDIO_S8:
slouken@0
   167
			/* Signed 8-bit audio unsupported, convert to U8 */
slouken@0
   168
			spec->format = AUDIO_U8;
slouken@0
   169
		case AUDIO_U8:
slouken@0
   170
			format.format = media_raw_audio_format::B_AUDIO_UCHAR;
slouken@0
   171
			format.byte_order = 0;
slouken@0
   172
			break;
slouken@0
   173
		case AUDIO_U16:
slouken@0
   174
			/* Unsigned 16-bit audio unsupported, convert to S16 */
slouken@0
   175
			spec->format ^= 0x8000;
slouken@0
   176
		case AUDIO_S16:
slouken@0
   177
			format.format = media_raw_audio_format::B_AUDIO_SHORT;
slouken@0
   178
			if ( spec->format & 0x1000 ) {
slouken@0
   179
				format.byte_order = 1; /* Big endian */
slouken@0
   180
			} else {
slouken@0
   181
				format.byte_order = 2; /* Little endian */
slouken@0
   182
			}
slouken@0
   183
			break;
slouken@0
   184
	}
slouken@0
   185
	format.buffer_size = spec->samples;
slouken@0
   186
	
slouken@0
   187
	/* Calculate the final parameters for this audio specification */
slouken@0
   188
	SDL_CalculateAudioSpec(spec);
slouken@0
   189
slouken@0
   190
	/* Subscribe to the audio stream (creates a new thread) */
slouken@0
   191
	{ sigset_t omask;
slouken@0
   192
		SDL_MaskSignals(&omask);
slouken@0
   193
		audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
slouken@0
   194
		                                                 NULL, _this);
slouken@0
   195
		SDL_UnmaskSignals(&omask);
slouken@0
   196
	}
slouken@114
   197
	if ( audio_obj->Start() == B_NO_ERROR ) {
slouken@114
   198
		audio_obj->SetHasData(true);
slouken@114
   199
	} else {
slouken@114
   200
		SDL_SetError("Unable to start Be audio");
slouken@114
   201
		return(-1);
slouken@114
   202
	}
slouken@0
   203
slouken@0
   204
	/* We're running! */
slouken@0
   205
	return(1);
slouken@0
   206
}
slouken@0
   207
slouken@0
   208
};	/* Extern C */