This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_irixaudio.c
238 lines (202 loc) · 7.04 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include <errno.h>
23
#include "SDL_config.h"
24
25
/* Allow access to a raw mixing buffer (For IRIX 6.5 and higher) */
26
/* patch for IRIX 5 by Georg Schwarz 18/07/2004 */
27
28
29
#include "SDL_timer.h"
#include "SDL_audio.h"
30
31
#include "../SDL_audiomem.h"
#include "../SDL_audio_c.h"
32
33
34
#include "SDL_irixaudio.h"
35
#ifndef AL_RESOURCE /* as a test whether we use the old IRIX audio libraries */
36
37
38
39
40
41
42
43
44
45
46
47
#define OLD_IRIX_AUDIO
#define alClosePort(x) ALcloseport(x)
#define alFreeConfig(x) ALfreeconfig(x)
#define alGetFillable(x) ALgetfillable(x)
#define alNewConfig() ALnewconfig()
#define alOpenPort(x,y,z) ALopenport(x,y,z)
#define alSetChannels(x,y) ALsetchannels(x,y)
#define alSetQueueSize(x,y) ALsetqueuesize(x,y)
#define alSetSampFmt(x,y) ALsetsampfmt(x,y)
#define alSetWidth(x,y) ALsetwidth(x,y)
#endif
48
void static
49
IRIXAUDIO_WaitDevice(_THIS)
50
{
51
Sint32 timeleft;
52
53
timeleft = this->spec.samples - alGetFillable(this->hidden->audio_port);
54
55
56
57
if (timeleft > 0) {
timeleft /= (this->spec.freq / 1000);
SDL_Delay((Uint32) timeleft);
}
58
59
}
60
static void
61
IRIXAUDIO_PlayDevice(_THIS)
62
{
63
/* Write the audio data out */
64
65
66
ALport port = this->hidden->audio_port;
Uint8 *mixbuf = this->hidden->mixbuf;
if (alWriteFrames(port, mixbuf, this->spec.samples) < 0) {
67
68
69
/* Assume fatal error, for now */
this->enabled = 0;
}
70
71
}
72
static Uint8 *
73
IRIXAUDIO_GetDeviceBuf(_THIS)
74
{
75
return (this->hidden->mixbuf);
76
77
}
78
static void
79
IRIXAUDIO_CloseDevice(_THIS)
80
{
81
82
83
84
85
86
87
88
89
90
91
if (this->hidden != NULL) {
if (this->hidden->mixbuf != NULL) {
SDL_FreeAudioMem(this->hidden->mixbuf);
this->hidden->mixbuf = NULL;
}
if (this->hidden->audio_port != NULL) {
alClosePort(this->hidden->audio_port);
this->hidden->audio_port = NULL;
}
SDL_free(this->hidden);
this->hidden = NULL;
92
}
93
94
}
95
static int
96
IRIXAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
97
{
98
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
99
100
101
102
long width = 0;
long fmt = 0;
int valid = 0;
103
104
105
106
/* !!! FIXME: Handle multiple devices and capture? */
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
107
SDL_malloc((sizeof *this->hidden));
108
109
110
111
112
113
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
114
#ifdef OLD_IRIX_AUDIO
115
116
117
{
long audio_param[2];
audio_param[0] = AL_OUTPUT_RATE;
118
audio_param[1] = this->spec.freq;
119
120
valid = (ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0);
}
121
#else
122
123
124
{
ALpv audio_param;
audio_param.param = AL_RATE;
125
audio_param.value.i = this->spec.freq;
126
127
valid = (alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0);
}
128
#endif
129
130
131
while ((!valid) && (test_format)) {
valid = 1;
132
this->spec.format = test_format;
133
134
switch (test_format) {
135
136
137
138
case AUDIO_S8:
width = AL_SAMPLE_8;
fmt = AL_SAMPFMT_TWOSCOMP;
break;
139
140
141
142
143
case AUDIO_S16SYS:
width = AL_SAMPLE_16;
fmt = AL_SAMPFMT_TWOSCOMP;
break;
144
145
146
147
148
case AUDIO_F32SYS:
width = 0; /* not used here... */
fmt = AL_SAMPFMT_FLOAT;
break;
149
150
151
/* Docs say there is int24, but not int32.... */
152
153
154
155
default:
valid = 0;
test_format = SDL_NextAudioFormat();
break;
156
157
}
158
159
160
161
if (valid) {
ALconfig audio_config = alNewConfig();
valid = 0;
if (audio_config) {
162
if (alSetChannels(audio_config, this->spec.channels) < 0) {
163
164
if (this->spec.channels > 2) { /* can't handle > stereo? */
this->spec.channels = 2; /* try again below. */
165
166
167
168
169
}
}
if ((alSetSampFmt(audio_config, fmt) >= 0) &&
((!width) || (alSetWidth(audio_config, width) >= 0)) &&
170
171
172
173
(alSetQueueSize(audio_config, this->spec.samples * 2) >=
0)
&& (alSetChannels(audio_config, this->spec.channels) >=
0)) {
174
175
176
177
this->hidden->audio_port = alOpenPort("SDL audio", "w",
audio_config);
if (this->hidden->audio_port == NULL) {
178
179
180
/* docs say AL_BAD_CHANNELS happens here, too. */
int err = oserror();
if (err == AL_BAD_CHANNELS) {
181
182
this->spec.channels = 2;
alSetChannels(audio_config, this->spec.channels);
183
184
this->hidden->audio_port =
alOpenPort("SDL audio", "w", audio_config);
185
186
187
}
}
188
if (this->hidden->audio_port != NULL) {
189
190
191
192
193
194
valid = 1;
}
}
alFreeConfig(audio_config);
}
195
196
197
}
}
198
if (!valid) {
199
IRIXAUDIO_CloseDevice(this);
200
SDL_SetError("Unsupported audio format");
201
return 0;
202
203
}
204
/* Update the fragment size as size in bytes */
205
SDL_CalculateAudioSpec(&this->spec);
206
207
/* Allocate mixing buffer */
208
209
210
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->spec.size);
if (this->hidden->mixbuf == NULL) {
IRIXAUDIO_CloseDevice(this);
211
SDL_OutOfMemory();
212
return 0;
213
}
214
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
215
216
/* We're ready to rock and roll. :-) */
217
return 1;
218
}
219
220
static int
221
IRIXAUDIO_Init(SDL_AudioDriverImpl * impl)
222
223
224
225
226
227
228
{
/* Set the function pointers */
impl->OpenDevice = DSP_OpenDevice;
impl->PlayDevice = DSP_PlayDevice;
impl->WaitDevice = DSP_WaitDevice;
impl->GetDeviceBuf = DSP_GetDeviceBuf;
impl->CloseDevice = DSP_CloseDevice;
229
impl->OnlyHasDefaultOutputDevice = 1; /* !!! FIXME: not true, I think. */
230
231
232
233
234
235
236
237
return 1;
}
AudioBootStrap IRIXAUDIO_bootstrap = {
"AL", "IRIX DMedia audio", IRIXAUDIO_Init, 0
};
238
/* vi: set ts=4 sw=4 expandtab: */