src/audio/nas/SDL_nasaudio.c
author Ryan C. Gordon
Thu, 05 Oct 2006 01:13:47 +0000
branchSDL-ryan-multiple-audio-device
changeset 3809 7852b5b78af5
parent 3798 c8b3d3d13ed1
child 3821 18393b045759
permissions -rw-r--r--
Cleaning out SDL_audiodev.c and related references ...
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@0
    22
    This driver was written by:
slouken@0
    23
    Erik Inge Bolsų
slouken@0
    24
    knan@mo.himolde.no
slouken@0
    25
*/
slouken@1402
    26
#include "SDL_config.h"
slouken@0
    27
slouken@0
    28
/* Allow access to a raw mixing buffer */
slouken@0
    29
slouken@0
    30
#include <signal.h>
slouken@0
    31
#include <unistd.h>
slouken@0
    32
slouken@1358
    33
#include "SDL_timer.h"
slouken@0
    34
#include "SDL_audio.h"
slouken@1361
    35
#include "../SDL_audiomem.h"
slouken@1361
    36
#include "../SDL_audio_c.h"
slouken@0
    37
#include "SDL_nasaudio.h"
slouken@0
    38
slouken@0
    39
/* The tag name used by artsc audio */
slouken@0
    40
#define NAS_DRIVER_NAME         "nas"
slouken@0
    41
slouken@0
    42
static struct SDL_PrivateAudioData *this2 = NULL;
slouken@0
    43
slouken@0
    44
/* Audio driver functions */
slouken@1895
    45
static int NAS_OpenAudio(_THIS, SDL_AudioSpec * spec);
slouken@0
    46
static void NAS_WaitAudio(_THIS);
slouken@0
    47
static void NAS_PlayAudio(_THIS);
slouken@0
    48
static Uint8 *NAS_GetAudioBuf(_THIS);
slouken@0
    49
static void NAS_CloseAudio(_THIS);
slouken@0
    50
slouken@0
    51
/* Audio driver bootstrap functions */
slouken@0
    52
slouken@1895
    53
static int
slouken@1895
    54
Audio_Available(void)
slouken@0
    55
{
slouken@1895
    56
    AuServer *aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
slouken@1895
    57
    if (!aud)
slouken@1895
    58
        return 0;
slouken@0
    59
slouken@1895
    60
    AuCloseServer(aud);
slouken@1895
    61
    return 1;
slouken@0
    62
}
slouken@0
    63
slouken@1895
    64
static void
slouken@1895
    65
Audio_DeleteDevice(SDL_AudioDevice * device)
slouken@0
    66
{
slouken@1895
    67
    SDL_free(device->hidden);
slouken@1895
    68
    SDL_free(device);
slouken@0
    69
}
slouken@0
    70
slouken@1895
    71
static SDL_AudioDevice *
slouken@1895
    72
Audio_CreateDevice(int devindex)
slouken@0
    73
{
slouken@1895
    74
    SDL_AudioDevice *this;
slouken@0
    75
slouken@1895
    76
    /* Initialize all variables that we clean on shutdown */
slouken@1895
    77
    this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
slouken@1895
    78
    if (this) {
slouken@1895
    79
        SDL_memset(this, 0, (sizeof *this));
slouken@1895
    80
        this->hidden = (struct SDL_PrivateAudioData *)
slouken@1895
    81
            SDL_malloc((sizeof *this->hidden));
slouken@1895
    82
    }
slouken@1895
    83
    if ((this == NULL) || (this->hidden == NULL)) {
slouken@1895
    84
        SDL_OutOfMemory();
slouken@1895
    85
        if (this) {
slouken@1895
    86
            SDL_free(this);
slouken@1895
    87
        }
slouken@1895
    88
        return (0);
slouken@1895
    89
    }
slouken@1895
    90
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
    91
slouken@1895
    92
    /* Set the function pointers */
slouken@1895
    93
    this->OpenAudio = NAS_OpenAudio;
slouken@1895
    94
    this->WaitAudio = NAS_WaitAudio;
slouken@1895
    95
    this->PlayAudio = NAS_PlayAudio;
slouken@1895
    96
    this->GetAudioBuf = NAS_GetAudioBuf;
slouken@1895
    97
    this->CloseAudio = NAS_CloseAudio;
slouken@0
    98
slouken@1895
    99
    this->free = Audio_DeleteDevice;
slouken@0
   100
slouken@1895
   101
    return this;
slouken@0
   102
}
slouken@0
   103
