src/audio/sun/SDL_sunaudio.c
author Sam Lantinga
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1464 af30090c0330
child 2015 152dcc2f089f
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Allow access to a raw mixing buffer */
slouken@0
    25
slouken@0
    26
#include <fcntl.h>
slouken@0
    27
#include <errno.h>
slouken@1402
    28
#ifdef __NETBSD__
slouken@0
    29
#include <sys/ioctl.h>
slouken@0
    30
#include <sys/audioio.h>
slouken@0
    31
#endif
slouken@0
    32
#ifdef __SVR4
slouken@0
    33
#include <sys/audioio.h>
slouken@0
    34
#else
slouken@0
    35
#include <sys/time.h>
slouken@0
    36
#include <sys/types.h>
slouken@0
    37
#endif
slouken@0
    38
#include <unistd.h>
slouken@0
    39
slouken@1358
    40
#include "SDL_timer.h"
slouken@0
    41
#include "SDL_audio.h"
slouken@1361
    42
#include "../SDL_audiomem.h"
slouken@1361
    43
#include "../SDL_audio_c.h"
slouken@1361
    44
#include "../SDL_audiodev_c.h"
slouken@0
    45
#include "SDL_sunaudio.h"
slouken@0
    46
slouken@0
    47
/* Open the audio device for playback, and don't block if busy */
slouken@0
    48
#define OPEN_FLAGS	(O_WRONLY|O_NONBLOCK)
slouken@0
    49
slouken@0
    50
/* Audio driver functions */
slouken@1895
    51
static int DSP_OpenAudio(_THIS, SDL_AudioSpec * spec);
slouken@0
    52
static void DSP_WaitAudio(_THIS);
slouken@0
    53
static void DSP_PlayAudio(_THIS);
slouken@0
    54
static Uint8 *DSP_GetAudioBuf(_THIS);
slouken@0
    55
static void DSP_CloseAudio(_THIS);
slouken@0
    56
slouken@1464
    57
static Uint8 snd2au(int sample);
slouken@1464
    58
slouken@0
    59
/* Audio driver bootstrap functions */
slouken@0
    60
slouken@1895
    61
static int
slouken@1895
    62
Audio_Available(void)
slouken@0
    63
{
slouken@1895
    64
    int fd;
slouken@1895
    65
    int available;
slouken@0
    66
slouken@1895
    67
    available = 0;
slouken@1895
    68
    fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 1);
slouken@1895
    69
    if (fd >= 0) {
slouken@1895
    70
        available = 1;
slouken@1895
    71
        close(fd);
slouken@1895
    72
    }
slouken@1895
    73
    return (available);
slouken@0
    74
}
slouken@0
    75
slouken@1895
    76
static void
slouken@1895
    77
Audio_DeleteDevice(SDL_AudioDevice * device)
slouken@0
    78
{
slouken@1895
    79
    SDL_free(device->hidden);
slouken@1895
    80
    SDL_free(device);
slouken@0
    81
}
slouken@0
    82
slouken@1895
    83
static SDL_AudioDevice *
slouken@1895
    84
Audio_CreateDevice(int devindex)
slouken@0
    85
{
slouken@1895
    86
    SDL_AudioDevice *this;
slouken@0
    87
slouken@1895
    88
    /* Initialize all variables that we clean on shutdown */
slouken@1895
    89
    this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
slouken@1895
    90
    if (this) {
slouken@1895
    91
        SDL_memset(this, 0, (sizeof *this));
slouken@1895
    92
        this->hidden = (struct SDL_PrivateAudioData *)
slouken@1895
    93
            SDL_malloc((sizeof *this->hidden));
slouken@1895
    94
    }
slouken@1895
    95
    if ((this == NULL) || (this->hidden == NULL)) {
slouken@1895
    96
        SDL_OutOfMemory();
slouken@1895
    97
        if (this) {
slouken@1895
    98
            SDL_free(this);
slouken@1895
    99
        }
slouken@1895
   100
        return (0);
slouken@1895
   101
    }
slouken@1895
   102
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@1895
   103
    audio_fd = -1;
slouken@0
   104
slouken@1895
   105
    /* Set the function pointers */
slouken@1895
   106
    this->OpenAudio = DSP_OpenAudio;
slouken@1895
   107
    this->WaitAudio = DSP_WaitAudio;
slouken@1895
   108
    this->PlayAudio = DSP_PlayAudio;
slouken@1895
   109
    this->GetAudioBuf = DSP_GetAudioBuf;
slouken@1895
   110
    this->CloseAudio = DSP_CloseAudio;
slouken@0
   111
slouken@1895
   112
    this->free = Audio_DeleteDevice;
slouken@0
   113
slouken@1895
   114
    return this;
slouken@0
   115
}
slouken@0
   116
