src/audio/baudio/SDL_beaudio.cc
branchSDL-ryan-multiple-audio-device
changeset 3819 b225d9820ee3
parent 2043 adf732f1f016
child 3830 29e83f221c62
     1.1 --- a/src/audio/baudio/SDL_beaudio.cc	Fri Oct 06 19:45:11 2006 +0000
     1.2 +++ b/src/audio/baudio/SDL_beaudio.cc	Fri Oct 06 20:36:23 2006 +0000
     1.3 @@ -36,138 +36,95 @@
     1.4  #include "../../thread/beos/SDL_systhread_c.h"
     1.5  #include "SDL_beaudio.h"
     1.6  
     1.7 +}
     1.8  
     1.9 -/* Audio driver functions */
    1.10 -    static int BE_OpenAudio(_THIS, SDL_AudioSpec * spec);
    1.11 -    static void BE_WaitAudio(_THIS);
    1.12 -    static void BE_PlayAudio(_THIS);
    1.13 -    static Uint8 *BE_GetAudioBuf(_THIS);
    1.14 -    static void BE_CloseAudio(_THIS);
    1.15  
    1.16 -/* Audio driver bootstrap functions */
    1.17 +static int BEAUDIO_Available(void)
    1.18 +{
    1.19 +    return 1;  /* Always available on BeOS. */
    1.20 +}
    1.21  
    1.22 -    static int Audio_Available(void)
    1.23 -    {
    1.24 -        return (1);
    1.25 -    }
    1.26  
    1.27 -    static void Audio_DeleteDevice(SDL_AudioDevice * device)
    1.28 -    {
    1.29 -        SDL_free(device->hidden);
    1.30 -        SDL_free(device);
    1.31 -    }
    1.32 +/* !!! FIXME: have the callback call the higher level to avoid code dupe. */
    1.33 +/* The BeOS callback for handling the audio buffer */
    1.34 +static void
    1.35 +FillSound(void *device, void *stream, size_t len,
    1.36 +          const media_raw_audio_format & format)
    1.37 +{
    1.38 +    SDL_AudioDevice *audio = (SDL_AudioDevice *) device;
    1.39  
    1.40 -    static SDL_AudioDevice *Audio_CreateDevice(int devindex)
    1.41 -    {
    1.42 -        SDL_AudioDevice *device;
    1.43 +    /* Silence the buffer, since it's ours */
    1.44 +    SDL_memset(stream, audio->spec.silence, len);
    1.45  
    1.46 -        /* Initialize all variables that we clean on shutdown */
    1.47 -        device = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
    1.48 -        if (device) {
    1.49 -            SDL_memset(device, 0, (sizeof *device));
    1.50 -            device->hidden = (struct SDL_PrivateAudioData *)
    1.51 -                SDL_malloc((sizeof *device->hidden));
    1.52 -        }
    1.53 -        if ((device == NULL) || (device->hidden == NULL)) {
    1.54 -            SDL_OutOfMemory();
    1.55 -            if (device) {
    1.56 -                SDL_free(device);
    1.57 -            }
    1.58 -            return (0);
    1.59 -        }
    1.60 -        SDL_memset(device->hidden, 0, (sizeof *device->hidden));
    1.61 +    /* Only do soemthing if audio is enabled */
    1.62 +    if (!audio->enabled)
    1.63 +        return;
    1.64  
    1.65 -        /* Set the function pointers */
    1.66 -        device->OpenAudio = BE_OpenAudio;
    1.67 -        device->WaitAudio = BE_WaitAudio;
    1.68 -        device->PlayAudio = BE_PlayAudio;
    1.69 -        device->GetAudioBuf = BE_GetAudioBuf;
    1.70 -        device->CloseAudio = BE_CloseAudio;
    1.71 -
    1.72 -        device->free = Audio_DeleteDevice;
    1.73 -
    1.74 -        return device;
    1.75 -    }
    1.76 -
    1.77 -    AudioBootStrap BAUDIO_bootstrap = {
    1.78 -        "baudio", "BeOS BSoundPlayer",
    1.79 -        Audio_Available, Audio_CreateDevice
    1.80 -    };
    1.81 -
    1.82 -/* The BeOS callback for handling the audio buffer */
    1.83 -    static void FillSound(void *device, void *stream, size_t len,
    1.84 -                          const media_raw_audio_format & format)
    1.85 -    {
    1.86 -        SDL_AudioDevice *audio = (SDL_AudioDevice *) device;
    1.87 -
    1.88 -        /* Silence the buffer, since it's ours */
    1.89 -        SDL_memset(stream, audio->spec.silence, len);
    1.90 -
    1.91 -        /* Only do soemthing if audio is enabled */
    1.92 -        if (!audio->enabled)
    1.93 -            return;
    1.94 -
    1.95 -        if (!audio->paused) {
    1.96 -            if (audio->convert.needed) {
    1.97 -                SDL_mutexP(audio->mixer_lock);
    1.98 -                (*audio->spec.callback) (audio->spec.userdata,
    1.99 +    if (!audio->paused) {
   1.100 +        if (audio->convert.needed) {
   1.101 +            SDL_mutexP(audio->mixer_lock);
   1.102 +            (*audio->spec.callback) (audio->spec.userdata,
   1.103                                           (Uint8 *) audio->convert.buf,
   1.104                                           audio->convert.len);
   1.105 -                SDL_mutexV(audio->mixer_lock);
   1.106 -                SDL_ConvertAudio(&audio->convert);
   1.107 -                SDL_memcpy(stream, audio->convert.buf,
   1.108 -                           audio->convert.len_cvt);
   1.109 -            } else {
   1.110 -                SDL_mutexP(audio->mixer_lock);
   1.111 -                (*audio->spec.callback) (audio->spec.userdata,
   1.112 -                                         (Uint8 *) stream, len);
   1.113 -                SDL_mutexV(audio->mixer_lock);
   1.114 -            }
   1.115 +            SDL_mutexV(audio->mixer_lock);
   1.116 +            SDL_ConvertAudio(&audio->convert);
   1.117 +            SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt);
   1.118 +        } else {
   1.119 +            SDL_mutexP(audio->mixer_lock);
   1.120 +            (*audio->spec.callback) (audio->spec.userdata,
   1.121 +                                        (Uint8 *) stream, len);
   1.122 +            SDL_mutexV(audio->mixer_lock);
   1.123          }
   1.124 -        return;
   1.125      }
   1.126 +}
   1.127  
   1.128 -/* Dummy functions -- we don't use thread-based audio */
   1.129 -    void BE_WaitAudio(_THIS)
   1.130 -    {
   1.131 -        return;
   1.132 -    }
   1.133 -    void BE_PlayAudio(_THIS)
   1.134 -    {
   1.135 -        return;
   1.136 -    }
   1.137 -    Uint8 *BE_GetAudioBuf(_THIS)
   1.138 -    {
   1.139 -        return (NULL);
   1.140 -    }
   1.141 +static void
   1.142 +BEAUDIO_CloseDevice(_THIS)
   1.143 +{
   1.144 +    if (_this->hidden != NULL) {
   1.145 +        if (_this->hidden->audio_obj) {
   1.146 +            _this->hidden->audio_obj->Stop();
   1.147 +            delete _this->hidden->audio_obj;
   1.148 +            _this->hidden->audio_obj = NULL;
   1.149 +        }
   1.150  
   1.151 -    void BE_CloseAudio(_THIS)
   1.152 -    {
   1.153 -        if (audio_obj) {
   1.154 -            audio_obj->Stop();
   1.155 -            delete audio_obj;
   1.156 -            audio_obj = NULL;
   1.157 -        }
   1.158 +        delete _this->hidden;
   1.159 +        _this->hidden = NULL;
   1.160  
   1.161          /* Quit the Be Application, if there's nothing left to do */
   1.162          SDL_QuitBeApp();
   1.163      }
   1.164 +}
   1.165  
   1.166 -    int BE_OpenAudio(_THIS, SDL_AudioSpec * spec)
   1.167 -    {
   1.168 -        int valid_datatype = 0;
   1.169 -        media_raw_audio_format format;
   1.170 -        SDL_AudioFormat test_format = SDL_FirstAudioFormat(spec->format);
   1.171 +static int
   1.172 +BEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
   1.173 +{
   1.174 +    int valid_datatype = 0;
   1.175 +    media_raw_audio_format format;
   1.176 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   1.177  
   1.178 -        /* Parse the audio format and fill the Be raw audio format */
   1.179 -        memset(&format, '\0', sizeof(media_raw_audio_format));
   1.180 -        format.byte_order = B_MEDIA_LITTLE_ENDIAN;
   1.181 -        format.frame_rate = (float) spec->freq;
   1.182 -        format.channel_count = spec->channels;  /* !!! FIXME: support > 2? */
   1.183 -        while ((!valid_datatype) && (test_format)) {
   1.184 -            valid_datatype = 1;
   1.185 -            spec->format = test_format;
   1.186 -            switch (test_format) {
   1.187 +    /* Initialize all variables that we clean on shutdown */
   1.188 +    _this->hidden = new SDL_PrivateAudioData;
   1.189 +    if (_this->hidden == NULL) {
   1.190 +        SDL_OutOfMemory();
   1.191 +        return 0;
   1.192 +    }
   1.193 +    SDL_memset(_this->hidden, 0, (sizeof *_this->hidden));
   1.194 +
   1.195 +    /* Initialize the Be Application, if it's not already started */
   1.196 +    if (SDL_InitBeApp() < 0) {
   1.197 +        return 0;
   1.198 +    }
   1.199 +
   1.200 +    /* Parse the audio format and fill the Be raw audio format */
   1.201 +    memset(&format, '\0', sizeof(media_raw_audio_format));
   1.202 +    format.byte_order = B_MEDIA_LITTLE_ENDIAN;
   1.203 +    format.frame_rate = (float) this->spec.freq;
   1.204 +    format.channel_count = this->spec.channels;  /* !!! FIXME: support > 2? */
   1.205 +    while ((!valid_datatype) && (test_format)) {
   1.206 +        valid_datatype = 1;
   1.207 +        this->spec.format = test_format;
   1.208 +        switch (test_format) {
   1.209              case AUDIO_S8:
   1.210                  format.format = media_raw_audio_format::B_AUDIO_CHAR;
   1.211                  break;
   1.212 @@ -207,43 +164,53 @@
   1.213                  valid_datatype = 0;
   1.214                  test_format = SDL_NextAudioFormat();
   1.215                  break;
   1.216 -            }
   1.217          }
   1.218 -
   1.219 -        format.buffer_size = spec->samples;
   1.220 -
   1.221 -        if (!valid_datatype) {  /* shouldn't happen, but just in case... */
   1.222 -            SDL_SetError("Unsupported audio format");
   1.223 -            return (-1);
   1.224 -        }
   1.225 -
   1.226 -        /* Initialize the Be Application, if it's not already started */
   1.227 -        if (SDL_InitBeApp() < 0) {
   1.228 -            return (-1);
   1.229 -        }
   1.230 -
   1.231 -        /* Calculate the final parameters for this audio specification */
   1.232 -        SDL_CalculateAudioSpec(spec);
   1.233 -
   1.234 -        /* Subscribe to the audio stream (creates a new thread) */
   1.235 -        {
   1.236 -            sigset_t omask;
   1.237 -            SDL_MaskSignals(&omask);
   1.238 -            audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
   1.239 -                                         NULL, _this);
   1.240 -            SDL_UnmaskSignals(&omask);
   1.241 -        }
   1.242 -        if (audio_obj->Start() == B_NO_ERROR) {
   1.243 -            audio_obj->SetHasData(true);
   1.244 -        } else {
   1.245 -            SDL_SetError("Unable to start Be audio");
   1.246 -            return (-1);
   1.247 -        }
   1.248 -
   1.249 -        /* We're running! */
   1.250 -        return (1);
   1.251      }
   1.252  
   1.253 -};                              /* Extern C */
   1.254 +    format.buffer_size = this->spec.samples;
   1.255 +
   1.256 +    if (!valid_datatype) {  /* shouldn't happen, but just in case... */
   1.257 +        SDL_SetError("Unsupported audio format");
   1.258 +        return 0;
   1.259 +    }
   1.260 +
   1.261 +    /* Calculate the final parameters for this audio specification */
   1.262 +    SDL_CalculateAudioSpec(&this->spec);
   1.263 +
   1.264 +    /* Subscribe to the audio stream (creates a new thread) */
   1.265 +    sigset_t omask;
   1.266 +    SDL_MaskSignals(&omask);
   1.267 +    _this->hidden->audio_obj = new BSoundPlayer(&format, "SDL Audio",
   1.268 +                                                FillSound, NULL, _this);
   1.269 +    SDL_UnmaskSignals(&omask);
   1.270 +
   1.271 +    if (_this->hidden->audio_obj->Start() == B_NO_ERROR) {
   1.272 +        _this->hidden->audio_obj->SetHasData(true);
   1.273 +    } else {
   1.274 +        SDL_SetError("Unable to start Be audio");
   1.275 +        return 0;
   1.276 +    }
   1.277 +
   1.278 +    /* We're running! */
   1.279 +    return 1;
   1.280 +}
   1.281 +
   1.282 +static int
   1.283 +BEAUDIO_Init(SDL_AudioDriverImpl *impl)
   1.284 +{
   1.285 +    /* Set the function pointers */
   1.286 +    impl->OpenDevice = DSP_OpenDevice;
   1.287 +    impl->CloseDevice = DSP_CloseDevice;
   1.288 +    impl->ProvidesOwnCallbackThread = 1;
   1.289 +    impl->OnlyHasDefaultOutputDevice = 1;
   1.290 +
   1.291 +    return 1;
   1.292 +}
   1.293 +
   1.294 +
   1.295 +AudioBootStrap BEAUDIO_bootstrap = {
   1.296 +    "baudio", "BeOS BSoundPlayer",
   1.297 +    BEAUDIO_Available, BEAUDIO_Init, 0
   1.298 +};
   1.299  
   1.300  /* vi: set ts=4 sw=4 expandtab: */