src/audio/dsp/SDL_dspaudio.c
author Sam Lantinga
Sat, 06 Oct 2012 11:23:47 -0700
changeset 6565 1f3c0df426dc
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
permissions -rw-r--r--
When using Xinerama, XVidMode always works on screen 0. Otherwise use the real X11 screen.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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
icculus@2049
    52
icculus@2049
    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
icculus@2049
    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@2049
    92
            SDL_SetError("No such audio device");
icculus@2049
    93
            return 0;
icculus@2049
    94
        }
icculus@2049
    95
    }
icculus@2049
    96
icculus@2071
    97
    /* Make sure fragment size stays a power of 2, or OSS fails. */
icculus@2071
    98
    /* I don't know which of these are actually legal values, though... */
icculus@2071
    99
    if (this->spec.channels > 8)
icculus@2071
   100
        this->spec.channels = 8;
icculus@2071
   101
    else if (this->spec.channels > 4)
icculus@2071
   102
        this->spec.channels = 4;
icculus@2071
   103
    else if (this->spec.channels > 2)
icculus@2071
   104
        this->spec.channels = 2;
icculus@2071
   105
icculus@2049
   106
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   107
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   108
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   109
    if (this->hidden == NULL) {
icculus@2049
   110
        SDL_OutOfMemory();
icculus@2049
   111
        return 0;
icculus@2049
   112
    }
icculus@2049
   113
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   114
slouken@1895
   115
    /* Open the audio device */
icculus@2049
   116
    this->hidden->audio_fd = open(devname, flags, 0);
icculus@2049
   117
    if (this->hidden->audio_fd < 0) {
icculus@2049
   118
        DSP_CloseDevice(this);
icculus@2049
   119
        SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
icculus@2049
   120
        return 0;
slouken@1895
   121
    }
icculus@2049
   122
    this->hidden->mixbuf = NULL;
slouken@968
   123
slouken@1895
   124
    /* Make the file descriptor use blocking writes with fcntl() */
slouken@1895
   125
    {
icculus@2049
   126
        long ctlflags;
icculus@2049
   127
        ctlflags = fcntl(this->hidden->audio_fd, F_GETFL);
icculus@2049
   128
        ctlflags &= ~O_NONBLOCK;
icculus@2049
   129
        if (fcntl(this->hidden->audio_fd, F_SETFL, ctlflags) < 0) {
icculus@2049
   130
            DSP_CloseDevice(this);
slouken@1895
   131
            SDL_SetError("Couldn't set audio blocking mode");
icculus@2049
   132
            return 0;
slouken@1895
   133
        }
slouken@1895
   134
    }
slouken@968
   135
slouken@1895
   136
    /* Get a list of supported hardware formats */
icculus@2049
   137
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) {
slouken@1895
   138
        perror("SNDCTL_DSP_GETFMTS");
icculus@2049
   139
        DSP_CloseDevice(this);
slouken@1895
   140
        SDL_SetError("Couldn't get audio format list");
icculus@2049
   141
        return 0;
slouken@1895
   142
    }
slouken@968
   143
slouken@1895
   144
    /* Try for a closest match on audio format */
slouken@1895
   145
    format = 0;
