Avoid holding the context lock for the buffer functions
[openal-soft/android.git] / OpenAL32 / alBuffer.c
blob040e723d8b36d4ba4889a221ddf6488e16f857a3
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.
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.
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
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)))
42 #define RemoveBuffer(m, k) ((ALbuffer*)PopUIntMapValue(&(m), (k)))
46 * Global Variables
49 /* IMA ADPCM Stepsize table */
50 static const long IMAStep_size[89] = {
51 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
52 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
53 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
54 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
55 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
56 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
57 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
58 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
59 32767
62 /* IMA4 ADPCM Codeword decode table */
63 static const long IMA4Codeword[16] = {
64 1, 3, 5, 7, 9, 11, 13, 15,
65 -1,-3,-5,-7,-9,-11,-13,-15,
68 /* IMA4 ADPCM Step index adjust decode table */
69 static const long IMA4Index_adjust[16] = {
70 -1,-1,-1,-1, 2, 4, 6, 8,
71 -1,-1,-1,-1, 2, 4, 6, 8
74 /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
75 * signed 16-bit sample */
76 static const ALshort muLawDecompressionTable[256] = {
77 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
78 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
79 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
80 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
81 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
82 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
83 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
84 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
85 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
86 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
87 -876, -844, -812, -780, -748, -716, -684, -652,
88 -620, -588, -556, -524, -492, -460, -428, -396,
89 -372, -356, -340, -324, -308, -292, -276, -260,
90 -244, -228, -212, -196, -180, -164, -148, -132,
91 -120, -112, -104, -96, -88, -80, -72, -64,
92 -56, -48, -40, -32, -24, -16, -8, 0,
93 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
94 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
95 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
96 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
97 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
98 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
99 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
100 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
101 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
102 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
103 876, 844, 812, 780, 748, 716, 684, 652,
104 620, 588, 556, 524, 492, 460, 428, 396,
105 372, 356, 340, 324, 308, 292, 276, 260,
106 244, 228, 212, 196, 180, 164, 148, 132,
107 120, 112, 104, 96, 88, 80, 72, 64,
108 56, 48, 40, 32, 24, 16, 8, 0
111 /* Values used when encoding a muLaw sample */
112 static const int muLawBias = 0x84;
113 static const int muLawClip = 32635;
114 static const char muLawCompressTable[256] =
116 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
117 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
118 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
119 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
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 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
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 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
134 /* TODO: These functions shouldn't need to take the device lock, but will need
135 * a RWLock to protect from concurrent writes.
139 * alGenBuffers(ALsizei n, ALuint *buffers)
141 * Generates n AL Buffers, and stores the Buffers Names in the array pointed
142 * to by buffers
144 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
146 ALCcontext *Context;
147 ALsizei i=0;
149 Context = GetContextRef();
150 if(!Context) return;
152 /* Check that we are actually generating some Buffers */
153 if(n < 0 || IsBadWritePtr((void*)buffers, n * sizeof(ALuint)))
154 alSetError(Context, AL_INVALID_VALUE);
155 else
157 ALCdevice *device = Context->Device;
158 ALenum err;
160 // Create all the new Buffers
161 while(i < n)
163 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
164 if(!buffer)
166 alSetError(Context, AL_OUT_OF_MEMORY);
167 alDeleteBuffers(i, buffers);
168 break;
171 err = NewThunkEntry(&buffer->buffer);
172 if(err == AL_NO_ERROR)
173 err = InsertUIntMapEntry(&device->BufferMap, buffer->buffer, buffer);
174 if(err != AL_NO_ERROR)
176 FreeThunkEntry(buffer->buffer);
177 memset(buffer, 0, sizeof(ALbuffer));
178 free(buffer);
180 alSetError(Context, err);
181 alDeleteBuffers(i, buffers);
182 break;
185 buffers[i++] = buffer->buffer;
189 ALCcontext_DecRef(Context);
193 * alDeleteBuffers(ALsizei n, ALuint *buffers)
195 * Deletes the n AL Buffers pointed to by buffers
197 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
199 ALCcontext *Context;
200 ALCdevice *device;
201 ALbuffer *ALBuf;
202 ALsizei i;
204 Context = GetContextRef();
205 if(!Context) return;
207 device = Context->Device;
208 /* Check we are actually Deleting some Buffers */
209 if(n < 0)
210 alSetError(Context, AL_INVALID_VALUE);
211 else
213 /* Check that all the buffers are valid and can actually be deleted */
214 for(i = 0;i < n;i++)
216 if(!buffers[i])
217 continue;
219 /* Check for valid Buffer ID */
220 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
222 alSetError(Context, AL_INVALID_NAME);
223 n = 0;
224 break;
226 else if(ALBuf->ref != 0)
228 /* Buffer still in use, cannot be deleted */
229 alSetError(Context, AL_INVALID_OPERATION);
230 n = 0;
231 break;
235 for(i = 0;i < n;i++)
237 if((ALBuf=RemoveBuffer(device->BufferMap, buffers[i])) == NULL)
238 continue;
239 FreeThunkEntry(ALBuf->buffer);
241 /* Release the memory used to store audio data */
242 free(ALBuf->data);
244 /* Release buffer structure */
245 memset(ALBuf, 0, sizeof(ALbuffer));
246 free(ALBuf);
250 ALCcontext_DecRef(Context);
254 * alIsBuffer(ALuint buffer)
256 * Checks if buffer is a valid Buffer Name
258 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
260 ALCcontext *Context;
261 ALboolean result;
263 Context = GetContextRef();
264 if(!Context) return AL_FALSE;
266 result = ((!buffer || LookupBuffer(Context->Device->BufferMap, buffer)) ?
267 AL_TRUE : AL_FALSE);
269 ALCcontext_DecRef(Context);
271 return result;
275 * alBufferData(ALuint buffer, ALenum format, const ALvoid *data,
276 * ALsizei size, ALsizei freq)
278 * Fill buffer with audio data
280 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
282 enum UserFmtChannels SrcChannels;
283 enum UserFmtType SrcType;
284 ALCcontext *Context;
285 ALCdevice *device;
286 ALbuffer *ALBuf;
287 ALenum err;
289 Context = GetContextRef();
290 if(!Context) return;
292 device = Context->Device;
293 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
294 alSetError(Context, AL_INVALID_NAME);
295 else if(size < 0 || freq < 0)
296 alSetError(Context, AL_INVALID_VALUE);
297 else if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
298 alSetError(Context, AL_INVALID_ENUM);
299 else switch(SrcType)
301 case UserFmtByte:
302 case UserFmtUByte:
303 case UserFmtShort:
304 case UserFmtUShort:
305 case UserFmtInt:
306 case UserFmtUInt:
307 case UserFmtFloat: {
308 ALuint FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
309 if((size%FrameSize) != 0)
310 err = AL_INVALID_VALUE;
311 else
313 LockDevice(device);
314 err = LoadData(ALBuf, freq, format, size/FrameSize,
315 SrcChannels, SrcType, data, AL_TRUE);
316 UnlockDevice(device);
318 if(err != AL_NO_ERROR)
319 alSetError(Context, err);
320 } break;
322 case UserFmtByte3:
323 case UserFmtUByte3:
324 case UserFmtDouble: {
325 ALuint FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
326 ALenum NewFormat = AL_FORMAT_MONO_FLOAT32;
327 switch(SrcChannels)
329 case UserFmtMono: NewFormat = AL_FORMAT_MONO_FLOAT32; break;
330 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO_FLOAT32; break;
331 case UserFmtRear: NewFormat = AL_FORMAT_REAR32; break;
332 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD32; break;
333 case UserFmtX51: NewFormat = AL_FORMAT_51CHN32; break;
334 case UserFmtX61: NewFormat = AL_FORMAT_61CHN32; break;
335 case UserFmtX71: NewFormat = AL_FORMAT_71CHN32; break;
337 if((size%FrameSize) != 0)
338 err = AL_INVALID_VALUE;
339 else
341 LockDevice(device);
342 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
343 SrcChannels, SrcType, data, AL_TRUE);
344 UnlockDevice(device);
346 if(err != AL_NO_ERROR)
347 alSetError(Context, err);
348 } break;
350 case UserFmtMulaw:
351 case UserFmtIMA4: {
352 /* Here is where things vary:
353 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
354 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
356 ALuint FrameSize = (SrcType == UserFmtIMA4) ?
357 (ChannelsFromUserFmt(SrcChannels) * 36) :
358 FrameSizeFromUserFmt(SrcChannels, SrcType);
359 ALenum NewFormat = AL_FORMAT_MONO16;
360 switch(SrcChannels)
362 case UserFmtMono: NewFormat = AL_FORMAT_MONO16; break;
363 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
364 case UserFmtRear: NewFormat = AL_FORMAT_REAR16; break;
365 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
366 case UserFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
367 case UserFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
368 case UserFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
370 if((size%FrameSize) != 0)
371 err = AL_INVALID_VALUE;
372 else
374 LockDevice(device);
375 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
376 SrcChannels, SrcType, data, AL_TRUE);
377 UnlockDevice(device);
379 if(err != AL_NO_ERROR)
380 alSetError(Context, err);
381 } break;
384 ALCcontext_DecRef(Context);
388 * alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data,
389 * ALsizei offset, ALsizei length)
391 * Update buffer's audio data
393 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
395 enum UserFmtChannels SrcChannels;
396 enum UserFmtType SrcType;
397 ALCcontext *Context;
398 ALCdevice *device;
399 ALbuffer *ALBuf;
401 Context = GetContextRef();
402 if(!Context) return;
404 device = Context->Device;
405 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
406 alSetError(Context, AL_INVALID_NAME);
407 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
408 alSetError(Context, AL_INVALID_VALUE);
409 else if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
410 alSetError(Context, AL_INVALID_ENUM);
411 else
413 LockDevice(device);
414 if(SrcChannels != ALBuf->OriginalChannels || SrcType != ALBuf->OriginalType)
415 alSetError(Context, AL_INVALID_ENUM);
416 else if(offset > ALBuf->OriginalSize ||
417 length > ALBuf->OriginalSize-offset ||
418 (offset%ALBuf->OriginalAlign) != 0 ||
419 (length%ALBuf->OriginalAlign) != 0)
420 alSetError(Context, AL_INVALID_VALUE);
421 else
423 ALuint Channels = ChannelsFromFmt(ALBuf->FmtChannels);
424 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
425 if(SrcType == UserFmtIMA4)
427 /* offset -> byte offset, length -> block count */
428 offset /= 36;
429 offset *= 65;
430 offset *= Bytes;
431 length /= ALBuf->OriginalAlign;
433 else
435 ALuint OldBytes = BytesFromUserFmt(SrcType);
437 offset /= OldBytes;
438 offset *= Bytes;
439 length /= OldBytes * Channels;
441 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
442 data, SrcType, Channels, length);
444 UnlockDevice(device);
447 ALCcontext_DecRef(Context);
451 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer,
452 ALuint samplerate, ALenum internalformat, ALsizei frames,
453 ALenum channels, ALenum type, const ALvoid *data)
455 ALCcontext *Context;
456 ALCdevice *device;
457 ALbuffer *ALBuf;
458 ALenum err;
460 Context = GetContextRef();
461 if(!Context) return;
463 device = Context->Device;
464 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
465 alSetError(Context, AL_INVALID_NAME);
466 else if(frames < 0 || samplerate == 0)
467 alSetError(Context, AL_INVALID_VALUE);
468 else if(IsValidType(type) == AL_FALSE || IsValidChannels(channels) == AL_FALSE)
469 alSetError(Context, AL_INVALID_ENUM);
470 else
472 err = AL_NO_ERROR;
473 if(type == UserFmtIMA4)
475 if((frames%65) == 0) frames /= 65;
476 else err = AL_INVALID_VALUE;
478 if(err == AL_NO_ERROR)
480 LockDevice(device);
481 err = LoadData(ALBuf, samplerate, internalformat, frames,
482 channels, type, data, AL_FALSE);
483 UnlockDevice(device);
485 if(err != AL_NO_ERROR)
486 alSetError(Context, err);
489 ALCcontext_DecRef(Context);
492 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint buffer,
493 ALsizei offset, ALsizei frames,
494 ALenum channels, ALenum type, const ALvoid *data)
496 ALCcontext *Context;
497 ALCdevice *device;
498 ALbuffer *ALBuf;
500 Context = GetContextRef();
501 if(!Context) return;
503 device = Context->Device;
504 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
505 alSetError(Context, AL_INVALID_NAME);
506 else if(frames < 0 || offset < 0 || (frames > 0 && data == NULL))
507 alSetError(Context, AL_INVALID_VALUE);
508 else if(IsValidType(type) == AL_FALSE)
509 alSetError(Context, AL_INVALID_ENUM);
510 else
512 ALuint FrameSize;
513 ALuint FrameCount;
515 LockDevice(device);
516 FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
517 FrameCount = ALBuf->size / FrameSize;
518 if(channels != (ALenum)ALBuf->FmtChannels)
519 alSetError(Context, AL_INVALID_ENUM);
520 else if((ALuint)offset > FrameCount || (ALuint)frames > FrameCount-offset)
521 alSetError(Context, AL_INVALID_VALUE);
522 else if(type == UserFmtIMA4 && (frames%65) != 0)
523 alSetError(Context, AL_INVALID_VALUE);
524 else
526 /* offset -> byte offset */
527 offset *= FrameSize;
528 /* frames -> IMA4 block count */
529 if(type == UserFmtIMA4) frames /= 65;
530 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
531 data, type,
532 ChannelsFromFmt(ALBuf->FmtChannels), frames);
534 UnlockDevice(device);
537 ALCcontext_DecRef(Context);
540 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer,
541 ALsizei offset, ALsizei frames,
542 ALenum channels, ALenum type, ALvoid *data)
544 ALCcontext *Context;
545 ALCdevice *device;
546 ALbuffer *ALBuf;
548 Context = GetContextRef();
549 if(!Context) return;
551 device = Context->Device;
552 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
553 alSetError(Context, AL_INVALID_NAME);
554 else if(frames < 0 || offset < 0 || (frames > 0 && data == NULL))
555 alSetError(Context, AL_INVALID_VALUE);
556 else if(IsValidType(type) == AL_FALSE)
557 alSetError(Context, AL_INVALID_ENUM);
558 else
560 ALuint FrameSize;
561 ALuint FrameCount;
563 LockDevice(device);
564 FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
565 FrameCount = ALBuf->size / FrameSize;
566 if(channels != (ALenum)ALBuf->FmtChannels)
567 alSetError(Context, AL_INVALID_ENUM);
568 else if((ALuint)offset > FrameCount || (ALuint)frames > FrameCount-offset)
569 alSetError(Context, AL_INVALID_VALUE);
570 else if(type == UserFmtIMA4 && (frames%65) != 0)
571 alSetError(Context, AL_INVALID_VALUE);
572 else
574 /* offset -> byte offset */
575 offset *= FrameSize;
576 /* frames -> IMA4 block count */
577 if(type == UserFmtIMA4) frames /= 65;
578 ConvertData(data, type,
579 &((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
580 ChannelsFromFmt(ALBuf->FmtChannels), frames);
582 UnlockDevice(device);
585 ALCcontext_DecRef(Context);
588 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format)
590 enum FmtChannels DstChannels;
591 enum FmtType DstType;
592 ALCcontext *Context;
593 ALboolean ret;
595 Context = GetContextRef();
596 if(!Context) return AL_FALSE;
598 ret = DecomposeFormat(format, &DstChannels, &DstType);
600 ALCcontext_DecRef(Context);
602 return ret;
606 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
608 ALCcontext *pContext;
609 ALCdevice *device;
611 (void)flValue;
613 pContext = GetContextRef();
614 if(!pContext) return;
616 device = pContext->Device;
617 if(LookupBuffer(device->BufferMap, buffer) == NULL)
618 alSetError(pContext, AL_INVALID_NAME);
619 else
621 switch(eParam)
623 default:
624 alSetError(pContext, AL_INVALID_ENUM);
625 break;
629 ALCcontext_DecRef(pContext);
633 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
635 ALCcontext *pContext;
636 ALCdevice *device;
638 (void)flValue1;
639 (void)flValue2;
640 (void)flValue3;
642 pContext = GetContextRef();
643 if(!pContext) return;
645 device = pContext->Device;
646 if(LookupBuffer(device->BufferMap, buffer) == NULL)
647 alSetError(pContext, AL_INVALID_NAME);
648 else
650 switch(eParam)
652 default:
653 alSetError(pContext, AL_INVALID_ENUM);
654 break;
658 ALCcontext_DecRef(pContext);
662 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
664 ALCcontext *pContext;
665 ALCdevice *device;
667 pContext = GetContextRef();
668 if(!pContext) return;
670 device = pContext->Device;
671 if(!flValues)
672 alSetError(pContext, AL_INVALID_VALUE);
673 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
674 alSetError(pContext, AL_INVALID_NAME);
675 else
677 switch(eParam)
679 default:
680 alSetError(pContext, AL_INVALID_ENUM);
681 break;
685 ALCcontext_DecRef(pContext);
689 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
691 ALCcontext *pContext;
692 ALCdevice *device;
694 (void)lValue;
696 pContext = GetContextRef();
697 if(!pContext) return;
699 device = pContext->Device;
700 if(LookupBuffer(device->BufferMap, buffer) == NULL)
701 alSetError(pContext, AL_INVALID_NAME);
702 else
704 switch(eParam)
706 default:
707 alSetError(pContext, AL_INVALID_ENUM);
708 break;
712 ALCcontext_DecRef(pContext);
716 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
718 ALCcontext *pContext;
719 ALCdevice *device;
721 (void)lValue1;
722 (void)lValue2;
723 (void)lValue3;
725 pContext = GetContextRef();
726 if(!pContext) return;
728 device = pContext->Device;
729 if(LookupBuffer(device->BufferMap, buffer) == NULL)
730 alSetError(pContext, AL_INVALID_NAME);
731 else
733 switch(eParam)
735 default:
736 alSetError(pContext, AL_INVALID_ENUM);
737 break;
741 ALCcontext_DecRef(pContext);
745 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
747 ALCcontext *pContext;
748 ALCdevice *device;
749 ALbuffer *ALBuf;
751 pContext = GetContextRef();
752 if(!pContext) return;
754 device = pContext->Device;
755 if(!plValues)
756 alSetError(pContext, AL_INVALID_VALUE);
757 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
758 alSetError(pContext, AL_INVALID_NAME);
759 else
761 switch(eParam)
763 case AL_LOOP_POINTS_SOFT:
764 LockDevice(device);
765 if(ALBuf->ref != 0)
766 alSetError(pContext, AL_INVALID_OPERATION);
767 else if(plValues[0] < 0 || plValues[1] < 0 ||
768 plValues[0] >= plValues[1] || ALBuf->size == 0)
769 alSetError(pContext, AL_INVALID_VALUE);
770 else
772 ALint maxlen = ALBuf->size /
773 FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
774 if(plValues[0] > maxlen || plValues[1] > maxlen)
775 alSetError(pContext, AL_INVALID_VALUE);
776 else
778 ALBuf->LoopStart = plValues[0];
779 ALBuf->LoopEnd = plValues[1];
782 UnlockDevice(device);
783 break;
785 default:
786 alSetError(pContext, AL_INVALID_ENUM);
787 break;
791 ALCcontext_DecRef(pContext);
795 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
797 ALCcontext *pContext;
798 ALCdevice *device;
800 pContext = GetContextRef();
801 if(!pContext) return;
803 device = pContext->Device;
804 if(!pflValue)
805 alSetError(pContext, AL_INVALID_VALUE);
806 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
807 alSetError(pContext, AL_INVALID_NAME);
808 else
810 switch(eParam)
812 default:
813 alSetError(pContext, AL_INVALID_ENUM);
814 break;
818 ALCcontext_DecRef(pContext);
822 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
824 ALCcontext *pContext;
825 ALCdevice *device;
827 pContext = GetContextRef();
828 if(!pContext) return;
830 device = pContext->Device;
831 if(!pflValue1 || !pflValue2 || !pflValue3)
832 alSetError(pContext, AL_INVALID_VALUE);
833 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
834 alSetError(pContext, AL_INVALID_NAME);
835 else
837 switch(eParam)
839 default:
840 alSetError(pContext, AL_INVALID_ENUM);
841 break;
845 ALCcontext_DecRef(pContext);
849 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
851 ALCcontext *pContext;
852 ALCdevice *device;
854 pContext = GetContextRef();
855 if(!pContext) return;
857 device = pContext->Device;
858 if(!pflValues)
859 alSetError(pContext, AL_INVALID_VALUE);
860 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
861 alSetError(pContext, AL_INVALID_NAME);
862 else
864 switch(eParam)
866 default:
867 alSetError(pContext, AL_INVALID_ENUM);
868 break;
872 ALCcontext_DecRef(pContext);
876 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
878 ALCcontext *pContext;
879 ALbuffer *pBuffer;
880 ALCdevice *device;
882 pContext = GetContextRef();
883 if(!pContext) return;
885 device = pContext->Device;
886 if(!plValue)
887 alSetError(pContext, AL_INVALID_VALUE);
888 else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
889 alSetError(pContext, AL_INVALID_NAME);
890 else
892 switch(eParam)
894 case AL_FREQUENCY:
895 *plValue = pBuffer->Frequency;
896 break;
898 case AL_BITS:
899 *plValue = BytesFromFmt(pBuffer->FmtType) * 8;
900 break;
902 case AL_CHANNELS:
903 *plValue = ChannelsFromFmt(pBuffer->FmtChannels);
904 break;
906 case AL_SIZE:
907 *plValue = pBuffer->size;
908 break;
910 default:
911 alSetError(pContext, AL_INVALID_ENUM);
912 break;
916 ALCcontext_DecRef(pContext);
920 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
922 ALCcontext *pContext;
923 ALCdevice *device;
925 pContext = GetContextRef();
926 if(!pContext) return;
928 device = pContext->Device;
929 if(!plValue1 || !plValue2 || !plValue3)
930 alSetError(pContext, AL_INVALID_VALUE);
931 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
932 alSetError(pContext, AL_INVALID_NAME);
933 else
935 switch(eParam)
937 default:
938 alSetError(pContext, AL_INVALID_ENUM);
939 break;
943 ALCcontext_DecRef(pContext);
947 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
949 ALCcontext *pContext;
950 ALCdevice *device;
951 ALbuffer *ALBuf;
953 switch(eParam)
955 case AL_FREQUENCY:
956 case AL_BITS:
957 case AL_CHANNELS:
958 case AL_SIZE:
959 alGetBufferi(buffer, eParam, plValues);
960 return;
963 pContext = GetContextRef();
964 if(!pContext) return;
966 device = pContext->Device;
967 if(!plValues)
968 alSetError(pContext, AL_INVALID_VALUE);
969 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
970 alSetError(pContext, AL_INVALID_NAME);
971 else
973 switch(eParam)
975 case AL_LOOP_POINTS_SOFT:
976 LockDevice(device);
977 plValues[0] = ALBuf->LoopStart;
978 plValues[1] = ALBuf->LoopEnd;
979 UnlockDevice(device);
980 break;
982 default:
983 alSetError(pContext, AL_INVALID_ENUM);
984 break;
988 ALCcontext_DecRef(pContext);
992 typedef ALubyte ALmulaw;
993 typedef ALubyte ALima4;
994 typedef struct {
995 ALbyte b[3];
996 } ALbyte3;
997 typedef struct {
998 ALubyte b[3];
999 } ALubyte3;
1001 static __inline ALshort DecodeMuLaw(ALmulaw val)
1002 { return muLawDecompressionTable[val]; }
1004 static ALmulaw EncodeMuLaw(ALshort val)
1006 ALint mant, exp, sign;
1008 sign = (val>>8) & 0x80;
1009 if(sign)
1011 /* -32768 doesn't properly negate on a short; it results in itself.
1012 * So clamp to -32767 */
1013 val = maxi(val, -32767);
1014 val = -val;
1017 val = mini(val, muLawClip);
1018 val += muLawBias;
1020 exp = muLawCompressTable[(val>>7) & 0xff];
1021 mant = (val >> (exp+3)) & 0x0f;
1023 return ~(sign | (exp<<4) | mant);
1026 static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans)
1028 ALint sample[MAXCHANNELS], index[MAXCHANNELS];
1029 ALuint code[MAXCHANNELS];
1030 ALsizei j,k,c;
1032 for(c = 0;c < numchans;c++)
1034 sample[c] = *(src++);
1035 sample[c] |= *(src++) << 8;
1036 sample[c] = (sample[c]^0x8000) - 32768;
1037 index[c] = *(src++);
1038 index[c] |= *(src++) << 8;
1039 index[c] = (index[c]^0x8000) - 32768;
1041 index[c] = clampi(index[c], 0, 88);
1043 dst[c] = sample[c];
1046 j = 1;
1047 while(j < 65)
1049 for(c = 0;c < numchans;c++)
1051 code[c] = *(src++);
1052 code[c] |= *(src++) << 8;
1053 code[c] |= *(src++) << 16;
1054 code[c] |= *(src++) << 24;
1057 for(k = 0;k < 8;k++,j++)
1059 for(c = 0;c < numchans;c++)
1061 int nibble = code[c]&0xf;
1062 code[c] >>= 4;
1064 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
1065 sample[c] = clampi(sample[c], -32768, 32767);
1067 index[c] += IMA4Index_adjust[nibble];
1068 index[c] = clampi(index[c], 0, 88);
1070 dst[j*numchans + c] = sample[c];
1076 static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans)
1078 ALsizei j,k,c;
1080 for(c = 0;c < numchans;c++)
1082 int diff = src[c] - sample[c];
1083 int step = IMAStep_size[index[c]];
1084 int nibble;
1086 nibble = 0;
1087 if(diff < 0)
1089 nibble = 0x8;
1090 diff = -diff;
1093 diff = mini(step*2, diff);
1094 nibble |= (diff*8/step - 1) / 2;
1096 sample[c] += IMA4Codeword[nibble] * step / 8;
1097 sample[c] = clampi(sample[c], -32768, 32767);
1099 index[c] += IMA4Index_adjust[nibble];
1100 index[c] = clampi(index[c], 0, 88);
1102 *(dst++) = sample[c] & 0xff;
1103 *(dst++) = (sample[c]>>8) & 0xff;
1104 *(dst++) = index[c] & 0xff;
1105 *(dst++) = (index[c]>>8) & 0xff;
1108 j = 1;
1109 while(j < 65)
1111 for(c = 0;c < numchans;c++)
1113 for(k = 0;k < 8;k++)
1115 int diff = src[(j+k)*numchans + c] - sample[c];
1116 int step = IMAStep_size[index[c]];
1117 int nibble;
1119 nibble = 0;
1120 if(diff < 0)
1122 nibble = 0x8;
1123 diff = -diff;
1126 diff = mini(step*2, diff);
1127 nibble |= (diff*8/step - 1) / 2;
1129 sample[c] += IMA4Codeword[nibble] * step / 8;
1130 sample[c] = clampi(sample[c], -32768, 32767);
1132 index[c] += IMA4Index_adjust[nibble];
1133 index[c] = clampi(index[c], 0, 88);
1135 if(!(k&1)) *dst = nibble;
1136 else *(dst++) |= nibble<<4;
1139 j += 8;
1143 static const union {
1144 ALuint u;
1145 ALubyte b[sizeof(ALuint)];
1146 } EndianTest = { 1 };
1147 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
1149 static __inline ALint DecodeByte3(ALbyte3 val)
1151 if(IS_LITTLE_ENDIAN)
1152 return (val.b[2]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[0]);
1153 return (val.b[0]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[2]);
1156 static __inline ALbyte3 EncodeByte3(ALint val)
1158 if(IS_LITTLE_ENDIAN)
1160 ALbyte3 ret = {{ val, val>>8, val>>16 }};
1161 return ret;
1163 else
1165 ALbyte3 ret = {{ val>>16, val>>8, val }};
1166 return ret;
1170 static __inline ALint DecodeUByte3(ALubyte3 val)
1172 if(IS_LITTLE_ENDIAN)
1173 return (val.b[2]<<16) | (val.b[1]<<8) | (val.b[0]);
1174 return (val.b[0]<<16) | (val.b[1]<<8) | val.b[2];
1177 static __inline ALubyte3 EncodeUByte3(ALint val)
1179 if(IS_LITTLE_ENDIAN)
1181 ALubyte3 ret = {{ val, val>>8, val>>16 }};
1182 return ret;
1184 else
1186 ALubyte3 ret = {{ val>>16, val>>8, val }};
1187 return ret;
1192 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
1193 { return val; }
1194 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
1195 { return val-128; }
1196 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
1197 { return val>>8; }
1198 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
1199 { return (val>>8)-128; }
1200 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
1201 { return val>>24; }
1202 static __inline ALbyte Conv_ALbyte_ALuint(ALuint val)
1203 { return (val>>24)-128; }
1204 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
1206 if(val > 1.0f) return 127;
1207 if(val < -1.0f) return -128;
1208 return (ALint)(val * 127.0f);
1210 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
1212 if(val > 1.0) return 127;
1213 if(val < -1.0) return -128;
1214 return (ALint)(val * 127.0);
1216 static __inline ALbyte Conv_ALbyte_ALmulaw(ALmulaw val)
1217 { return Conv_ALbyte_ALshort(DecodeMuLaw(val)); }
1218 static __inline ALbyte Conv_ALbyte_ALbyte3(ALbyte3 val)
1219 { return DecodeByte3(val)>>16; }
1220 static __inline ALbyte Conv_ALbyte_ALubyte3(ALubyte3 val)
1221 { return (DecodeUByte3(val)>>16)-128; }
1223 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
1224 { return val+128; }
1225 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
1226 { return val; }
1227 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
1228 { return (val>>8)+128; }
1229 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
1230 { return val>>8; }
1231 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
1232 { return (val>>24)+128; }
1233 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
1234 { return val>>24; }
1235 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
1237 if(val > 1.0f) return 255;
1238 if(val < -1.0f) return 0;
1239 return (ALint)(val * 127.0f) + 128;
1241 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
1243 if(val > 1.0) return 255;
1244 if(val < -1.0) return 0;
1245 return (ALint)(val * 127.0) + 128;
1247 static __inline ALubyte Conv_ALubyte_ALmulaw(ALmulaw val)
1248 { return Conv_ALubyte_ALshort(DecodeMuLaw(val)); }
1249 static __inline ALubyte Conv_ALubyte_ALbyte3(ALbyte3 val)
1250 { return (DecodeByte3(val)>>16)+128; }
1251 static __inline ALubyte Conv_ALubyte_ALubyte3(ALubyte3 val)
1252 { return DecodeUByte3(val)>>16; }
1254 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
1255 { return val<<8; }
1256 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
1257 { return (val-128)<<8; }
1258 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
1259 { return val; }
1260 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
1261 { return val-32768; }
1262 static __inline ALshort Conv_ALshort_ALint(ALint val)
1263 { return val>>16; }
1264 static __inline ALshort Conv_ALshort_ALuint(ALuint val)
1265 { return (val>>16)-32768; }
1266 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
1268 if(val > 1.0f) return 32767;
1269 if(val < -1.0f) return -32768;
1270 return (ALint)(val * 32767.0f);
1272 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
1274 if(val > 1.0) return 32767;
1275 if(val < -1.0) return -32768;
1276 return (ALint)(val * 32767.0);
1278 static __inline ALshort Conv_ALshort_ALmulaw(ALmulaw val)
1279 { return Conv_ALshort_ALshort(DecodeMuLaw(val)); }
1280 static __inline ALshort Conv_ALshort_ALbyte3(ALbyte3 val)
1281 { return DecodeByte3(val)>>8; }
1282 static __inline ALshort Conv_ALshort_ALubyte3(ALubyte3 val)
1283 { return (DecodeUByte3(val)>>8)-32768; }
1285 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
1286 { return (val+128)<<8; }
1287 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
1288 { return val<<8; }
1289 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
1290 { return val+32768; }
1291 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
1292 { return val; }
1293 static __inline ALushort Conv_ALushort_ALint(ALint val)
1294 { return (val>>16)+32768; }
1295 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
1296 { return val>>16; }
1297 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
1299 if(val > 1.0f) return 65535;
1300 if(val < -1.0f) return 0;
1301 return (ALint)(val * 32767.0f) + 32768;
1303 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
1305 if(val > 1.0) return 65535;
1306 if(val < -1.0) return 0;
1307 return (ALint)(val * 32767.0) + 32768;
1309 static __inline ALushort Conv_ALushort_ALmulaw(ALmulaw val)
1310 { return Conv_ALushort_ALshort(DecodeMuLaw(val)); }
1311 static __inline ALushort Conv_ALushort_ALbyte3(ALbyte3 val)
1312 { return (DecodeByte3(val)>>8)+32768; }
1313 static __inline ALushort Conv_ALushort_ALubyte3(ALubyte3 val)
1314 { return DecodeUByte3(val)>>8; }
1316 static __inline ALint Conv_ALint_ALbyte(ALbyte val)
1317 { return val<<24; }
1318 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
1319 { return (val-128)<<24; }
1320 static __inline ALint Conv_ALint_ALshort(ALshort val)
1321 { return val<<16; }
1322 static __inline ALint Conv_ALint_ALushort(ALushort val)
1323 { return (val-32768)<<16; }
1324 static __inline ALint Conv_ALint_ALint(ALint val)
1325 { return val; }
1326 static __inline ALint Conv_ALint_ALuint(ALuint val)
1327 { return val-2147483648u; }
1328 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
1330 if(val > 1.0f) return 2147483647;
1331 if(val < -1.0f) return -2147483647-1;
1332 return (ALint)(val * 2147483647.0);
1334 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
1336 if(val > 1.0) return 2147483647;
1337 if(val < -1.0) return -2147483647-1;
1338 return (ALint)(val * 2147483647.0);
1340 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
1341 { return Conv_ALint_ALshort(DecodeMuLaw(val)); }
1342 static __inline ALint Conv_ALint_ALbyte3(ALbyte3 val)
1343 { return DecodeByte3(val)<<8; }
1344 static __inline ALint Conv_ALint_ALubyte3(ALubyte3 val)
1345 { return (DecodeUByte3(val)-8388608)<<8; }
1347 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
1348 { return (val+128)<<24; }
1349 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
1350 { return val<<24; }
1351 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
1352 { return (val+32768)<<16; }
1353 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
1354 { return val<<16; }
1355 static __inline ALuint Conv_ALuint_ALint(ALint val)
1356 { return val+2147483648u; }
1357 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
1358 { return val; }
1359 static __inline ALuint Conv_ALuint_ALfloat(ALfloat val)
1361 if(val > 1.0f) return 4294967295u;
1362 if(val < -1.0f) return 0;
1363 return (ALint)(val * 2147483647.0) + 2147483648u;
1365 static __inline ALuint Conv_ALuint_ALdouble(ALdouble val)
1367 if(val > 1.0) return 4294967295u;
1368 if(val < -1.0) return 0;
1369 return (ALint)(val * 2147483647.0) + 2147483648u;
1371 static __inline ALuint Conv_ALuint_ALmulaw(ALmulaw val)
1372 { return Conv_ALuint_ALshort(DecodeMuLaw(val)); }
1373 static __inline ALuint Conv_ALuint_ALbyte3(ALbyte3 val)
1374 { return (DecodeByte3(val)+8388608)<<8; }
1375 static __inline ALuint Conv_ALuint_ALubyte3(ALubyte3 val)
1376 { return DecodeUByte3(val)<<8; }
1378 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
1379 { return val * (1.0f/127.0f); }
1380 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
1381 { return (val-128) * (1.0f/127.0f); }
1382 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
1383 { return val * (1.0f/32767.0f); }
1384 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
1385 { return (val-32768) * (1.0f/32767.0f); }
1386 static __inline ALfloat Conv_ALfloat_ALint(ALint val)
1387 { return val * (1.0/2147483647.0); }
1388 static __inline ALfloat Conv_ALfloat_ALuint(ALuint val)
1389 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1390 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
1391 { return (val==val) ? val : 0.0f; }
1392 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1393 { return (val==val) ? val : 0.0; }
1394 static __inline ALfloat Conv_ALfloat_ALmulaw(ALmulaw val)
1395 { return Conv_ALfloat_ALshort(DecodeMuLaw(val)); }
1396 static __inline ALfloat Conv_ALfloat_ALbyte3(ALbyte3 val)
1397 { return DecodeByte3(val) * (1.0/8388607.0); }
1398 static __inline ALfloat Conv_ALfloat_ALubyte3(ALubyte3 val)
1399 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
1401 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
1402 { return val * (1.0/127.0); }
1403 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
1404 { return (val-128) * (1.0/127.0); }
1405 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
1406 { return val * (1.0/32767.0); }
1407 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
1408 { return (val-32768) * (1.0/32767.0); }
1409 static __inline ALdouble Conv_ALdouble_ALint(ALint val)
1410 { return val * (1.0/2147483647.0); }
1411 static __inline ALdouble Conv_ALdouble_ALuint(ALuint val)
1412 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1413 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
1414 { return (val==val) ? val : 0.0f; }
1415 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1416 { return (val==val) ? val : 0.0; }
1417 static __inline ALdouble Conv_ALdouble_ALmulaw(ALmulaw val)
1418 { return Conv_ALdouble_ALshort(DecodeMuLaw(val)); }
1419 static __inline ALdouble Conv_ALdouble_ALbyte3(ALbyte3 val)
1420 { return DecodeByte3(val) * (1.0/8388607.0); }
1421 static __inline ALdouble Conv_ALdouble_ALubyte3(ALubyte3 val)
1422 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
1424 #define DECL_TEMPLATE(T) \
1425 static __inline ALmulaw Conv_ALmulaw_##T(T val) \
1426 { return EncodeMuLaw(Conv_ALshort_##T(val)); }
1428 DECL_TEMPLATE(ALbyte)
1429 DECL_TEMPLATE(ALubyte)
1430 DECL_TEMPLATE(ALshort)
1431 DECL_TEMPLATE(ALushort)
1432 DECL_TEMPLATE(ALint)
1433 DECL_TEMPLATE(ALuint)
1434 DECL_TEMPLATE(ALfloat)
1435 DECL_TEMPLATE(ALdouble)
1436 static __inline ALmulaw Conv_ALmulaw_ALmulaw(ALmulaw val)
1437 { return val; }
1438 DECL_TEMPLATE(ALbyte3)
1439 DECL_TEMPLATE(ALubyte3)
1441 #undef DECL_TEMPLATE
1443 #define DECL_TEMPLATE(T) \
1444 static __inline ALbyte3 Conv_ALbyte3_##T(T val) \
1445 { return EncodeByte3(Conv_ALint_##T(val)>>8); }
1447 DECL_TEMPLATE(ALbyte)
1448 DECL_TEMPLATE(ALubyte)
1449 DECL_TEMPLATE(ALshort)
1450 DECL_TEMPLATE(ALushort)
1451 DECL_TEMPLATE(ALint)
1452 DECL_TEMPLATE(ALuint)
1453 DECL_TEMPLATE(ALfloat)
1454 DECL_TEMPLATE(ALdouble)
1455 DECL_TEMPLATE(ALmulaw)
1456 static __inline ALbyte3 Conv_ALbyte3_ALbyte3(ALbyte3 val)
1457 { return val; }
1458 DECL_TEMPLATE(ALubyte3)
1460 #undef DECL_TEMPLATE
1462 #define DECL_TEMPLATE(T) \
1463 static __inline ALubyte3 Conv_ALubyte3_##T(T val) \
1464 { return EncodeUByte3(Conv_ALuint_##T(val)>>8); }
1466 DECL_TEMPLATE(ALbyte)
1467 DECL_TEMPLATE(ALubyte)
1468 DECL_TEMPLATE(ALshort)
1469 DECL_TEMPLATE(ALushort)
1470 DECL_TEMPLATE(ALint)
1471 DECL_TEMPLATE(ALuint)
1472 DECL_TEMPLATE(ALfloat)
1473 DECL_TEMPLATE(ALdouble)
1474 DECL_TEMPLATE(ALmulaw)
1475 DECL_TEMPLATE(ALbyte3)
1476 static __inline ALubyte3 Conv_ALubyte3_ALubyte3(ALubyte3 val)
1477 { return val; }
1479 #undef DECL_TEMPLATE
1482 #define DECL_TEMPLATE(T1, T2) \
1483 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
1484 ALuint len) \
1486 ALuint i, j; \
1487 for(i = 0;i < len;i++) \
1489 for(j = 0;j < numchans;j++) \
1490 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1494 DECL_TEMPLATE(ALbyte, ALbyte)
1495 DECL_TEMPLATE(ALbyte, ALubyte)
1496 DECL_TEMPLATE(ALbyte, ALshort)
1497 DECL_TEMPLATE(ALbyte, ALushort)
1498 DECL_TEMPLATE(ALbyte, ALint)
1499 DECL_TEMPLATE(ALbyte, ALuint)
1500 DECL_TEMPLATE(ALbyte, ALfloat)
1501 DECL_TEMPLATE(ALbyte, ALdouble)
1502 DECL_TEMPLATE(ALbyte, ALmulaw)
1503 DECL_TEMPLATE(ALbyte, ALbyte3)
1504 DECL_TEMPLATE(ALbyte, ALubyte3)
1506 DECL_TEMPLATE(ALubyte, ALbyte)
1507 DECL_TEMPLATE(ALubyte, ALubyte)
1508 DECL_TEMPLATE(ALubyte, ALshort)
1509 DECL_TEMPLATE(ALubyte, ALushort)
1510 DECL_TEMPLATE(ALubyte, ALint)
1511 DECL_TEMPLATE(ALubyte, ALuint)
1512 DECL_TEMPLATE(ALubyte, ALfloat)
1513 DECL_TEMPLATE(ALubyte, ALdouble)
1514 DECL_TEMPLATE(ALubyte, ALmulaw)
1515 DECL_TEMPLATE(ALubyte, ALbyte3)
1516 DECL_TEMPLATE(ALubyte, ALubyte3)
1518 DECL_TEMPLATE(ALshort, ALbyte)
1519 DECL_TEMPLATE(ALshort, ALubyte)
1520 DECL_TEMPLATE(ALshort, ALshort)
1521 DECL_TEMPLATE(ALshort, ALushort)
1522 DECL_TEMPLATE(ALshort, ALint)
1523 DECL_TEMPLATE(ALshort, ALuint)
1524 DECL_TEMPLATE(ALshort, ALfloat)
1525 DECL_TEMPLATE(ALshort, ALdouble)
1526 DECL_TEMPLATE(ALshort, ALmulaw)
1527 DECL_TEMPLATE(ALshort, ALbyte3)
1528 DECL_TEMPLATE(ALshort, ALubyte3)
1530 DECL_TEMPLATE(ALushort, ALbyte)
1531 DECL_TEMPLATE(ALushort, ALubyte)
1532 DECL_TEMPLATE(ALushort, ALshort)
1533 DECL_TEMPLATE(ALushort, ALushort)
1534 DECL_TEMPLATE(ALushort, ALint)
1535 DECL_TEMPLATE(ALushort, ALuint)
1536 DECL_TEMPLATE(ALushort, ALfloat)
1537 DECL_TEMPLATE(ALushort, ALdouble)
1538 DECL_TEMPLATE(ALushort, ALmulaw)
1539 DECL_TEMPLATE(ALushort, ALbyte3)
1540 DECL_TEMPLATE(ALushort, ALubyte3)
1542 DECL_TEMPLATE(ALint, ALbyte)
1543 DECL_TEMPLATE(ALint, ALubyte)
1544 DECL_TEMPLATE(ALint, ALshort)
1545 DECL_TEMPLATE(ALint, ALushort)
1546 DECL_TEMPLATE(ALint, ALint)
1547 DECL_TEMPLATE(ALint, ALuint)
1548 DECL_TEMPLATE(ALint, ALfloat)
1549 DECL_TEMPLATE(ALint, ALdouble)
1550 DECL_TEMPLATE(ALint, ALmulaw)
1551 DECL_TEMPLATE(ALint, ALbyte3)
1552 DECL_TEMPLATE(ALint, ALubyte3)
1554 DECL_TEMPLATE(ALuint, ALbyte)
1555 DECL_TEMPLATE(ALuint, ALubyte)
1556 DECL_TEMPLATE(ALuint, ALshort)
1557 DECL_TEMPLATE(ALuint, ALushort)
1558 DECL_TEMPLATE(ALuint, ALint)
1559 DECL_TEMPLATE(ALuint, ALuint)
1560 DECL_TEMPLATE(ALuint, ALfloat)
1561 DECL_TEMPLATE(ALuint, ALdouble)
1562 DECL_TEMPLATE(ALuint, ALmulaw)
1563 DECL_TEMPLATE(ALuint, ALbyte3)
1564 DECL_TEMPLATE(ALuint, ALubyte3)
1566 DECL_TEMPLATE(ALfloat, ALbyte)
1567 DECL_TEMPLATE(ALfloat, ALubyte)
1568 DECL_TEMPLATE(ALfloat, ALshort)
1569 DECL_TEMPLATE(ALfloat, ALushort)
1570 DECL_TEMPLATE(ALfloat, ALint)
1571 DECL_TEMPLATE(ALfloat, ALuint)
1572 DECL_TEMPLATE(ALfloat, ALfloat)
1573 DECL_TEMPLATE(ALfloat, ALdouble)
1574 DECL_TEMPLATE(ALfloat, ALmulaw)
1575 DECL_TEMPLATE(ALfloat, ALbyte3)
1576 DECL_TEMPLATE(ALfloat, ALubyte3)
1578 DECL_TEMPLATE(ALdouble, ALbyte)
1579 DECL_TEMPLATE(ALdouble, ALubyte)
1580 DECL_TEMPLATE(ALdouble, ALshort)
1581 DECL_TEMPLATE(ALdouble, ALushort)
1582 DECL_TEMPLATE(ALdouble, ALint)
1583 DECL_TEMPLATE(ALdouble, ALuint)
1584 DECL_TEMPLATE(ALdouble, ALfloat)
1585 DECL_TEMPLATE(ALdouble, ALdouble)
1586 DECL_TEMPLATE(ALdouble, ALmulaw)
1587 DECL_TEMPLATE(ALdouble, ALbyte3)
1588 DECL_TEMPLATE(ALdouble, ALubyte3)
1590 DECL_TEMPLATE(ALmulaw, ALbyte)
1591 DECL_TEMPLATE(ALmulaw, ALubyte)
1592 DECL_TEMPLATE(ALmulaw, ALshort)
1593 DECL_TEMPLATE(ALmulaw, ALushort)
1594 DECL_TEMPLATE(ALmulaw, ALint)
1595 DECL_TEMPLATE(ALmulaw, ALuint)
1596 DECL_TEMPLATE(ALmulaw, ALfloat)
1597 DECL_TEMPLATE(ALmulaw, ALdouble)
1598 DECL_TEMPLATE(ALmulaw, ALmulaw)
1599 DECL_TEMPLATE(ALmulaw, ALbyte3)
1600 DECL_TEMPLATE(ALmulaw, ALubyte3)
1602 DECL_TEMPLATE(ALbyte3, ALbyte)
1603 DECL_TEMPLATE(ALbyte3, ALubyte)
1604 DECL_TEMPLATE(ALbyte3, ALshort)
1605 DECL_TEMPLATE(ALbyte3, ALushort)
1606 DECL_TEMPLATE(ALbyte3, ALint)
1607 DECL_TEMPLATE(ALbyte3, ALuint)
1608 DECL_TEMPLATE(ALbyte3, ALfloat)
1609 DECL_TEMPLATE(ALbyte3, ALdouble)
1610 DECL_TEMPLATE(ALbyte3, ALmulaw)
1611 DECL_TEMPLATE(ALbyte3, ALbyte3)
1612 DECL_TEMPLATE(ALbyte3, ALubyte3)
1614 DECL_TEMPLATE(ALubyte3, ALbyte)
1615 DECL_TEMPLATE(ALubyte3, ALubyte)
1616 DECL_TEMPLATE(ALubyte3, ALshort)
1617 DECL_TEMPLATE(ALubyte3, ALushort)
1618 DECL_TEMPLATE(ALubyte3, ALint)
1619 DECL_TEMPLATE(ALubyte3, ALuint)
1620 DECL_TEMPLATE(ALubyte3, ALfloat)
1621 DECL_TEMPLATE(ALubyte3, ALdouble)
1622 DECL_TEMPLATE(ALubyte3, ALmulaw)
1623 DECL_TEMPLATE(ALubyte3, ALbyte3)
1624 DECL_TEMPLATE(ALubyte3, ALubyte3)
1626 #undef DECL_TEMPLATE
1628 #define DECL_TEMPLATE(T) \
1629 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
1630 ALuint numblocks) \
1632 ALuint i, j; \
1633 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1634 for(i = 0;i < numblocks;i++) \
1636 DecodeIMA4Block(tmp, src, numchans); \
1637 src += 36*numchans; \
1638 for(j = 0;j < 65*numchans;j++) \
1639 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
1643 DECL_TEMPLATE(ALbyte)
1644 DECL_TEMPLATE(ALubyte)
1645 DECL_TEMPLATE(ALshort)
1646 DECL_TEMPLATE(ALushort)
1647 DECL_TEMPLATE(ALint)
1648 DECL_TEMPLATE(ALuint)
1649 DECL_TEMPLATE(ALfloat)
1650 DECL_TEMPLATE(ALdouble)
1651 DECL_TEMPLATE(ALmulaw)
1652 DECL_TEMPLATE(ALbyte3)
1653 DECL_TEMPLATE(ALubyte3)
1655 #undef DECL_TEMPLATE
1657 #define DECL_TEMPLATE(T) \
1658 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
1659 ALuint numblocks) \
1661 ALuint i, j; \
1662 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1663 ALint sample[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1664 ALint index[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1665 for(i = 0;i < numblocks;i++) \
1667 for(j = 0;j < 65*numchans;j++) \
1668 tmp[j] = Conv_ALshort_##T(*(src++)); \
1669 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
1670 dst += 36*numchans; \
1674 DECL_TEMPLATE(ALbyte)
1675 DECL_TEMPLATE(ALubyte)
1676 DECL_TEMPLATE(ALshort)
1677 DECL_TEMPLATE(ALushort)
1678 DECL_TEMPLATE(ALint)
1679 DECL_TEMPLATE(ALuint)
1680 DECL_TEMPLATE(ALfloat)
1681 DECL_TEMPLATE(ALdouble)
1682 DECL_TEMPLATE(ALmulaw)
1683 static void Convert_ALima4_ALima4(ALima4 *dst, const ALima4 *src,
1684 ALuint numchans, ALuint numblocks)
1685 { memcpy(dst, src, numblocks*36*numchans); }
1686 DECL_TEMPLATE(ALbyte3)
1687 DECL_TEMPLATE(ALubyte3)
1689 #undef DECL_TEMPLATE
1691 #define DECL_TEMPLATE(T) \
1692 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
1693 ALsizei numchans, ALsizei len) \
1695 switch(srcType) \
1697 case UserFmtByte: \
1698 Convert_##T##_ALbyte(dst, src, numchans, len); \
1699 break; \
1700 case UserFmtUByte: \
1701 Convert_##T##_ALubyte(dst, src, numchans, len); \
1702 break; \
1703 case UserFmtShort: \
1704 Convert_##T##_ALshort(dst, src, numchans, len); \
1705 break; \
1706 case UserFmtUShort: \
1707 Convert_##T##_ALushort(dst, src, numchans, len); \
1708 break; \
1709 case UserFmtInt: \
1710 Convert_##T##_ALint(dst, src, numchans, len); \
1711 break; \
1712 case UserFmtUInt: \
1713 Convert_##T##_ALuint(dst, src, numchans, len); \
1714 break; \
1715 case UserFmtFloat: \
1716 Convert_##T##_ALfloat(dst, src, numchans, len); \
1717 break; \
1718 case UserFmtDouble: \
1719 Convert_##T##_ALdouble(dst, src, numchans, len); \
1720 break; \
1721 case UserFmtMulaw: \
1722 Convert_##T##_ALmulaw(dst, src, numchans, len); \
1723 break; \
1724 case UserFmtIMA4: \
1725 Convert_##T##_ALima4(dst, src, numchans, len); \
1726 break; \
1727 case UserFmtByte3: \
1728 Convert_##T##_ALbyte3(dst, src, numchans, len); \
1729 break; \
1730 case UserFmtUByte3: \
1731 Convert_##T##_ALubyte3(dst, src, numchans, len); \
1732 break; \
1736 DECL_TEMPLATE(ALbyte)
1737 DECL_TEMPLATE(ALubyte)
1738 DECL_TEMPLATE(ALshort)
1739 DECL_TEMPLATE(ALushort)
1740 DECL_TEMPLATE(ALint)
1741 DECL_TEMPLATE(ALuint)
1742 DECL_TEMPLATE(ALfloat)
1743 DECL_TEMPLATE(ALdouble)
1744 DECL_TEMPLATE(ALmulaw)
1745 DECL_TEMPLATE(ALima4)
1746 DECL_TEMPLATE(ALbyte3)
1747 DECL_TEMPLATE(ALubyte3)
1749 #undef DECL_TEMPLATE
1752 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len)
1754 switch(dstType)
1756 case UserFmtByte:
1757 Convert_ALbyte(dst, src, srcType, numchans, len);
1758 break;
1759 case UserFmtUByte:
1760 Convert_ALubyte(dst, src, srcType, numchans, len);
1761 break;
1762 case UserFmtShort:
1763 Convert_ALshort(dst, src, srcType, numchans, len);
1764 break;
1765 case UserFmtUShort:
1766 Convert_ALushort(dst, src, srcType, numchans, len);
1767 break;
1768 case UserFmtInt:
1769 Convert_ALint(dst, src, srcType, numchans, len);
1770 break;
1771 case UserFmtUInt:
1772 Convert_ALuint(dst, src, srcType, numchans, len);
1773 break;
1774 case UserFmtFloat:
1775 Convert_ALfloat(dst, src, srcType, numchans, len);
1776 break;
1777 case UserFmtDouble:
1778 Convert_ALdouble(dst, src, srcType, numchans, len);
1779 break;
1780 case UserFmtMulaw:
1781 Convert_ALmulaw(dst, src, srcType, numchans, len);
1782 break;
1783 case UserFmtIMA4:
1784 Convert_ALima4(dst, src, srcType, numchans, len);
1785 break;
1786 case UserFmtByte3:
1787 Convert_ALbyte3(dst, src, srcType, numchans, len);
1788 break;
1789 case UserFmtUByte3:
1790 Convert_ALubyte3(dst, src, srcType, numchans, len);
1791 break;
1797 * LoadData
1799 * Loads the specified data into the buffer, using the specified formats.
1800 * Currently, the new format must have the same channel configuration as the
1801 * original format.
1803 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels SrcChannels, enum UserFmtType SrcType, const ALvoid *data, ALboolean storesrc)
1805 ALuint NewChannels, NewBytes;
1806 enum FmtChannels DstChannels;
1807 enum FmtType DstType;
1808 ALuint64 newsize;
1809 ALvoid *temp;
1811 if(ALBuf->ref != 0)
1812 return AL_INVALID_OPERATION;
1814 if(DecomposeFormat(NewFormat, &DstChannels, &DstType) == AL_FALSE ||
1815 (long)SrcChannels != (long)DstChannels)
1816 return AL_INVALID_ENUM;
1818 NewChannels = ChannelsFromFmt(DstChannels);
1819 NewBytes = BytesFromFmt(DstType);
1821 if(SrcType == UserFmtIMA4)
1823 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
1825 newsize = frames;
1826 newsize *= 65;
1827 newsize *= NewBytes;
1828 newsize *= NewChannels;
1829 if(newsize > INT_MAX)
1830 return AL_OUT_OF_MEMORY;
1832 temp = realloc(ALBuf->data, newsize);
1833 if(!temp && newsize) return AL_OUT_OF_MEMORY;
1834 ALBuf->data = temp;
1835 ALBuf->size = newsize;
1837 if(data != NULL)
1838 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
1840 if(storesrc)
1842 ALBuf->OriginalChannels = SrcChannels;
1843 ALBuf->OriginalType = SrcType;
1844 ALBuf->OriginalSize = frames * 36 * OrigChannels;
1845 ALBuf->OriginalAlign = 36 * OrigChannels;
1848 else
1850 ALuint OrigBytes = BytesFromUserFmt(SrcType);
1851 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
1853 newsize = frames;
1854 newsize *= NewBytes;
1855 newsize *= NewChannels;
1856 if(newsize > INT_MAX)
1857 return AL_OUT_OF_MEMORY;
1859 temp = realloc(ALBuf->data, newsize);
1860 if(!temp && newsize) return AL_OUT_OF_MEMORY;
1861 ALBuf->data = temp;
1862 ALBuf->size = newsize;
1864 if(data != NULL)
1865 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
1867 if(storesrc)
1869 ALBuf->OriginalChannels = SrcChannels;
1870 ALBuf->OriginalType = SrcType;
1871 ALBuf->OriginalSize = frames * OrigBytes * OrigChannels;
1872 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
1876 if(!storesrc)
1878 ALBuf->OriginalChannels = DstChannels;
1879 ALBuf->OriginalType = DstType;
1880 ALBuf->OriginalSize = frames * NewBytes * NewChannels;
1881 ALBuf->OriginalAlign = NewBytes * NewChannels;
1883 ALBuf->Frequency = freq;
1884 ALBuf->FmtChannels = DstChannels;
1885 ALBuf->FmtType = DstType;
1887 ALBuf->LoopStart = 0;
1888 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
1890 return AL_NO_ERROR;
1894 ALuint BytesFromUserFmt(enum UserFmtType type)
1896 switch(type)
1898 case UserFmtByte: return sizeof(ALbyte);
1899 case UserFmtUByte: return sizeof(ALubyte);
1900 case UserFmtShort: return sizeof(ALshort);
1901 case UserFmtUShort: return sizeof(ALushort);
1902 case UserFmtInt: return sizeof(ALint);
1903 case UserFmtUInt: return sizeof(ALuint);
1904 case UserFmtFloat: return sizeof(ALfloat);
1905 case UserFmtDouble: return sizeof(ALdouble);
1906 case UserFmtByte3: return sizeof(ALbyte3);
1907 case UserFmtUByte3: return sizeof(ALubyte3);
1908 case UserFmtMulaw: return sizeof(ALubyte);
1909 case UserFmtIMA4: break; /* not handled here */
1911 return 0;
1913 ALuint ChannelsFromUserFmt(enum UserFmtChannels chans)
1915 switch(chans)
1917 case UserFmtMono: return 1;
1918 case UserFmtStereo: return 2;
1919 case UserFmtRear: return 2;
1920 case UserFmtQuad: return 4;
1921 case UserFmtX51: return 6;
1922 case UserFmtX61: return 7;
1923 case UserFmtX71: return 8;
1925 return 0;
1927 ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
1928 enum UserFmtType *type)
1930 switch(format)
1932 case AL_FORMAT_MONO8:
1933 *chans = UserFmtMono;
1934 *type = UserFmtUByte;
1935 return AL_TRUE;
1936 case AL_FORMAT_MONO16:
1937 *chans = UserFmtMono;
1938 *type = UserFmtShort;
1939 return AL_TRUE;
1940 case AL_FORMAT_MONO_FLOAT32:
1941 *chans = UserFmtMono;
1942 *type = UserFmtFloat;
1943 return AL_TRUE;
1944 case AL_FORMAT_MONO_DOUBLE_EXT:
1945 *chans = UserFmtMono;
1946 *type = UserFmtDouble;
1947 return AL_TRUE;
1948 case AL_FORMAT_MONO_IMA4:
1949 *chans = UserFmtMono;
1950 *type = UserFmtIMA4;
1951 return AL_TRUE;
1952 case AL_FORMAT_STEREO8:
1953 *chans = UserFmtStereo;
1954 *type = UserFmtUByte;
1955 return AL_TRUE;
1956 case AL_FORMAT_STEREO16:
1957 *chans = UserFmtStereo;
1958 *type = UserFmtShort;
1959 return AL_TRUE;
1960 case AL_FORMAT_STEREO_FLOAT32:
1961 *chans = UserFmtStereo;
1962 *type = UserFmtFloat;
1963 return AL_TRUE;
1964 case AL_FORMAT_STEREO_DOUBLE_EXT:
1965 *chans = UserFmtStereo;
1966 *type = UserFmtDouble;
1967 return AL_TRUE;
1968 case AL_FORMAT_STEREO_IMA4:
1969 *chans = UserFmtStereo;
1970 *type = UserFmtIMA4;
1971 return AL_TRUE;
1972 case AL_FORMAT_QUAD8_LOKI:
1973 case AL_FORMAT_QUAD8:
1974 *chans = UserFmtQuad;
1975 *type = UserFmtUByte;
1976 return AL_TRUE;
1977 case AL_FORMAT_QUAD16_LOKI:
1978 case AL_FORMAT_QUAD16:
1979 *chans = UserFmtQuad;
1980 *type = UserFmtShort;
1981 return AL_TRUE;
1982 case AL_FORMAT_QUAD32:
1983 *chans = UserFmtQuad;
1984 *type = UserFmtFloat;
1985 return AL_TRUE;
1986 case AL_FORMAT_REAR8:
1987 *chans = UserFmtRear;
1988 *type = UserFmtUByte;
1989 return AL_TRUE;
1990 case AL_FORMAT_REAR16:
1991 *chans = UserFmtRear;
1992 *type = UserFmtShort;
1993 return AL_TRUE;
1994 case AL_FORMAT_REAR32:
1995 *chans = UserFmtRear;
1996 *type = UserFmtFloat;
1997 return AL_TRUE;
1998 case AL_FORMAT_51CHN8:
1999 *chans = UserFmtX51;
2000 *type = UserFmtUByte;
2001 return AL_TRUE;
2002 case AL_FORMAT_51CHN16:
2003 *chans = UserFmtX51;
2004 *type = UserFmtShort;
2005 return AL_TRUE;
2006 case AL_FORMAT_51CHN32:
2007 *chans = UserFmtX51;
2008 *type = UserFmtFloat;
2009 return AL_TRUE;
2010 case AL_FORMAT_61CHN8:
2011 *chans = UserFmtX61;
2012 *type = UserFmtUByte;
2013 return AL_TRUE;
2014 case AL_FORMAT_61CHN16:
2015 *chans = UserFmtX61;
2016 *type = UserFmtShort;
2017 return AL_TRUE;
2018 case AL_FORMAT_61CHN32:
2019 *chans = UserFmtX61;
2020 *type = UserFmtFloat;
2021 return AL_TRUE;
2022 case AL_FORMAT_71CHN8:
2023 *chans = UserFmtX71;
2024 *type = UserFmtUByte;
2025 return AL_TRUE;
2026 case AL_FORMAT_71CHN16:
2027 *chans = UserFmtX71;
2028 *type = UserFmtShort;
2029 return AL_TRUE;
2030 case AL_FORMAT_71CHN32:
2031 *chans = UserFmtX71;
2032 *type = UserFmtFloat;
2033 return AL_TRUE;
2034 case AL_FORMAT_MONO_MULAW:
2035 *chans = UserFmtMono;
2036 *type = UserFmtMulaw;
2037 return AL_TRUE;
2038 case AL_FORMAT_STEREO_MULAW:
2039 *chans = UserFmtStereo;
2040 *type = UserFmtMulaw;
2041 return AL_TRUE;
2042 case AL_FORMAT_QUAD_MULAW:
2043 *chans = UserFmtQuad;
2044 *type = UserFmtMulaw;
2045 return AL_TRUE;
2046 case AL_FORMAT_REAR_MULAW:
2047 *chans = UserFmtRear;
2048 *type = UserFmtMulaw;
2049 return AL_TRUE;
2050 case AL_FORMAT_51CHN_MULAW:
2051 *chans = UserFmtX51;
2052 *type = UserFmtMulaw;
2053 return AL_TRUE;
2054 case AL_FORMAT_61CHN_MULAW:
2055 *chans = UserFmtX61;
2056 *type = UserFmtMulaw;
2057 return AL_TRUE;
2058 case AL_FORMAT_71CHN_MULAW:
2059 *chans = UserFmtX71;
2060 *type = UserFmtMulaw;
2061 return AL_TRUE;
2063 return AL_FALSE;
2066 ALuint BytesFromFmt(enum FmtType type)
2068 switch(type)
2070 case FmtByte: return sizeof(ALbyte);
2071 case FmtShort: return sizeof(ALshort);
2072 case FmtFloat: return sizeof(ALfloat);
2074 return 0;
2076 ALuint ChannelsFromFmt(enum FmtChannels chans)
2078 switch(chans)
2080 case FmtMono: return 1;
2081 case FmtStereo: return 2;
2082 case FmtRear: return 2;
2083 case FmtQuad: return 4;
2084 case FmtX51: return 6;
2085 case FmtX61: return 7;
2086 case FmtX71: return 8;
2088 return 0;
2090 ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
2092 switch(format)
2094 case AL_MONO8:
2095 *chans = FmtMono;
2096 *type = FmtByte;
2097 return AL_TRUE;
2098 case AL_MONO16:
2099 *chans = FmtMono;
2100 *type = FmtShort;
2101 return AL_TRUE;
2102 case AL_MONO32F:
2103 *chans = FmtMono;
2104 *type = FmtFloat;
2105 return AL_TRUE;
2106 case AL_STEREO8:
2107 *chans = FmtStereo;
2108 *type = FmtByte;
2109 return AL_TRUE;
2110 case AL_STEREO16:
2111 *chans = FmtStereo;
2112 *type = FmtShort;
2113 return AL_TRUE;
2114 case AL_STEREO32F:
2115 *chans = FmtStereo;
2116 *type = FmtFloat;
2117 return AL_TRUE;
2118 case AL_FORMAT_QUAD8_LOKI:
2119 case AL_QUAD8:
2120 *chans = FmtQuad;
2121 *type = FmtByte;
2122 return AL_TRUE;
2123 case AL_FORMAT_QUAD16_LOKI:
2124 case AL_QUAD16:
2125 *chans = FmtQuad;
2126 *type = FmtShort;
2127 return AL_TRUE;
2128 case AL_QUAD32F:
2129 *chans = FmtQuad;
2130 *type = FmtFloat;
2131 return AL_TRUE;
2132 case AL_REAR8:
2133 *chans = FmtRear;
2134 *type = FmtByte;
2135 return AL_TRUE;
2136 case AL_REAR16:
2137 *chans = FmtRear;
2138 *type = FmtShort;
2139 return AL_TRUE;
2140 case AL_REAR32F:
2141 *chans = FmtRear;
2142 *type = FmtFloat;
2143 return AL_TRUE;
2144 case AL_5POINT1_8:
2145 *chans = FmtX51;
2146 *type = FmtByte;
2147 return AL_TRUE;
2148 case AL_5POINT1_16:
2149 *chans = FmtX51;
2150 *type = FmtShort;
2151 return AL_TRUE;
2152 case AL_5POINT1_32F:
2153 *chans = FmtX51;
2154 *type = FmtFloat;
2155 return AL_TRUE;
2156 case AL_6POINT1_8:
2157 *chans = FmtX61;
2158 *type = FmtByte;
2159 return AL_TRUE;
2160 case AL_6POINT1_16:
2161 *chans = FmtX61;
2162 *type = FmtShort;
2163 return AL_TRUE;
2164 case AL_6POINT1_32F:
2165 *chans = FmtX61;
2166 *type = FmtFloat;
2167 return AL_TRUE;
2168 case AL_7POINT1_8:
2169 *chans = FmtX71;
2170 *type = FmtByte;
2171 return AL_TRUE;
2172 case AL_7POINT1_16:
2173 *chans = FmtX71;
2174 *type = FmtShort;
2175 return AL_TRUE;
2176 case AL_7POINT1_32F:
2177 *chans = FmtX71;
2178 *type = FmtFloat;
2179 return AL_TRUE;
2181 return AL_FALSE;
2185 static ALboolean IsValidType(ALenum type)
2187 switch(type)
2189 case AL_BYTE:
2190 case AL_UNSIGNED_BYTE:
2191 case AL_SHORT:
2192 case AL_UNSIGNED_SHORT:
2193 case AL_INT:
2194 case AL_UNSIGNED_INT:
2195 case AL_FLOAT:
2196 case AL_DOUBLE:
2197 case AL_MULAW:
2198 case AL_IMA4:
2199 case AL_BYTE3:
2200 case AL_UNSIGNED_BYTE3:
2201 return AL_TRUE;
2203 return AL_FALSE;
2206 static ALboolean IsValidChannels(ALenum channels)
2208 switch(channels)
2210 case AL_MONO:
2211 case AL_STEREO:
2212 case AL_REAR:
2213 case AL_QUAD:
2214 case AL_5POINT1:
2215 case AL_6POINT1:
2216 case AL_7POINT1:
2217 return AL_TRUE;
2219 return AL_FALSE;
2224 * ReleaseALBuffers()
2226 * INTERNAL: Called to destroy any buffers that still exist on the device
2228 ALvoid ReleaseALBuffers(ALCdevice *device)
2230 ALsizei i;
2231 for(i = 0;i < device->BufferMap.size;i++)
2233 ALbuffer *temp = device->BufferMap.array[i].value;
2234 device->BufferMap.array[i].value = NULL;
2236 free(temp->data);
2238 FreeThunkEntry(temp->buffer);
2239 memset(temp, 0, sizeof(ALbuffer));
2240 free(temp);