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