4 * Copyright (c) 2011 by Chris Robinson <chris.kcat@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* This file contains routines to help with some menial OpenAL-related tasks,
26 * such as opening a device and setting up a context, closing the device and
27 * destroying its context, converting between frame counts and byte lengths,
28 * finding an appropriate buffer format, and getting readable strings for
29 * channel configs and sample types. */
37 #include "alhelpers.h"
40 const char *ChannelsName(ALenum chans
)
44 case AL_MONO_SOFT
: return "Mono";
45 case AL_STEREO_SOFT
: return "Stereo";
46 case AL_REAR_SOFT
: return "Rear";
47 case AL_QUAD_SOFT
: return "Quadraphonic";
48 case AL_5POINT1_SOFT
: return "5.1 Surround";
49 case AL_6POINT1_SOFT
: return "6.1 Surround";
50 case AL_7POINT1_SOFT
: return "7.1 Surround";
52 return "Unknown Channels";
55 const char *TypeName(ALenum type
)
59 case AL_BYTE_SOFT
: return "S8";
60 case AL_UNSIGNED_BYTE_SOFT
: return "U8";
61 case AL_SHORT_SOFT
: return "S16";
62 case AL_UNSIGNED_SHORT_SOFT
: return "U16";
63 case AL_INT_SOFT
: return "S32";
64 case AL_UNSIGNED_INT_SOFT
: return "U32";
65 case AL_FLOAT_SOFT
: return "Float32";
66 case AL_DOUBLE_SOFT
: return "Float64";
68 return "Unknown Type";
72 ALsizei
FramesToBytes(ALsizei size
, ALenum channels
, ALenum type
)
76 case AL_MONO_SOFT
: size
*= 1; break;
77 case AL_STEREO_SOFT
: size
*= 2; break;
78 case AL_REAR_SOFT
: size
*= 2; break;
79 case AL_QUAD_SOFT
: size
*= 4; break;
80 case AL_5POINT1_SOFT
: size
*= 6; break;
81 case AL_6POINT1_SOFT
: size
*= 7; break;
82 case AL_7POINT1_SOFT
: size
*= 8; break;
87 case AL_BYTE_SOFT
: size
*= sizeof(ALbyte
); break;
88 case AL_UNSIGNED_BYTE_SOFT
: size
*= sizeof(ALubyte
); break;
89 case AL_SHORT_SOFT
: size
*= sizeof(ALshort
); break;
90 case AL_UNSIGNED_SHORT_SOFT
: size
*= sizeof(ALushort
); break;
91 case AL_INT_SOFT
: size
*= sizeof(ALint
); break;
92 case AL_UNSIGNED_INT_SOFT
: size
*= sizeof(ALuint
); break;
93 case AL_FLOAT_SOFT
: size
*= sizeof(ALfloat
); break;
94 case AL_DOUBLE_SOFT
: size
*= sizeof(ALdouble
); break;
100 ALsizei
BytesToFrames(ALsizei size
, ALenum channels
, ALenum type
)
102 return size
/ FramesToBytes(1, channels
, type
);
106 ALenum
GetFormat(ALenum channels
, ALenum type
, LPALISBUFFERFORMATSUPPORTEDSOFT palIsBufferFormatSupportedSOFT
)
108 ALenum format
= AL_NONE
;
110 /* If using AL_SOFT_buffer_samples, try looking through its formats */
111 if(palIsBufferFormatSupportedSOFT
)
113 /* AL_SOFT_buffer_samples is more lenient with matching formats. The
114 * specified sample type does not need to match the returned format,
115 * but it is nice to try to get something close. */
116 if(type
== AL_UNSIGNED_BYTE_SOFT
|| type
== AL_BYTE_SOFT
)
118 if(channels
== AL_MONO_SOFT
) format
= AL_MONO8_SOFT
;
119 else if(channels
== AL_STEREO_SOFT
) format
= AL_STEREO8_SOFT
;
120 else if(channels
== AL_QUAD_SOFT
) format
= AL_QUAD8_SOFT
;
121 else if(channels
== AL_5POINT1_SOFT
) format
= AL_5POINT1_8_SOFT
;
122 else if(channels
== AL_6POINT1_SOFT
) format
= AL_6POINT1_8_SOFT
;
123 else if(channels
== AL_7POINT1_SOFT
) format
= AL_7POINT1_8_SOFT
;
125 else if(type
== AL_UNSIGNED_SHORT_SOFT
|| type
== AL_SHORT_SOFT
)
127 if(channels
== AL_MONO_SOFT
) format
= AL_MONO16_SOFT
;
128 else if(channels
== AL_STEREO_SOFT
) format
= AL_STEREO16_SOFT
;
129 else if(channels
== AL_QUAD_SOFT
) format
= AL_QUAD16_SOFT
;
130 else if(channels
== AL_5POINT1_SOFT
) format
= AL_5POINT1_16_SOFT
;
131 else if(channels
== AL_6POINT1_SOFT
) format
= AL_6POINT1_16_SOFT
;
132 else if(channels
== AL_7POINT1_SOFT
) format
= AL_7POINT1_16_SOFT
;
134 else if(type
== AL_UNSIGNED_BYTE3_SOFT
|| type
== AL_BYTE3_SOFT
||
135 type
== AL_UNSIGNED_INT_SOFT
|| type
== AL_INT_SOFT
||
136 type
== AL_FLOAT_SOFT
|| type
== AL_DOUBLE_SOFT
)
138 if(channels
== AL_MONO_SOFT
) format
= AL_MONO32F_SOFT
;
139 else if(channels
== AL_STEREO_SOFT
) format
= AL_STEREO32F_SOFT
;
140 else if(channels
== AL_QUAD_SOFT
) format
= AL_QUAD32F_SOFT
;
141 else if(channels
== AL_5POINT1_SOFT
) format
= AL_5POINT1_32F_SOFT
;
142 else if(channels
== AL_6POINT1_SOFT
) format
= AL_6POINT1_32F_SOFT
;
143 else if(channels
== AL_7POINT1_SOFT
) format
= AL_7POINT1_32F_SOFT
;
146 if(format
!= AL_NONE
&& !palIsBufferFormatSupportedSOFT(format
))
149 /* A matching format was not found or supported. Try 32-bit float. */
150 if(format
== AL_NONE
)
152 if(channels
== AL_MONO_SOFT
) format
= AL_MONO32F_SOFT
;
153 else if(channels
== AL_STEREO_SOFT
) format
= AL_STEREO32F_SOFT
;
154 else if(channels
== AL_QUAD_SOFT
) format
= AL_QUAD32F_SOFT
;
155 else if(channels
== AL_5POINT1_SOFT
) format
= AL_5POINT1_32F_SOFT
;
156 else if(channels
== AL_6POINT1_SOFT
) format
= AL_6POINT1_32F_SOFT
;
157 else if(channels
== AL_7POINT1_SOFT
) format
= AL_7POINT1_32F_SOFT
;
159 if(format
!= AL_NONE
&& !palIsBufferFormatSupportedSOFT(format
))
162 /* 32-bit float not supported. Try 16-bit int. */
163 if(format
== AL_NONE
)
165 if(channels
== AL_MONO_SOFT
) format
= AL_MONO16_SOFT
;
166 else if(channels
== AL_STEREO_SOFT
) format
= AL_STEREO16_SOFT
;
167 else if(channels
== AL_QUAD_SOFT
) format
= AL_QUAD16_SOFT
;
168 else if(channels
== AL_5POINT1_SOFT
) format
= AL_5POINT1_16_SOFT
;
169 else if(channels
== AL_6POINT1_SOFT
) format
= AL_6POINT1_16_SOFT
;
170 else if(channels
== AL_7POINT1_SOFT
) format
= AL_7POINT1_16_SOFT
;
172 if(format
!= AL_NONE
&& !palIsBufferFormatSupportedSOFT(format
))
175 /* 16-bit int not supported. Try 8-bit int. */
176 if(format
== AL_NONE
)
178 if(channels
== AL_MONO_SOFT
) format
= AL_MONO8_SOFT
;
179 else if(channels
== AL_STEREO_SOFT
) format
= AL_STEREO8_SOFT
;
180 else if(channels
== AL_QUAD_SOFT
) format
= AL_QUAD8_SOFT
;
181 else if(channels
== AL_5POINT1_SOFT
) format
= AL_5POINT1_8_SOFT
;
182 else if(channels
== AL_6POINT1_SOFT
) format
= AL_6POINT1_8_SOFT
;
183 else if(channels
== AL_7POINT1_SOFT
) format
= AL_7POINT1_8_SOFT
;
185 if(format
!= AL_NONE
&& !palIsBufferFormatSupportedSOFT(format
))
192 /* We use the AL_EXT_MCFORMATS extension to provide output of Quad, 5.1,
193 * and 7.1 channel configs, AL_EXT_FLOAT32 for 32-bit float samples, and
194 * AL_EXT_DOUBLE for 64-bit float samples. */
195 if(type
== AL_UNSIGNED_BYTE_SOFT
)
197 if(channels
== AL_MONO_SOFT
)
198 format
= AL_FORMAT_MONO8
;
199 else if(channels
== AL_STEREO_SOFT
)
200 format
= AL_FORMAT_STEREO8
;
201 else if(alIsExtensionPresent("AL_EXT_MCFORMATS"))
203 if(channels
== AL_QUAD_SOFT
)
204 format
= alGetEnumValue("AL_FORMAT_QUAD8");
205 else if(channels
== AL_5POINT1_SOFT
)
206 format
= alGetEnumValue("AL_FORMAT_51CHN8");
207 else if(channels
== AL_6POINT1_SOFT
)
208 format
= alGetEnumValue("AL_FORMAT_61CHN8");
209 else if(channels
== AL_7POINT1_SOFT
)
210 format
= alGetEnumValue("AL_FORMAT_71CHN8");
213 else if(type
== AL_SHORT_SOFT
)
215 if(channels
== AL_MONO_SOFT
)
216 format
= AL_FORMAT_MONO16
;
217 else if(channels
== AL_STEREO_SOFT
)
218 format
= AL_FORMAT_STEREO16
;
219 else if(alIsExtensionPresent("AL_EXT_MCFORMATS"))
221 if(channels
== AL_QUAD_SOFT
)
222 format
= alGetEnumValue("AL_FORMAT_QUAD16");
223 else if(channels
== AL_5POINT1_SOFT
)
224 format
= alGetEnumValue("AL_FORMAT_51CHN16");
225 else if(channels
== AL_6POINT1_SOFT
)
226 format
= alGetEnumValue("AL_FORMAT_61CHN16");
227 else if(channels
== AL_7POINT1_SOFT
)
228 format
= alGetEnumValue("AL_FORMAT_71CHN16");
231 else if(type
== AL_FLOAT_SOFT
&& alIsExtensionPresent("AL_EXT_FLOAT32"))
233 if(channels
== AL_MONO_SOFT
)
234 format
= alGetEnumValue("AL_FORMAT_MONO_FLOAT32");
235 else if(channels
== AL_STEREO_SOFT
)
236 format
= alGetEnumValue("AL_FORMAT_STEREO_FLOAT32");
237 else if(alIsExtensionPresent("AL_EXT_MCFORMATS"))
239 if(channels
== AL_QUAD_SOFT
)
240 format
= alGetEnumValue("AL_FORMAT_QUAD32");
241 else if(channels
== AL_5POINT1_SOFT
)
242 format
= alGetEnumValue("AL_FORMAT_51CHN32");
243 else if(channels
== AL_6POINT1_SOFT
)
244 format
= alGetEnumValue("AL_FORMAT_61CHN32");
245 else if(channels
== AL_7POINT1_SOFT
)
246 format
= alGetEnumValue("AL_FORMAT_71CHN32");
249 else if(type
== AL_DOUBLE_SOFT
&& alIsExtensionPresent("AL_EXT_DOUBLE"))
251 if(channels
== AL_MONO_SOFT
)
252 format
= alGetEnumValue("AL_FORMAT_MONO_DOUBLE");
253 else if(channels
== AL_STEREO_SOFT
)
254 format
= alGetEnumValue("AL_FORMAT_STEREO_DOUBLE");
257 /* NOTE: It seems OSX returns -1 from alGetEnumValue for unknown enums, as
258 * opposed to 0. Correct it. */
266 void AL_APIENTRY
wrap_BufferSamples(ALuint buffer
, ALuint samplerate
,
267 ALenum internalformat
, ALsizei samples
,
268 ALenum channels
, ALenum type
,
271 alBufferData(buffer
, internalformat
, data
,
272 FramesToBytes(samples
, channels
, type
),
282 /* Open and initialize a device with default settings */
283 device
= alcOpenDevice(NULL
);
286 fprintf(stderr
, "Could not open a device!\n");
290 ctx
= alcCreateContext(device
, NULL
);
291 if(ctx
== NULL
|| alcMakeContextCurrent(ctx
) == ALC_FALSE
)
294 alcDestroyContext(ctx
);
295 alcCloseDevice(device
);
296 fprintf(stderr
, "Could not set a context!\n");
308 /* Close the device belonging to the current context, and destroy the
310 ctx
= alcGetCurrentContext();
314 device
= alcGetContextsDevice(ctx
);
316 alcMakeContextCurrent(NULL
);
317 alcDestroyContext(ctx
);
318 alcCloseDevice(device
);