src/audio/baudio/SDL_beaudio.cc
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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>
slouken@0
    28
slouken@1367
    29
#include "../../main/beos/SDL_BeApp.h"
slouken@0
    30
slouken@1895
    31
extern "C"
slouken@1895
    32
{
slouken@0
    33
slouken@0
    34
#include "SDL_audio.h"
slouken@1361
    35
#include "../SDL_audio_c.h"
slouken@1361
    36
#include "../SDL_sysaudio.h"
slouken@1367
    37
#include "../../thread/beos/SDL_systhread_c.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
    /* Silence the buffer, since it's ours */
icculus@2049
    52
    SDL_memset(stream, audio->spec.silence, len);
slouken@0
    53
icculus@2049
    54
    /* Only do soemthing if audio is enabled */
icculus@2049
    55
    if (!audio->enabled)
icculus@2049
    56
        return;
slouken@0
    57
icculus@2049
    58
    if (!audio->paused) {
icculus@2049
    59
        if (audio->convert.needed) {
icculus@2049
    60
            SDL_mutexP(audio->mixer_lock);
icculus@2049
    61
            (*audio->spec.callback) (audio->spec.userdata,
slouken@2060
    62
                                     (Uint8 *) audio->convert.buf,
slouken@2060
    63
                                     audio->convert.len);
icculus@2049
    64
            SDL_mutexV(audio->mixer_lock);
icculus@2049
    65
            SDL_ConvertAudio(&audio->convert);
icculus@2049
    66
            SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt);
icculus@2049
    67
        } else {
icculus@2049
    68
            SDL_mutexP(audio->mixer_lock);
icculus@2049
    69
            (*audio->spec.callback) (audio->spec.userdata,
slouken@2060
    70
                                     (Uint8 *) stream, len);
icculus@2049
    71
            SDL_mutexV(audio->mixer_lock);
slouken@1895
    72
        }
slouken@1895
    73
    }
icculus@2049
    74
}
slouken@0
    75
icculus@2049
    76
static void
icculus@2049
    77
BEOSAUDIO_CloseDevice(_THIS)
icculus@2049
    78
{
icculus@2049
    79
    if (_this->hidden != NULL) {
icculus@2049
    80
        if (_this->hidden->audio_obj) {
icculus@2049
    81
            _this->hidden->audio_obj->Stop();
icculus@2049
    82
            delete _this->hidden->audio_obj;
icculus@2049
    83
            _this->hidden->audio_obj = NULL;
slouken@1895
    84
        }
slouken@0
    85
icculus@2049
    86
        delete _this->hidden;
icculus@2049
    87
        _this->hidden = NULL;
slouken@1895
    88
    }
icculus@2049
    89
}
slouken@0
    90
icculus@2049
    91
static int
icculus@2049
    92
BEOSAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
icculus@2049
    93
{
icculus@2049
    94
    int valid_datatype = 0;
icculus@2049
    95
    media_raw_audio_format format;
icculus@2049
    96
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(_this->spec.format);
icculus@1997
    97
icculus@2049
    98
    /* Initialize all variables that we clean on shutdown */
icculus@2049
    99
    _this->hidden = new SDL_PrivateAudioData;
icculus@2049
   100
    if (_this->hidden == NULL) {
icculus@2049
   101
        SDL_OutOfMemory();
icculus@2049
   102
        return 0;
icculus@2049
   103
    }
icculus@2049
   104
    SDL_memset(_this->hidden, 0, (sizeof *_this->hidden));
icculus@2049
   105
icculus@2049
   106
    /* Parse the audio format and fill the Be raw audio format */
icculus@2049
   107
    SDL_memset(&format, '\0', sizeof(media_raw_audio_format));
icculus@2049
   108
    format.byte_order = B_MEDIA_LITTLE_ENDIAN;
icculus@2049
   109
    format.frame_rate = (float) _this->spec.freq;
slouken@2060
   110
    format.channel_count = _this->spec.channels;        /* !!! FIXME: support > 2? */
icculus@2049
   111
    while ((!valid_datatype) && (test_format)) {
icculus@2049
   112
        valid_datatype = 1;
icculus@2049
   113
        _this->spec.format = test_format;
icculus@2049
   114
        switch (test_format) {
slouken@2060
   115
        case AUDIO_S8:
slouken@2060
   116
            format.format = media_raw_audio_format::B_AUDIO_CHAR;
slouken@2060
   117
            break;
icculus@1997
   118
slouken@2060
   119
        case AUDIO_U8:
slouken@2060
   120
            format.format = media_raw_audio_format::B_AUDIO_UCHAR;
slouken@2060
   121
            break;
icculus@1997
   122
slouken@2060
   123
        case AUDIO_S16LSB:
slouken@2060
   124
            format.format = media_raw_audio_format::B_AUDIO_SHORT;
slouken@2060
   125
            break;
icculus@1997
   126
slouken@2060
   127
        case AUDIO_S16MSB:
slouken@2060
   128
            format.format = media_raw_audio_format::B_AUDIO_SHORT;
slouken@2060
   129
            format.byte_order = B_MEDIA_BIG_ENDIAN;
slouken@2060
   130
            break;
icculus@1997
   131
slouken@2060
   132
        case AUDIO_S32LSB:
slouken@2060
   133
            format.format = media_raw_audio_format::B_AUDIO_INT;
slouken@2060
   134
            break;
icculus@1997
   135
slouken@2060
   136
        case AUDIO_S32MSB:
slouken@2060
   137
            format.format = media_raw_audio_format::B_AUDIO_INT;
slouken@2060
   138
            format.byte_order = B_MEDIA_BIG_ENDIAN;
slouken@2060
   139
            break;
icculus@1997
   140
slouken@2060
   141
        case AUDIO_F32LSB:
slouken@2060
   142
            format.format = media_raw_audio_format::B_AUDIO_FLOAT;
slouken@2060
   143
            break;
icculus@1997
   144
slouken@2060
   145
        case AUDIO_F32MSB:
slouken@2060
   146
            format.format = media_raw_audio_format::B_AUDIO_FLOAT;
slouken@2060
   147
            format.byte_order = B_MEDIA_BIG_ENDIAN;
slouken@2060
   148
            break;
icculus@1997
   149
slouken@2060
   150
        default:
slouken@2060
   151
            valid_datatype = 0;
slouken@2060
   152
            test_format = SDL_NextAudioFormat();
slouken@2060
   153
            break;
icculus@1997
   154
        }
slouken@1895
   155
    }
slouken@1895
   156
icculus@2049
   157
    format.buffer_size = _this->spec.samples;
icculus@2049
   158
slouken@2060
   159
    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
icculus@2049
   160
        BEOSAUDIO_CloseDevice(_this);
icculus@2049
   161
        SDL_SetError("Unsupported audio format");
icculus@2049
   162
        return 0;
icculus@2049
   163
    }
