src/audio/dsp/SDL_dspaudio.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 01 Jun 2013 21:09:36 +0200
changeset 7252 8ecb54eeaeec
parent 7191 75360622e65f
child 7719 31b5f9ff36ca
permissions -rw-r--r--
Corrected indentation of license.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@6044
    23
#if SDL_AUDIO_DRIVER_OSS
slouken@6044
    24
slouken@0
    25
/* Allow access to a raw mixing buffer */
slouken@0
    26
slouken@1895
    27
#include <stdio.h>              /* For perror() */
slouken@1895
    28
#include <string.h>             /* For strerror() */
slouken@0
    29
#include <errno.h>
slouken@0
    30
#include <unistd.h>
slouken@0
    31
#include <fcntl.h>
slouken@0
    32
#include <signal.h>
slouken@0
    33
#include <sys/time.h>
slouken@0
    34
#include <sys/ioctl.h>
slouken@0
    35
#include <sys/stat.h>
slouken@1361
    36
slouken@1361
    37
#if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H
slouken@94
    38
/* This is installed on some systems */
slouken@94
    39
#include <soundcard.h>
slouken@94
    40
#else
slouken@94
    41
/* This is recommended by OSS */
slouken@0
    42
#include <sys/soundcard.h>
slouken@94
    43
#endif
slouken@0
    44
slouken@1358
    45
#include "SDL_timer.h"
slouken@0
    46
#include "SDL_audio.h"
slouken@1361
    47
#include "../SDL_audiomem.h"
slouken@1361
    48
#include "../SDL_audio_c.h"
slouken@1361
    49
#include "../SDL_audiodev_c.h"
slouken@0
    50
#include "SDL_dspaudio.h"
slouken@0
    51
slouken@0
    52
icculus@5593
    53
static void
icculus@5601
    54