slouken@0
   104
AudioBootStrap NAS_bootstrap = {
slouken@1895
   105
    NAS_DRIVER_NAME, "Network Audio System",
icculus@3798
   106
    Audio_Available, Audio_CreateDevice, 0
slouken@0
   107
};
slouken@0
   108
slouken@0
   109
/* This function waits until it is possible to write a full sound buffer */
slouken@1895
   110
static void
slouken@1895
   111
NAS_WaitAudio(_THIS)
slouken@0
   112
{
slouken@1895
   113
    while (this->hidden->buf_free < this->hidden->mixlen) {
slouken@1895
   114
        AuEvent ev;
slouken@1895
   115
        AuNextEvent(this->hidden->aud, AuTrue, &ev);
slouken@1895
   116
        AuDispatchEvent(this->hidden->aud, &ev);
slouken@1895
   117
    }
slouken@0
   118
}
slouken@0
   119
slouken@1895
   120
static void
slouken@1895
   121
NAS_PlayAudio(_THIS)
slouken@0
   122
{
slouken@1895
   123
    while (this->hidden->mixlen > this->hidden->buf_free) {     /* We think the buffer is full? Yikes! Ask the server for events,
slouken@1895
   124
                                                                   in the hope that some of them is LowWater events telling us more
slouken@1895
   125
                                                                   of the buffer is free now than what we think. */
slouken@1895
   126
        AuEvent ev;
slouken@1895
   127
        AuNextEvent(this->hidden->aud, AuTrue, &ev);
slouken@1895
   128
        AuDispatchEvent(this->hidden->aud, &ev);
slouken@1895
   129
    }
slouken@1895
   130
    this->hidden->buf_free -= this->hidden->mixlen;
slouken@0
   131
slouken@1895
   132
    /* Write the audio data */
slouken@1895
   133
    AuWriteElement(this->hidden->aud, this->hidden->flow, 0,
slouken@1895
   134
                   this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
slouken@0
   135
slouken@1895
   136
    this->hidden->written += this->hidden->mixlen;
slouken@1895
   137
slouken@0
   138
#ifdef DEBUG_AUDIO
slouken@1895
   139
    fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
slouken@0
   140
#endif
slouken@0
   141
}
slouken@0
   142
slouken@1895
   143
static Uint8 *
slouken@1895
   144
NAS_GetAudioBuf(_THIS)
slouken@0
   145
{
slouken@1895
   146
    return (this->hidden->mixbuf);
slouken@0
   147
}
slouken@0
   148
slouken@1895
   149
static void
slouken@1895
   150
NAS_CloseAudio(_THIS)
slouken@0
   151
{
slouken@1895
   152
    if (this->hidden->mixbuf != NULL) {
slouken@1895
   153
        SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@1895
   154
        this->hidden->mixbuf = NULL;
slouken@1895
   155
    }
slouken@1895
   156
    if (this->hidden->aud) {
slouken@1895
   157
        AuCloseServer(this->hidden->aud);
slouken@1895
   158
        this->hidden->aud = 0;
slouken@1895
   159
    }
slouken@0
   160
}
slouken@0
   161
slouken@1895
   162
static unsigned char
slouken@1895
   163
sdlformat_to_auformat(unsigned int fmt)
slouken@0
   164
{
slouken@1895
   165
    switch (fmt) {
slouken@0
   166
    case AUDIO_U8:
slouken@1895
   167
        return AuFormatLinearUnsigned8;
slouken@0
   168
    case AUDIO_S8:
slouken@1895
   169
        return AuFormatLinearSigned8;
slouken@0
   170
    case AUDIO_U16LSB:
slouken@1895
   171
        return AuFormatLinearUnsigned16LSB;
slouken@0
   172
    case AUDIO_U16MSB:
slouken@1895
   173
        return AuFormatLinearUnsigned16MSB;
slouken@0
   174
    case AUDIO_S16LSB:
slouken@1895
   175
        return AuFormatLinearSigned16LSB;
slouken@0
   176
    case AUDIO_S16MSB:
slouken@1895
   177
        return AuFormatLinearSigned16MSB;
slouken@0
   178
    }
slouken@1895
   179
    return AuNone;
slouken@0
   180
}
slouken@0
   181
slouken@0
   182
static AuBool
slouken@1895
   183
event_handler(AuServer * aud, AuEvent * ev, AuEventHandlerRec * hnd)
slouken@0
   184
{
slouken@1895
   185
    switch (ev->type) {
slouken@1895
   186
    case AuEventTypeElementNotify:
slouken@1895
   187
        {
slouken@1895
   188
            AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
slouken@0
   189
slouken@1895
   190
            switch (event->kind) {
slouken@1895
   191
            case AuElementNotifyKindLowWater:
slouken@1895
   192
                if (this2->buf_free >= 0) {
slouken@1895
   193
                    this2->really += event->num_bytes;
slouken@1895
   194
                    gettimeofday(&this2->last_tv, 0);
slouken@1895
   195
                    this2->buf_free += event->num_bytes;
slouken@1895
   196
                } else {
slouken@1895
   197
                    this2->buf_free = event->num_bytes;
slouken@1895
   198
                }
slouken@1895
   199
                break;
slouken@1895
   200
            case AuElementNotifyKindState:
slouken@1895
   201
                switch (event->cur_state) {
slouken@1895
   202
                case AuStatePause:
slouken@1895
   203
                    if (event->reason != AuReasonUser) {
slouken@1895
   204
                        if (this2->buf_free >= 0) {
slouken@1895
   205
                            this2->really += event->num_bytes;
slouken@1895
   206
                            gettimeofday(&this2->last_tv, 0);
slouken@1895
   207
                            this2->buf_free += event->num_bytes;
slouken@1895
   208
                        } else {
slouken@1895
   209
                            this2->buf_free = event->num_bytes;
slouken@1895
   210
                        }
slouken@1895
   211
                    }
slouken@1895
   212
                    break;
slouken@1895
   213
                }
slouken@1895
   214
            }
slouken@1895
   215
        }
slouken@1895
   216
    }
slouken@1895
   217
    return AuTrue;
slouken@0
   218
}
slouken@0
   219
slouken@0
   220
static AuDeviceID
slouken@0
   221
find_device(_THIS, int nch)
slouken@0
   222
{
slouken@1895
   223
    int i;
slouken@1895
   224
    for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
slouken@1895
   225
        if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
slouken@1895
   226
             AuComponentKindPhysicalOutput) &&
slouken@1895
   227
            AuDeviceNumTracks(AuServerDevice(this->hidden->aud, i)) == nch) {
slouken@1895
   228
            return AuDeviceIdentifier(AuServerDevice(this->hidden->aud, i));
slouken@1895
   229
        }
