src/audio/baudio/SDL_beaudio.cc
author Ryan C. Gordon <icculus@icculus.org>
Thu, 14 Nov 2013 01:00:08 -0500
changeset 7979 93d31af030bc
parent 7977 5272ae8fccd7
permissions -rw-r--r--
Patched to compile on Haiku.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1403
    21
#include "SDL_config.h"
slouken@0
    22
slouken@6044
    23
#if SDL_AUDIO_DRIVER_BEOSAUDIO
slouken@6044
    24
slouken@0
    25
/* Allow access to the audio stream on BeOS */
slouken@0
    26
slouken@0
    27
#include <SoundPlayer.h>
icculus@7977
    28
#include <signal.h>
slouken@0
    29
slouken@1367
    30
#include "../../main/beos/SDL_BeApp.h"
slouken@0
    31
slouken@1895
    32
extern "C"
slouken@1895
    33
{
slouken@0
    34
slouken@0
    35
#include "SDL_audio.h"
slouken@1361
    36
#include "../SDL_audio_c.h"
slouken@1361
    37
#include "../SDL_sysaudio.h"
slouken@0
    38
#include "SDL_beaudio.h"
slouken@0
    39
icculus@2049
    40
}
slouken@0
    41
slouken@0
    42
icculus@2049
    43
/* !!! FIXME: have the callback call the higher level to avoid code dupe. */
icculus@2049
    44
/* The BeOS callback for handling the audio buffer */
icculus@2049
    45
static void
icculus@2049
    46
FillSound(void *device, void *stream, size_t len,
icculus@2049
    47
          const media_raw_audio_format & format)
icculus@2049
    48
{
icculus@2049
    49
    SDL_AudioDevice *audio = (SDL_AudioDevice *) device;
slouken@0
    50
icculus@2049
    51
    /* Only do soemthing if audio is enabled */
icculus@2049
    52
    if (!audio->enabled)
icculus@2049
    53
        return;
slouken@0
    54
icculus@2049
    55
    if (!audio->paused) {
icculus@2049
    56
        if (audio->convert.needed) {
slouken@6977
    57
            SDL_LockMutex(audio->mixer_lock);
icculus@2049
    58
            (*audio->spec.callback) (audio->spec.userdata,
slouken@2060
    59
                                     (Uint8 *) audio->convert.buf,
slouken@2060
    60
                                     audio->convert.len);
slouken@6977
    61
            SDL_UnlockMutex(audio->mixer_lock);
icculus@2049
    62
            SDL_ConvertAudio(&audio->convert);
icculus@2049
    63
            SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt);
icculus@2049
    64
        } else {
slouken@6977
    65
            SDL_LockMutex(audio->mixer_lock);
icculus@2049
    66
            (*audio->spec.callback) (audio->spec.userdata,
slouken@2060
    67
                                     (Uint8 *) stream, len);
slouken@6977
    68
            SDL_UnlockMutex(audio->mixer_lock);
slouken@1895
    69
        }
slouken@1895
    70
    }
icculus@2049
    71
}
slouken@0
    72
icculus@2049
    73
static void
icculus@2049
    74
BEOSAUDIO_CloseDevice(_THIS)
icculus@2049
    75
{
icculus@2049
    76
    if (_this->hidden != NULL) {
icculus@2049
    77
        if (_this->hidden->audio_obj) {
icculus@2049
    78
            _this->hidden->audio_obj->Stop();
icculus@2049
    79
            delete _this->hidden->audio_obj;
icculus@2049
    80
            _this->hidden->audio_obj = NULL;
slouken@1895
    81
        }
slouken@0
    82
icculus@2049
    83
        delete _this->hidden;
icculus@2049
    84
        _this->hidden = NULL;
slouken@1895
    85
    }
icculus@2049
    86
}
slouken@0
    87
icculus@7977
    88
icculus@7977
    89
static const int sig_list[] = {
icculus@7977
    90
    SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGWINCH, 0
icculus@7977
    91
};
icculus@7977
    92
icculus@7977
    93
static inline void
icculus@7977
    94
