src/audio/dmedia/SDL_irixaudio.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 01 Sep 2006 00:04:07 +0000
changeset 2002 7590aaf89a60
parent 1895 c121d94672cb
child 2043 adf732f1f016
permissions -rw-r--r--
Cleaned up IRIX audio driver, added float32 support and fallbacks when
a specific audiospec isn't supported or the hardware fails.

Usual disclaimer: No IRIX box, so this may not even compile.
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
*/
icculus@2002
    22
#include <errno.h>
slouken@1402
    23
#include "SDL_config.h"
slouken@0
    24
slouken@0
    25
/* Allow access to a raw mixing buffer (For IRIX 6.5 and higher) */
slouken@903
    26
/* patch for IRIX 5 by Georg Schwarz 18/07/2004 */
slouken@0
    27
slouken@0
    28
#include "SDL_timer.h"
slouken@0
    29
#include "SDL_audio.h"
slouken@1361
    30
#include "../SDL_audiomem.h"
slouken@1361
    31
#include "../SDL_audio_c.h"
slouken@0
    32
#include "SDL_irixaudio.h"
slouken@0
    33
slouken@0
    34
slouken@1895
    35
#ifndef AL_RESOURCE             /* as a test whether we use the old IRIX audio libraries */
slouken@903
    36
#define OLD_IRIX_AUDIO
slouken@903
    37
#define alClosePort(x) ALcloseport(x)
slouken@903
    38
#define alFreeConfig(x) ALfreeconfig(x)
slouken@903
    39
#define alGetFillable(x) ALgetfillable(x)
slouken@903
    40
#define alNewConfig() ALnewconfig()
slouken@903
    41
#define alOpenPort(x,y,z) ALopenport(x,y,z)
slouken@903
    42
#define alSetChannels(x,y) ALsetchannels(x,y)
slouken@903
    43
#define alSetQueueSize(x,y) ALsetqueuesize(x,y)
slouken@903
    44
#define alSetSampFmt(x,y) ALsetsampfmt(x,y)
slouken@903
    45
#define alSetWidth(x,y) ALsetwidth(x,y)
slouken@903
    46
#endif
slouken@903
    47
slouken@0
    48
/* Audio driver functions */
slouken@1895
    49
static int AL_OpenAudio(_THIS, SDL_AudioSpec * spec);
slouken@0
    50
static void AL_WaitAudio(_THIS);
slouken@0
    51
static void AL_PlayAudio(_THIS);
slouken@0
    52
static Uint8 *AL_GetAudioBuf(_THIS);
slouken@0
    53
static void AL_CloseAudio(_THIS);
slouken@0
    54
slouken@0
    55
/* Audio driver bootstrap functions */
slouken@0
    56
slouken@1895
    57
static int
slouken@1895
    58
Audio_Available(void)
slouken@0
    59
{
slouken@1895
    60
    return 1;
slouken@0
    61
}
slouken@0
    62
slouken@1895
    63
static void
slouken@1895
    64
Audio_DeleteDevice(SDL_AudioDevice * device)
slouken@0
    65
{
slouken@1895
    66
    SDL_free(device->hidden);
slouken@1895
    67
    SDL_free(device);
slouken@0
    68
}
slouken@0
    69
slouken@1895
    70
static SDL_AudioDevice *
slouken@1895
    71
Audio_CreateDevice(int devindex)
slouken@0
    72
{
slouken@1895
    73
    SDL_AudioDevice *this;
slouken@0
    74
slouken@1895
    75
    /* Initialize all variables that we clean on shutdown */
slouken@1895
    76
    this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
slouken@1895
    77
    if (this) {
slouken@1895
    78
        SDL_memset(this, 0, (sizeof *this));
slouken@1895
    79
        this->hidden = (struct SDL_PrivateAudioData *)
slouken@1895
    80
            SDL_malloc((sizeof *this->hidden));
slouken@1895
    81
    }
slouken@1895
    82
    if ((this == NULL) || (this->hidden == NULL)) {
slouken@1895
    83
        SDL_OutOfMemory();
slouken@1895
    84
        if (this) {
slouken@1895
    85
            SDL_free(this);
slouken@1895
    86
        }
slouken@1895
    87
        return (0);
slouken@1895
    88
    }
slouken@1895
    89
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
    90
slouken@1895
    91
    /* Set the function pointers */
slouken@1895
    92
    this->OpenAudio = AL_OpenAudio;
slouken@1895
    93
    this->WaitAudio = AL_WaitAudio;
slouken@1895
    94
    this->PlayAudio = AL_PlayAudio;
slouken@1895
    95
    this->GetAudioBuf = AL_GetAudioBuf;
slouken@1895
    96
    this->CloseAudio = AL_CloseAudio;
slouken@0
    97
slouken@1895
    98
    this->free = Audio_DeleteDevice;
slouken@0
    99
slouken@1895
   100
    return this;
slouken@0
   101
}
slouken@0
   102
