2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Allow access to the audio stream on BeOS */
26 #include <SoundPlayer.h>
28 #include "../../main/beos/SDL_BeApp.h"
32 #include "SDL_audio.h"
33 #include "../SDL_audio_c.h"
34 #include "../SDL_sysaudio.h"
35 #include "../../thread/beos/SDL_systhread_c.h"
36 #include "SDL_beaudio.h"
39 /* Audio driver functions */
40 static int BE_OpenAudio(_THIS, SDL_AudioSpec *spec);
41 static void BE_WaitAudio(_THIS);
42 static void BE_PlayAudio(_THIS);
43 static Uint8 *BE_GetAudioBuf(_THIS);
44 static void BE_CloseAudio(_THIS);
46 /* Audio driver bootstrap functions */
48 static int Audio_Available(void)
53 static void Audio_DeleteDevice(SDL_AudioDevice *device)
55 SDL_free(device->hidden);
59 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
61 SDL_AudioDevice *device;
63 /* Initialize all variables that we clean on shutdown */
64 device = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
66 SDL_memset(device, 0, (sizeof *device));
67 device->hidden = (struct SDL_PrivateAudioData *)
68 SDL_malloc((sizeof *device->hidden));
70 if ( (device == NULL) || (device->hidden == NULL) ) {
77 SDL_memset(device->hidden, 0, (sizeof *device->hidden));
79 /* Set the function pointers */
80 device->OpenAudio = BE_OpenAudio;
81 device->WaitAudio = BE_WaitAudio;
82 device->PlayAudio = BE_PlayAudio;
83 device->GetAudioBuf = BE_GetAudioBuf;
84 device->CloseAudio = BE_CloseAudio;
86 device->free = Audio_DeleteDevice;
91 AudioBootStrap BAUDIO_bootstrap = {
92 "baudio", "BeOS BSoundPlayer",
93 Audio_Available, Audio_CreateDevice
96 /* The BeOS callback for handling the audio buffer */
97 static void FillSound(void *device, void *stream, size_t len,
98 const media_raw_audio_format &format)
100 SDL_AudioDevice *audio = (SDL_AudioDevice *)device;
102 /* Silence the buffer, since it's ours */
103 SDL_memset(stream, audio->spec.silence, len);
105 /* Only do soemthing if audio is enabled */
106 if ( ! audio->enabled )
109 if ( ! audio->paused ) {
110 if ( audio->convert.needed ) {
111 SDL_mutexP(audio->mixer_lock);
112 (*audio->spec.callback)(audio->spec.userdata,
113 (Uint8 *)audio->convert.buf,audio->convert.len);
114 SDL_mutexV(audio->mixer_lock);
115 SDL_ConvertAudio(&audio->convert);
116 SDL_memcpy(stream,audio->convert.buf,audio->convert.len_cvt);
118 SDL_mutexP(audio->mixer_lock);
119 (*audio->spec.callback)(audio->spec.userdata,
120 (Uint8 *)stream, len);
121 SDL_mutexV(audio->mixer_lock);
127 /* Dummy functions -- we don't use thread-based audio */
128 void BE_WaitAudio(_THIS)
132 void BE_PlayAudio(_THIS)
136 Uint8 *BE_GetAudioBuf(_THIS)
141 void BE_CloseAudio(_THIS)
149 /* Quit the Be Application, if there's nothing left to do */
153 int BE_OpenAudio(_THIS, SDL_AudioSpec *spec)
155 int valid_datatype = 0;
156 media_raw_audio_format format;
157 Uint16 test_format = SDL_FirstAudioFormat(spec->format);
159 /* Parse the audio format and fill the Be raw audio format */
160 memset(&format, '\0', sizeof (media_raw_audio_format));
161 format.byte_order = B_MEDIA_LITTLE_ENDIAN;
162 format.frame_rate = (float) spec->freq;
163 format.channel_count = spec->channels; /* !!! FIXME: support > 2? */
164 while ((!valid_datatype) && (test_format)) {
166 spec->format = test_format;
167 switch (test_format) {
169 format.format = media_raw_audio_format::B_AUDIO_CHAR;
173 format.format = media_raw_audio_format::B_AUDIO_UCHAR;
177 format.format = media_raw_audio_format::B_AUDIO_SHORT;
181 format.format = media_raw_audio_format::B_AUDIO_SHORT;
182 format.byte_order = B_MEDIA_BIG_ENDIAN;
187 test_format = SDL_NextAudioFormat();
192 if (!valid_datatype) { /* shouldn't happen, but just in case... */
193 SDL_SetError("Unsupported audio format");
197 /* Initialize the Be Application, if it's not already started */
198 if (SDL_InitBeApp() < 0) {
202 format.buffer_size = spec->samples;
204 /* Calculate the final parameters for this audio specification */
205 SDL_CalculateAudioSpec(spec);
207 /* Subscribe to the audio stream (creates a new thread) */
209 SDL_MaskSignals(&omask);
210 audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
212 SDL_UnmaskSignals(&omask);
214 if ( audio_obj->Start() == B_NO_ERROR ) {
215 audio_obj->SetHasData(true);
217 SDL_SetError("Unable to start Be audio");