DSP_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
slouken@0
    55
{
icculus@5601
    56
    SDL_EnumUnixAudioDevices(iscapture, 0, NULL, addfn);
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
slouken@1895
    60
static void
icculus@2049
    61
DSP_CloseDevice(_THIS)
slouken@0
    62
{
icculus@2049
    63
    if (this->hidden != NULL) {
icculus@2049
    64
        if (this->hidden->mixbuf != NULL) {
icculus@2049
    65
            SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@2049
    66
            this->hidden->mixbuf = NULL;
icculus@2049
    67
        }
icculus@2049
    68
        if (this->hidden->audio_fd >= 0) {
icculus@2049
    69
            close(this->hidden->audio_fd);
icculus@2049
    70
            this->hidden->audio_fd = -1;
icculus@2049
    71
        }
icculus@2049
    72
        SDL_free(this->hidden);
icculus@2049
    73
        this->hidden = NULL;
slouken@1895
    74
    }
slouken@0
    75
}
slouken@0
    76
icculus@2049
    77
slouken@1895
    78
static int
icculus@2049
    79
DSP_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@0
    80
{
icculus@2049
    81
    const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
slouken@1895
    82
    int format;
slouken@1895
    83
    int value;
slouken@1895
    84
    int frag_spec;
icculus@1982
    85
    SDL_AudioFormat test_format;
slouken@0
    86
icculus@2049
    87
    /* We don't care what the devname is...we'll try to open anything. */
icculus@2049
    88
    /*  ...but default to first name in the list... */
icculus@2049
    89
    if (devname == NULL) {
icculus@5593
    90
        devname = SDL_GetAudioDeviceName(0, iscapture);
icculus@5593
    91
        if (devname == NULL) {
icculus@7038
    92
            return SDL_SetError("No such audio device");
icculus@2049
    93
        }
icculus@2049
    94
    }
icculus@2049
    95
icculus@2071
    96
    /* Make sure fragment size stays a power of 2, or OSS fails. */
icculus@2071
    97
    /* I don't know which of these are actually legal values, though... */
icculus@2071
    98
    if (this->spec.channels > 8)
icculus@2071
    99
        this->spec.channels = 8;
icculus@2071
   100
    else if (this->spec.channels > 4)
icculus@2071
   101
        this->spec.channels = 4;
icculus@2071
   102
    else if (this->spec.channels > 2)
icculus@2071
   103
        this->spec.channels = 2;
icculus@2071
   104
icculus@2049
   105
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   106
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   107
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   108
    if (this->hidden == NULL) {
icculus@7038
   109
        return SDL_OutOfMemory();
icculus@2049
   110
    }
icculus@2049
   111
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   112
slouken@1895
   113
    /* Open the audio device */
icculus@2049
   114
    this->hidden->audio_fd = open(devname, flags, 0);
icculus@2049
   115
    if (this->hidden->audio_fd < 0) {
icculus@2049
   116
        DSP_CloseDevice(this);
icculus@7038
   117
        return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
slouken@1895
   118
    }
icculus@2049
   119
    this->hidden->mixbuf = NULL;
slouken@968
   120
slouken@1895
   121
    /* Make the file descriptor use blocking writes with fcntl() */
slouken@1895
   122
    {
icculus@2049
   123
        long ctlflags;
icculus@2049
   124
        ctlflags = fcntl(this->hidden->audio_fd, F_GETFL);
icculus@2049
   125
        ctlflags &= ~O_NONBLOCK;
icculus@2049
   126
        if (fcntl(this->hidden->audio_fd, F_SETFL, ctlflags) < 0) {
icculus@2049
   127
            DSP_CloseDevice(this);
icculus@7038
   128
            return SDL_SetError("Couldn't set audio blocking mode");
slouken@1895
   129
        }
slouken@1895
   130
    }
slouken@968
   131
slouken@1895
   132
    /* Get a list of supported hardware formats */
icculus@2049
   133
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) {
slouken@1895
   134
        perror("SNDCTL_DSP_GETFMTS");
icculus@2049
   135
        DSP_CloseDevice(this);
icculus@7038
   136
        return SDL_SetError("Couldn't get audio format list");
slouken@1895
   137
    }
slouken@968
   138
slouken@1895
   139
    /* Try for a closest match on audio format */
slouken@1895
   140
    format = 0;
icculus@2049
   141
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@1895
   142
         !format && test_format;) {
slouken@968
   143
#ifdef DEBUG_AUDIO
slouken@1895
   144
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@968
   145
#endif
slouken@1895
   146
        switch (test_format) {
slouken@1895
   147
        case AUDIO_U8:
slouken@1895
   148
            if (value & AFMT_U8) {
slouken@1895
   149
                format = AFMT_U8;
slouken@1895
   150
            }
slouken@1895
   151
            break;
slouken@1895
   152
        case AUDIO_S16LSB:
slouken@1895
   153
            if (value & AFMT_S16_LE) {
slouken@1895
   154
                format = AFMT_S16_LE;
slouken@1895
   155
            }
slouken@1895
   156
            break;
slouken@1895
   157
        case AUDIO_S16MSB:
slouken@1895
   158
            if (value & AFMT_S16_BE) {
slouken@1895
   159
                format = AFMT_S16_BE;
slouken@1895
   160
            }
slouken@1895
   161
            break;
slouken@968
   162
#if 0
slouken@968
   163
/*
slouken@7191
   164
 * These formats are not used by any real life systems so they are not
slouken@968
   165
 * needed here.
slouken@968
   166
 */
slouken@1895
   167
        case AUDIO_S8:
slouken@1895
   168
            if (value & AFMT_S8) {
slouken@1895
   169
                format = AFMT_S8;
slouken@1895
   170
            }
slouken@1895
   171
            break;
slouken@1895
   172
        case AUDIO_U16LSB:
slouken@1895
   173
            if (value & AFMT_U16_LE) {
slouken@1895
   174
                format = AFMT_U16_LE;
slouken@1895
   175
            }
slouken@1895
   176
            break;
slouken@1895
   177
        case AUDIO_U16MSB:
slouken@1895
   178
            if (value & AFMT_U16_BE) {
slouken@1895
   179
                format = AFMT_U16_BE;
slouken@1895
   180
            }
slouken@1895
   181
            break;
slouken@968
   182
#endif
slouken@1895
   183
        default:
slouken@1895
   184
            format = 0;
slouken@1895
   185
            break;
slouken@1895
   186
        }
slouken@1895
   187
        if (!format) {
slouken@1895
   188
            test_format = SDL_NextAudioFormat();
slouken@1895
   189
        }
slouken@1895
   190
    }
slouken@1895
   191
    if (format == 0) {
icculus@2049
   192
        DSP_CloseDevice(this);
icculus@7038
   193
        return SDL_SetError("Couldn't find any hardware audio formats");
slouken@1895
   194
    }
icculus@2049
   195
    this->spec.format = test_format;
slouken@968
   196
slouken@1895
   197
    /* Set the audio format */
slouken@1895
   198
    value = format;
slouken@2060
   199
    if ((ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
slouken@2060
   200
        (value != format)) {
slouken@1895
   201
        perror("SNDCTL_DSP_SETFMT");
icculus@2049
   202
        DSP_CloseDevice(this);
icculus@7038
   203
        return SDL_SetError("Couldn't set audio format");
slouken@1895
   204
    }
slouken@968
   205
slouken@1895
   206
    /* Set the number of channels of output */
icculus@2049
   207
    value = this->spec.channels;
icculus@2049
   208
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) {
slouken@1895
   209
        perror("SNDCTL_DSP_CHANNELS");
icculus@2049
   210
        DSP_CloseDevice(this);
icculus@7038
   211
        return SDL_SetError("Cannot set the number of channels");
slouken@1895
   212
    }
icculus@2049
   213
    this->spec.channels = value;
slouken@968
   214
slouken@1895
   215
    /* Set the DSP frequency */
icculus@2049
   216
    value = this->spec.freq;
