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