slouken@148
   117
AudioBootStrap SUNAUDIO_bootstrap = {
slouken@1895
   118
    "audio", "UNIX /dev/audio interface",
slouken@1895
   119
    Audio_Available, Audio_CreateDevice
slouken@0
   120
};
slouken@0
   121
slouken@0
   122
#ifdef DEBUG_AUDIO
slouken@1895
   123
void
slouken@1895
   124
CheckUnderflow(_THIS)
slouken@0
   125
{
slouken@0
   126
#ifdef AUDIO_GETINFO
slouken@1895
   127
    audio_info_t info;
slouken@1895
   128
    int left;
slouken@0
   129
slouken@1895
   130
    ioctl(audio_fd, AUDIO_GETINFO, &info);
slouken@1895
   131
    left = (written - info.play.samples);
slouken@1895
   132
    if (written && (left == 0)) {
slouken@1895
   133
        fprintf(stderr, "audio underflow!\n");
slouken@1895
   134
    }
slouken@0
   135
#endif
slouken@0
   136
}
slouken@0
   137
#endif
slouken@0
   138
slouken@1895
   139
void
slouken@1895
   140
DSP_WaitAudio(_THIS)
slouken@0
   141
{
slouken@0
   142
#ifdef AUDIO_GETINFO
slouken@1895
   143
#define SLEEP_FUDGE	10      /* 10 ms scheduling fudge factor */
slouken@1895
   144
    audio_info_t info;
slouken@1895
   145
    Sint32 left;
slouken@0
   146
slouken@1895
   147
    ioctl(audio_fd, AUDIO_GETINFO, &info);
slouken@1895
   148
    left = (written - info.play.samples);
slouken@1895
   149
    if (left > fragsize) {
slouken@1895
   150
        Sint32 sleepy;
slouken@0
   151
slouken@1895
   152
        sleepy = ((left - fragsize) / frequency);
slouken@1895
   153
        sleepy -= SLEEP_FUDGE;
slouken@1895
   154
        if (sleepy > 0) {
slouken@1895
   155
            SDL_Delay(sleepy);
slouken@1895
   156
        }
slouken@1895
   157
    }
slouken@0
   158
#else
slouken@1895
   159
    fd_set fdset;
slouken@0
   160
slouken@1895
   161
    FD_ZERO(&fdset);
slouken@1895
   162
    FD_SET(audio_fd, &fdset);
slouken@1895
   163
    select(audio_fd + 1, NULL, &fdset, NULL, NULL);
slouken@0
   164
#endif
slouken@0
   165
}
slouken@0
   166
slouken@1895
   167
void
slouken@1895
   168
DSP_PlayAudio(_THIS)
slouken@0
   169
{
slouken@1895
   170
    /* Write the audio data */
slouken@1895
   171
    if (ulaw_only) {
slouken@1895
   172
        /* Assuming that this->spec.freq >= 8000 Hz */
slouken@1895
   173
        int accum, incr, pos;
slouken@1895
   174
        Uint8 *aubuf;
slouken@0
   175
slouken@1895
   176
        accum = 0;
slouken@1895
   177
        incr = this->spec.freq / 8;
slouken@1895
   178
        aubuf = ulaw_buf;
slouken@1895
   179
        switch (audio_fmt & 0xFF) {
slouken@1895
   180
        case 8:
slouken@1895
   181
            {
slouken@1895
   182
                Uint8 *sndbuf;
slouken@0
   183
slouken@1895
   184
                sndbuf = mixbuf;
slouken@1895
   185
                for (pos = 0; pos < fragsize; ++pos) {
slouken@1895
   186
                    *aubuf = snd2au((0x80 - *sndbuf) * 64);
slouken@1895
   187
                    accum += incr;
slouken@1895
   188
                    while (accum > 0) {
slouken@1895
   189
                        accum -= 1000;
slouken@1895
   190
                        sndbuf += 1;
slouken@1895
   191
                    }
slouken@1895
   192
                    aubuf += 1;
slouken@1895
   193
                }
slouken@1895
   194
            }
slouken@1895
   195
            break;
slouken@1895
   196
        case 16:
slouken@1895
   197
            {
slouken@1895
   198
                Sint16 *sndbuf;
slouken@1895
   199
slouken@1895
   200
                sndbuf = (Sint16 *) mixbuf;
slouken@1895
   201
                for (pos = 0; pos < fragsize; ++pos) {
slouken@1895
   202
                    *aubuf = snd2au(*sndbuf / 4);
slouken@1895
   203
                    accum += incr;
slouken@1895
   204
                    while (accum > 0) {
slouken@1895
   205
                        accum -= 1000;
slouken@1895
   206
                        sndbuf += 1;
slouken@1895
   207
                    }
slouken@1895
   208
                    aubuf += 1;
slouken@1895
   209
                }
slouken@1895
   210
            }
slouken@1895
   211
            break;
slouken@1895
   212
        }
slouken@0
   213
#ifdef DEBUG_AUDIO
slouken@1895
   214
        CheckUnderflow(this);
slouken@0
   215
#endif
slouken@1895
   216
        if (write(audio_fd, ulaw_buf, fragsize) < 0) {
slouken@1895
   217
            /* Assume fatal error, for now */
slouken@1895
   218
            this->enabled = 0;
slouken@1895
   219
        }
slouken@1895
   220
        written += fragsize;
slouken@1895
   221
    } else {
slouken@0
   222
#ifdef DEBUG_AUDIO
slouken@1895
   223
        CheckUnderflow(this);
slouken@0
   224
#endif
slouken@1895
   225
        if (write(audio_fd, mixbuf, this->spec.size) < 0) {
slouken@1895
   226
            /* Assume fatal error, for now */
slouken@1895
   227
            this->enabled = 0;
slouken@1895
   228
        }
slouken@1895
   229
        written += fragsize;
slouken@1895
   230
    }
slouken@0
   231
}
slouken@0
   232
