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