MaskSignals(sigset_t * omask)
icculus@7977
    95
{
icculus@7977
    96
    sigset_t mask;
icculus@7977
    97
    int i;
icculus@7977
    98
icculus@7977
    99
    sigemptyset(&mask);
icculus@7977
   100
    for (i = 0; sig_list[i]; ++i) {
icculus@7977
   101
        sigaddset(&mask, sig_list[i]);
icculus@7977
   102
    }
icculus@7977
   103
    sigprocmask(SIG_BLOCK, &mask, omask);
icculus@7977
   104
}
icculus@7977
   105
icculus@7977
   106
static inline void
icculus@7977
   107
UnmaskSignals(sigset_t * omask)
icculus@7977
   108
{
icculus@7977
   109
    sigprocmask(SIG_SETMASK, omask, NULL);
icculus@7977
   110
}
icculus@7977
   111
icculus@7977
   112
icculus@2049
   113
static int
icculus@2049
   114
BEOSAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
icculus@2049
   115
{
icculus@2049
   116
    int valid_datatype = 0;
icculus@2049
   117
    media_raw_audio_format format;
icculus@2049
   118
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(_this->spec.format);
icculus@1997
   119
icculus@2049
   120
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   121
    _this->hidden = new SDL_PrivateAudioData;
icculus@2049
   122
    if (_this->hidden == NULL) {
icculus@7038
   123
        return SDL_OutOfMemory();
icculus@2049
   124
    }
icculus@2049
   125
    SDL_memset(_this->hidden, 0, (sizeof *_this->hidden));
icculus@2049
   126
icculus@2049
   127
    /* Parse the audio format and fill the Be raw audio format */
icculus@2049
   128
    SDL_memset(&format, '\0', sizeof(media_raw_audio_format));
icculus@2049
   129
    format.byte_order = B_MEDIA_LITTLE_ENDIAN;
icculus@2049
   130
    format.frame_rate = (float) _this->spec.freq;
slouken@2060
   131
    format.channel_count = _this->spec.channels;        /* !!! FIXME: support > 2? */
icculus@2049
   132
    while ((!valid_datatype) && (test_format)) {
icculus@2049
   133
        valid_datatype = 1;
icculus@2049
   134
        _this->spec.format = test_format;
icculus@2049
   135
        switch (test_format) {
slouken@2060
   136
        case AUDIO_S8:
slouken@2060
   137
            format.format = media_raw_audio_format::B_AUDIO_CHAR;
slouken@2060
   138
            break;
icculus@1997
   139
slouken@2060
   140
        case AUDIO_U8:
slouken@2060
   141
            format.format = media_raw_audio_format::B_AUDIO_UCHAR;
slouken@2060
   142
            break;
icculus@1997
   143
slouken@2060
   144
        case AUDIO_S16LSB:
slouken@2060
   145
            format.format = media_raw_audio_format::B_AUDIO_SHORT;
slouken@2060
   146
            break;
icculus@1997
   147
slouken@2060
   148
        case AUDIO_S16MSB:
slouken@2060
   149
            format.format = media_raw_audio_format::B_AUDIO_SHORT;
slouken@2060
   150
            format.byte_order = B_MEDIA_BIG_ENDIAN;
slouken@2060
   151
            break;
icculus@1997
   152
slouken@2060
   153
        case AUDIO_S32LSB:
slouken@2060
   154
            format.format = media_raw_audio_format::B_AUDIO_INT;
slouken@2060
   155
            break;
icculus@1997
   156
slouken@2060
   157
        case AUDIO_S32MSB:
slouken@2060
   158
            format.format = media_raw_audio_format::B_AUDIO_INT;
slouken@2060
   159
            format.byte_order = B_MEDIA_BIG_ENDIAN;
slouken@2060
   160
            break;
icculus@1997
   161
slouken@2060
   162
        case AUDIO_F32LSB:
slouken@2060
   163
            format.format = media_raw_audio_format::B_AUDIO_FLOAT;
slouken@2060
   164
            break;
icculus@1997
   165
slouken@2060
   166
        case AUDIO_F32MSB:
slouken@2060
   167
            format.format = media_raw_audio_format::B_AUDIO_FLOAT;
slouken@2060
   168
            format.byte_order = B_MEDIA_BIG_ENDIAN;
slouken@2060
   169
            break;
icculus@1997
   170
slouken@2060
   171
        default:
slouken@2060
   172
            valid_datatype = 0;
slouken@2060
   173
            test_format = SDL_NextAudioFormat();
slouken@2060
   174
            break;
icculus@1997
   175
        }
slouken@1895
   176
    }
slouken@1895
   177
slouken@2060
   178
    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
icculus@2049
   179
        BEOSAUDIO_CloseDevice(_this);
icculus@7038
   180
        return SDL_SetError("Unsupported audio format");
icculus@2049
   181
    }