slouken@1895
   233
Uint8 *
slouken@1895
   234
DSP_GetAudioBuf(_THIS)
slouken@0
   235
{
slouken@1895
   236
    return (mixbuf);
slouken@0
   237
}
slouken@0
   238
slouken@1895
   239
void
slouken@1895
   240
DSP_CloseAudio(_THIS)
slouken@0
   241
{
slouken@1895
   242
    if (mixbuf != NULL) {
slouken@1895
   243
        SDL_FreeAudioMem(mixbuf);
slouken@1895
   244
        mixbuf = NULL;
slouken@1895
   245
    }
slouken@1895
   246
    if (ulaw_buf != NULL) {
slouken@1895
   247
        SDL_free(ulaw_buf);
slouken@1895
   248
        ulaw_buf = NULL;
slouken@1895
   249
    }
slouken@1895
   250
    close(audio_fd);
slouken@0
   251
}
slouken@0
   252
slouken@1895
   253
int
slouken@1895
   254
DSP_OpenAudio(_THIS, SDL_AudioSpec * spec)
slouken@0
   255
{
slouken@1895
   256
    char audiodev[1024];
slouken@0
   257
#ifdef AUDIO_SETINFO
slouken@1895
   258
    int enc;
slouken@0
   259
#endif
slouken@1895
   260
    int desired_freq = spec->freq;
slouken@0
   261
slouken@1895
   262
    /* Initialize our freeable variables, in case we fail */
slouken@1895
   263
    audio_fd = -1;
slouken@1895
   264
    mixbuf = NULL;
slouken@1895
   265
    ulaw_buf = NULL;
slouken@0
   266
slouken@1895
   267
    /* Determine the audio parameters from the AudioSpec */
slouken@1895
   268
    switch (spec->format & 0xFF) {
slouken@1895
   269
slouken@1895
   270
    case 8:
slouken@1895
   271
        {                       /* Unsigned 8 bit audio data */
slouken@1895
   272
            spec->format = AUDIO_U8;
slouken@0
   273
#ifdef AUDIO_SETINFO
slouken@1895
   274
            enc = AUDIO_ENCODING_LINEAR8;
slouken@0
   275
#endif
slouken@1895
   276
        }
slouken@1895
   277
        break;
slouken@0
   278
slouken@1895
   279
    case 16:
slouken@1895
   280
        {                       /* Signed 16 bit audio data */
slouken@1895
   281
            spec->format = AUDIO_S16SYS;
slouken@1895
   282
#ifdef AUDIO_SETINFO
slouken@1895
   283
            enc = AUDIO_ENCODING_LINEAR;
slouken@1895
   284
#endif
slouken@1895
   285
        }
slouken@1895
   286
        break;
slouken@0
   287
slouken@1895
   288
    default:
slouken@1895
   289
        {
slouken@1895
   290
            SDL_SetError("Unsupported audio format");
slouken@1895
   291
            return (-1);
slouken@1895
   292
        }
slouken@1895
   293
    }
slouken@1895
   294
    audio_fmt = spec->format;
slouken@0
   295
slouken@1895
   296
    /* Open the audio device */
slouken@1895
   297
    audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 1);
