~ubuntu-branches/ubuntu/trusty/mpeg4ip/trusty

« back to all changes in this revision

Viewing changes to lib/SDLAudio/src/audio/dmedia/SDL_irixaudio.c

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-01-12 15:59:56 UTC
  • Revision ID: james.westby@ubuntu.com-20080112155956-1vznw5njidvrh649
Tags: upstream-1.6dfsg
ImportĀ upstreamĀ versionĀ 1.6dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    SDL - Simple DirectMedia Layer
 
3
    Copyright (C) 1997, 1998  Sam Lantinga
 
4
 
 
5
    This library is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU Library General Public
 
7
    License as published by the Free Software Foundation; either
 
8
    version 2 of the License, or (at your option) any later version.
 
9
 
 
10
    This library is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
    Library General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU Library General Public
 
16
    License along with this library; if not, write to the Free
 
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
    Sam Lantinga
 
20
    5635-34 Springhouse Dr.
 
21
    Pleasanton, CA 94588 (USA)
 
22
    slouken@libsdl.org
 
23
*/
 
24
 
 
25
#ifdef SAVE_RCSID
 
26
static char rcsid =
 
27
 "@(#) $Id$";
 
28
#endif
 
29
 
 
30
/* Allow access to a raw mixing buffer (For IRIX 6.5 and higher) */
 
31
/* patch for IRIX 5 by Georg Schwarz 18/07/2004 */
 
32
 
 
33
#include <stdlib.h>
 
34
 
 
35
#include "Our_SDL_audio.h"
 
36
#include "SDL_audiomem.h"
 
37
#include "SDL_audio_c.h"
 
38
#include "SDL_irixaudio.h"
 
39
 
 
40
#ifndef AL_RESOURCE /* as a test whether we use the old IRIX audio libraries */
 
41
#define OLD_IRIX_AUDIO
 
42
#define alClosePort(x) ALcloseport(x)
 
43
#define alFreeConfig(x) ALfreeconfig(x)
 
44
#define alGetFillable(x) ALgetfillable(x)
 
45
#define alNewConfig() ALnewconfig()
 
46
#define alOpenPort(x,y,z) ALopenport(x,y,z)
 
47
#define alSetChannels(x,y) ALsetchannels(x,y)
 
48
#define alSetQueueSize(x,y) ALsetqueuesize(x,y)
 
49
#define alSetSampFmt(x,y) ALsetsampfmt(x,y)
 
50
#define alSetWidth(x,y) ALsetwidth(x,y)
 
51
#endif
 
52
 
 
53
/* Audio driver functions */
 
54
static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec);
 
55
static void AL_WaitAudio(_THIS);
 
56
static void AL_PlayAudio(_THIS);
 
57
static Uint8 *AL_GetAudioBuf(_THIS);
 
58
static void AL_CloseAudio(_THIS);
 
59
 
 
60
/* Audio driver bootstrap functions */
 
61
 
 
62
static int Audio_Available(void)
 
63
{
 
64
        return 1;
 
65
}
 
66
 
 
67
static void Audio_DeleteDevice(SDL_AudioDevice *device)
 
68
{
 
69
        free(device->hidden);
 
70
        free(device);
 
71
}
 
72
 
 
73
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
 
74
{
 
75
        SDL_AudioDevice *this;
 
76
 
 
77
        /* Initialize all variables that we clean on shutdown */
 
78
        this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
 
79
        if ( this ) {
 
80
                memset(this, 0, (sizeof *this));
 
81
                this->hidden = (struct SDL_PrivateAudioData *)
 
82
                                malloc((sizeof *this->hidden));
 
83
        }
 
84
        if ( (this == NULL) || (this->hidden == NULL) ) {
 
85
                SDL_OutOfMemory();
 
86
                if ( this ) {
 
87
                        free(this);
 
88
                }
 
89
                return(0);
 
90
        }
 
91
        memset(this->hidden, 0, (sizeof *this->hidden));
 
92
 
 
93
        /* Set the function pointers */
 
94
        this->OpenAudio = AL_OpenAudio;
 
95
        this->WaitAudio = AL_WaitAudio;
 
96
        this->PlayAudio = AL_PlayAudio;
 
97
        this->GetAudioBuf = AL_GetAudioBuf;
 
98
        this->CloseAudio = AL_CloseAudio;
 
99
 
 
100
        this->free = Audio_DeleteDevice;
 
101
 
 
102
        return this;
 
103
}
 