icculus@2049
   217
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SPEED, &value) < 0) {
slouken@1895
   218
        perror("SNDCTL_DSP_SPEED");
icculus@2049
   219
        DSP_CloseDevice(this);
icculus@7038
   220
        return SDL_SetError("Couldn't set audio frequency");
slouken@1895
   221
    }
icculus@2049
   222
    this->spec.freq = value;
slouken@0
   223
slouken@1895
   224
    /* Calculate the final parameters for this audio specification */
icculus@2049
   225
    SDL_CalculateAudioSpec(&this->spec);
slouken@0
   226
slouken@1895
   227
    /* Determine the power of two of the fragment size */
icculus@2049
   228
    for (frag_spec = 0; (0x01U << frag_spec) < this->spec.size; ++frag_spec);
icculus@2049
   229
    if ((0x01U << frag_spec) != this->spec.size) {
icculus@2049
   230
        DSP_CloseDevice(this);
icculus@7038
   231
        return SDL_SetError("Fragment size must be a power of two");
slouken@1895
   232
    }
slouken@1895
   233
    frag_spec |= 0x00020000;    /* two fragments, for low latency */
slouken@0
   234
slouken@1895
   235
    /* Set the audio buffering parameters */
slouken@0
   236
#ifdef DEBUG_AUDIO
slouken@1895
   237
    fprintf(stderr, "Requesting %d fragments of size %d\n",
slouken@1895
   238
            (frag_spec >> 16), 1 << (frag_spec & 0xFFFF));
slouken@0
   239
#endif
icculus@2049
   240
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) {
slouken@1895
   241
        perror("SNDCTL_DSP_SETFRAGMENT");
slouken@1895
   242
    }
slouken@0
   243
#ifdef DEBUG_AUDIO
slouken@1895
   244
    {
slouken@1895
   245
        audio_buf_info info;
icculus@2049
   246
        ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETOSPACE, &info);
slouken@1895
   247
        fprintf(stderr, "fragments = %d\n", info.fragments);
slouken@1895
   248
        fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
slouken@1895
   249
        fprintf(stderr, "fragsize = %d\n", info.fragsize);
slouken@1895
   250
        fprintf(stderr, "bytes = %d\n", info.bytes);
slouken@1895
   251
    }
slouken@0
   252
#endif
slouken@0
   253
slouken@1895
   254
    /* Allocate mixing buffer */
icculus@2049
   255
    this->hidden->mixlen = this->spec.size;
icculus@2049
   256
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@2049
   257
    if (this->hidden->mixbuf == NULL) {
icculus@2049
   258
        DSP_CloseDevice(this);
icculus@7038
   259
        return SDL_OutOfMemory();
slouken@1895
   260
    }
icculus@2049
   261
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@0
   262
slouken@1895
   263
    /* We're ready to rock and roll. :-) */
icculus@7038
   264
    return 0;
slouken@0
   265
}
slouken@1895
   266
icculus@2049
   267
icculus@2049
   268
static void
icculus@2049
   269
DSP_PlayDevice(_THIS)
icculus@2049
   270
{
icculus@2049
   271
    const Uint8 *mixbuf = this->hidden->mixbuf;
icculus@2049
   272
    const int mixlen = this->hidden->mixlen;
icculus@2049
   273
    if (write(this->hidden->audio_fd, mixbuf, mixlen) == -1) {
icculus@2049
   274
        perror("Audio write");
icculus@2049
   275
        this->enabled = 0;
icculus@2049
   276
    }
icculus@2049
   277
#ifdef DEBUG_AUDIO
icculus@2049
   278
    fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
icculus@2049
   279
#endif
icculus@2049
   280
}
icculus@2049
   281
icculus@2049
   282
static Uint8 *
icculus@2049
   283
DSP_GetDeviceBuf(_THIS)
icculus@2049
   284
{
icculus@2049
   285
    return (this->hidden->mixbuf);
icculus@2049
   286
}
icculus@2049
   287
icculus@2049
   288
static int
slouken@2060
   289
DSP_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   290
{
icculus@2049
   291
    /* Set the function pointers */
icculus@2049
   292
    impl->DetectDevices = DSP_DetectDevices;
icculus@2049
   293
    impl->OpenDevice = DSP_OpenDevice;
icculus@2049
   294
    impl->PlayDevice = DSP_PlayDevice;
icculus@2049
   295
    impl->GetDeviceBuf = DSP_GetDeviceBuf;
icculus@2049
   296
    impl->CloseDevice = DSP_CloseDevice;
icculus@2049
   297
icculus@3699
   298
    return 1;   /* this audio target is available. */
icculus@2049
   299
}
icculus@2049
   300
icculus@2049
   301
icculus@2049
   302
AudioBootStrap DSP_bootstrap = {
icculus@5594
   303
    "dsp", "OSS /dev/dsp standard audio", DSP_Init, 0
icculus@2049
   304
};
icculus@2049
   305
slouken@6044
   306
#endif /* SDL_AUDIO_DRIVER_OSS */
slouken@6044
   307
slouken@1895
   308
/* vi: set ts=4 sw=4 expandtab: */