slouken@1895
   230
    }
slouken@1895
   231
    return AuNone;
slouken@0
   232
}
slouken@0
   233
slouken@1895
   234
static int
slouken@1895
   235
NAS_OpenAudio(_THIS, SDL_AudioSpec * spec)
slouken@0
   236
{
slouken@1895
   237
    AuElement elms[3];
slouken@1895
   238
    int buffer_size;
icculus@1982
   239
    SDL_AudioFormat test_format, format;
slouken@1895
   240
slouken@1895
   241
    this->hidden->mixbuf = NULL;
slouken@0
   242
slouken@1895
   243
    /* Try for a closest match on audio format */
slouken@1895
   244
    format = 0;
slouken@1895
   245
    for (test_format = SDL_FirstAudioFormat(spec->format);
slouken@1895
   246
         !format && test_format;) {
slouken@1895
   247
        format = sdlformat_to_auformat(test_format);
slouken@0
   248
slouken@1895
   249
        if (format == AuNone) {
slouken@1895
   250
            test_format = SDL_NextAudioFormat();
slouken@1895
   251
        }
slouken@1895
   252
    }
slouken@1895
   253
    if (format == 0) {
slouken@1895
   254
        SDL_SetError("Couldn't find any hardware audio formats");
slouken@1895
   255
        return (-1);
slouken@1895
   256
    }
slouken@1895
   257
    spec->format = test_format;
slouken@0
   258
slouken@1895
   259
    this->hidden->aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
slouken@1895
   260
    if (this->hidden->aud == 0) {
slouken@1895
   261
        SDL_SetError("Couldn't open connection to NAS server");
slouken@1895
   262
        return (-1);
slouken@1895
   263
    }
slouken@1895
   264
slouken@1895
   265
    this->hidden->dev = find_device(this, spec->channels);
slouken@1895
   266
    if ((this->hidden->dev == AuNone)
slouken@1895
   267
        || (!(this->hidden->flow = AuCreateFlow(this->hidden->aud, NULL)))) {
slouken@1895
   268
        AuCloseServer(this->hidden->aud);
slouken@1895
   269
        this->hidden->aud = 0;
slouken@1895
   270
        SDL_SetError("Couldn't find a fitting playback device on NAS server");
slouken@1895
   271
        return (-1);
slouken@1895
   272
    }
slouken@0
   273
slouken@1895
   274
    buffer_size = spec->freq;
slouken@1895
   275
    if (buffer_size < 4096)
slouken@1895
   276
        buffer_size = 4096;
slouken@0
   277
slouken@1895
   278
    if (buffer_size > 32768)
slouken@1895
   279
        buffer_size = 32768;    /* So that the buffer won't get unmanageably big. */
slouken@0
   280
slouken@1895
   281
    /* Calculate the final parameters for this audio specification */
slouken@1895
   282
    SDL_CalculateAudioSpec(spec);
slouken@1895
   283
slouken@1895
   284
    this2 = this->hidden;
slouken@0
   285
slouken@1895
   286
    AuMakeElementImportClient(elms, spec->freq, format, spec->channels,
slouken@1895
   287
                              AuTrue, buffer_size, buffer_size / 4, 0, NULL);
slouken@1895
   288
    AuMakeElementExportDevice(elms + 1, 0, this->hidden->dev, spec->freq,
slouken@1895
   289
                              AuUnlimitedSamples, 0, NULL);
slouken@1895
   290
    AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms,
slouken@1895
   291
                  NULL);
slouken@1895
   292
    AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0,
slouken@1895
   293
                           this->hidden->flow, event_handler,
slouken@1895
   294
                           (AuPointer) NULL);
slouken@0
   295
slouken@1895
   296
    AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
slouken@0
   297
slouken@1895
   298
    /* Allocate mixing buffer */
slouken@1895
   299
    this->hidden->mixlen = spec->size;
slouken@1895
   300
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
slouken@1895
   301
    if (this->hidden->mixbuf == NULL) {
slouken@1895
   302
        return (-1);
slouken@1895
   303
    }
slouken@1895
   304
    SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
slouken@0
   305
slouken@1895
   306
    /* Get the parent process id (we're the parent of the audio thread) */
slouken@1895
   307
    this->hidden->parent = getpid();
slouken@0
   308
slouken@1895
   309
    /* We're ready to rock and roll. :-) */
slouken@1895
   310
    return (0);
slouken@0
   311
}
slouken@1895
   312
slouken@1895
   313
/* vi: set ts=4 sw=4 expandtab: */