src/audio/dsp/SDL_dspaudio.c
author Sam Lantinga
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 5593 ab22ca13c47f
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@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* Allow access to a raw mixing buffer */
slouken@0
    24
slouken@1895
    25
#include <stdio.h>              /* For perror() */
slouken@1895
    26
#include <string.h>             /* For strerror() */
slouken@0
    27
#include <errno.h>
slouken@0
    28
#include <unistd.h>
slouken@0
    29
#include <fcntl.h>
slouken@0
    30
#include <signal.h>
slouken@0
    31
#include <sys/time.h>
slouken@0
    32
#include <sys/ioctl.h>
slouken@0
    33
#include <sys/stat.h>
slouken@1361
    34
slouken@1361
    35
#if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H
slouken@94
    36
/* This is installed on some systems */
slouken@94
    37
#include <soundcard.h>
slouken@94
    38
#else
slouken@94
    39
/* This is recommended by OSS */
slouken@0
    40
#include <sys/soundcard.h>
slouken@94
    41
#endif
slouken@0
    42
slouken@1358
    43
#include "SDL_timer.h"
slouken@0
    44
#include "SDL_audio.h"
slouken@1361
    45
#include "../SDL_audiomem.h"
slouken@1361
    46
#include "../SDL_audio_c.h"
slouken@1361
    47
#include "../SDL_audiodev_c.h"
slouken@0
    48
#include "SDL_dspaudio.h"
slouken@0
    49
slouken@0
    50
/* The tag name used by DSP audio */
slouken@0
    51
#define DSP_DRIVER_NAME         "dsp"
slouken@0
    52
slouken@0
    53
/* Open the audio device for playback, and don't block if busy */
icculus@2049
    54
#define OPEN_FLAGS_OUTPUT    (O_WRONLY|O_NONBLOCK)
icculus@2049
    55
#define OPEN_FLAGS_INPUT    (O_RDONLY|O_NONBLOCK)
slouken@0
    56
icculus@2049
    57
static char **outputDevices = NULL;
icculus@2049
    58
static int outputDeviceCount = 0;
icculus@2049
    59
static char **inputDevices = NULL;
icculus@2049
    60
static int inputDeviceCount = 0;
slouken@0
    61
icculus@2049
    62
static inline void
icculus@2049
    63
free_device_list(char ***devs, int *count)
icculus@2049
    64
{
icculus@2049
    65
    SDL_FreeUnixAudioDevices(devs, count);
slouken@0
    66
}
slouken@0
    67
icculus@2049
    68
static inline void
icculus@2049
    69
build_device_list(int iscapture, char ***devs, int *count)
icculus@2049
    70
{
icculus@2049
    71
    const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
icculus@2049
    72
    free_device_list(devs, count);
icculus@2049
    73
    SDL_EnumUnixAudioDevices(flags, 0, NULL, devs, count);
icculus@2049
    74
}
icculus@2049
    75
icculus@2049
    76
static inline void
icculus@2049
    77
build_device_lists(void)
slouken@0
    78
{
icculus@2049
    79
    build_device_list(0, &outputDevices, &outputDeviceCount);
icculus@2049
    80
    build_device_list(1, &inputDevices, &inputDeviceCount);
icculus@2049
    81
}
icculus@2049
    82
icculus@2049
    83
icculus@2049
    84
static inline void
icculus@2049
    85
free_device_lists(void)
icculus@2049
    86
{
icculus@2049
    87
    free_device_list(&outputDevices, &outputDeviceCount);
icculus@2049
    88
    free_device_list(&inputDevices, &inputDeviceCount);
icculus@2049
    89
}
icculus@2049
    90
icculus@2049
    91
icculus@2049
    92
static void
icculus@2049
    93
DSP_Deinitialize(void)
icculus@2049
    94
{
icculus@2049
    95
    free_device_lists();
slouken@0
    96
}
slouken@0
    97
icculus@2049
    98
icculus@2049
    99
static int
icculus@2049
   100
DSP_DetectDevices(int iscapture)
slouken@0
   101
{
icculus@2049
   102
    if (iscapture) {
icculus@2049
   103
        build_device_list(1, &inputDevices, &inputDeviceCount);
icculus@2049
   104
        return inputDeviceCount;
icculus@2049
   105
    } else {
icculus@2049
   106
        build_device_list(0, &outputDevices, &outputDeviceCount);
icculus@2049
   107
        return outputDeviceCount;
slouken@1895
   108
    }
slouken@0
   109
slouken@2060
   110
    return 0;                   /* shouldn't ever hit this. */
slouken@0
   111
}
slouken@0
   112
icculus@2049
   113
static const char *
icculus@2049
   114