icculus@2049
   146
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@1895
   147
         !format && test_format;) {
slouken@968
   148
#ifdef DEBUG_AUDIO
slouken@1895
   149
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@968
   150
#endif
slouken@1895
   151
        switch (test_format) {
slouken@1895
   152
        case AUDIO_U8:
slouken@1895
   153
            if (value & AFMT_U8) {
slouken@1895
   154
                format = AFMT_U8;
slouken@1895
   155
            }
slouken@1895
   156
            break;
slouken@1895
   157
        case AUDIO_S16LSB:
slouken@1895
   158
            if (value & AFMT_S16_LE) {
slouken@1895
   159
                format = AFMT_S16_LE;
slouken@1895
   160
            }
slouken@1895
   161
            break;
slouken@1895
   162
        case AUDIO_S16MSB:
slouken@1895
   163
            if (value & AFMT_S16_BE) {
slouken@1895
   164
                format = AFMT_S16_BE;
slouken@1895
   165
            }
slouken@1895
   166
            break;
slouken@968
   167
#if 0
slouken@968
   168
/*
slouken@968
   169
 * These formats are not used by any real life systems so they are not 
slouken@968
   170
 * needed here.
slouken@968
   171
 */
slouken@1895
   172
        case AUDIO_S8:
slouken@1895
   173
            if (value & AFMT_S8) {
slouken@1895
   174
                format = AFMT_S8;
slouken@1895
   175
            }
slouken@1895
   176
            break;
slouken@1895
   177
        case AUDIO_U16LSB:
slouken@1895
   178
            if (value & AFMT_U16_LE) {
slouken@1895
   179
                format = AFMT_U16_LE;
slouken@1895
   180
            }
slouken@1895
   181
            break;
slouken@1895
   182
        case AUDIO_U16MSB:
slouken@1895
   183
            if (value & AFMT_U16_BE) {
slouken@1895
   184
                format = AFMT_U16_BE;
slouken@1895
   185
            }
slouken@1895
   186
            break;
slouken@968
   187
#endif
slouken@1895
   188
        default:
slouken@1895
   189
            format = 0;
slouken@1895
   190
            break;
slouken@1895
   191
        }
slouken@1895
   192
        if (!format) {
slouken@1895
   193
            test_format = SDL_NextAudioFormat();
slouken@1895
   194
        }
slouken@1895
   195
    }
slouken@1895
   196
    if (format == 0) {
icculus@2049
   197
        DSP_CloseDevice(this);
slouken@1895
   198
        SDL_SetError("Couldn't find any hardware audio formats");
icculus@2049
   199
        return 0;
slouken@1895
   200
    }
icculus@2049
   201
    this->spec.format = test_format;
slouken@968
   202
slouken@1895
   203
    /* Set the audio format */
slouken@1895
   204
    value = format;
slouken@2060
   205
    if ((ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
slouken@2060
   206
        (value != format)) {
slouken@1895
   207
        perror("SNDCTL_DSP_SETFMT");
icculus@2049
   208
        DSP_CloseDevice(this);
slouken@1895
   209
        SDL_SetError("Couldn't set audio format");
icculus@2049
   210
        return 0;
slouken@1895
   211
    }
slouken@968
   212
slouken@1895
   213
    /* Set the number of channels of output */
icculus@2049
   214
    value = this->spec.channels;
icculus@2049
   215
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) {
slouken@1895
   216
        perror("SNDCTL_DSP_CHANNELS");
icculus@2049
   217
        DSP_CloseDevice(this);
slouken@1895
   218
        SDL_SetError("Cannot set the number of channels");
icculus@2049
   219
        return 0;
slouken@1895
   220
    }
icculus@2049
   221
    this->spec.channels = value;
slouken@968
   222
slouken@1895
   223
    /* Set the DSP frequency */
icculus@2049
   224
    value = this->spec.freq;
icculus@2049
   225
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SPEED, &value) < 0) {
slouken@1895
   226
        perror("SNDCTL_DSP_SPEED");
icculus@2049
   227
        DSP_CloseDevice(this);
slouken@1895
   228
        SDL_SetError("Couldn't set audio frequency");
icculus@2049
   229
        return 0;
slouken@1895
   230
    }
icculus@2049
   231
    this->spec.freq = value;
slouken@0
   232
slouken@1895
   233
    /* Calculate the final parameters for this audio specification */
icculus@2049
   234
    SDL_CalculateAudioSpec(&this->spec);
slouken@0
   235
slouken@1895
   236
    /* Determine the power of two of the fragment size */
icculus@2049
   237
    for (frag_spec = 0; (0x01U << frag_spec) < this->spec.size; ++frag_spec);
icculus@2049
   238
    if ((0x01U << frag_spec) != this->spec.size) {
icculus@2049
   239
        DSP_CloseDevice(this);
slouken@1895
   240
        SDL_SetError("Fragment size must be a power of two");
icculus@2049
   241
        return 0;
slouken@1895
   242
    }
