src/audio/baudio/SDL_beaudio.cc
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:16:08 -0500
branchSDL-1.2
changeset 6137 4720145f848b
parent 4159 a1b03ba2fcd0
child 6855 dbaee3350e32
permissions -rw-r--r--
Happy New Year!
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 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
{
icculus@3851
   155
    int valid_datatype = 0;
icculus@3851
   156
    media_raw_audio_format format;
icculus@3852
   157
    Uint16 test_format = SDL_FirstAudioFormat(spec->format);
slouken@0
   158
icculus@3851
   159
    /* Parse the audio format and fill the Be raw audio format */
icculus@3851
   160
    memset(&format, '\0', sizeof (media_raw_audio_format));
icculus@3851
   161
    format.byte_order = B_MEDIA_LITTLE_ENDIAN;
icculus@3851
   162
    format.frame_rate = (float) spec->freq;
icculus@3851
   163
    format.channel_count = spec->channels;  /* !!! FIXME: support > 2? */
icculus@3851
   164
    while ((!valid_datatype) && (test_format)) {
icculus@3851
   165
        valid_datatype = 1;
icculus@3851
   166
        spec->format = test_format;
icculus@3851
   167
        switch (test_format) {
icculus@3851
   168
            case AUDIO_S8:
icculus@3851
   169
                format.format = media_raw_audio_format::B_AUDIO_CHAR;
icculus@3851
   170
                break;
slouken@0
   171
icculus@3851
   172
            case AUDIO_U8:
icculus@3851
   173
                format.format = media_raw_audio_format::B_AUDIO_UCHAR;
icculus@3851
   174
                break;
icculus@3851
   175
icculus@3851
   176
            case AUDIO_S16LSB:
icculus@3851
   177
                format.format = media_raw_audio_format::B_AUDIO_SHORT;
icculus@3851
   178
                break;
icculus@3851
   179
icculus@3851
   180
            case AUDIO_S16MSB:
icculus@3851
   181
                format.format = media_raw_audio_format::B_AUDIO_SHORT;
icculus@3851
   182
                format.byte_order = B_MEDIA_BIG_ENDIAN;
icculus@3851
   183
                break;
icculus@3851
   184
icculus@3851
   185
            default:
icculus@3851
   186
                valid_datatype = 0;
icculus@3851
   187
                test_format = SDL_NextAudioFormat();
icculus@3851
   188
                break;
icculus@3851
   189
        }
icculus@3851
   190
    }
icculus@3851
   191
icculus@3851
   192
    if (!valid_datatype) { /* shouldn't happen, but just in case... */
icculus@3851
   193
        SDL_SetError("Unsupported audio format");
icculus@3851
   194
        return (-1);
icculus@3851
   195
    }
icculus@3851
   196
icculus@3851
   197
    /* Initialize the Be Application, if it's not already started */
icculus@3851
   198
    if (SDL_InitBeApp() < 0) {
icculus@3851
   199
        return (-1);
icculus@3851
   200
    }
icculus@3851
   201
icculus@3851
   202
    format.buffer_size = spec->samples;
icculus@3851
   203
slouken@0
   204
	/* Calculate the final parameters for this audio specification */
slouken@0
   205
	SDL_CalculateAudioSpec(spec);
slouken@0
   206
slouken@0
   207
	/* Subscribe to the audio stream (creates a new thread) */
slouken@0
   208
	{ sigset_t omask;
slouken@0
   209
		SDL_MaskSignals(&omask);
slouken@0
   210
		audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
slouken@0
   211
		                                                 NULL, _this);
slouken@0
   212
		SDL_UnmaskSignals(&omask);
slouken@0
   213
	}
slouken@114
   214
	if ( audio_obj->Start() == B_NO_ERROR ) {
slouken@114
   215
		audio_obj->SetHasData(true);
slouken@114
   216
	} else {
slouken@114
   217
		SDL_SetError("Unable to start Be audio");
slouken@114
   218
		return(-1);
slouken@114
   219
	}
slouken@0
   220
slouken@0
   221
	/* We're running! */
slouken@0
   222
	return(1);
slouken@0
   223
}
slouken@0
   224
slouken@0
   225
};	/* Extern C */