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