104
 
 
105
AudioBootStrap DMEDIA_bootstrap_ours = {
 
106
        "AL", "IRIX DMedia audio",
 
107
        Audio_Available, Audio_CreateDevice
 
108
};
 
109
 
 
110
 
 
111
void static AL_WaitAudio(_THIS)
 
112
{
 
113
        Sint32 timeleft;
 
114
 
 
115
        timeleft = this->spec.samples - alGetFillable(audio_port);
 
116
        if ( timeleft > 0 ) {
 
117
                timeleft /= (this->spec.freq/1000);
 
118
                SDL_Delay((Uint32)timeleft);
 
119
        }
 
120
}
 
121
 
 
122
static void AL_PlayAudio(_THIS)
 
123
{
 
124
        /* Write the audio data out */
 
125
        if ( alWriteFrames(audio_port, mixbuf, this->spec.samples) < 0 ) {
 
126
                /* Assume fatal error, for now */
 
127
                this->enabled = 0;
 
128
        }
 
129
}
 
130
 
 
131
static Uint8 *AL_GetAudioBuf(_THIS)
 
132
{
 
133
        return(mixbuf);
 
134
}
 
135
 
 
136
static void AL_CloseAudio(_THIS)
 
137
{
 
138
        if ( mixbuf != NULL ) {
 
139
                SDL_FreeAudioMem(mixbuf);
 
140
                mixbuf = NULL;
 
141
        }
 
142
        if ( audio_port != NULL ) {
 
143
                alClosePort(audio_port);
 
144
                audio_port = NULL;
 
145
        }
 
146
}
 
147
 
 
148
static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec)
 
149
{
 
150
        ALconfig audio_config;
 
151
#ifdef OLD_IRIX_AUDIO
 
152
        long audio_param[2];
 
153
#else
 
154
        ALpv audio_param;
 
155
#endif
 
156
        int width;
 
157
 
 
158
        /* Determine the audio parameters from the AudioSpec */
 
159
        switch ( spec->format & 0xFF ) {
 
160
 
 
161
                case 8: { /* Signed 8 bit audio data */
 
162
                        spec->format = AUDIO_S8;
 
163
                        width = AL_SAMPLE_8;
 
164
                }
 
165
                break;
 
166
 
 
167
                case 16: { /* Signed 16 bit audio data */
 
168
                        spec->format = AUDIO_S16MSB;
 
169
                        width = AL_SAMPLE_16;
 
170
                }
 
171
                break;
 
172
 
 
173
                default: {
 
174
                        SDL_SetError("Unsupported audio format");
 
175
                        return(-1);
 
176
                }
 
177
        }
 
178
 
 
179
        /* Update the fragment size as size in bytes */
 
180
        SDL_CalculateAudioSpec(spec);
 
181
 
 
182
        /* Set output frequency */
 
183
#ifdef OLD_IRIX_AUDIO
 
184
        audio_param[0] = AL_OUTPUT_RATE;
 
185
        audio_param[1] = spec->freq;
 
186
        if( ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0 ) {
 
187
#else
 
188
        audio_param.param = AL_RATE;
 
189
        audio_param.value.i = spec->freq;
 
190
        if( alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0 ) {
 
191
#endif
 
192
                SDL_SetError("alSetParams failed");
 
193
                return(-1);
 
194
        }
 
195
 
 
196
        /* Open the audio port with the requested frequency */
 
197
        audio_port = NULL;
 
198
        audio_config = alNewConfig();
 
199
        if ( audio_config &&
 
200
             (alSetSampFmt(audio_config, AL_SAMPFMT_TWOSCOMP) >= 0) &&
 
201
             (alSetWidth(audio_config, width) >= 0) &&
 
202
             (alSetQueueSize(audio_config, spec->samples*2) >= 0) &&
 
203
             (alSetChannels(audio_config, spec->channels) >= 0) ) {
 
204
                audio_port = alOpenPort("SDL audio", "w", audio_config);
 
205
        }
 
206
        alFreeConfig(audio_config);
 
207
        if( audio_port == NULL ) {
 
208
                SDL_SetError("Unable to open audio port");
 
209
                return(-1);
 
210
        }
 
211
 
 
212
        /* Allocate mixing buffer */
 
213
        mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size);
 
214
        if ( mixbuf == NULL ) {
 
215
                SDL_OutOfMemory();
 
216
                return(-1);
 
217
        }
 
218
        memset(mixbuf, spec->silence, spec->size);
 
219
 
 
220
        /* We're ready to rock and roll. :-) */
 
221
        return(0);
 
222
}