src/audio/nas/SDL_nasaudio.c
author Sam Lantinga
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1402 d910939febfa
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    46
static int NAS_OpenAudio (_THIS, SDL_AudioSpec * spec);
slouken@1662
    47
static void NAS_WaitAudio (_THIS);
slouken@1662
    48
static void NAS_PlayAudio (_THIS);
slouken@1662
    49
static Uint8 *NAS_GetAudioBuf (_THIS);
slouken@1662
    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@1662
    55
Audio_Available (void)
slouken@0
    56
{
slouken@1662
    57
    AuServer *aud = AuOpenServer ("", 0, NULL, 0, NULL, NULL);
slouken@1662
    58
    if (!aud)
slouken@1662
    59
        return 0;
slouken@0
    60
slouken@1662
    61
    AuCloseServer (aud);
slouken@1662
    62
    return 1;
slouken@0
    63
}
slouken@0
    64
slouken@1662
    65
static void
slouken@1662
    66
Audio_DeleteDevice (SDL_AudioDevice * device)
slouken@0
    67
{
slouken@1662
    68
    SDL_free (device->hidden);
slouken@1662
    69
    SDL_free (device);
slouken@0
    70
}
slouken@0
    71
slouken@1662
    72
static SDL_AudioDevice *
slouken@1662
    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@1662
    78
    this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice));
slouken@1662
    79
    if (this) {
slouken@1662
    80
        SDL_memset (this, 0, (sizeof *this));
slouken@1662
    81
        this->hidden = (struct SDL_PrivateAudioData *)
slouken@1662
    82
            SDL_malloc ((sizeof *this->hidden));
slouken@1662
    83
    }
slouken@1662
    84
    if ((this == NULL) || (this->hidden == NULL)) {
slouken@1662
    85
        SDL_OutOfMemory ();
slouken@1662
    86
        if (this) {
slouken@1662
    87
            SDL_free (this);
slouken@1662
    88
        }
slouken@1662
    89
        return (0);
slouken@1662
    90
    }
slouken@1662
    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@1662
   112
NAS_WaitAudio (_THIS)
slouken@0
   113
{
slouken@1662
   114
    while (this->hidden->buf_free < this->hidden->mixlen) {
slouken@1662
   115
        AuEvent ev;
slouken@1662
   116
        AuNextEvent (this->hidden->aud, AuTrue, &ev);
slouken@1662
   117
        AuDispatchEvent (this->hidden->aud, &ev);
slouken@1662
   118
    }
slouken@0
   119
}
slouken@0
   120
slouken@1662
   121
static void
slouken@1662
   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@1662
   128
        AuNextEvent (this->hidden->aud, AuTrue, &ev);
slouken@1662
   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@1662
   134
    AuWriteElement (this->hidden->aud, this->hidden->flow, 0,
slouken@1662
   135
                    this->hidden->mixlen, this->hidden->mixbuf, AuFalse,
slouken@1662
   136
                    NULL);
slouken@0
   137
slouken@1662
   138
    this->hidden->written += this->hidden->mixlen;
slouken@1662
   139
slouken@0
   140
#ifdef DEBUG_AUDIO
slouken@1662
   141
    fprintf (stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
slouken@0
   142
#endif
slouken@0
   143
}
slouken@0
   144
slouken@1662
   145
static Uint8 *
slouken@1662
   146
NAS_GetAudioBuf (_THIS)
slouken@0
   147
{
slouken@1662
   148
    return (this->hidden->mixbuf);
slouken@0
   149
}
slouken@0
   150
slouken@1662
   151
static void
slouken@1662
   152
NAS_CloseAudio (_THIS)
slouken@0
   153
{
slouken@1662
   154
    if (this->hidden->mixbuf != NULL) {
slouken@1662
   155
        SDL_FreeAudioMem (this->hidden->mixbuf);
slouken@1662
   156
        this->hidden->mixbuf = NULL;
slouken@1662
   157
    }
slouken@1662
   158
    if (this->hidden->aud) {
slouken@1662
   159
        AuCloseServer (this->hidden->aud);
slouken@1662
   160
        this->hidden->aud = 0;
slouken@1662
   161
    }
slouken@0
   162
}
slouken@0
   163
slouken@1662
   164
static unsigned char
slouken@1662
   165