icculus@2049
   164
icculus@2049
   165
    /* Calculate the final parameters for this audio specification */
icculus@2049
   166
    SDL_CalculateAudioSpec(&_this->spec);
icculus@2049
   167
icculus@2049
   168
    /* Subscribe to the audio stream (creates a new thread) */
icculus@2049
   169
    sigset_t omask;
icculus@2049
   170
    SDL_MaskSignals(&omask);
icculus@2049
   171
    _this->hidden->audio_obj = new BSoundPlayer(&format, "SDL Audio",
icculus@2049
   172
                                                FillSound, NULL, _this);
icculus@2049
   173
    SDL_UnmaskSignals(&omask);
icculus@2049
   174
icculus@2049
   175
    if (_this->hidden->audio_obj->Start() == B_NO_ERROR) {
icculus@2049
   176
        _this->hidden->audio_obj->SetHasData(true);
icculus@2049
   177
    } else {
icculus@2049
   178
        BEOSAUDIO_CloseDevice(_this);
icculus@2049
   179
        SDL_SetError("Unable to start Be audio");
icculus@2049
   180
        return 0;
icculus@2049
   181
    }
icculus@2049
   182
icculus@2049
   183
    /* We're running! */
icculus@2049
   184
    return 1;
icculus@2049
   185
}
icculus@2049
   186
icculus@2049
   187
static void
icculus@2049
   188
BEOSAUDIO_Deinitialize(void)
icculus@2049
   189
{
icculus@2049
   190
    SDL_QuitBeApp();
icculus@2049
   191
}
icculus@2049
   192
icculus@2049
   193
static int
slouken@2060
   194
BEOSAUDIO_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   195
{
icculus@2049
   196
    /* Initialize the Be Application, if it's not already started */
icculus@2049
   197
    if (SDL_InitBeApp() < 0) {
icculus@2049
   198
        return 0;
icculus@2049
   199
    }
icculus@2049
   200
icculus@2049
   201
    /* Set the function pointers */
icculus@2049
   202
    impl->OpenDevice = BEOSAUDIO_OpenDevice;
icculus@2049
   203
    impl->CloseDevice = BEOSAUDIO_CloseDevice;
icculus@2049
   204
    impl->Deinitialize = BEOSAUDIO_Deinitialize;
icculus@2049
   205
    impl->ProvidesOwnCallbackThread = 1;
icculus@2049
   206
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   207
icculus@3699
   208
    return 1;   /* this audio target is available. */
icculus@2049
   209
}
icculus@2049
   210
slouken@2060
   211
extern "C"
slouken@2060
   212
{
slouken@2060
   213
    extern AudioBootStrap BEOSAUDIO_bootstrap;
slouken@2060
   214
}
icculus@2049
   215
AudioBootStrap BEOSAUDIO_bootstrap = {
icculus@2049
   216
    "baudio", "BeOS BSoundPlayer", BEOSAUDIO_Init, 0
icculus@2049
   217
};
slouken@1895
   218
slouken@6044
   219
#endif /* SDL_AUDIO_DRIVER_BEOSAUDIO */
slouken@6044
   220
slouken@1895
   221
/* vi: set ts=4 sw=4 expandtab: */