DSP_GetDeviceName(int index, int iscapture)
icculus@2049
   115
{
icculus@2049
   116
    if ((iscapture) && (index < inputDeviceCount)) {
icculus@2049
   117
        return inputDevices[index];
icculus@2049
   118
    } else if ((!iscapture) && (index < outputDeviceCount)) {
icculus@2049
   119
        return outputDevices[index];
icculus@2049
   120
    }
slouken@0
   121
icculus@2049
   122
    SDL_SetError("No such device");
icculus@2049
   123
    return NULL;
slouken@0
   124
}
slouken@0
   125
icculus@2049
   126
slouken@1895
   127
static void
icculus@2049
   128
DSP_CloseDevice(_THIS)
slouken@0
   129
{
icculus@2049
   130
    if (this->hidden != NULL) {
icculus@2049
   131
        if (this->hidden->mixbuf != NULL) {
icculus@2049
   132
            SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@2049
   133
            this->hidden->mixbuf = NULL;
icculus@2049
   134
        }
icculus@2049
   135
        if (this->hidden->audio_fd >= 0) {
icculus@2049
   136
            close(this->hidden->audio_fd);
icculus@2049
   137
            this->hidden->audio_fd = -1;
icculus@2049
   138
        }
icculus@2049
   139
        SDL_free(this->hidden);
icculus@2049
   140
        this->hidden = NULL;
slouken@1895
   141
    }
slouken@0
   142
}
slouken@0
   143
icculus@2049
   144
slouken@1895
   145
static int
icculus@2049
   146
DSP_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@0
   147
{
icculus@2049
   148
    const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
slouken@1895
   149
    int format;
slouken@1895
   150
    int value;
slouken@1895
   151
    int frag_spec;
icculus@1982
   152
    SDL_AudioFormat test_format;
slouken@0
   153
icculus@2049
   154
    /* We don't care what the devname is...we'll try to open anything. */
icculus@2049
   155
    /*  ...but default to first name in the list... */
icculus@2049
   156
    if (devname == NULL) {
slouken@2060
   157
        if (((iscapture) && (inputDeviceCount == 0)) ||
slouken@2060
   158
            ((!iscapture) && (outputDeviceCount == 0))) {
icculus@2049
   159
            SDL_SetError("No such audio device");
icculus@2049
   160
            return 0;
icculus@2049
   161
        }
icculus@2049
   162
        devname = ((iscapture) ? inputDevices[0] : outputDevices[0]);
icculus@2049
   163
    }
icculus@2049
   164
icculus@2071
   165
    /* Make sure fragment size stays a power of 2, or OSS fails. */
icculus@2071
   166
    /* I don't know which of these are actually legal values, though... */
icculus@2071
   167
    if (this->spec.channels > 8)
icculus@2071
   168
        this->spec.channels = 8;
icculus@2071
   169
    else if (this->spec.channels > 4)
icculus@2071
   170
        this->spec.channels = 4;
icculus@2071
   171
    else if (this->spec.channels > 2)
icculus@2071
   172
        this->spec.channels = 2;
icculus@2071
   173
icculus@2049
   174
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   175
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   176
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   177
    if (this->hidden == NULL) {
icculus@2049
   178
        SDL_OutOfMemory();
icculus@2049
   179
        return 0;
icculus@2049
   180
    }
icculus@2049
   181
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   182
slouken@1895
   183
    /* Open the audio device */
icculus@2049
   184
    this->hidden->audio_fd = open(devname, flags, 0);
icculus@2049
   185
    if (this->hidden->audio_fd < 0) {
icculus@2049
   186
        DSP_CloseDevice(this);
icculus@2049
   187
        SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
icculus@2049
   188
        return 0;
slouken@1895
   189
    }
icculus@2049
   190
    this->hidden->mixbuf = NULL;
slouken@968
   191
slouken@1895
   192
    /* Make the file descriptor use blocking writes with fcntl() */
slouken@1895
   193
    {
icculus@2049
   194
        long ctlflags;
icculus@2049
   195
        ctlflags = fcntl(this->hidden->audio_fd, F_GETFL);
icculus@2049
   196
        ctlflags &= ~O_NONBLOCK;
icculus@2049
   197
        if (fcntl(this->hidden->audio_fd, F_SETFL, ctlflags) < 0) {
icculus@2049
   198
            DSP_CloseDevice(this);
slouken@1895
   199
            SDL_SetError("Couldn't set audio blocking mode");
icculus@2049
   200
            return 0;
slouken@1895
   201
        }
slouken@1895
   202
    }
slouken@968
   203
slouken@1895
   204
    /* Get a list of supported hardware formats */
icculus@2049
   205
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) {
slouken@1895
   206
        perror("SNDCTL_DSP_GETFMTS");
icculus@2049
   207
        DSP_CloseDevice(this);
slouken@1895
   208
        SDL_SetError("Couldn't get audio format list");
icculus@2049
   209
        return 0;
slouken@1895
   210
    }
