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