slouken@148
   103
AudioBootStrap DMEDIA_bootstrap = {
slouken@1895
   104
    "AL", "IRIX DMedia audio",
slouken@1895
   105
    Audio_Available, Audio_CreateDevice
slouken@0
   106
};
slouken@0
   107
slouken@0
   108
slouken@1895
   109
void static
slouken@1895
   110
AL_WaitAudio(_THIS)
slouken@0
   111
{
slouken@1895
   112
    Sint32 timeleft;
slouken@0
   113
slouken@1895
   114
    timeleft = this->spec.samples - alGetFillable(audio_port);
slouken@1895
   115
    if (timeleft > 0) {
slouken@1895
   116
        timeleft /= (this->spec.freq / 1000);
slouken@1895
   117
        SDL_Delay((Uint32) timeleft);
slouken@1895
   118
    }
slouken@0
   119
}
slouken@0
   120
slouken@1895
   121
static void
slouken@1895
   122
AL_PlayAudio(_THIS)
slouken@0
   123
{
slouken@1895
   124
    /* Write the audio data out */
slouken@1895
   125
    if (alWriteFrames(audio_port, mixbuf, this->spec.samples) < 0) {
slouken@1895
   126
        /* Assume fatal error, for now */
slouken@1895
   127
        this->enabled = 0;
slouken@1895
   128
    }
slouken@0
   129
}
slouken@0
   130
slouken@1895
   131
static Uint8 *
slouken@1895
   132
AL_GetAudioBuf(_THIS)
slouken@0
   133
{
slouken@1895
   134
    return (mixbuf);
slouken@0
   135
}
slouken@0
   136
slouken@1895
   137
static void
slouken@1895
   138
AL_CloseAudio(_THIS)
slouken@0
   139
{
slouken@1895
   140
    if (mixbuf != NULL) {
slouken@1895
   141
        SDL_FreeAudioMem(mixbuf);
slouken@1895
   142
        mixbuf = NULL;
slouken@1895
   143
    }
slouken@1895
   144
    if (audio_port != NULL) {
slouken@1895
   145
        alClosePort(audio_port);
slouken@1895
   146
        audio_port = NULL;
slouken@1895
   147
    }
slouken@0
   148
}
slouken@0
   149
slouken@1895
   150
static int
slouken@1895
   151