slouken@968
   211
slouken@1895
   212
    /* Try for a closest match on audio format */
slouken@1895
   213
    format = 0;
icculus@2049
   214
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@1895
   215
         !format && test_format;) {
slouken@968
   216
#ifdef DEBUG_AUDIO
slouken@1895
   217
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@968
   218
#endif
slouken@1895
   219
        switch (test_format) {
slouken@1895
   220
        case AUDIO_U8:
slouken@1895
   221
            if (value & AFMT_U8) {
slouken@1895
   222
                format = AFMT_U8;
slouken@1895
   223
            }
slouken@1895
   224
            break;
slouken@1895
   225
        case AUDIO_S16LSB:
slouken@1895
   226
            if (value & AFMT_S16_LE) {
slouken@1895
   227
                format = AFMT_S16_LE;
slouken@1895
   228
            }
slouken@1895
   229
            break;
slouken@1895
   230
        case AUDIO_S16MSB:
slouken@1895
   231
            if (value & AFMT_S16_BE) {
slouken@1895
   232
                format = AFMT_S16_BE;
slouken@1895
   233
            }
slouken@1895
   234
            break;
slouken@968
   235
#if 0
slouken@968
   236
/*
slouken@968
   237
 * These formats are not used by any real life systems so they are not 
slouken@968
   238
 * needed here.
slouken@968
   239
 */
slouken@1895
   240
        case AUDIO_S8:
slouken@1895
   241
            if (value & AFMT_S8) {
slouken@1895
   242
                format = AFMT_S8;
slouken@1895
   243
            }
slouken@1895
   244
            break;
slouken@1895
   245
        case AUDIO_U16LSB:
slouken@1895
   246
            if (value & AFMT_U16_LE) {
slouken@1895
   247
                format = AFMT_U16_LE;
slouken@1895
   248
            }
slouken@1895
   249
            break;
slouken@1895
   250
        case AUDIO_U16MSB:
slouken@1895
   251
            if (value & AFMT_U16_BE) {
slouken@1895
   252
                format = AFMT_U16_BE;
slouken@1895
   253
            }
slouken@1895
   254
            break;
slouken@968
   255
#endif
slouken@1895
   256
        default:
slouken@1895
   257
            format = 0;
slouken@1895
   258
            break;
slouken@1895
   259
        }
slouken@1895
   260
        if (!format) {
slouken@1895
   261
            test_format = SDL_NextAudioFormat();
slouken@1895
   262
        }
slouken@1895
   263
    }
slouken@1895
   264
    if (format == 0) {
icculus@2049
   265
        DSP_CloseDevice(this);
slouken@1895
   266
        SDL_SetError("Couldn't find any hardware audio formats");
icculus@2049
   267
        return 0;
slouken@1895
   268
    }
icculus@2049
   269
    this->spec.format = test_format;
slouken@968
   270
slouken@1895
   271
    /* Set the audio format */
slouken@1895
   272
    value = format;
slouken@2060
   273
    if ((ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
slouken@2060
   274
        (value != format)) {
slouken@1895
   275
        perror("SNDCTL_DSP_SETFMT");
icculus@2049
   276
        DSP_CloseDevice(this);
slouken@1895
   277
        SDL_SetError("Couldn't set audio format");
icculus@2049
   278
        return 0;
slouken@1895
   279
    }
slouken@968
   280
slouken@1895
   281
    /* Set the number of channels of output */
icculus@2049
   282
    value = this->spec.channels;
icculus@2049
   283
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) {
slouken@1895
   284
        perror("SNDCTL_DSP_CHANNELS");
icculus@2049
   285
        DSP_CloseDevice(this);
slouken@1895
   286
        SDL_SetError("Cannot set the number of channels");
icculus@2049
   287
        return 0;
slouken@1895
   288
    }
icculus@2049
   289
    this->spec.channels = value;
slouken@968
   290
slouken@1895
   291
    /* Set the DSP frequency */
icculus@2049
   292
    value = this->spec.freq;
icculus@2049
   293
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SPEED, &value) < 0) {
slouken@1895
   294
        perror("SNDCTL_DSP_SPEED");
icculus@2049
   295
        DSP_CloseDevice(this);
slouken@1895
   296
        SDL_SetError("Couldn't set audio frequency");
icculus@2049
   297
        return 0;
slouken@1895
   298
    }
icculus@2049
   299
    this->spec.freq = value;
slouken@0
   300
slouken@1895
   301
    /* Calculate the final parameters for this audio specification */
icculus@2049
   302
    SDL_CalculateAudioSpec(&this->spec);
slouken@0
   303