sdlformat_to_auformat (unsigned int fmt)
slouken@0
   166
{
slouken@1662
   167
    switch (fmt) {
slouken@0
   168
    case AUDIO_U8:
slouken@1662
   169
        return AuFormatLinearUnsigned8;
slouken@0
   170
    case AUDIO_S8:
slouken@1662
   171
        return AuFormatLinearSigned8;
slouken@0
   172
    case AUDIO_U16LSB:
slouken@1662
   173
        return AuFormatLinearUnsigned16LSB;
slouken@0
   174
    case AUDIO_U16MSB:
slouken@1662
   175
        return AuFormatLinearUnsigned16MSB;
slouken@0
   176
    case AUDIO_S16LSB:
slouken@1662
   177
        return AuFormatLinearSigned16LSB;
slouken@0
   178
    case AUDIO_S16MSB:
slouken@1662
   179
        return AuFormatLinearSigned16MSB;
slouken@0
   180
    }
slouken@1662
   181
    return AuNone;
slouken@0
   182
}
slouken@0
   183
slouken@0
   184
static AuBool
slouken@1662
   185
event_handler (AuServer * aud, AuEvent * ev, AuEventHandlerRec * hnd)
slouken@0
   186
{
slouken@1662
   187
    switch (ev->type) {
slouken@1662
   188
    case AuEventTypeElementNotify:
slouken@1662
   189
        {
slouken@1662
   190
            AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
slouken@0
   191
slouken@1662
   192
            switch (event->kind) {
slouken@1662
   193
            case AuElementNotifyKindLowWater:
slouken@1662
   194
                if (this2->buf_free >= 0) {
slouken@1662
   195
                    this2->really += event->num_bytes;
slouken@1662
   196
                    gettimeofday (&this2->last_tv, 0);
slouken@1662
   197
                    this2->buf_free += event->num_bytes;
slouken@1662
   198
                } else {
slouken@1662
   199
                    this2->buf_free = event->num_bytes;
slouken@1662
   200
                }
slouken@1662
   201
                break;
slouken@1662
   202
            case AuElementNotifyKindState:
slouken@1662
   203
                switch (event->cur_state) {
slouken@1662
   204
                case AuStatePause:
slouken@1662
   205
                    if (event->reason != AuReasonUser) {
slouken@1662
   206
                        if (this2->buf_free >= 0) {
slouken@1662
   207
                            this2->really += event->num_bytes;
slouken@1662
   208
                            gettimeofday (&this2->last_tv, 0);
slouken@1662
   209
                            this2->buf_free += event->num_bytes;
slouken@1662
   210
                        } else {
slouken@1662
   211
                            this2->buf_free = event->num_bytes;
slouken@1662
   212
                        }
slouken@1662
   213
                    }
slouken@1662
   214
                    break;
slouken@1662
   215
                }
slouken@1662
   216
            }
slouken@1662
   217
        }
slouken@1662
   218
    }
slouken@1662
   219
    return AuTrue;
slouken@0
   220
}
slouken@0
   221
slouken@0
   222
static AuDeviceID
slouken@1662
   223
find_device (_THIS, int nch)
slouken@0
   224
{
slouken@1662
   225
    int i;
slouken@1662
   226
    for (i = 0; i < AuServerNumDevices (this->hidden->aud); i++) {
slouken@1662
   227
        if ((AuDeviceKind (AuServerDevice (this->hidden->aud, i)) ==
slouken@1662
   228
             AuComponentKindPhysicalOutput) &&
slouken@1662
   229
            AuDeviceNumTracks (AuServerDevice (this->hidden->aud, i)) ==
slouken@1662
   230
            nch) {
slouken@1662
   231
            return AuDeviceIdentifier (AuServerDevice (this->hidden->aud, i));
slouken@1662
   232
        }
slouken@1662
   233
    }
slouken@1662
   234
    return AuNone;
slouken@0
   235
}
slouken@0
   236
slouken@1662
   237
static int
slouken@1662
   238