slouken@1895
   243
    frag_spec |= 0x00020000;    /* two fragments, for low latency */
slouken@0
   244
slouken@1895
   245
    /* Set the audio buffering parameters */
slouken@0
   246
#ifdef DEBUG_AUDIO
slouken@1895
   247
    fprintf(stderr, "Requesting %d fragments of size %d\n",
slouken@1895
   248
            (frag_spec >> 16), 1 << (frag_spec & 0xFFFF));
slouken@0
   249
#endif
icculus@2049
   250
    if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) {
slouken@1895
   251
        perror("SNDCTL_DSP_SETFRAGMENT");
slouken@1895
   252
    }
slouken@0
   253
#ifdef DEBUG_AUDIO
slouken@1895
   254
    {
slouken@1895
   255
        audio_buf_info info;
icculus@2049
   256
        ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETOSPACE, &info);
slouken@1895
   257
        fprintf(stderr, "fragments = %d\n", info.fragments);
slouken@1895
   258
        fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
slouken@1895
   259
        fprintf(stderr, "fragsize = %d\n", info.fragsize);
slouken@1895
   260
        fprintf(stderr, "bytes = %d\n", info.bytes);
slouken@1895
   261
    }
slouken@0
   262
#endif
slouken@0
   263
slouken@1895
   264
    /* Allocate mixing buffer */
icculus@2049
   265
    this->hidden->mixlen = this->spec.size;
icculus@2049
   266
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@2049
   267
    if (this->hidden->mixbuf == NULL) {
icculus@2049
   268
        DSP_CloseDevice(this);
icculus@2049
   269
        SDL_OutOfMemory();
icculus@2049
   270
        return 0;
slouken@1895
   271
    }
icculus@2049
   272
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@0
   273
slouken@1895
   274
    /* We're ready to rock and roll. :-) */
icculus@2049
   275
    return 1;
icculus@2049
   276
}
icculus@2049
   277
icculus@2049
   278
icculus@2049
   279
static void
icculus@2049
   280
DSP_PlayDevice(_THIS)
icculus@2049
   281
{
icculus@2049
   282
    const Uint8 *mixbuf = this->hidden->mixbuf;
icculus@2049
   283
    const int mixlen = this->hidden->mixlen;
icculus@2049
   284
    if (write(this->hidden->audio_fd, mixbuf, mixlen) == -1) {
icculus@2049
   285
        perror("Audio write");
icculus@2049
   286
        this->enabled = 0;
icculus@2049
   287
    }
icculus@2049
   288
#ifdef DEBUG_AUDIO
icculus@2049
   289
    fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
icculus@2049
   290
#endif
slouken@0
   291
}
slouken@1895
   292
icculus@2049
   293
static Uint8 *
icculus@2049
   294
DSP_GetDeviceBuf(_THIS)
icculus@2049
   295
{
icculus@2049
   296
    return (this->hidden->mixbuf);
icculus@2049
   297
}
icculus@2049
   298
icculus@2049
   299
static int
slouken@2060
   300
DSP_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   301
{
icculus@2049
   302
    /* Set the function pointers */
icculus@2049
   303
    impl->DetectDevices = DSP_DetectDevices;
icculus@2049
   304
    impl->OpenDevice = DSP_OpenDevice;
icculus@2049
   305
    impl->PlayDevice = DSP_PlayDevice;
icculus@2049
   306
    impl->GetDeviceBuf = DSP_GetDeviceBuf;
icculus@2049
   307
    impl->CloseDevice = DSP_CloseDevice;
icculus@2049
   308
icculus@3699
   309
    return 1;   /* this audio target is available. */
icculus@2049
   310
}
icculus@2049
   311
icculus@2049
   312
icculus@2049
   313
AudioBootStrap DSP_bootstrap = {
icculus@5594
   314
    "dsp", "OSS /dev/dsp standard audio", DSP_Init, 0
icculus@2049
   315
};
icculus@2049
   316
slouken@6044
   317
#endif /* SDL_AUDIO_DRIVER_OSS */
slouken@6044
   318
slouken@1895
   319
/* vi: set ts=4 sw=4 expandtab: */