slouken@1895
   304
    /* Determine the power of two of the fragment size */
icculus@2049
   305
    for (frag_spec = 0; (0x01U << frag_spec) < this->spec.size; ++frag_spec);
icculus@2049
   306
    if ((0x01U << frag_spec) != this->spec.size) {
icculus@2049
   307
        DSP_CloseDevice(this);
slouken@1895
   308
        SDL_SetError("Fragment size must be a power of two");
icculus@2049
   309
        return 0;
slouken@1895
   310
    }
slouken@1895
   311
    frag_spec |= 0x00020000;    /* two fragments, for low latency */
slouken@0
   312
slouken@1895
   313
    /* Set the audio buffering parameters */
slouken@0
   314
#ifdef DEBUG_AUDIO
slouken@1895
   315
    fprintf(stderr, "Requesting %d fragments of size %d\n",
slouken@1895
   316
            (frag_spec >> 16), 1 << (frag_spec & 0xFFFF));
slouken@0
   317
#endif
icculus@2049
   318
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) {
slouken@1895
   319
        perror("SNDCTL_DSP_SETFRAGMENT");
slouken@1895
   320
    }
slouken@0
   321
#ifdef DEBUG_AUDIO
slouken@1895
   322
    {
slouken@1895
   323
        audio_buf_info info;
icculus@2049
   324
        ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETOSPACE, &info);
slouken@1895
   325
        fprintf(stderr, "fragments = %d\n", info.fragments);
slouken@1895
   326
        fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
slouken@1895
   327
        fprintf(stderr, "fragsize = %d\n", info.fragsize);
slouken@1895
   328
        fprintf(stderr, "bytes = %d\n", info.bytes);
slouken@1895
   329
    }
slouken@0
   330
#endif
slouken@0
   331
slouken@1895
   332
    /* Allocate mixing buffer */
icculus@2049
   333
    this->hidden->mixlen = this->spec.size;
icculus@2049
   334
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@2049
   335
    if (this->hidden->mixbuf == NULL) {
icculus@2049
   336
        DSP_CloseDevice(this);
icculus@2049
   337
        SDL_OutOfMemory();
icculus@2049
   338
        return 0;
slouken@1895
   339
    }
icculus@2049
   340
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@0
   341
slouken@1895
   342
    /* We're ready to rock and roll. :-) */
icculus@2049
   343
    return 1;
icculus@2049
   344
}
icculus@2049
   345
icculus@2049
   346
icculus@2049
   347
static void
icculus@2049
   348
DSP_PlayDevice(_THIS)
icculus@2049
   349
{
icculus@2049
   350
    const Uint8 *mixbuf = this->hidden->mixbuf;
icculus@2049
   351
    const int mixlen = this->hidden->mixlen;
icculus@2049
   352
    if (write(this->hidden->audio_fd, mixbuf, mixlen) == -1) {
icculus@2049
   353
        perror("Audio write");
icculus@2049
   354
        this->enabled = 0;
icculus@2049
   355
    }
icculus@2049
   356
#ifdef DEBUG_AUDIO
icculus@2049
   357
    fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
icculus@2049
   358
#endif
slouken@0
   359
}
slouken@1895
   360
icculus@2049
   361
static Uint8 *
icculus@2049
   362
DSP_GetDeviceBuf(_THIS)
icculus@2049
   363
{
icculus@2049
   364
    return (this->hidden->mixbuf);
icculus@2049
   365
}
icculus@2049
   366
icculus@2049
   367
static int
slouken@2060
   368
DSP_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   369
{
icculus@2049
   370
    /* Set the function pointers */
icculus@2049
   371
    impl->DetectDevices = DSP_DetectDevices;
icculus@2049
   372
    impl->GetDeviceName = DSP_GetDeviceName;
icculus@2049
   373
    impl->OpenDevice = DSP_OpenDevice;
icculus@2049
   374
    impl->PlayDevice = DSP_PlayDevice;
icculus@2049
   375
    impl->GetDeviceBuf = DSP_GetDeviceBuf;
icculus@2049
   376
    impl->CloseDevice = DSP_CloseDevice;
icculus@2049
   377
    impl->Deinitialize = DSP_Deinitialize;
icculus@2049
   378
icculus@2049
   379
    build_device_lists();
icculus@3699
   380
icculus@3699
   381
    return 1;   /* this audio target is available. */
icculus@2049
   382
}
icculus@2049
   383
icculus@2049
   384
icculus@2049
   385
AudioBootStrap DSP_bootstrap = {
icculus@2049
   386
    DSP_DRIVER_NAME, "OSS /dev/dsp standard audio", DSP_Init, 0
icculus@2049
   387
};
icculus@2049
   388
slouken@1895
   389
/* vi: set ts=4 sw=4 expandtab: */