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