src/audio/baudio/SDL_beaudio.cc
author Ryan C. Gordon <icculus@icculus.org>
Thu, 31 Aug 2006 22:22:34 +0000
changeset 1998 cbac0f77a799
parent 1997 46319c67b3d7
child 2001 02108bfd6550
permissions -rw-r--r--
Fixed silly logic thing.
     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         media_raw_audio_format format;
   159         SDL_AudioFormat test_format = SDL_FirstAudioFormat(spec->format);
   160 
   161         /* Parse the audio format and fill the Be raw audio format */
   162         memset(&format, '\0', sizeof (media_raw_audio_format));
   163         format.byte_order = B_MEDIA_LITTLE_ENDIAN;
   164         format.frame_rate = (float) spec->freq;
   165         format.channel_count = spec->channels;  /* !!! FIXME: support > 2? */
   166         while (test_format) {
   167             spec->format = test_format;
   168             switch (test_format) {
   169                 case AUDIO_S8:
   170                     format.format = media_raw_audio_format::B_AUDIO_CHAR;
   171                     break;
   172 
   173                 case AUDIO_U8:
   174                     format.format = media_raw_audio_format::B_AUDIO_UCHAR;
   175                     break;
   176 
   177                 case AUDIO_S16LSB:
   178                     format.format = media_raw_audio_format::B_AUDIO_SHORT;
   179                     break;
   180 
   181                 case AUDIO_S16MSB:
   182                     format.format = media_raw_audio_format::B_AUDIO_SHORT;
   183                     format.byte_order = B_MEDIA_BIG_ENDIAN;
   184                     break;
   185 
   186                 case AUDIO_S32LSB:
   187                     format.format = media_raw_audio_format::B_AUDIO_INT;
   188                     break;
   189 
   190                 case AUDIO_S32MSB:
   191                     format.format = media_raw_audio_format::B_AUDIO_INT;
   192                     format.byte_order = B_MEDIA_BIG_ENDIAN;
   193                     break;
   194 
   195                 case AUDIO_F32LSB:
   196                     format.format = media_raw_audio_format::B_AUDIO_FLOAT;
   197                     break;
   198 
   199                 case AUDIO_F32MSB:
   200                     format.format = media_raw_audio_format::B_AUDIO_FLOAT;
   201                     format.byte_order = B_MEDIA_BIG_ENDIAN;
   202                     break;
   203 
   204                 default:
   205                     test_format = SDL_NextAudioFormat();
   206                     break;
   207             }
   208         }
   209 
   210         format.buffer_size = spec->samples;
   211 
   212         if (!test_format) { /* shouldn't happen, but just in case... */
   213             SDL_SetError("Unsupported audio format");
   214             return (-1);
   215         }
   216 
   217         /* Initialize the Be Application, if it's not already started */
   218         if (SDL_InitBeApp() < 0) {
   219             return (-1);
   220         }
   221 
   222         /* Calculate the final parameters for this audio specification */
   223         SDL_CalculateAudioSpec(spec);
   224 
   225         /* Subscribe to the audio stream (creates a new thread) */
   226         {
   227             sigset_t omask;
   228             SDL_MaskSignals(&omask);
   229             audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
   230                                          NULL, _this);
   231             SDL_UnmaskSignals(&omask);
   232         }
   233         if (audio_obj->Start() == B_NO_ERROR) {
   234             audio_obj->SetHasData(true);
   235         } else {
   236             SDL_SetError("Unable to start Be audio");
   237             return (-1);
   238         }
   239 
   240         /* We're running! */
   241         return (1);
   242     }
   243 
   244 };                              /* Extern C */
   245 
   246 /* vi: set ts=4 sw=4 expandtab: */