AL_OpenAudio(_THIS, SDL_AudioSpec * spec)
slouken@0
   152
{
icculus@2002
   153
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(spec->format);
icculus@2002
   154
    long width = 0;
icculus@2002
   155
    long fmt = 0;
icculus@2002
   156
    int valid = 0;
icculus@2002
   157
slouken@903
   158
#ifdef OLD_IRIX_AUDIO
icculus@2002
   159
    {
icculus@2002
   160
        long audio_param[2];
icculus@2002
   161
        audio_param[0] = AL_OUTPUT_RATE;
icculus@2002
   162
        audio_param[1] = spec->freq;
icculus@2002
   163
        valid = (ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0);
icculus@2002
   164
    }
slouken@903
   165
#else
icculus@2002
   166
    {
icculus@2002
   167
        ALpv audio_param;
icculus@2002
   168
        audio_param.param = AL_RATE;
icculus@2002
   169
        audio_param.value.i = spec->freq;
icculus@2002
   170
        valid = (alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0);
icculus@2002
   171
    }
slouken@903
   172
#endif
slouken@0
   173
icculus@2002
   174
    while ((!valid) && (test_format)) {
icculus@2002
   175
        valid = 1;
icculus@2002
   176
        spec->format = test_format;
slouken@0
   177
icculus@2002
   178
        switch (test_format) {
icculus@2002
   179
            case AUDIO_S8:
icculus@2002
   180
                width = AL_SAMPLE_8;
icculus@2002
   181
                fmt = AL_SAMPFMT_TWOSCOMP;
icculus@2002
   182
                break;
icculus@2002
   183
icculus@2002
   184
            case AUDIO_S16SYS:
icculus@2002
   185
                width = AL_SAMPLE_16;
icculus@2002
   186
                fmt = AL_SAMPFMT_TWOSCOMP;
icculus@2002
   187
                break;
icculus@2002
   188
icculus@2002
   189
            case AUDIO_F32SYS:
icculus@2002
   190
                width = 0;  /* not used here... */
icculus@2002
   191
                fmt = AL_SAMPFMT_FLOAT;
icculus@2002
   192
                break;
icculus@2002
   193
icculus@2002
   194
            /* Docs say there is int24, but not int32.... */
icculus@2002
   195
icculus@2002
   196
            default:
icculus@2002
   197
                valid = 0;
icculus@2002
   198
                test_format = SDL_NextAudioFormat();
icculus@2002
   199
                break;
slouken@1895
   200
        }
slouken@0
   201
icculus@2002
   202
        if (valid) {
icculus@2002
   203
            ALconfig audio_config = alNewConfig();
icculus@2002
   204
            valid = 0;
icculus@2002
   205
            if (audio_config) {
icculus@2002
   206
                if (alSetChannels(audio_config, spec->channels) < 0) {
icculus@2002
   207
                    if (spec->channels > 2) {  /* can't handle > stereo? */
icculus@2002
   208
                        spec->channels = 2;  /* try again below. */
icculus@2002
   209
                    }
icculus@2002
   210
                }
icculus@2002
   211
icculus@2002
   212
                if ((alSetSampFmt(audio_config, fmt) >= 0) &&
icculus@2002
   213
                    ((!width) || (alSetWidth(audio_config, width) >= 0)) &&
icculus@2002
   214
                    (alSetQueueSize(audio_config, spec->samples * 2) >= 0) &&
icculus@2002
   215
                    (alSetChannels(audio_config, spec->channels) >= 0)) {
icculus@2002
   216
icculus@2002
   217
                    audio_port = alOpenPort("SDL audio", "w", audio_config);
icculus@2002
   218
                    if (audio_port == NULL) {
icculus@2002
   219
                        /* docs say AL_BAD_CHANNELS happens here, too. */
icculus@2002
   220
                        int err = oserror();
icculus@2002
   221
                        if (err == AL_BAD_CHANNELS) {
icculus@2002
   222
                            spec->channels = 2;
icculus@2002
   223
                            alSetChannels(audio_config, spec->channels);
icculus@2002
   224
                            audio_port = alOpenPort("SDL audio", "w",
icculus@2002
   225
                                                    audio_config);
icculus@2002
   226
                        }
icculus@2002
   227
                    }
icculus@2002
   228
icculus@2002
   229
                    if (audio_port != NULL) {
icculus@2002
   230
                        valid = 1;
icculus@2002
   231
                    }
icculus@2002
   232
                }
icculus@2002
   233
icculus@2002
   234
                alFreeConfig(audio_config);
icculus@2002
   235
            }
slouken@1895
   236
        }
icculus@2002
   237
    }
slouken@0
   238
icculus@2002
   239
    if (!valid) {
icculus@2002
   240
        SDL_SetError("Unsupported audio format");
icculus@2002
   241
        return (-1);
slouken@1895
   242
    }
slouken@0
   243
slouken@1895
   244
    /* Update the fragment size as size in bytes */
slouken@1895
   245
    SDL_CalculateAudioSpec(spec);
slouken@0
   246
slouken@1895
   247
    /* Allocate mixing buffer */
slouken@1895
   248
    mixbuf = (Uint8 *) SDL_AllocAudioMem(spec->size);
slouken@1895
   249
    if (mixbuf == NULL) {
slouken@1895
   250
        SDL_OutOfMemory();
slouken@1895
   251
        return (-1);
slouken@1895
   252
    }
slouken@1895
   253
    SDL_memset(mixbuf, spec->silence, spec->size);
slouken@0
   254
slouken@1895
   255
    /* We're ready to rock and roll. :-) */
slouken@1895
   256
    return (0);
slouken@0
   257
}
slouken@1895
   258
slouken@1895
   259
/* vi: set ts=4 sw=4 expandtab: */