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