view OpenAL32/alBuffer.c @ 0:f9476ff7637e

initial forking of open-al to create multiple listeners
author Robert McIntyre <rlm@mit.edu>
date Tue, 25 Oct 2011 13:02:31 -0700
parents
children
line wrap: on
line source
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
19 */
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <limits.h>
28 #include "alMain.h"
29 #include "AL/al.h"
30 #include "AL/alc.h"
31 #include "alError.h"
32 #include "alBuffer.h"
33 #include "alThunk.h"
36 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels chans, enum UserFmtType type, const ALvoid *data, ALboolean storesrc);
37 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len);
38 static ALboolean IsValidType(ALenum type);
39 static ALboolean IsValidChannels(ALenum channels);
41 #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
44 /*
45 * Global Variables
46 */
48 /* IMA ADPCM Stepsize table */
49 static const long IMAStep_size[89] = {
50 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
51 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
52 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
53 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
54 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
55 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
56 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
57 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
58 32767
59 };
61 /* IMA4 ADPCM Codeword decode table */
62 static const long IMA4Codeword[16] = {
63 1, 3, 5, 7, 9, 11, 13, 15,
64 -1,-3,-5,-7,-9,-11,-13,-15,
65 };
67 /* IMA4 ADPCM Step index adjust decode table */
68 static const long IMA4Index_adjust[16] = {
69 -1,-1,-1,-1, 2, 4, 6, 8,
70 -1,-1,-1,-1, 2, 4, 6, 8
71 };
73 /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
74 * signed 16-bit sample */
75 static const ALshort muLawDecompressionTable[256] = {
76 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
77 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
78 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
79 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
80 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
81 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
82 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
83 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
84 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
85 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
86 -876, -844, -812, -780, -748, -716, -684, -652,
87 -620, -588, -556, -524, -492, -460, -428, -396,
88 -372, -356, -340, -324, -308, -292, -276, -260,
89 -244, -228, -212, -196, -180, -164, -148, -132,
90 -120, -112, -104, -96, -88, -80, -72, -64,
91 -56, -48, -40, -32, -24, -16, -8, 0,
92 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
93 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
94 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
95 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
96 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
97 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
98 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
99 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
100 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
101 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
102 876, 844, 812, 780, 748, 716, 684, 652,
103 620, 588, 556, 524, 492, 460, 428, 396,
104 372, 356, 340, 324, 308, 292, 276, 260,
105 244, 228, 212, 196, 180, 164, 148, 132,
106 120, 112, 104, 96, 88, 80, 72, 64,
107 56, 48, 40, 32, 24, 16, 8, 0
108 };
110 /* Values used when encoding a muLaw sample */
111 static const int muLawBias = 0x84;
112 static const int muLawClip = 32635;
113 static const char muLawCompressTable[256] =
114 {
115 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
116 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
117 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
118 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
119 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
120 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
121 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
122 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
123 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
124 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
125 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
126 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
127 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
128 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
129 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
130 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
131 };
133 /*
134 * alGenBuffers(ALsizei n, ALuint *buffers)
135 *
136 * Generates n AL Buffers, and stores the Buffers Names in the array pointed
137 * to by buffers
138 */
139 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
140 {
141 ALCcontext *Context;
142 ALsizei i=0;
144 Context = GetLockedContext();
145 if(!Context) return;
147 /* Check that we are actually generating some Buffers */
148 if(n < 0 || IsBadWritePtr((void*)buffers, n * sizeof(ALuint)))
149 alSetError(Context, AL_INVALID_VALUE);
150 else
151 {
152 ALCdevice *device = Context->Device;
153 ALenum err;
155 // Create all the new Buffers
156 while(i < n)
157 {
158 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
159 if(!buffer)
160 {
161 alSetError(Context, AL_OUT_OF_MEMORY);
162 alDeleteBuffers(i, buffers);
163 break;
164 }
166 err = NewThunkEntry(&buffer->buffer);
167 if(err == AL_NO_ERROR)
168 err = InsertUIntMapEntry(&device->BufferMap, buffer->buffer, buffer);
169 if(err != AL_NO_ERROR)
170 {
171 FreeThunkEntry(buffer->buffer);
172 memset(buffer, 0, sizeof(ALbuffer));
173 free(buffer);
175 alSetError(Context, err);
176 alDeleteBuffers(i, buffers);
177 break;
178 }
179 buffers[i++] = buffer->buffer;
180 }
181 }
183 UnlockContext(Context);
184 }
186 /*
187 * alDeleteBuffers(ALsizei n, ALuint *buffers)
188 *
189 * Deletes the n AL Buffers pointed to by buffers
190 */
191 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
192 {
193 ALCcontext *Context;
194 ALCdevice *device;
195 ALboolean Failed;
196 ALbuffer *ALBuf;
197 ALsizei i;
199 Context = GetLockedContext();
200 if(!Context) return;
202 Failed = AL_TRUE;
203 device = Context->Device;
204 /* Check we are actually Deleting some Buffers */
205 if(n < 0)
206 alSetError(Context, AL_INVALID_VALUE);
207 else
208 {
209 Failed = AL_FALSE;
211 /* Check that all the buffers are valid and can actually be deleted */
212 for(i = 0;i < n;i++)
213 {
214 if(!buffers[i])
215 continue;
217 /* Check for valid Buffer ID */
218 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
219 {
220 alSetError(Context, AL_INVALID_NAME);
221 Failed = AL_TRUE;
222 break;
223 }
224 else if(ALBuf->refcount != 0)
225 {
226 /* Buffer still in use, cannot be deleted */
227 alSetError(Context, AL_INVALID_OPERATION);
228 Failed = AL_TRUE;
229 break;
230 }
231 }
232 }
234 /* If all the Buffers were valid (and have Reference Counts of 0), then we
235 * can delete them */
236 if(!Failed)
237 {
238 for(i = 0;i < n;i++)
239 {
240 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
241 continue;
243 /* Release the memory used to store audio data */
244 free(ALBuf->data);
246 /* Release buffer structure */
247 RemoveUIntMapKey(&device->BufferMap, ALBuf->buffer);
248 FreeThunkEntry(ALBuf->buffer);
250 memset(ALBuf, 0, sizeof(ALbuffer));
251 free(ALBuf);
252 }
253 }
255 UnlockContext(Context);
256 }
258 /*
259 * alIsBuffer(ALuint buffer)
260 *
261 * Checks if buffer is a valid Buffer Name
262 */
263 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
264 {
265 ALCcontext *Context;
266 ALboolean result;
268 Context = GetLockedContext();
269 if(!Context) return AL_FALSE;
271 result = ((!buffer || LookupBuffer(Context->Device->BufferMap, buffer)) ?
272 AL_TRUE : AL_FALSE);
274 UnlockContext(Context);
276 return result;
277 }
279 /*
280 * alBufferData(ALuint buffer, ALenum format, const ALvoid *data,
281 * ALsizei size, ALsizei freq)
282 *
283 * Fill buffer with audio data
284 */
285 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
286 {
287 enum UserFmtChannels SrcChannels;
288 enum UserFmtType SrcType;
289 ALCcontext *Context;
290 ALCdevice *device;
291 ALbuffer *ALBuf;
292 ALenum err;
294 Context = GetLockedContext();
295 if(!Context) return;
297 device = Context->Device;
298 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
299 alSetError(Context, AL_INVALID_NAME);
300 else if(ALBuf->refcount != 0)
301 alSetError(Context, AL_INVALID_VALUE);
302 else if(size < 0 || freq < 0)
303 alSetError(Context, AL_INVALID_VALUE);
304 else if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
305 alSetError(Context, AL_INVALID_ENUM);
306 else switch(SrcType)
307 {
308 case UserFmtByte:
309 case UserFmtUByte:
310 case UserFmtShort:
311 case UserFmtUShort:
312 case UserFmtInt:
313 case UserFmtUInt:
314 case UserFmtFloat: {
315 ALuint FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
316 if((size%FrameSize) != 0)
317 err = AL_INVALID_VALUE;
318 else
319 err = LoadData(ALBuf, freq, format, size/FrameSize,
320 SrcChannels, SrcType, data, AL_TRUE);
321 if(err != AL_NO_ERROR)
322 alSetError(Context, err);
323 } break;
325 case UserFmtByte3:
326 case UserFmtUByte3:
327 case UserFmtDouble: {
328 ALuint FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
329 ALenum NewFormat = AL_FORMAT_MONO_FLOAT32;
330 switch(SrcChannels)
331 {
332 case UserFmtMono: NewFormat = AL_FORMAT_MONO_FLOAT32; break;
333 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO_FLOAT32; break;
334 case UserFmtRear: NewFormat = AL_FORMAT_REAR32; break;
335 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD32; break;
336 case UserFmtX51: NewFormat = AL_FORMAT_51CHN32; break;
337 case UserFmtX61: NewFormat = AL_FORMAT_61CHN32; break;
338 case UserFmtX71: NewFormat = AL_FORMAT_71CHN32; break;
339 }
340 if((size%FrameSize) != 0)
341 err = AL_INVALID_VALUE;
342 else
343 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
344 SrcChannels, SrcType, data, AL_TRUE);
345 if(err != AL_NO_ERROR)
346 alSetError(Context, err);
347 } break;
349 case UserFmtMulaw:
350 case UserFmtIMA4: {
351 /* Here is where things vary:
352 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
353 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
354 */
355 ALuint FrameSize = (SrcType == UserFmtIMA4) ?
356 (ChannelsFromUserFmt(SrcChannels) * 36) :
357 FrameSizeFromUserFmt(SrcChannels, SrcType);
358 ALenum NewFormat = AL_FORMAT_MONO16;
359 switch(SrcChannels)
360 {
361 case UserFmtMono: NewFormat = AL_FORMAT_MONO16; break;
362 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
363 case UserFmtRear: NewFormat = AL_FORMAT_REAR16; break;
364 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
365 case UserFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
366 case UserFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
367 case UserFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
368 }
369 if((size%FrameSize) != 0)
370 err = AL_INVALID_VALUE;
371 else
372 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
373 SrcChannels, SrcType, data, AL_TRUE);
374 if(err != AL_NO_ERROR)
375 alSetError(Context, err);
376 } break;
377 }
379 UnlockContext(Context);
380 }
382 /*
383 * alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data,
384 * ALsizei offset, ALsizei length)
385 *
386 * Update buffer's audio data
387 */
388 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
389 {
390 enum UserFmtChannels SrcChannels;
391 enum UserFmtType SrcType;
392 ALCcontext *Context;
393 ALCdevice *device;
394 ALbuffer *ALBuf;
396 Context = GetLockedContext();
397 if(!Context) return;
399 device = Context->Device;
400 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
401 alSetError(Context, AL_INVALID_NAME);
402 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
403 alSetError(Context, AL_INVALID_VALUE);
404 else if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE ||
405 SrcChannels != ALBuf->OriginalChannels ||
406 SrcType != ALBuf->OriginalType)
407 alSetError(Context, AL_INVALID_ENUM);
408 else if(offset > ALBuf->OriginalSize ||
409 length > ALBuf->OriginalSize-offset ||
410 (offset%ALBuf->OriginalAlign) != 0 ||
411 (length%ALBuf->OriginalAlign) != 0)
412 alSetError(Context, AL_INVALID_VALUE);
413 else
414 {
415 ALuint Channels = ChannelsFromFmt(ALBuf->FmtChannels);
416 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
417 if(SrcType == UserFmtIMA4)
418 {
419 /* offset -> byte offset, length -> block count */
420 offset /= 36;
421 offset *= 65;
422 offset *= Bytes;
423 length /= ALBuf->OriginalAlign;
424 }
425 else
426 {
427 ALuint OldBytes = BytesFromUserFmt(SrcType);
429 offset /= OldBytes;
430 offset *= Bytes;
431 length /= OldBytes * Channels;
432 }
433 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
434 data, SrcType, Channels, length);
435 }
437 UnlockContext(Context);
438 }
441 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer,
442 ALuint samplerate, ALenum internalformat, ALsizei frames,
443 ALenum channels, ALenum type, const ALvoid *data)
444 {
445 ALCcontext *Context;
446 ALCdevice *device;
447 ALbuffer *ALBuf;
448 ALenum err;
450 Context = GetLockedContext();
451 if(!Context) return;
453 device = Context->Device;
454 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
455 alSetError(Context, AL_INVALID_NAME);
456 else if(ALBuf->refcount != 0)
457 alSetError(Context, AL_INVALID_VALUE);
458 else if(frames < 0 || samplerate == 0)
459 alSetError(Context, AL_INVALID_VALUE);
460 else if(IsValidType(type) == AL_FALSE || IsValidChannels(channels) == AL_FALSE)
461 alSetError(Context, AL_INVALID_ENUM);
462 else
463 {
464 err = AL_NO_ERROR;
465 if(type == UserFmtIMA4)
466 {
467 if((frames%65) == 0) frames /= 65;
468 else err = AL_INVALID_VALUE;
469 }
470 if(err == AL_NO_ERROR)
471 err = LoadData(ALBuf, samplerate, internalformat, frames,
472 channels, type, data, AL_FALSE);
473 if(err != AL_NO_ERROR)
474 alSetError(Context, err);
475 }
477 UnlockContext(Context);
478 }
480 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint buffer,
481 ALsizei offset, ALsizei frames,
482 ALenum channels, ALenum type, const ALvoid *data)
483 {
484 ALCcontext *Context;
485 ALCdevice *device;
486 ALbuffer *ALBuf;
488 Context = GetLockedContext();
489 if(!Context) return;
491 device = Context->Device;
492 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
493 alSetError(Context, AL_INVALID_NAME);
494 else if(frames < 0 || offset < 0 || (frames > 0 && data == NULL))
495 alSetError(Context, AL_INVALID_VALUE);
496 else if(channels != (ALenum)ALBuf->FmtChannels ||
497 IsValidType(type) == AL_FALSE)
498 alSetError(Context, AL_INVALID_ENUM);
499 else
500 {
501 ALuint FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
502 ALuint FrameCount = ALBuf->size / FrameSize;
503 if((ALuint)offset > FrameCount || (ALuint)frames > FrameCount-offset)
504 alSetError(Context, AL_INVALID_VALUE);
505 else if(type == UserFmtIMA4 && (frames%65) != 0)
506 alSetError(Context, AL_INVALID_VALUE);
507 else
508 {
509 /* offset -> byte offset */
510 offset *= FrameSize;
511 /* frames -> IMA4 block count */
512 if(type == UserFmtIMA4) frames /= 65;
513 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
514 data, type,
515 ChannelsFromFmt(ALBuf->FmtChannels), frames);
516 }
517 }
519 UnlockContext(Context);
520 }
522 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer,
523 ALsizei offset, ALsizei frames,
524 ALenum channels, ALenum type, ALvoid *data)
525 {
526 ALCcontext *Context;
527 ALCdevice *device;
528 ALbuffer *ALBuf;
530 Context = GetLockedContext();
531 if(!Context) return;
533 device = Context->Device;
534 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
535 alSetError(Context, AL_INVALID_NAME);
536 else if(frames < 0 || offset < 0 || (frames > 0 && data == NULL))
537 alSetError(Context, AL_INVALID_VALUE);
538 else if(channels != (ALenum)ALBuf->FmtChannels ||
539 IsValidType(type) == AL_FALSE)
540 alSetError(Context, AL_INVALID_ENUM);
541 else
542 {
543 ALuint FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
544 ALuint FrameCount = ALBuf->size / FrameSize;
545 if((ALuint)offset > FrameCount || (ALuint)frames > FrameCount-offset)
546 alSetError(Context, AL_INVALID_VALUE);
547 else if(type == UserFmtIMA4 && (frames%65) != 0)
548 alSetError(Context, AL_INVALID_VALUE);
549 else
550 {
551 /* offset -> byte offset */
552 offset *= FrameSize;
553 /* frames -> IMA4 block count */
554 if(type == UserFmtIMA4) frames /= 65;
555 ConvertData(data, type,
556 &((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
557 ChannelsFromFmt(ALBuf->FmtChannels), frames);
558 }
559 }
561 UnlockContext(Context);
562 }
564 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format)
565 {
566 enum FmtChannels DstChannels;
567 enum FmtType DstType;
568 ALCcontext *Context;
569 ALboolean ret;
571 Context = GetLockedContext();
572 if(!Context) return AL_FALSE;
574 ret = DecomposeFormat(format, &DstChannels, &DstType);
576 UnlockContext(Context);
578 return ret;
579 }
582 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
583 {
584 ALCcontext *pContext;
585 ALCdevice *device;
587 (void)flValue;
589 pContext = GetLockedContext();
590 if(!pContext) return;
592 device = pContext->Device;
593 if(LookupBuffer(device->BufferMap, buffer) == NULL)
594 alSetError(pContext, AL_INVALID_NAME);
595 else
596 {
597 switch(eParam)
598 {
599 default:
600 alSetError(pContext, AL_INVALID_ENUM);
601 break;
602 }
603 }
605 UnlockContext(pContext);
606 }
609 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
610 {
611 ALCcontext *pContext;
612 ALCdevice *device;
614 (void)flValue1;
615 (void)flValue2;
616 (void)flValue3;
618 pContext = GetLockedContext();
619 if(!pContext) return;
621 device = pContext->Device;
622 if(LookupBuffer(device->BufferMap, buffer) == NULL)
623 alSetError(pContext, AL_INVALID_NAME);
624 else
625 {
626 switch(eParam)
627 {
628 default:
629 alSetError(pContext, AL_INVALID_ENUM);
630 break;
631 }
632 }
634 UnlockContext(pContext);
635 }
638 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
639 {
640 ALCcontext *pContext;
641 ALCdevice *device;
643 pContext = GetLockedContext();
644 if(!pContext) return;
646 device = pContext->Device;
647 if(!flValues)
648 alSetError(pContext, AL_INVALID_VALUE);
649 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
650 alSetError(pContext, AL_INVALID_NAME);
651 else
652 {
653 switch(eParam)
654 {
655 default:
656 alSetError(pContext, AL_INVALID_ENUM);
657 break;
658 }
659 }
661 UnlockContext(pContext);
662 }
665 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
666 {
667 ALCcontext *pContext;
668 ALCdevice *device;
670 (void)lValue;
672 pContext = GetLockedContext();
673 if(!pContext) return;
675 device = pContext->Device;
676 if(LookupBuffer(device->BufferMap, buffer) == NULL)
677 alSetError(pContext, AL_INVALID_NAME);
678 else
679 {
680 switch(eParam)
681 {
682 default:
683 alSetError(pContext, AL_INVALID_ENUM);
684 break;
685 }
686 }
688 UnlockContext(pContext);
689 }
692 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
693 {
694 ALCcontext *pContext;
695 ALCdevice *device;
697 (void)lValue1;
698 (void)lValue2;
699 (void)lValue3;
701 pContext = GetLockedContext();
702 if(!pContext) return;
704 device = pContext->Device;
705 if(LookupBuffer(device->BufferMap, buffer) == NULL)
706 alSetError(pContext, AL_INVALID_NAME);
707 else
708 {
709 switch(eParam)
710 {
711 default:
712 alSetError(pContext, AL_INVALID_ENUM);
713 break;
714 }
715 }
717 UnlockContext(pContext);
718 }
721 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
722 {
723 ALCcontext *pContext;
724 ALCdevice *device;
725 ALbuffer *ALBuf;
727 pContext = GetLockedContext();
728 if(!pContext) return;
730 device = pContext->Device;
731 if(!plValues)
732 alSetError(pContext, AL_INVALID_VALUE);
733 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
734 alSetError(pContext, AL_INVALID_NAME);
735 else
736 {
737 switch(eParam)
738 {
739 case AL_LOOP_POINTS_SOFT:
740 if(ALBuf->refcount > 0)
741 alSetError(pContext, AL_INVALID_OPERATION);
742 else if(plValues[0] < 0 || plValues[1] < 0 ||
743 plValues[0] >= plValues[1] || ALBuf->size == 0)
744 alSetError(pContext, AL_INVALID_VALUE);
745 else
746 {
747 ALint maxlen = ALBuf->size /
748 FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
749 if(plValues[0] > maxlen || plValues[1] > maxlen)
750 alSetError(pContext, AL_INVALID_VALUE);
751 else
752 {
753 ALBuf->LoopStart = plValues[0];
754 ALBuf->LoopEnd = plValues[1];
755 }
756 }
757 break;
759 default:
760 alSetError(pContext, AL_INVALID_ENUM);
761 break;
762 }
763 }
765 UnlockContext(pContext);
766 }
769 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
770 {
771 ALCcontext *pContext;
772 ALCdevice *device;
774 pContext = GetLockedContext();
775 if(!pContext) return;
777 device = pContext->Device;
778 if(!pflValue)
779 alSetError(pContext, AL_INVALID_VALUE);
780 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
781 alSetError(pContext, AL_INVALID_NAME);
782 else
783 {
784 switch(eParam)
785 {
786 default:
787 alSetError(pContext, AL_INVALID_ENUM);
788 break;
789 }
790 }
792 UnlockContext(pContext);
793 }
796 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
797 {
798 ALCcontext *pContext;
799 ALCdevice *device;
801 pContext = GetLockedContext();
802 if(!pContext) return;
804 device = pContext->Device;
805 if(!pflValue1 || !pflValue2 || !pflValue3)
806 alSetError(pContext, AL_INVALID_VALUE);
807 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
808 alSetError(pContext, AL_INVALID_NAME);
809 else
810 {
811 switch(eParam)
812 {
813 default:
814 alSetError(pContext, AL_INVALID_ENUM);
815 break;
816 }
817 }
819 UnlockContext(pContext);
820 }
823 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
824 {
825 ALCcontext *pContext;
826 ALCdevice *device;
828 pContext = GetLockedContext();
829 if(!pContext) return;
831 device = pContext->Device;
832 if(!pflValues)
833 alSetError(pContext, AL_INVALID_VALUE);
834 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
835 alSetError(pContext, AL_INVALID_NAME);
836 else
837 {
838 switch(eParam)
839 {
840 default:
841 alSetError(pContext, AL_INVALID_ENUM);
842 break;
843 }
844 }
846 UnlockContext(pContext);
847 }
850 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
851 {
852 ALCcontext *pContext;
853 ALbuffer *pBuffer;
854 ALCdevice *device;
856 pContext = GetLockedContext();
857 if(!pContext) return;
859 device = pContext->Device;
860 if(!plValue)
861 alSetError(pContext, AL_INVALID_VALUE);
862 else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
863 alSetError(pContext, AL_INVALID_NAME);
864 else
865 {
866 switch(eParam)
867 {
868 case AL_FREQUENCY:
869 *plValue = pBuffer->Frequency;
870 break;
872 case AL_BITS:
873 *plValue = BytesFromFmt(pBuffer->FmtType) * 8;
874 break;
876 case AL_CHANNELS:
877 *plValue = ChannelsFromFmt(pBuffer->FmtChannels);
878 break;
880 case AL_SIZE:
881 *plValue = pBuffer->size;
882 break;
884 default:
885 alSetError(pContext, AL_INVALID_ENUM);
886 break;
887 }
888 }
890 UnlockContext(pContext);
891 }
894 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
895 {
896 ALCcontext *pContext;
897 ALCdevice *device;
899 pContext = GetLockedContext();
900 if(!pContext) return;
902 device = pContext->Device;
903 if(!plValue1 || !plValue2 || !plValue3)
904 alSetError(pContext, AL_INVALID_VALUE);
905 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
906 alSetError(pContext, AL_INVALID_NAME);
907 else
908 {
909 switch(eParam)
910 {
911 default:
912 alSetError(pContext, AL_INVALID_ENUM);
913 break;
914 }
915 }
917 UnlockContext(pContext);
918 }
921 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
922 {
923 ALCcontext *pContext;
924 ALCdevice *device;
925 ALbuffer *ALBuf;
927 switch(eParam)
928 {
929 case AL_FREQUENCY:
930 case AL_BITS:
931 case AL_CHANNELS:
932 case AL_SIZE:
933 alGetBufferi(buffer, eParam, plValues);
934 return;
935 }
937 pContext = GetLockedContext();
938 if(!pContext) return;
940 device = pContext->Device;
941 if(!plValues)
942 alSetError(pContext, AL_INVALID_VALUE);
943 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
944 alSetError(pContext, AL_INVALID_NAME);
945 else
946 {
947 switch(eParam)
948 {
949 case AL_LOOP_POINTS_SOFT:
950 plValues[0] = ALBuf->LoopStart;
951 plValues[1] = ALBuf->LoopEnd;
952 break;
954 default:
955 alSetError(pContext, AL_INVALID_ENUM);
956 break;
957 }
958 }
960 UnlockContext(pContext);
961 }
964 typedef ALubyte ALmulaw;
965 typedef ALubyte ALima4;
966 typedef struct {
967 ALbyte b[3];
968 } ALbyte3;
969 typedef struct {
970 ALubyte b[3];
971 } ALubyte3;
973 static __inline ALshort DecodeMuLaw(ALmulaw val)
974 { return muLawDecompressionTable[val]; }
976 static ALmulaw EncodeMuLaw(ALshort val)
977 {
978 ALint mant, exp, sign;
980 sign = (val>>8) & 0x80;
981 if(sign)
982 {
983 /* -32768 doesn't properly negate on a short; it results in itself.
984 * So clamp to -32767 */
985 val = maxi(val, -32767);
986 val = -val;
987 }
989 val = mini(val, muLawClip);
990 val += muLawBias;
992 exp = muLawCompressTable[(val>>7) & 0xff];
993 mant = (val >> (exp+3)) & 0x0f;
995 return ~(sign | (exp<<4) | mant);
996 }
998 static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans)
999 {
1000 ALint sample[MAXCHANNELS], index[MAXCHANNELS];
1001 ALuint code[MAXCHANNELS];
1002 ALsizei j,k,c;
1004 for(c = 0;c < numchans;c++)
1006 sample[c] = *(src++);
1007 sample[c] |= *(src++) << 8;
1008 sample[c] = (sample[c]^0x8000) - 32768;
1009 index[c] = *(src++);
1010 index[c] |= *(src++) << 8;
1011 index[c] = (index[c]^0x8000) - 32768;
1013 index[c] = clampi(index[c], 0, 88);
1015 dst[c] = sample[c];
1018 j = 1;
1019 while(j < 65)
1021 for(c = 0;c < numchans;c++)
1023 code[c] = *(src++);
1024 code[c] |= *(src++) << 8;
1025 code[c] |= *(src++) << 16;
1026 code[c] |= *(src++) << 24;
1029 for(k = 0;k < 8;k++,j++)
1031 for(c = 0;c < numchans;c++)
1033 int nibble = code[c]&0xf;
1034 code[c] >>= 4;
1036 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
1037 sample[c] = clampi(sample[c], -32768, 32767);
1039 index[c] += IMA4Index_adjust[nibble];
1040 index[c] = clampi(index[c], 0, 88);
1042 dst[j*numchans + c] = sample[c];
1048 static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans)
1050 ALsizei j,k,c;
1052 for(c = 0;c < numchans;c++)
1054 int diff = src[c] - sample[c];
1055 int step = IMAStep_size[index[c]];
1056 int nibble;
1058 nibble = 0;
1059 if(diff < 0)
1061 nibble = 0x8;
1062 diff = -diff;
1065 diff = mini(step*2, diff);
1066 nibble |= (diff*8/step - 1) / 2;
1068 sample[c] += IMA4Codeword[nibble] * step / 8;
1069 sample[c] = clampi(sample[c], -32768, 32767);
1071 index[c] += IMA4Index_adjust[nibble];
1072 index[c] = clampi(index[c], 0, 88);
1074 *(dst++) = sample[c] & 0xff;
1075 *(dst++) = (sample[c]>>8) & 0xff;
1076 *(dst++) = index[c] & 0xff;
1077 *(dst++) = (index[c]>>8) & 0xff;
1080 j = 1;
1081 while(j < 65)
1083 for(c = 0;c < numchans;c++)
1085 for(k = 0;k < 8;k++)
1087 int diff = src[(j+k)*numchans + c] - sample[c];
1088 int step = IMAStep_size[index[c]];
1089 int nibble;
1091 nibble = 0;
1092 if(diff < 0)
1094 nibble = 0x8;
1095 diff = -diff;
1098 diff = mini(step*2, diff);
1099 nibble |= (diff*8/step - 1) / 2;
1101 sample[c] += IMA4Codeword[nibble] * step / 8;
1102 sample[c] = clampi(sample[c], -32768, 32767);
1104 index[c] += IMA4Index_adjust[nibble];
1105 index[c] = clampi(index[c], 0, 88);
1107 if(!(k&1)) *dst = nibble;
1108 else *(dst++) |= nibble<<4;
1111 j += 8;
1115 static const union {
1116 ALuint u;
1117 ALubyte b[sizeof(ALuint)];
1118 } EndianTest = { 1 };
1119 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
1121 static __inline ALint DecodeByte3(ALbyte3 val)
1123 if(IS_LITTLE_ENDIAN)
1124 return (val.b[2]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[0]);
1125 return (val.b[0]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[2]);
1128 static __inline ALbyte3 EncodeByte3(ALint val)
1130 if(IS_LITTLE_ENDIAN)
1132 ALbyte3 ret = {{ val, val>>8, val>>16 }};
1133 return ret;
1135 else
1137 ALbyte3 ret = {{ val>>16, val>>8, val }};
1138 return ret;
1142 static __inline ALint DecodeUByte3(ALubyte3 val)
1144 if(IS_LITTLE_ENDIAN)
1145 return (val.b[2]<<16) | (val.b[1]<<8) | (val.b[0]);
1146 return (val.b[0]<<16) | (val.b[1]<<8) | val.b[2];
1149 static __inline ALubyte3 EncodeUByte3(ALint val)
1151 if(IS_LITTLE_ENDIAN)
1153 ALubyte3 ret = {{ val, val>>8, val>>16 }};
1154 return ret;
1156 else
1158 ALubyte3 ret = {{ val>>16, val>>8, val }};
1159 return ret;
1164 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
1165 { return val; }
1166 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
1167 { return val-128; }
1168 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
1169 { return val>>8; }
1170 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
1171 { return (val>>8)-128; }
1172 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
1173 { return val>>24; }
1174 static __inline ALbyte Conv_ALbyte_ALuint(ALuint val)
1175 { return (val>>24)-128; }
1176 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
1178 if(val > 1.0f) return 127;
1179 if(val < -1.0f) return -128;
1180 return (ALint)(val * 127.0f);
1182 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
1184 if(val > 1.0) return 127;
1185 if(val < -1.0) return -128;
1186 return (ALint)(val * 127.0);
1188 static __inline ALbyte Conv_ALbyte_ALmulaw(ALmulaw val)
1189 { return Conv_ALbyte_ALshort(DecodeMuLaw(val)); }
1190 static __inline ALbyte Conv_ALbyte_ALbyte3(ALbyte3 val)
1191 { return DecodeByte3(val)>>16; }
1192 static __inline ALbyte Conv_ALbyte_ALubyte3(ALubyte3 val)
1193 { return (DecodeUByte3(val)>>16)-128; }
1195 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
1196 { return val+128; }
1197 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
1198 { return val; }
1199 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
1200 { return (val>>8)+128; }
1201 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
1202 { return val>>8; }
1203 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
1204 { return (val>>24)+128; }
1205 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
1206 { return val>>24; }
1207 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
1209 if(val > 1.0f) return 255;
1210 if(val < -1.0f) return 0;
1211 return (ALint)(val * 127.0f) + 128;
1213 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
1215 if(val > 1.0) return 255;
1216 if(val < -1.0) return 0;
1217 return (ALint)(val * 127.0) + 128;
1219 static __inline ALubyte Conv_ALubyte_ALmulaw(ALmulaw val)
1220 { return Conv_ALubyte_ALshort(DecodeMuLaw(val)); }
1221 static __inline ALubyte Conv_ALubyte_ALbyte3(ALbyte3 val)
1222 { return (DecodeByte3(val)>>16)+128; }
1223 static __inline ALubyte Conv_ALubyte_ALubyte3(ALubyte3 val)
1224 { return DecodeUByte3(val)>>16; }
1226 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
1227 { return val<<8; }
1228 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
1229 { return (val-128)<<8; }
1230 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
1231 { return val; }
1232 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
1233 { return val-32768; }
1234 static __inline ALshort Conv_ALshort_ALint(ALint val)
1235 { return val>>16; }
1236 static __inline ALshort Conv_ALshort_ALuint(ALuint val)
1237 { return (val>>16)-32768; }
1238 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
1240 if(val > 1.0f) return 32767;
1241 if(val < -1.0f) return -32768;
1242 return (ALint)(val * 32767.0f);
1244 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
1246 if(val > 1.0) return 32767;
1247 if(val < -1.0) return -32768;
1248 return (ALint)(val * 32767.0);
1250 static __inline ALshort Conv_ALshort_ALmulaw(ALmulaw val)
1251 { return Conv_ALshort_ALshort(DecodeMuLaw(val)); }
1252 static __inline ALshort Conv_ALshort_ALbyte3(ALbyte3 val)
1253 { return DecodeByte3(val)>>8; }
1254 static __inline ALshort Conv_ALshort_ALubyte3(ALubyte3 val)
1255 { return (DecodeUByte3(val)>>8)-32768; }
1257 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
1258 { return (val+128)<<8; }
1259 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
1260 { return val<<8; }
1261 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
1262 { return val+32768; }
1263 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
1264 { return val; }
1265 static __inline ALushort Conv_ALushort_ALint(ALint val)
1266 { return (val>>16)+32768; }
1267 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
1268 { return val>>16; }
1269 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
1271 if(val > 1.0f) return 65535;
1272 if(val < -1.0f) return 0;
1273 return (ALint)(val * 32767.0f) + 32768;
1275 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
1277 if(val > 1.0) return 65535;
1278 if(val < -1.0) return 0;
1279 return (ALint)(val * 32767.0) + 32768;
1281 static __inline ALushort Conv_ALushort_ALmulaw(ALmulaw val)
1282 { return Conv_ALushort_ALshort(DecodeMuLaw(val)); }
1283 static __inline ALushort Conv_ALushort_ALbyte3(ALbyte3 val)
1284 { return (DecodeByte3(val)>>8)+32768; }
1285 static __inline ALushort Conv_ALushort_ALubyte3(ALubyte3 val)
1286 { return DecodeUByte3(val)>>8; }
1288 static __inline ALint Conv_ALint_ALbyte(ALbyte val)
1289 { return val<<24; }
1290 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
1291 { return (val-128)<<24; }
1292 static __inline ALint Conv_ALint_ALshort(ALshort val)
1293 { return val<<16; }
1294 static __inline ALint Conv_ALint_ALushort(ALushort val)
1295 { return (val-32768)<<16; }
1296 static __inline ALint Conv_ALint_ALint(ALint val)
1297 { return val; }
1298 static __inline ALint Conv_ALint_ALuint(ALuint val)
1299 { return val-2147483648u; }
1300 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
1302 if(val > 1.0f) return 2147483647;
1303 if(val < -1.0f) return -2147483647-1;
1304 return (ALint)(val * 2147483647.0);
1306 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
1308 if(val > 1.0) return 2147483647;
1309 if(val < -1.0) return -2147483647-1;
1310 return (ALint)(val * 2147483647.0);
1312 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
1313 { return Conv_ALint_ALshort(DecodeMuLaw(val)); }
1314 static __inline ALint Conv_ALint_ALbyte3(ALbyte3 val)
1315 { return DecodeByte3(val)<<8; }
1316 static __inline ALint Conv_ALint_ALubyte3(ALubyte3 val)
1317 { return (DecodeUByte3(val)-8388608)<<8; }
1319 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
1320 { return (val+128)<<24; }
1321 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
1322 { return val<<24; }
1323 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
1324 { return (val+32768)<<16; }
1325 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
1326 { return val<<16; }
1327 static __inline ALuint Conv_ALuint_ALint(ALint val)
1328 { return val+2147483648u; }
1329 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
1330 { return val; }
1331 static __inline ALuint Conv_ALuint_ALfloat(ALfloat val)
1333 if(val > 1.0f) return 4294967295u;
1334 if(val < -1.0f) return 0;
1335 return (ALint)(val * 2147483647.0) + 2147483648u;
1337 static __inline ALuint Conv_ALuint_ALdouble(ALdouble val)
1339 if(val > 1.0) return 4294967295u;
1340 if(val < -1.0) return 0;
1341 return (ALint)(val * 2147483647.0) + 2147483648u;
1343 static __inline ALuint Conv_ALuint_ALmulaw(ALmulaw val)
1344 { return Conv_ALuint_ALshort(DecodeMuLaw(val)); }
1345 static __inline ALuint Conv_ALuint_ALbyte3(ALbyte3 val)
1346 { return (DecodeByte3(val)+8388608)<<8; }
1347 static __inline ALuint Conv_ALuint_ALubyte3(ALubyte3 val)
1348 { return DecodeUByte3(val)<<8; }
1350 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
1351 { return val * (1.0f/127.0f); }
1352 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
1353 { return (val-128) * (1.0f/127.0f); }
1354 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
1355 { return val * (1.0f/32767.0f); }
1356 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
1357 { return (val-32768) * (1.0f/32767.0f); }
1358 static __inline ALfloat Conv_ALfloat_ALint(ALint val)
1359 { return val * (1.0/2147483647.0); }
1360 static __inline ALfloat Conv_ALfloat_ALuint(ALuint val)
1361 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1362 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
1363 { return (val==val) ? val : 0.0f; }
1364 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1365 { return (val==val) ? val : 0.0; }
1366 static __inline ALfloat Conv_ALfloat_ALmulaw(ALmulaw val)
1367 { return Conv_ALfloat_ALshort(DecodeMuLaw(val)); }
1368 static __inline ALfloat Conv_ALfloat_ALbyte3(ALbyte3 val)
1369 { return DecodeByte3(val) * (1.0/8388607.0); }
1370 static __inline ALfloat Conv_ALfloat_ALubyte3(ALubyte3 val)
1371 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
1373 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
1374 { return val * (1.0/127.0); }
1375 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
1376 { return (val-128) * (1.0/127.0); }
1377 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
1378 { return val * (1.0/32767.0); }
1379 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
1380 { return (val-32768) * (1.0/32767.0); }
1381 static __inline ALdouble Conv_ALdouble_ALint(ALint val)
1382 { return val * (1.0/2147483647.0); }
1383 static __inline ALdouble Conv_ALdouble_ALuint(ALuint val)
1384 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1385 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
1386 { return (val==val) ? val : 0.0f; }
1387 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1388 { return (val==val) ? val : 0.0; }
1389 static __inline ALdouble Conv_ALdouble_ALmulaw(ALmulaw val)
1390 { return Conv_ALdouble_ALshort(DecodeMuLaw(val)); }
1391 static __inline ALdouble Conv_ALdouble_ALbyte3(ALbyte3 val)
1392 { return DecodeByte3(val) * (1.0/8388607.0); }
1393 static __inline ALdouble Conv_ALdouble_ALubyte3(ALubyte3 val)
1394 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
1396 #define DECL_TEMPLATE(T) \
1397 static __inline ALmulaw Conv_ALmulaw_##T(T val) \
1398 { return EncodeMuLaw(Conv_ALshort_##T(val)); }
1400 DECL_TEMPLATE(ALbyte)
1401 DECL_TEMPLATE(ALubyte)
1402 DECL_TEMPLATE(ALshort)
1403 DECL_TEMPLATE(ALushort)
1404 DECL_TEMPLATE(ALint)
1405 DECL_TEMPLATE(ALuint)
1406 DECL_TEMPLATE(ALfloat)
1407 DECL_TEMPLATE(ALdouble)
1408 static __inline ALmulaw Conv_ALmulaw_ALmulaw(ALmulaw val)
1409 { return val; }
1410 DECL_TEMPLATE(ALbyte3)
1411 DECL_TEMPLATE(ALubyte3)
1413 #undef DECL_TEMPLATE
1415 #define DECL_TEMPLATE(T) \
1416 static __inline ALbyte3 Conv_ALbyte3_##T(T val) \
1417 { return EncodeByte3(Conv_ALint_##T(val)>>8); }
1419 DECL_TEMPLATE(ALbyte)
1420 DECL_TEMPLATE(ALubyte)
1421 DECL_TEMPLATE(ALshort)
1422 DECL_TEMPLATE(ALushort)
1423 DECL_TEMPLATE(ALint)
1424 DECL_TEMPLATE(ALuint)
1425 DECL_TEMPLATE(ALfloat)
1426 DECL_TEMPLATE(ALdouble)
1427 DECL_TEMPLATE(ALmulaw)
1428 static __inline ALbyte3 Conv_ALbyte3_ALbyte3(ALbyte3 val)
1429 { return val; }
1430 DECL_TEMPLATE(ALubyte3)
1432 #undef DECL_TEMPLATE
1434 #define DECL_TEMPLATE(T) \
1435 static __inline ALubyte3 Conv_ALubyte3_##T(T val) \
1436 { return EncodeUByte3(Conv_ALuint_##T(val)>>8); }
1438 DECL_TEMPLATE(ALbyte)
1439 DECL_TEMPLATE(ALubyte)
1440 DECL_TEMPLATE(ALshort)
1441 DECL_TEMPLATE(ALushort)
1442 DECL_TEMPLATE(ALint)
1443 DECL_TEMPLATE(ALuint)
1444 DECL_TEMPLATE(ALfloat)
1445 DECL_TEMPLATE(ALdouble)
1446 DECL_TEMPLATE(ALmulaw)
1447 DECL_TEMPLATE(ALbyte3)
1448 static __inline ALubyte3 Conv_ALubyte3_ALubyte3(ALubyte3 val)
1449 { return val; }
1451 #undef DECL_TEMPLATE
1454 #define DECL_TEMPLATE(T1, T2) \
1455 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
1456 ALuint len) \
1457 { \
1458 ALuint i, j; \
1459 for(i = 0;i < len;i++) \
1460 { \
1461 for(j = 0;j < numchans;j++) \
1462 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1463 } \
1466 DECL_TEMPLATE(ALbyte, ALbyte)
1467 DECL_TEMPLATE(ALbyte, ALubyte)
1468 DECL_TEMPLATE(ALbyte, ALshort)
1469 DECL_TEMPLATE(ALbyte, ALushort)
1470 DECL_TEMPLATE(ALbyte, ALint)
1471 DECL_TEMPLATE(ALbyte, ALuint)
1472 DECL_TEMPLATE(ALbyte, ALfloat)
1473 DECL_TEMPLATE(ALbyte, ALdouble)
1474 DECL_TEMPLATE(ALbyte, ALmulaw)
1475 DECL_TEMPLATE(ALbyte, ALbyte3)
1476 DECL_TEMPLATE(ALbyte, ALubyte3)
1478 DECL_TEMPLATE(ALubyte, ALbyte)
1479 DECL_TEMPLATE(ALubyte, ALubyte)
1480 DECL_TEMPLATE(ALubyte, ALshort)
1481 DECL_TEMPLATE(ALubyte, ALushort)
1482 DECL_TEMPLATE(ALubyte, ALint)
1483 DECL_TEMPLATE(ALubyte, ALuint)
1484 DECL_TEMPLATE(ALubyte, ALfloat)
1485 DECL_TEMPLATE(ALubyte, ALdouble)
1486 DECL_TEMPLATE(ALubyte, ALmulaw)
1487 DECL_TEMPLATE(ALubyte, ALbyte3)
1488 DECL_TEMPLATE(ALubyte, ALubyte3)
1490 DECL_TEMPLATE(ALshort, ALbyte)
1491 DECL_TEMPLATE(ALshort, ALubyte)
1492 DECL_TEMPLATE(ALshort, ALshort)
1493 DECL_TEMPLATE(ALshort, ALushort)
1494 DECL_TEMPLATE(ALshort, ALint)
1495 DECL_TEMPLATE(ALshort, ALuint)
1496 DECL_TEMPLATE(ALshort, ALfloat)
1497 DECL_TEMPLATE(ALshort, ALdouble)
1498 DECL_TEMPLATE(ALshort, ALmulaw)
1499 DECL_TEMPLATE(ALshort, ALbyte3)
1500 DECL_TEMPLATE(ALshort, ALubyte3)
1502 DECL_TEMPLATE(ALushort, ALbyte)
1503 DECL_TEMPLATE(ALushort, ALubyte)
1504 DECL_TEMPLATE(ALushort, ALshort)
1505 DECL_TEMPLATE(ALushort, ALushort)
1506 DECL_TEMPLATE(ALushort, ALint)
1507 DECL_TEMPLATE(ALushort, ALuint)
1508 DECL_TEMPLATE(ALushort, ALfloat)
1509 DECL_TEMPLATE(ALushort, ALdouble)
1510 DECL_TEMPLATE(ALushort, ALmulaw)
1511 DECL_TEMPLATE(ALushort, ALbyte3)
1512 DECL_TEMPLATE(ALushort, ALubyte3)
1514 DECL_TEMPLATE(ALint, ALbyte)
1515 DECL_TEMPLATE(ALint, ALubyte)
1516 DECL_TEMPLATE(ALint, ALshort)
1517 DECL_TEMPLATE(ALint, ALushort)
1518 DECL_TEMPLATE(ALint, ALint)
1519 DECL_TEMPLATE(ALint, ALuint)
1520 DECL_TEMPLATE(ALint, ALfloat)
1521 DECL_TEMPLATE(ALint, ALdouble)
1522 DECL_TEMPLATE(ALint, ALmulaw)
1523 DECL_TEMPLATE(ALint, ALbyte3)
1524 DECL_TEMPLATE(ALint, ALubyte3)
1526 DECL_TEMPLATE(ALuint, ALbyte)
1527 DECL_TEMPLATE(ALuint, ALubyte)
1528 DECL_TEMPLATE(ALuint, ALshort)
1529 DECL_TEMPLATE(ALuint, ALushort)
1530 DECL_TEMPLATE(ALuint, ALint)
1531 DECL_TEMPLATE(ALuint, ALuint)
1532 DECL_TEMPLATE(ALuint, ALfloat)
1533 DECL_TEMPLATE(ALuint, ALdouble)
1534 DECL_TEMPLATE(ALuint, ALmulaw)
1535 DECL_TEMPLATE(ALuint, ALbyte3)
1536 DECL_TEMPLATE(ALuint, ALubyte3)
1538 DECL_TEMPLATE(ALfloat, ALbyte)
1539 DECL_TEMPLATE(ALfloat, ALubyte)
1540 DECL_TEMPLATE(ALfloat, ALshort)
1541 DECL_TEMPLATE(ALfloat, ALushort)
1542 DECL_TEMPLATE(ALfloat, ALint)
1543 DECL_TEMPLATE(ALfloat, ALuint)
1544 DECL_TEMPLATE(ALfloat, ALfloat)
1545 DECL_TEMPLATE(ALfloat, ALdouble)
1546 DECL_TEMPLATE(ALfloat, ALmulaw)
1547 DECL_TEMPLATE(ALfloat, ALbyte3)
1548 DECL_TEMPLATE(ALfloat, ALubyte3)
1550 DECL_TEMPLATE(ALdouble, ALbyte)
1551 DECL_TEMPLATE(ALdouble, ALubyte)
1552 DECL_TEMPLATE(ALdouble, ALshort)
1553 DECL_TEMPLATE(ALdouble, ALushort)
1554 DECL_TEMPLATE(ALdouble, ALint)
1555 DECL_TEMPLATE(ALdouble, ALuint)
1556 DECL_TEMPLATE(ALdouble, ALfloat)
1557 DECL_TEMPLATE(ALdouble, ALdouble)
1558 DECL_TEMPLATE(ALdouble, ALmulaw)
1559 DECL_TEMPLATE(ALdouble, ALbyte3)
1560 DECL_TEMPLATE(ALdouble, ALubyte3)
1562 DECL_TEMPLATE(ALmulaw, ALbyte)
1563 DECL_TEMPLATE(ALmulaw, ALubyte)
1564 DECL_TEMPLATE(ALmulaw, ALshort)
1565 DECL_TEMPLATE(ALmulaw, ALushort)
1566 DECL_TEMPLATE(ALmulaw, ALint)
1567 DECL_TEMPLATE(ALmulaw, ALuint)
1568 DECL_TEMPLATE(ALmulaw, ALfloat)
1569 DECL_TEMPLATE(ALmulaw, ALdouble)
1570 DECL_TEMPLATE(ALmulaw, ALmulaw)
1571 DECL_TEMPLATE(ALmulaw, ALbyte3)
1572 DECL_TEMPLATE(ALmulaw, ALubyte3)
1574 DECL_TEMPLATE(ALbyte3, ALbyte)
1575 DECL_TEMPLATE(ALbyte3, ALubyte)
1576 DECL_TEMPLATE(ALbyte3, ALshort)
1577 DECL_TEMPLATE(ALbyte3, ALushort)
1578 DECL_TEMPLATE(ALbyte3, ALint)
1579 DECL_TEMPLATE(ALbyte3, ALuint)
1580 DECL_TEMPLATE(ALbyte3, ALfloat)
1581 DECL_TEMPLATE(ALbyte3, ALdouble)
1582 DECL_TEMPLATE(ALbyte3, ALmulaw)
1583 DECL_TEMPLATE(ALbyte3, ALbyte3)
1584 DECL_TEMPLATE(ALbyte3, ALubyte3)
1586 DECL_TEMPLATE(ALubyte3, ALbyte)
1587 DECL_TEMPLATE(ALubyte3, ALubyte)
1588 DECL_TEMPLATE(ALubyte3, ALshort)
1589 DECL_TEMPLATE(ALubyte3, ALushort)
1590 DECL_TEMPLATE(ALubyte3, ALint)
1591 DECL_TEMPLATE(ALubyte3, ALuint)
1592 DECL_TEMPLATE(ALubyte3, ALfloat)
1593 DECL_TEMPLATE(ALubyte3, ALdouble)
1594 DECL_TEMPLATE(ALubyte3, ALmulaw)
1595 DECL_TEMPLATE(ALubyte3, ALbyte3)
1596 DECL_TEMPLATE(ALubyte3, ALubyte3)
1598 #undef DECL_TEMPLATE
1600 #define DECL_TEMPLATE(T) \
1601 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
1602 ALuint numblocks) \
1603 { \
1604 ALuint i, j; \
1605 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1606 for(i = 0;i < numblocks;i++) \
1607 { \
1608 DecodeIMA4Block(tmp, src, numchans); \
1609 src += 36*numchans; \
1610 for(j = 0;j < 65*numchans;j++) \
1611 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
1612 } \
1615 DECL_TEMPLATE(ALbyte)
1616 DECL_TEMPLATE(ALubyte)
1617 DECL_TEMPLATE(ALshort)
1618 DECL_TEMPLATE(ALushort)
1619 DECL_TEMPLATE(ALint)
1620 DECL_TEMPLATE(ALuint)
1621 DECL_TEMPLATE(ALfloat)
1622 DECL_TEMPLATE(ALdouble)
1623 DECL_TEMPLATE(ALmulaw)
1624 DECL_TEMPLATE(ALbyte3)
1625 DECL_TEMPLATE(ALubyte3)
1627 #undef DECL_TEMPLATE
1629 #define DECL_TEMPLATE(T) \
1630 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
1631 ALuint numblocks) \
1632 { \
1633 ALuint i, j; \
1634 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1635 ALint sample[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1636 ALint index[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1637 for(i = 0;i < numblocks;i++) \
1638 { \
1639 for(j = 0;j < 65*numchans;j++) \
1640 tmp[j] = Conv_ALshort_##T(*(src++)); \
1641 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
1642 dst += 36*numchans; \
1643 } \
1646 DECL_TEMPLATE(ALbyte)
1647 DECL_TEMPLATE(ALubyte)
1648 DECL_TEMPLATE(ALshort)
1649 DECL_TEMPLATE(ALushort)
1650 DECL_TEMPLATE(ALint)
1651 DECL_TEMPLATE(ALuint)
1652 DECL_TEMPLATE(ALfloat)
1653 DECL_TEMPLATE(ALdouble)
1654 DECL_TEMPLATE(ALmulaw)
1655 static void Convert_ALima4_ALima4(ALima4 *dst, const ALima4 *src,
1656 ALuint numchans, ALuint numblocks)
1657 { memcpy(dst, src, numblocks*36*numchans); }
1658 DECL_TEMPLATE(ALbyte3)
1659 DECL_TEMPLATE(ALubyte3)
1661 #undef DECL_TEMPLATE
1663 #define DECL_TEMPLATE(T) \
1664 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
1665 ALsizei numchans, ALsizei len) \
1666 { \
1667 switch(srcType) \
1668 { \
1669 case UserFmtByte: \
1670 Convert_##T##_ALbyte(dst, src, numchans, len); \
1671 break; \
1672 case UserFmtUByte: \
1673 Convert_##T##_ALubyte(dst, src, numchans, len); \
1674 break; \
1675 case UserFmtShort: \
1676 Convert_##T##_ALshort(dst, src, numchans, len); \
1677 break; \
1678 case UserFmtUShort: \
1679 Convert_##T##_ALushort(dst, src, numchans, len); \
1680 break; \
1681 case UserFmtInt: \
1682 Convert_##T##_ALint(dst, src, numchans, len); \
1683 break; \
1684 case UserFmtUInt: \
1685 Convert_##T##_ALuint(dst, src, numchans, len); \
1686 break; \
1687 case UserFmtFloat: \
1688 Convert_##T##_ALfloat(dst, src, numchans, len); \
1689 break; \
1690 case UserFmtDouble: \
1691 Convert_##T##_ALdouble(dst, src, numchans, len); \
1692 break; \
1693 case UserFmtMulaw: \
1694 Convert_##T##_ALmulaw(dst, src, numchans, len); \
1695 break; \
1696 case UserFmtIMA4: \
1697 Convert_##T##_ALima4(dst, src, numchans, len); \
1698 break; \
1699 case UserFmtByte3: \
1700 Convert_##T##_ALbyte3(dst, src, numchans, len); \
1701 break; \
1702 case UserFmtUByte3: \
1703 Convert_##T##_ALubyte3(dst, src, numchans, len); \
1704 break; \
1705 } \
1708 DECL_TEMPLATE(ALbyte)
1709 DECL_TEMPLATE(ALubyte)
1710 DECL_TEMPLATE(ALshort)
1711 DECL_TEMPLATE(ALushort)
1712 DECL_TEMPLATE(ALint)
1713 DECL_TEMPLATE(ALuint)
1714 DECL_TEMPLATE(ALfloat)
1715 DECL_TEMPLATE(ALdouble)
1716 DECL_TEMPLATE(ALmulaw)
1717 DECL_TEMPLATE(ALima4)
1718 DECL_TEMPLATE(ALbyte3)
1719 DECL_TEMPLATE(ALubyte3)
1721 #undef DECL_TEMPLATE
1724 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len)
1726 switch(dstType)
1728 case UserFmtByte:
1729 Convert_ALbyte(dst, src, srcType, numchans, len);
1730 break;
1731 case UserFmtUByte:
1732 Convert_ALubyte(dst, src, srcType, numchans, len);
1733 break;
1734 case UserFmtShort:
1735 Convert_ALshort(dst, src, srcType, numchans, len);
1736 break;
1737 case UserFmtUShort:
1738 Convert_ALushort(dst, src, srcType, numchans, len);
1739 break;
1740 case UserFmtInt:
1741 Convert_ALint(dst, src, srcType, numchans, len);
1742 break;
1743 case UserFmtUInt:
1744 Convert_ALuint(dst, src, srcType, numchans, len);
1745 break;
1746 case UserFmtFloat:
1747 Convert_ALfloat(dst, src, srcType, numchans, len);
1748 break;
1749 case UserFmtDouble:
1750 Convert_ALdouble(dst, src, srcType, numchans, len);
1751 break;
1752 case UserFmtMulaw:
1753 Convert_ALmulaw(dst, src, srcType, numchans, len);
1754 break;
1755 case UserFmtIMA4:
1756 Convert_ALima4(dst, src, srcType, numchans, len);
1757 break;
1758 case UserFmtByte3:
1759 Convert_ALbyte3(dst, src, srcType, numchans, len);
1760 break;
1761 case UserFmtUByte3:
1762 Convert_ALubyte3(dst, src, srcType, numchans, len);
1763 break;
1768 /*
1769 * LoadData
1771 * Loads the specified data into the buffer, using the specified formats.
1772 * Currently, the new format must have the same channel configuration as the
1773 * original format.
1774 */
1775 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels SrcChannels, enum UserFmtType SrcType, const ALvoid *data, ALboolean storesrc)
1777 ALuint NewChannels, NewBytes;
1778 enum FmtChannels DstChannels;
1779 enum FmtType DstType;
1780 ALuint64 newsize;
1781 ALvoid *temp;
1783 if(DecomposeFormat(NewFormat, &DstChannels, &DstType) == AL_FALSE ||
1784 (long)SrcChannels != (long)DstChannels)
1785 return AL_INVALID_ENUM;
1787 NewChannels = ChannelsFromFmt(DstChannels);
1788 NewBytes = BytesFromFmt(DstType);
1790 if(SrcType == UserFmtIMA4)
1792 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
1794 newsize = frames;
1795 newsize *= 65;
1796 newsize *= NewBytes;
1797 newsize *= NewChannels;
1798 if(newsize > INT_MAX)
1799 return AL_OUT_OF_MEMORY;
1801 temp = realloc(ALBuf->data, newsize);
1802 if(!temp && newsize) return AL_OUT_OF_MEMORY;
1803 ALBuf->data = temp;
1804 ALBuf->size = newsize;
1806 if(data != NULL)
1807 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
1809 if(storesrc)
1811 ALBuf->OriginalChannels = SrcChannels;
1812 ALBuf->OriginalType = SrcType;
1813 ALBuf->OriginalSize = frames * 36 * OrigChannels;
1814 ALBuf->OriginalAlign = 36 * OrigChannels;
1817 else
1819 ALuint OrigBytes = BytesFromUserFmt(SrcType);
1820 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
1822 newsize = frames;
1823 newsize *= NewBytes;
1824 newsize *= NewChannels;
1825 if(newsize > INT_MAX)
1826 return AL_OUT_OF_MEMORY;
1828 temp = realloc(ALBuf->data, newsize);
1829 if(!temp && newsize) return AL_OUT_OF_MEMORY;
1830 ALBuf->data = temp;
1831 ALBuf->size = newsize;
1833 if(data != NULL)
1834 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
1836 if(storesrc)
1838 ALBuf->OriginalChannels = SrcChannels;
1839 ALBuf->OriginalType = SrcType;
1840 ALBuf->OriginalSize = frames * OrigBytes * OrigChannels;
1841 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
1845 if(!storesrc)
1847 ALBuf->OriginalChannels = DstChannels;
1848 ALBuf->OriginalType = DstType;
1849 ALBuf->OriginalSize = frames * NewBytes * NewChannels;
1850 ALBuf->OriginalAlign = NewBytes * NewChannels;
1852 ALBuf->Frequency = freq;
1853 ALBuf->FmtChannels = DstChannels;
1854 ALBuf->FmtType = DstType;
1856 ALBuf->LoopStart = 0;
1857 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
1859 return AL_NO_ERROR;
1863 ALuint BytesFromUserFmt(enum UserFmtType type)
1865 switch(type)
1867 case UserFmtByte: return sizeof(ALbyte);
1868 case UserFmtUByte: return sizeof(ALubyte);
1869 case UserFmtShort: return sizeof(ALshort);
1870 case UserFmtUShort: return sizeof(ALushort);
1871 case UserFmtInt: return sizeof(ALint);
1872 case UserFmtUInt: return sizeof(ALuint);
1873 case UserFmtFloat: return sizeof(ALfloat);
1874 case UserFmtDouble: return sizeof(ALdouble);
1875 case UserFmtByte3: return sizeof(ALbyte3);
1876 case UserFmtUByte3: return sizeof(ALubyte3);
1877 case UserFmtMulaw: return sizeof(ALubyte);
1878 case UserFmtIMA4: break; /* not handled here */
1880 return 0;
1882 ALuint ChannelsFromUserFmt(enum UserFmtChannels chans)
1884 switch(chans)
1886 case UserFmtMono: return 1;
1887 case UserFmtStereo: return 2;
1888 case UserFmtRear: return 2;
1889 case UserFmtQuad: return 4;
1890 case UserFmtX51: return 6;
1891 case UserFmtX61: return 7;
1892 case UserFmtX71: return 8;
1894 return 0;
1896 ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
1897 enum UserFmtType *type)
1899 switch(format)
1901 case AL_FORMAT_MONO8:
1902 *chans = UserFmtMono;
1903 *type = UserFmtUByte;
1904 return AL_TRUE;
1905 case AL_FORMAT_MONO16:
1906 *chans = UserFmtMono;
1907 *type = UserFmtShort;
1908 return AL_TRUE;
1909 case AL_FORMAT_MONO_FLOAT32:
1910 *chans = UserFmtMono;
1911 *type = UserFmtFloat;
1912 return AL_TRUE;
1913 case AL_FORMAT_MONO_DOUBLE_EXT:
1914 *chans = UserFmtMono;
1915 *type = UserFmtDouble;
1916 return AL_TRUE;
1917 case AL_FORMAT_MONO_IMA4:
1918 *chans = UserFmtMono;
1919 *type = UserFmtIMA4;
1920 return AL_TRUE;
1921 case AL_FORMAT_STEREO8:
1922 *chans = UserFmtStereo;
1923 *type = UserFmtUByte;
1924 return AL_TRUE;
1925 case AL_FORMAT_STEREO16:
1926 *chans = UserFmtStereo;
1927 *type = UserFmtShort;
1928 return AL_TRUE;
1929 case AL_FORMAT_STEREO_FLOAT32:
1930 *chans = UserFmtStereo;
1931 *type = UserFmtFloat;
1932 return AL_TRUE;
1933 case AL_FORMAT_STEREO_DOUBLE_EXT:
1934 *chans = UserFmtStereo;
1935 *type = UserFmtDouble;
1936 return AL_TRUE;
1937 case AL_FORMAT_STEREO_IMA4:
1938 *chans = UserFmtStereo;
1939 *type = UserFmtIMA4;
1940 return AL_TRUE;
1941 case AL_FORMAT_QUAD8_LOKI:
1942 case AL_FORMAT_QUAD8:
1943 *chans = UserFmtQuad;
1944 *type = UserFmtUByte;
1945 return AL_TRUE;
1946 case AL_FORMAT_QUAD16_LOKI:
1947 case AL_FORMAT_QUAD16:
1948 *chans = UserFmtQuad;
1949 *type = UserFmtShort;
1950 return AL_TRUE;
1951 case AL_FORMAT_QUAD32:
1952 *chans = UserFmtQuad;
1953 *type = UserFmtFloat;
1954 return AL_TRUE;
1955 case AL_FORMAT_REAR8:
1956 *chans = UserFmtRear;
1957 *type = UserFmtUByte;
1958 return AL_TRUE;
1959 case AL_FORMAT_REAR16:
1960 *chans = UserFmtRear;
1961 *type = UserFmtShort;
1962 return AL_TRUE;
1963 case AL_FORMAT_REAR32:
1964 *chans = UserFmtRear;
1965 *type = UserFmtFloat;
1966 return AL_TRUE;
1967 case AL_FORMAT_51CHN8:
1968 *chans = UserFmtX51;
1969 *type = UserFmtUByte;
1970 return AL_TRUE;
1971 case AL_FORMAT_51CHN16:
1972 *chans = UserFmtX51;
1973 *type = UserFmtShort;
1974 return AL_TRUE;
1975 case AL_FORMAT_51CHN32:
1976 *chans = UserFmtX51;
1977 *type = UserFmtFloat;
1978 return AL_TRUE;
1979 case AL_FORMAT_61CHN8:
1980 *chans = UserFmtX61;
1981 *type = UserFmtUByte;
1982 return AL_TRUE;
1983 case AL_FORMAT_61CHN16:
1984 *chans = UserFmtX61;
1985 *type = UserFmtShort;
1986 return AL_TRUE;
1987 case AL_FORMAT_61CHN32:
1988 *chans = UserFmtX61;
1989 *type = UserFmtFloat;
1990 return AL_TRUE;
1991 case AL_FORMAT_71CHN8:
1992 *chans = UserFmtX71;
1993 *type = UserFmtUByte;
1994 return AL_TRUE;
1995 case AL_FORMAT_71CHN16:
1996 *chans = UserFmtX71;
1997 *type = UserFmtShort;
1998 return AL_TRUE;
1999 case AL_FORMAT_71CHN32:
2000 *chans = UserFmtX71;
2001 *type = UserFmtFloat;
2002 return AL_TRUE;
2003 case AL_FORMAT_MONO_MULAW:
2004 *chans = UserFmtMono;
2005 *type = UserFmtMulaw;
2006 return AL_TRUE;
2007 case AL_FORMAT_STEREO_MULAW:
2008 *chans = UserFmtStereo;
2009 *type = UserFmtMulaw;
2010 return AL_TRUE;
2011 case AL_FORMAT_QUAD_MULAW:
2012 *chans = UserFmtQuad;
2013 *type = UserFmtMulaw;
2014 return AL_TRUE;
2015 case AL_FORMAT_REAR_MULAW:
2016 *chans = UserFmtRear;
2017 *type = UserFmtMulaw;
2018 return AL_TRUE;
2019 case AL_FORMAT_51CHN_MULAW:
2020 *chans = UserFmtX51;
2021 *type = UserFmtMulaw;
2022 return AL_TRUE;
2023 case AL_FORMAT_61CHN_MULAW:
2024 *chans = UserFmtX61;
2025 *type = UserFmtMulaw;
2026 return AL_TRUE;
2027 case AL_FORMAT_71CHN_MULAW:
2028 *chans = UserFmtX71;
2029 *type = UserFmtMulaw;
2030 return AL_TRUE;
2032 return AL_FALSE;
2035 ALuint BytesFromFmt(enum FmtType type)
2037 switch(type)
2039 case FmtByte: return sizeof(ALbyte);
2040 case FmtShort: return sizeof(ALshort);
2041 case FmtFloat: return sizeof(ALfloat);
2043 return 0;
2045 ALuint ChannelsFromFmt(enum FmtChannels chans)
2047 switch(chans)
2049 case FmtMono: return 1;
2050 case FmtStereo: return 2;
2051 case FmtRear: return 2;
2052 case FmtQuad: return 4;
2053 case FmtX51: return 6;
2054 case FmtX61: return 7;
2055 case FmtX71: return 8;
2057 return 0;
2059 ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
2061 switch(format)
2063 case AL_MONO8:
2064 *chans = FmtMono;
2065 *type = FmtByte;
2066 return AL_TRUE;
2067 case AL_MONO16:
2068 *chans = FmtMono;
2069 *type = FmtShort;
2070 return AL_TRUE;
2071 case AL_MONO32F:
2072 *chans = FmtMono;
2073 *type = FmtFloat;
2074 return AL_TRUE;
2075 case AL_STEREO8:
2076 *chans = FmtStereo;
2077 *type = FmtByte;
2078 return AL_TRUE;
2079 case AL_STEREO16:
2080 *chans = FmtStereo;
2081 *type = FmtShort;
2082 return AL_TRUE;
2083 case AL_STEREO32F:
2084 *chans = FmtStereo;
2085 *type = FmtFloat;
2086 return AL_TRUE;
2087 case AL_FORMAT_QUAD8_LOKI:
2088 case AL_QUAD8:
2089 *chans = FmtQuad;
2090 *type = FmtByte;
2091 return AL_TRUE;
2092 case AL_FORMAT_QUAD16_LOKI:
2093 case AL_QUAD16:
2094 *chans = FmtQuad;
2095 *type = FmtShort;
2096 return AL_TRUE;
2097 case AL_QUAD32F:
2098 *chans = FmtQuad;
2099 *type = FmtFloat;
2100 return AL_TRUE;
2101 case AL_REAR8:
2102 *chans = FmtRear;
2103 *type = FmtByte;
2104 return AL_TRUE;
2105 case AL_REAR16:
2106 *chans = FmtRear;
2107 *type = FmtShort;
2108 return AL_TRUE;
2109 case AL_REAR32F:
2110 *chans = FmtRear;
2111 *type = FmtFloat;
2112 return AL_TRUE;
2113 case AL_5POINT1_8:
2114 *chans = FmtX51;
2115 *type = FmtByte;
2116 return AL_TRUE;
2117 case AL_5POINT1_16:
2118 *chans = FmtX51;
2119 *type = FmtShort;
2120 return AL_TRUE;
2121 case AL_5POINT1_32F:
2122 *chans = FmtX51;
2123 *type = FmtFloat;
2124 return AL_TRUE;
2125 case AL_6POINT1_8:
2126 *chans = FmtX61;
2127 *type = FmtByte;
2128 return AL_TRUE;
2129 case AL_6POINT1_16:
2130 *chans = FmtX61;
2131 *type = FmtShort;
2132 return AL_TRUE;
2133 case AL_6POINT1_32F:
2134 *chans = FmtX61;
2135 *type = FmtFloat;
2136 return AL_TRUE;
2137 case AL_7POINT1_8:
2138 *chans = FmtX71;
2139 *type = FmtByte;
2140 return AL_TRUE;
2141 case AL_7POINT1_16:
2142 *chans = FmtX71;
2143 *type = FmtShort;
2144 return AL_TRUE;
2145 case AL_7POINT1_32F:
2146 *chans = FmtX71;
2147 *type = FmtFloat;
2148 return AL_TRUE;
2150 return AL_FALSE;
2154 static ALboolean IsValidType(ALenum type)
2156 switch(type)
2158 case AL_BYTE:
2159 case AL_UNSIGNED_BYTE:
2160 case AL_SHORT:
2161 case AL_UNSIGNED_SHORT:
2162 case AL_INT:
2163 case AL_UNSIGNED_INT:
2164 case AL_FLOAT:
2165 case AL_DOUBLE:
2166 case AL_MULAW:
2167 case AL_IMA4:
2168 case AL_BYTE3:
2169 case AL_UNSIGNED_BYTE3:
2170 return AL_TRUE;
2172 return AL_FALSE;
2175 static ALboolean IsValidChannels(ALenum channels)
2177 switch(channels)
2179 case AL_MONO:
2180 case AL_STEREO:
2181 case AL_REAR:
2182 case AL_QUAD:
2183 case AL_5POINT1:
2184 case AL_6POINT1:
2185 case AL_7POINT1:
2186 return AL_TRUE;
2188 return AL_FALSE;
2192 /*
2193 * ReleaseALBuffers()
2195 * INTERNAL: Called to destroy any buffers that still exist on the device
2196 */
2197 ALvoid ReleaseALBuffers(ALCdevice *device)
2199 ALsizei i;
2200 for(i = 0;i < device->BufferMap.size;i++)
2202 ALbuffer *temp = device->BufferMap.array[i].value;
2203 device->BufferMap.array[i].value = NULL;
2205 free(temp->data);
2207 FreeThunkEntry(temp->buffer);
2208 memset(temp, 0, sizeof(ALbuffer));
2209 free(temp);