src/audio/baudio/SDL_beaudio.cc
author Ryan C. Gordon <icculus@icculus.org>
Thu, 31 Aug 2006 23:16:48 +0000
changeset 2001 02108bfd6550
parent 1998 cbac0f77a799
child 2043 adf732f1f016
permissions -rw-r--r--
Actually, my silly logic bug was actually correct. :/
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     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.
     9 
    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.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Allow access to the audio stream on BeOS */
    25 
    26 #include <SoundPlayer.h>
    27 
    28 #include "../../main/beos/SDL_BeApp.h"
    29 
    30 extern "C"
    31 {
    32 
    33 #include "SDL_audio.h"
    34 #include "../SDL_audio_c.h"
    35 #include "../SDL_sysaudio.h"
    36 #include "../../thread/beos/SDL_systhread_c.h"
    37 #include "SDL_beaudio.h"
    38 
    39 
    40 /* Audio driver functions */
    41     static int BE_OpenAudio(_THIS, SDL_AudioSpec * spec);
    42     static void BE_WaitAudio(_THIS);
    43     static void BE_PlayAudio(_THIS);
    44     static Uint8 *BE_GetAudioBuf(_THIS);
    45     static void BE_CloseAudio(_THIS);
    46 
    47 /* Audio driver bootstrap functions */
    48 
    49     static int Audio_Available(void)
    50     {
    51         return (1);
    52     }
    53 
    54     static void Audio_DeleteDevice(SDL_AudioDevice * device)
    55     {
    56         SDL_free(device->hidden);
    57         SDL_free(device);
    58     }
    59 
    60     static SDL_AudioDevice *Audio_CreateDevice(int devindex)
    61     {
    62         SDL_AudioDevice *device;
    63 
    64         /* Initialize all variables that we clean on shutdown */
    65         device = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
    66         if (device) {
    67             SDL_memset(device, 0, (sizeof *device));
    68             device->hidden = (struct SDL_PrivateAudioData *)
    69                 SDL_malloc((sizeof *device->hidden));
    70         }
    71         if ((device == NULL) || (device->hidden == NULL)) {
    72             SDL_OutOfMemory();
    73             if (device) {
    74                 SDL_free(device);
    75             }
    76             return (0);
    77         }
    78         SDL_memset(device->hidden, 0, (sizeof *device->hidden));
    79 
    80         /* Set the function pointers */
    81         device->OpenAudio = BE_OpenAudio;
    82         device->WaitAudio = BE_WaitAudio;
    83         device->PlayAudio = BE_PlayAudio;
    84         device->GetAudioBuf = BE_GetAudioBuf;
    85         device->CloseAudio = BE_CloseAudio;
    86 
    87         device->free = Audio_DeleteDevice;
    88 
    89         return device;
    90     }
    91 
    92     AudioBootStrap BAUDIO_bootstrap = {
    93         "baudio", "BeOS BSoundPlayer",
    94         Audio_Available, Audio_CreateDevice
    95     };
    96 
    97 /* The BeOS callback for handling the audio buffer */
    98     static void FillSound(void *device, void *stream, size_t len,
    99                           const media_raw_audio_format & format)
   100     {
   101         SDL_AudioDevice *audio = (SDL_AudioDevice *) device;
   102 
   103         /* Silence the buffer, since it's ours */
   104         SDL_memset(stream, audio->spec.silence, len);
   105 
   106         /* Only do soemthing if audio is enabled */
   107         if (!audio->enabled)
   108             return;
   109 
   110         if (!audio->paused) {
   111             if (audio->convert.needed) {
   112                 SDL_mutexP(audio->mixer_lock);
   113                 (*audio->spec.callback) (audio->spec.userdata,
   114                                          (Uint8 *) audio->convert.buf,
   115                                          audio->convert.len);
   116                 SDL_mutexV(audio->mixer_lock);
   117                 SDL_ConvertAudio(&audio->convert);
   118                 SDL_memcpy(stream, audio->convert.buf,
   119                            audio->convert.len_cvt);
   120             } else {
   121                 SDL_mutexP(audio->mixer_lock);
   122                 (*audio->spec.callback) (audio->spec.userdata,
   123                                          (Uint8 *) stream, len);
   124                 SDL_mutexV(audio->mixer_lock);
   125             }
   126         }
   127         return;
   128     }
   129 
   130 /* Dummy functions -- we don't use thread-based audio */
   131     void BE_WaitAudio(_THIS)
   132     {
   133         return;
   134     }
   135     void BE_PlayAudio(_THIS)
   136     {
   137         return;
   138     }
   139     Uint8 *BE_GetAudioBuf(_THIS)
   140     {
   141         return (NULL);
   142     }
   143 
   144     void BE_CloseAudio(_THIS)
   145     {
   146         if (audio_obj) {
   147             audio_obj->Stop();
   148             delete audio_obj;
   149             audio_obj = NULL;
   150         }
   151 
   152         /* Quit the Be Application, if there's nothing left to do */
   153         SDL_QuitBeApp();
   154     }
   155 
   156     int BE_OpenAudio(_THIS, SDL_AudioSpec * spec)
   157     {
   158         int valid_datatype = 0;
   159         media_raw_audio_format format;
   160         SDL_AudioFormat test_format = SDL_FirstAudioFormat(spec->format);
   161 
   162         /* Parse the audio format and fill the Be raw audio format */
   163         memset(&format, '\0', sizeof (media_raw_audio_format));
   164         format.byte_order = B_MEDIA_LITTLE_ENDIAN;
   165         format.frame_rate = (float) spec->freq;
   166         format.channel_count = spec->channels;  /* !!! FIXME: support > 2? */
   167         while ((!valid_datatype) && (test_format)) {
   168             valid_datatype = 1;
   169             spec->format = test_format;
   170             switch (test_format) {
   171                 case AUDIO_S8:
   172                     format.format = media_raw_audio_format::B_AUDIO_CHAR;
   173                     break;
   174 
   175                 case AUDIO_U8:
   176                     format.format = media_raw_audio_format::B_AUDIO_UCHAR;
   177                     break;
   178 
   179                 case AUDIO_S16LSB:
   180                     format.format = media_raw_audio_format::B_AUDIO_SHORT;
   181                     break;
   182 
   183                 case AUDIO_S16MSB:
   184                     format.format = media_raw_audio_format::B_AUDIO_SHORT;
   185                     format.byte_order = B_MEDIA_BIG_ENDIAN;
   186                     break;
   187 
   188                 case AUDIO_S32LSB:
   189                     format.format = media_raw_audio_format::B_AUDIO_INT;
   190                     break;
   191 
   192                 case AUDIO_S32MSB:
   193                     format.format = media_raw_audio_format::B_AUDIO_INT;
   194                     format.byte_order = B_MEDIA_BIG_ENDIAN;
   195                     break;
   196 
   197                 case AUDIO_F32LSB:
   198                     format.format = media_raw_audio_format::B_AUDIO_FLOAT;
   199                     break;
   200 
   201                 case AUDIO_F32MSB:
   202                     format.format = media_raw_audio_format::B_AUDIO_FLOAT;
   203                     format.byte_order = B_MEDIA_BIG_ENDIAN;
   204                     break;
   205 
   206                 default:
   207                     valid_datatype = 0;
   208                     test_format = SDL_NextAudioFormat();
   209                     break;
   210             }
   211         }
   212 
   213         format.buffer_size = spec->samples;
   214 
   215         if (!valid_datatype) { /* shouldn't happen, but just in case... */
   216             SDL_SetError("Unsupported audio format");
   217             return (-1);
   218         }
   219 
   220         /* Initialize the Be Application, if it's not already started */
   221         if (SDL_InitBeApp() < 0) {
   222             return (-1);
   223         }
   224 
   225         /* Calculate the final parameters for this audio specification */
   226         SDL_CalculateAudioSpec(spec);
   227 
   228         /* Subscribe to the audio stream (creates a new thread) */
   229         {
   230             sigset_t omask;
   231             SDL_MaskSignals(&omask);
   232             audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
   233                                          NULL, _this);
   234             SDL_UnmaskSignals(&omask);
   235         }
   236         if (audio_obj->Start() == B_NO_ERROR) {
   237             audio_obj->SetHasData(true);
   238         } else {
   239             SDL_SetError("Unable to start Be audio");
   240             return (-1);
   241         }
   242 
   243         /* We're running! */
   244         return (1);
   245     }
   246 
   247 };                              /* Extern C */
   248 
   249 /* vi: set ts=4 sw=4 expandtab: */