icculus@2049
   182
icculus@2049
   183
    /* Calculate the final parameters for this audio specification */
icculus@2049
   184
    SDL_CalculateAudioSpec(&_this->spec);
icculus@2049
   185
slouken@6856
   186
    format.buffer_size = _this->spec.size;
slouken@6856
   187
icculus@2049
   188
    /* Subscribe to the audio stream (creates a new thread) */
icculus@2049
   189
    sigset_t omask;
icculus@7977
   190
    MaskSignals(&omask);
icculus@2049
   191
    _this->hidden->audio_obj = new BSoundPlayer(&format, "SDL Audio",
icculus@2049
   192
                                                FillSound, NULL, _this);
icculus@7977
   193
    UnmaskSignals(&omask);
icculus@2049
   194
icculus@2049
   195
    if (_this->hidden->audio_obj->Start() == B_NO_ERROR) {
icculus@2049
   196
        _this->hidden->audio_obj->SetHasData(true);
icculus@2049
   197
    } else {
icculus@2049
   198
        BEOSAUDIO_CloseDevice(_this);
icculus@7038
   199
        return SDL_SetError("Unable to start Be audio");
icculus@2049
   200
    }
icculus@2049
   201
icculus@2049
   202
    /* We're running! */
icculus@7038
   203
    return 0;
icculus@2049
   204
}
icculus@2049
   205
icculus@2049
   206
static void
icculus@2049
   207
BEOSAUDIO_Deinitialize(void)
icculus@2049
   208
{
icculus@2049
   209
    SDL_QuitBeApp();
icculus@2049
   210
}
icculus@2049
   211
icculus@2049
   212
static int
slouken@2060
   213
BEOSAUDIO_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   214
{
icculus@2049
   215
    /* Initialize the Be Application, if it's not already started */
icculus@2049
   216
    if (SDL_InitBeApp() < 0) {
icculus@2049
   217
        return 0;
icculus@2049
   218
    }
icculus@2049
   219
icculus@2049
   220
    /* Set the function pointers */
icculus@2049
   221
    impl->OpenDevice = BEOSAUDIO_OpenDevice;
icculus@2049
   222
    impl->CloseDevice = BEOSAUDIO_CloseDevice;
icculus@2049
   223
    impl->Deinitialize = BEOSAUDIO_Deinitialize;
icculus@2049
   224
    impl->ProvidesOwnCallbackThread = 1;
icculus@2049
   225
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   226
icculus@3699
   227
    return 1;   /* this audio target is available. */
icculus@2049
   228
}
icculus@2049
   229
slouken@2060
   230
extern "C"
slouken@2060
   231
{
slouken@2060
   232
    extern AudioBootStrap BEOSAUDIO_bootstrap;
slouken@2060
   233
}
icculus@2049
   234
AudioBootStrap BEOSAUDIO_bootstrap = {
icculus@2049
   235
    "baudio", "BeOS BSoundPlayer", BEOSAUDIO_Init, 0
icculus@2049
   236
};
slouken@1895
   237
slouken@6044
   238
#endif /* SDL_AUDIO_DRIVER_BEOSAUDIO */
slouken@6044
   239
slouken@1895
   240
/* vi: set ts=4 sw=4 expandtab: */