NAS_OpenAudio (_THIS, SDL_AudioSpec * spec)
slouken@0
   239
{
slouken@1662
   240
    AuElement elms[3];
slouken@1662
   241
    int buffer_size;
slouken@1662
   242
    Uint16 test_format, format;
slouken@1662
   243
slouken@1662
   244
    this->hidden->mixbuf = NULL;
slouken@0
   245
slouken@1662
   246
    /* Try for a closest match on audio format */
slouken@1662
   247
    format = 0;
slouken@1662
   248
    for (test_format = SDL_FirstAudioFormat (spec->format);
slouken@1662
   249
         !format && test_format;) {
slouken@1662
   250
        format = sdlformat_to_auformat (test_format);
slouken@0
   251
slouken@1662
   252
        if (format == AuNone) {
slouken@1662
   253
            test_format = SDL_NextAudioFormat ();
slouken@1662
   254
        }
slouken@1662
   255
    }
slouken@1662
   256
    if (format == 0) {
slouken@1662
   257
        SDL_SetError ("Couldn't find any hardware audio formats");
slouken@1662
   258
        return (-1);
slouken@1662
   259
    }
slouken@1662
   260
    spec->format = test_format;
slouken@0
   261
slouken@1662
   262
    this->hidden->aud = AuOpenServer ("", 0, NULL, 0, NULL, NULL);
slouken@1662
   263
    if (this->hidden->aud == 0) {
slouken@1662
   264
        SDL_SetError ("Couldn't open connection to NAS server");
slouken@1662
   265
        return (-1);
slouken@1662
   266
    }
slouken@1662
   267
slouken@1662
   268
    this->hidden->dev = find_device (this, spec->channels);
slouken@1662
   269
    if ((this->hidden->dev == AuNone)
slouken@1662
   270
        || (!(this->hidden->flow = AuCreateFlow (this->hidden->aud, NULL)))) {
slouken@1662
   271
        AuCloseServer (this->hidden->aud);
slouken@1662
   272
        this->hidden->aud = 0;
slouken@1662
   273
        SDL_SetError
slouken@1662
   274
            ("Couldn't find a fitting playback device on NAS server");
slouken@1662
   275
        return (-1);
slouken@1662
   276
    }
slouken@0
   277
slouken@1662
   278
    buffer_size = spec->freq;
slouken@1662
   279
    if (buffer_size < 4096)
slouken@1662
   280
        buffer_size = 4096;
slouken@0
   281
slouken@1662
   282
    if (buffer_size > 32768)
slouken@1662
   283
        buffer_size = 32768;    /* So that the buffer won't get unmanageably big. */
slouken@0
   284
slouken@1662
   285
    /* Calculate the final parameters for this audio specification */
slouken@1662
   286
    SDL_CalculateAudioSpec (spec);
slouken@1662
   287
slouken@1662
   288
    this2 = this->hidden;
slouken@0
   289
slouken@1662
   290
    AuMakeElementImportClient (elms, spec->freq, format, spec->channels,
slouken@1662
   291
                               AuTrue, buffer_size, buffer_size / 4, 0, NULL);
slouken@1662
   292
    AuMakeElementExportDevice (elms + 1, 0, this->hidden->dev, spec->freq,
slouken@1662
   293
                               AuUnlimitedSamples, 0, NULL);
slouken@1662
   294
    AuSetElements (this->hidden->aud, this->hidden->flow, AuTrue, 2, elms,
slouken@1662
   295
                   NULL);
slouken@1662
   296
    AuRegisterEventHandler (this->hidden->aud, AuEventHandlerIDMask, 0,
slouken@1662
   297
                            this->hidden->flow, event_handler,
slouken@1662
   298
                            (AuPointer) NULL);
slouken@0
   299
slouken@1662
   300
    AuStartFlow (this->hidden->aud, this->hidden->flow, NULL);
slouken@0
   301
slouken@1662
   302
    /* Allocate mixing buffer */
slouken@1662
   303
    this->hidden->mixlen = spec->size;
slouken@1662
   304
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem (this->hidden->mixlen);
slouken@1662
   305
    if (this->hidden->mixbuf == NULL) {
slouken@1662
   306
        return (-1);
slouken@1662
   307
    }
slouken@1662
   308
    SDL_memset (this->hidden->mixbuf, spec->silence, spec->size);
slouken@0
   309
slouken@1662
   310
    /* Get the parent process id (we're the parent of the audio thread) */
slouken@1662
   311
    this->hidden->parent = getpid ();
slouken@0
   312
slouken@1662
   313
    /* We're ready to rock and roll. :-) */
slouken@1662
   314
    return (0);
slouken@0
   315
}
slouken@1662
   316
slouken@1662
   317
/* vi: set ts=4 sw=4 expandtab: */