slouken@1895
   298
    if (audio_fd < 0) {
slouken@1895
   299
        SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
slouken@1895
   300
        return (-1);
slouken@1895
   301
    }
slouken@1895
   302
slouken@1895
   303
    ulaw_only = 0;              /* modern Suns do support linear audio */
slouken@0
   304
#ifdef AUDIO_SETINFO
slouken@1895
   305
    for (;;) {
slouken@1895
   306
        audio_info_t info;
slouken@1895
   307
        AUDIO_INITINFO(&info);  /* init all fields to "no change" */
slouken@0
   308
slouken@1895
   309
        /* Try to set the requested settings */
slouken@1895
   310
        info.play.sample_rate = spec->freq;
slouken@1895
   311
        info.play.channels = spec->channels;
slouken@1895
   312
        info.play.precision = (enc == AUDIO_ENCODING_ULAW)
slouken@1895
   313
            ? 8 : spec->format & 0xff;
slouken@1895
   314
        info.play.encoding = enc;
slouken@1895
   315
        if (ioctl(audio_fd, AUDIO_SETINFO, &info) == 0) {
slouken@0
   316
slouken@1895
   317
            /* Check to be sure we got what we wanted */
slouken@1895
   318
            if (ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) {
slouken@1895
   319
                SDL_SetError("Error getting audio parameters: %s",
slouken@1895
   320
                             strerror(errno));
slouken@1895
   321
                return -1;
slouken@1895
   322
            }
slouken@1895
   323
            if (info.play.encoding == enc
slouken@1895
   324
                && info.play.precision == (spec->format & 0xff)
slouken@1895
   325
                && info.play.channels == spec->channels) {
slouken@1895
   326
                /* Yow! All seems to be well! */
slouken@1895
   327
                spec->freq = info.play.sample_rate;
slouken@1895
   328
                break;
slouken@1895
   329
            }
slouken@1895
   330
        }
slouken@0
   331
slouken@1895
   332
        switch (enc) {
slouken@1895
   333
        case AUDIO_ENCODING_LINEAR8:
slouken@1895
   334
            /* unsigned 8bit apparently not supported here */
slouken@1895
   335
            enc = AUDIO_ENCODING_LINEAR;
slouken@1895
   336
            spec->format = AUDIO_S16SYS;
slouken@1895
   337
            break;              /* try again */
slouken@0
   338
slouken@1895
   339
        case AUDIO_ENCODING_LINEAR:
slouken@1895
   340
            /* linear 16bit didn't work either, resort to -law */
slouken@1895
   341
            enc = AUDIO_ENCODING_ULAW;
slouken@1895
   342
            spec->channels = 1;
slouken@1895
   343
            spec->freq = 8000;
slouken@1895
   344
            spec->format = AUDIO_U8;
slouken@1895
   345
            ulaw_only = 1;
slouken@1895
   346
            break;
slouken@1895
   347
slouken@1895
   348
        default:
slouken@1895
   349
            /* oh well... */
slouken@1895
   350
            SDL_SetError("Error setting audio parameters: %s",
slouken@1895
   351
                         strerror(errno));
slouken@1895
   352
            return -1;
slouken@1895
   353
        }
slouken@1895
   354
    }
slouken@0
   355
#endif /* AUDIO_SETINFO */
slouken@1895
   356
    written = 0;
slouken@0
   357
slouken@1895
   358
    /* We can actually convert on-the-fly to U-Law */
slouken@1895
   359
    if (ulaw_only) {
slouken@1895
   360
        spec->freq = desired_freq;
slouken@1895
   361
        fragsize = (spec->samples * 1000) / (spec->freq / 8);
slouken@1895
   362
        frequency = 8;
slouken@1895
   363
        ulaw_buf = (Uint8 *) SDL_malloc(fragsize);
slouken@1895
   364
        if (ulaw_buf == NULL) {
slouken@1895
   365
            SDL_OutOfMemory();
slouken@1895
   366
            return (-1);
slouken@1895
   367
        }
slouken@1895
   368
        spec->channels = 1;
slouken@1895
   369
    } else {
slouken@1895
   370
        fragsize = spec->samples;
slouken@1895
   371
        frequency = spec->freq / 1000;
slouken@1895
   372
    }
