src/audio/baudio/SDL_beaudio.cc
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1403 376665398b25
child 1997 46319c67b3d7
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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;
slouken@0
   159
slouken@1895
   160
        /* Initialize the Be Application, if it's not already started */
slouken@1895
   161
        if (SDL_InitBeApp() < 0) {
slouken@1895
   162
            return (-1);
slouken@1895
   163
        }
slouken@0
   164
slouken@1895
   165
        /* Parse the audio format and fill the Be raw audio format */
slouken@1895
   166
        format.frame_rate = (float) spec->freq;
slouken@1895
   167
        format.channel_count = spec->channels;
slouken@1895
   168
        switch (spec->format & ~0x1000) {
slouken@1895
   169
        case AUDIO_S8:
slouken@1895
   170
            /* Signed 8-bit audio unsupported, convert to U8 */
slouken@1895
   171
            spec->format = AUDIO_U8;
slouken@1895
   172
        case AUDIO_U8:
slouken@1895
   173
            format.format = media_raw_audio_format::B_AUDIO_UCHAR;
slouken@1895
   174
            format.byte_order = 0;
slouken@1895
   175
            break;
slouken@1895
   176
        case AUDIO_U16:
slouken@1895
   177
            /* Unsigned 16-bit audio unsupported, convert to S16 */
slouken@1895
   178
            spec->format ^= 0x8000;
slouken@1895
   179
        case AUDIO_S16:
slouken@1895
   180
            format.format = media_raw_audio_format::B_AUDIO_SHORT;
slouken@1895
   181
            if (spec->format & 0x1000) {
slouken@1895
   182
                format.byte_order = 1;  /* Big endian */
slouken@1895
   183
            } else {
slouken@1895
   184
                format.byte_order = 2;  /* Little endian */
slouken@1895
   185
            }
slouken@1895
   186
            break;
slouken@1895
   187
        }
slouken@1895
   188
        format.buffer_size = spec->samples;
slouken@0
   189
slouken@1895
   190
        /* Calculate the final parameters for this audio specification */
slouken@1895
   191
        SDL_CalculateAudioSpec(spec);
slouken@0
   192
slouken@1895
   193
        /* Subscribe to the audio stream (creates a new thread) */
slouken@1895
   194
        {
slouken@1895
   195
            sigset_t omask;
slouken@1895
   196
            SDL_MaskSignals(&omask);
slouken@1895
   197
            audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound,
slouken@1895
   198
                                         NULL, _this);
slouken@1895
   199
            SDL_UnmaskSignals(&omask);
slouken@1895
   200
        }
slouken@1895
   201
        if (audio_obj->Start() == B_NO_ERROR) {
slouken@1895
   202
            audio_obj->SetHasData(true);
slouken@1895
   203
        } else {
slouken@1895
   204
            SDL_SetError("Unable to start Be audio");
slouken@1895
   205
            return (-1);
slouken@1895
   206
        }
slouken@0
   207
slouken@1895
   208
        /* We're running! */
slouken@1895
   209
        return (1);
slouken@1895
   210
    }
slouken@1895
   211
slouken@1895
   212
};                              /* Extern C */
slouken@1895
   213
slouken@1895
   214
/* vi: set ts=4 sw=4 expandtab: */