slouken@0
   373
#ifdef DEBUG_AUDIO
slouken@1895
   374
    fprintf(stderr, "Audio device %s U-Law only\n",
slouken@1895
   375
            ulaw_only ? "is" : "is not");
slouken@1895
   376
    fprintf(stderr, "format=0x%x chan=%d freq=%d\n",
slouken@1895
   377
            spec->format, spec->channels, spec->freq);
slouken@0
   378
#endif
slouken@0
   379
slouken@1895
   380
    /* Update the fragment size as size in bytes */
slouken@1895
   381
    SDL_CalculateAudioSpec(spec);
slouken@0
   382
slouken@1895
   383
    /* Allocate mixing buffer */
slouken@1895
   384
    mixbuf = (Uint8 *) SDL_AllocAudioMem(spec->size);
slouken@1895
   385
    if (mixbuf == NULL) {
slouken@1895
   386
        SDL_OutOfMemory();
slouken@1895
   387
        return (-1);
slouken@1895
   388
    }
slouken@1895
   389
    SDL_memset(mixbuf, spec->silence, spec->size);
slouken@0
   390
slouken@1895
   391
    /* We're ready to rock and roll. :-) */
slouken@1895
   392
    return (0);
slouken@0
   393
}
slouken@0
   394
slouken@0
   395
/************************************************************************/
slouken@0
   396
/* This function (snd2au()) copyrighted:                                */
slouken@0
   397
/************************************************************************/
slouken@0
   398
/*      Copyright 1989 by Rich Gopstein and Harris Corporation          */
slouken@0
   399
/*                                                                      */
slouken@0
   400
/*      Permission to use, copy, modify, and distribute this software   */
slouken@0
   401
/*      and its documentation for any purpose and without fee is        */
slouken@0
   402
/*      hereby granted, provided that the above copyright notice        */
slouken@0
   403
/*      appears in all copies and that both that copyright notice and   */
slouken@0
   404
/*      this permission notice appear in supporting documentation, and  */
slouken@0
   405
/*      that the name of Rich Gopstein and Harris Corporation not be    */
slouken@0
   406
/*      used in advertising or publicity pertaining to distribution     */
slouken@0
   407
/*      of the software without specific, written prior permission.     */
slouken@0
   408
/*      Rich Gopstein and Harris Corporation make no representations    */
slouken@0
   409
/*      about the suitability of this software for any purpose.  It     */
slouken@0
   410
/*      provided "as is" without express or implied warranty.           */
slouken@0
   411
/************************************************************************/
slouken@0
   412
slouken@1895
   413
static Uint8
slouken@1895
   414
snd2au(int sample)
slouken@0
   415
{
slouken@0
   416
slouken@1895
   417
    int mask;
slouken@0
   418
slouken@1895
   419
    if (sample < 0) {
slouken@1895
   420
        sample = -sample;
slouken@1895
   421
        mask = 0x7f;
slouken@1895
   422
    } else {
slouken@1895
   423
        mask = 0xff;
slouken@1895
   424
    }
slouken@0
   425
slouken@1895
   426
    if (sample < 32) {
slouken@1895
   427
        sample = 0xF0 | (15 - sample / 2);
slouken@1895
   428
    } else if (sample < 96) {
slouken@1895
   429
        sample = 0xE0 | (15 - (sample - 32) / 4);
slouken@1895
   430
    } else if (sample < 224) {
slouken@1895
   431
        sample = 0xD0 | (15 - (sample - 96) / 8);
slouken@1895
   432
    } else if (sample < 480) {
slouken@1895
   433
        sample = 0xC0 | (15 - (sample - 224) / 16);
slouken@1895
   434
    } else if (sample < 992) {
slouken@1895
   435
        sample = 0xB0 | (15 - (sample - 480) / 32);
slouken@1895
   436
    } else if (sample < 2016) {
slouken@1895
   437
        sample = 0xA0 | (15 - (sample - 992) / 64);
slouken@1895
   438
    } else if (sample < 4064) {
slouken@1895
   439
        sample = 0x90 | (15 - (sample - 2016) / 128);
slouken@1895
   440
    } else if (sample < 8160) {
slouken@1895
   441
        sample = 0x80 | (15 - (sample - 4064) / 256);
slouken@1895
   442
    } else {
slouken@1895
   443
        sample = 0x80;
slouken@1895
   444
    }
slouken@1895
   445
    return (mask & sample);
slouken@0
   446
}
slouken@1895
   447
slouken@1895
   448
/* vi: set ts=4 sw=4 expandtab: */