Check the number of available samples before calling the capture method
[openal-soft/android.git] / OpenAL32 / alBuffer.c
blobdc7baecb8022f932dadf570bb89b9bc588bd8421
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);
40 static ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans, enum UserFmtType *type);
41 static ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type);
43 #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
44 #define RemoveBuffer(m, k) ((ALbuffer*)PopUIntMapValue(&(m), (k)))
48 * Global Variables
51 /* IMA ADPCM Stepsize table */
52 static const long IMAStep_size[89] = {
53 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
54 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
55 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
56 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
57 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
58 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
59 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
60 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
61 32767
64 /* IMA4 ADPCM Codeword decode table */
65 static const long IMA4Codeword[16] = {
66 1, 3, 5, 7, 9, 11, 13, 15,
67 -1,-3,-5,-7,-9,-11,-13,-15,
70 /* IMA4 ADPCM Step index adjust decode table */
71 static const long IMA4Index_adjust[16] = {
72 -1,-1,-1,-1, 2, 4, 6, 8,
73 -1,-1,-1,-1, 2, 4, 6, 8
76 /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
77 * signed 16-bit sample */
78 static const ALshort muLawDecompressionTable[256] = {
79 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
80 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
81 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
82 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
83 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
84 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
85 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
86 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
87 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
88 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
89 -876, -844, -812, -780, -748, -716, -684, -652,
90 -620, -588, -556, -524, -492, -460, -428, -396,
91 -372, -356, -340, -324, -308, -292, -276, -260,
92 -244, -228, -212, -196, -180, -164, -148, -132,
93 -120, -112, -104, -96, -88, -80, -72, -64,
94 -56, -48, -40, -32, -24, -16, -8, 0,
95 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
96 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
97 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
98 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
99 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
100 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
101 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
102 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
103 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
104 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
105 876, 844, 812, 780, 748, 716, 684, 652,
106 620, 588, 556, 524, 492, 460, 428, 396,
107 372, 356, 340, 324, 308, 292, 276, 260,
108 244, 228, 212, 196, 180, 164, 148, 132,
109 120, 112, 104, 96, 88, 80, 72, 64,
110 56, 48, 40, 32, 24, 16, 8, 0
113 /* Values used when encoding a muLaw sample */
114 static const int muLawBias = 0x84;
115 static const int muLawClip = 32635;
116 static const char muLawCompressTable[256] =
118 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
119 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
120 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
121 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
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 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
125 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
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,
132 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
133 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
138 * alGenBuffers(ALsizei n, ALuint *buffers)
140 * Generates n AL Buffers, and stores the Buffers Names in the array pointed
141 * to by buffers
143 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
145 ALCcontext *Context;
146 ALsizei i=0;
148 Context = GetContextRef();
149 if(!Context) return;
151 /* Check that we are actually generating some Buffers */
152 if(n < 0 || IsBadWritePtr((void*)buffers, n * sizeof(ALuint)))
153 alSetError(Context, AL_INVALID_VALUE);
154 else
156 ALCdevice *device = Context->Device;
157 ALenum err;
159 // Create all the new Buffers
160 while(i < n)
162 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
163 if(!buffer)
165 alSetError(Context, AL_OUT_OF_MEMORY);
166 alDeleteBuffers(i, buffers);
167 break;
169 RWLockInit(&buffer->lock);
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
312 err = LoadData(ALBuf, freq, format, size/FrameSize,
313 SrcChannels, SrcType, data, AL_TRUE);
314 if(err != AL_NO_ERROR)
315 alSetError(Context, err);
316 } break;
318 case UserFmtByte3:
319 case UserFmtUByte3:
320 case UserFmtDouble: {
321 ALuint FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
322 ALenum NewFormat = AL_FORMAT_MONO_FLOAT32;
323 switch(SrcChannels)
325 case UserFmtMono: NewFormat = AL_FORMAT_MONO_FLOAT32; break;
326 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO_FLOAT32; break;
327 case UserFmtRear: NewFormat = AL_FORMAT_REAR32; break;
328 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD32; break;
329 case UserFmtX51: NewFormat = AL_FORMAT_51CHN32; break;
330 case UserFmtX61: NewFormat = AL_FORMAT_61CHN32; break;
331 case UserFmtX71: NewFormat = AL_FORMAT_71CHN32; break;
333 if((size%FrameSize) != 0)
334 err = AL_INVALID_VALUE;
335 else
336 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
337 SrcChannels, SrcType, data, AL_TRUE);
338 if(err != AL_NO_ERROR)
339 alSetError(Context, err);
340 } break;
342 case UserFmtMulaw:
343 case UserFmtIMA4: {
344 /* Here is where things vary:
345 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
346 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
348 ALuint FrameSize = (SrcType == UserFmtIMA4) ?
349 (ChannelsFromUserFmt(SrcChannels) * 36) :
350 FrameSizeFromUserFmt(SrcChannels, SrcType);
351 ALenum NewFormat = AL_FORMAT_MONO16;
352 switch(SrcChannels)
354 case UserFmtMono: NewFormat = AL_FORMAT_MONO16; break;
355 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
356 case UserFmtRear: NewFormat = AL_FORMAT_REAR16; break;
357 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
358 case UserFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
359 case UserFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
360 case UserFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
362 if((size%FrameSize) != 0)
363 err = AL_INVALID_VALUE;
364 else
365 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
366 SrcChannels, SrcType, data, AL_TRUE);
367 if(err != AL_NO_ERROR)
368 alSetError(Context, err);
369 } break;
372 ALCcontext_DecRef(Context);
376 * alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data,
377 * ALsizei offset, ALsizei length)
379 * Update buffer's audio data
381 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
383 enum UserFmtChannels SrcChannels;
384 enum UserFmtType SrcType;
385 ALCcontext *Context;
386 ALCdevice *device;
387 ALbuffer *ALBuf;
389 Context = GetContextRef();
390 if(!Context) return;
392 device = Context->Device;
393 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
394 alSetError(Context, AL_INVALID_NAME);
395 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
396 alSetError(Context, AL_INVALID_VALUE);
397 else if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
398 alSetError(Context, AL_INVALID_ENUM);
399 else
401 WriteLock(&ALBuf->lock);
402 if(SrcChannels != ALBuf->OriginalChannels || SrcType != ALBuf->OriginalType)
403 alSetError(Context, AL_INVALID_ENUM);
404 else if(offset > ALBuf->OriginalSize ||
405 length > ALBuf->OriginalSize-offset ||
406 (offset%ALBuf->OriginalAlign) != 0 ||
407 (length%ALBuf->OriginalAlign) != 0)
408 alSetError(Context, AL_INVALID_VALUE);
409 else
411 ALuint Channels = ChannelsFromFmt(ALBuf->FmtChannels);
412 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
413 if(SrcType == UserFmtIMA4)
415 /* offset -> byte offset, length -> block count */
416 offset /= 36;
417 offset *= 65;
418 offset *= Bytes;
419 length /= ALBuf->OriginalAlign;
421 else
423 ALuint OldBytes = BytesFromUserFmt(SrcType);
425 offset /= OldBytes;
426 offset *= Bytes;
427 length /= OldBytes * Channels;
429 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
430 data, SrcType, Channels, length);
432 WriteUnlock(&ALBuf->lock);
435 ALCcontext_DecRef(Context);
439 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer,
440 ALuint samplerate, ALenum internalformat, ALsizei frames,
441 ALenum channels, ALenum type, const ALvoid *data)
443 ALCcontext *Context;
444 ALCdevice *device;
445 ALbuffer *ALBuf;
446 ALenum err;
448 Context = GetContextRef();
449 if(!Context) return;
451 device = Context->Device;
452 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
453 alSetError(Context, AL_INVALID_NAME);
454 else if(frames < 0 || samplerate == 0)
455 alSetError(Context, AL_INVALID_VALUE);
456 else if(IsValidType(type) == AL_FALSE || IsValidChannels(channels) == AL_FALSE)
457 alSetError(Context, AL_INVALID_ENUM);
458 else
460 err = AL_NO_ERROR;
461 if(type == UserFmtIMA4)
463 if((frames%65) == 0) frames /= 65;
464 else err = AL_INVALID_VALUE;
466 if(err == AL_NO_ERROR)
467 err = LoadData(ALBuf, samplerate, internalformat, frames,
468 channels, type, data, AL_FALSE);
469 if(err != AL_NO_ERROR)
470 alSetError(Context, err);
473 ALCcontext_DecRef(Context);
476 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint buffer,
477 ALsizei offset, ALsizei frames,
478 ALenum channels, ALenum type, const ALvoid *data)
480 ALCcontext *Context;
481 ALCdevice *device;
482 ALbuffer *ALBuf;
484 Context = GetContextRef();
485 if(!Context) return;
487 device = Context->Device;
488 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
489 alSetError(Context, AL_INVALID_NAME);
490 else if(frames < 0 || offset < 0 || (frames > 0 && data == NULL))
491 alSetError(Context, AL_INVALID_VALUE);
492 else if(IsValidType(type) == AL_FALSE)
493 alSetError(Context, AL_INVALID_ENUM);
494 else
496 ALuint FrameSize;
497 ALuint FrameCount;
499 WriteLock(&ALBuf->lock);
500 FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
501 FrameCount = ALBuf->size / FrameSize;
502 if(channels != (ALenum)ALBuf->FmtChannels)
503 alSetError(Context, AL_INVALID_ENUM);
504 else if((ALuint)offset > FrameCount || (ALuint)frames > FrameCount-offset)
505 alSetError(Context, AL_INVALID_VALUE);
506 else if(type == UserFmtIMA4 && (frames%65) != 0)
507 alSetError(Context, AL_INVALID_VALUE);
508 else
510 /* offset -> byte offset */
511 offset *= FrameSize;
512 /* frames -> IMA4 block count */
513 if(type == UserFmtIMA4) frames /= 65;
514 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
515 data, type,
516 ChannelsFromFmt(ALBuf->FmtChannels), frames);
518 WriteUnlock(&ALBuf->lock);
521 ALCcontext_DecRef(Context);
524 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer,
525 ALsizei offset, ALsizei frames,
526 ALenum channels, ALenum type, ALvoid *data)
528 ALCcontext *Context;
529 ALCdevice *device;
530 ALbuffer *ALBuf;
532 Context = GetContextRef();
533 if(!Context) return;
535 device = Context->Device;
536 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
537 alSetError(Context, AL_INVALID_NAME);
538 else if(frames < 0 || offset < 0 || (frames > 0 && data == NULL))
539 alSetError(Context, AL_INVALID_VALUE);
540 else if(IsValidType(type) == AL_FALSE)
541 alSetError(Context, AL_INVALID_ENUM);
542 else
544 ALuint FrameSize;
545 ALuint FrameCount;
547 ReadLock(&ALBuf->lock);
548 FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
549 FrameCount = ALBuf->size / FrameSize;
550 if(channels != (ALenum)ALBuf->FmtChannels)
551 alSetError(Context, AL_INVALID_ENUM);
552 else if((ALuint)offset > FrameCount || (ALuint)frames > FrameCount-offset)
553 alSetError(Context, AL_INVALID_VALUE);
554 else if(type == UserFmtIMA4 && (frames%65) != 0)
555 alSetError(Context, AL_INVALID_VALUE);
556 else
558 /* offset -> byte offset */
559 offset *= FrameSize;
560 /* frames -> IMA4 block count */
561 if(type == UserFmtIMA4) frames /= 65;
562 ConvertData(data, type,
563 &((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
564 ChannelsFromFmt(ALBuf->FmtChannels), frames);
566 ReadUnlock(&ALBuf->lock);
569 ALCcontext_DecRef(Context);
572 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format)
574 enum FmtChannels DstChannels;
575 enum FmtType DstType;
576 ALCcontext *Context;
577 ALboolean ret;
579 Context = GetContextRef();
580 if(!Context) return AL_FALSE;
582 ret = DecomposeFormat(format, &DstChannels, &DstType);
584 ALCcontext_DecRef(Context);
586 return ret;
590 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
592 ALCcontext *pContext;
593 ALCdevice *device;
595 (void)flValue;
597 pContext = GetContextRef();
598 if(!pContext) return;
600 device = pContext->Device;
601 if(LookupBuffer(device->BufferMap, buffer) == NULL)
602 alSetError(pContext, AL_INVALID_NAME);
603 else
605 switch(eParam)
607 default:
608 alSetError(pContext, AL_INVALID_ENUM);
609 break;
613 ALCcontext_DecRef(pContext);
617 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
619 ALCcontext *pContext;
620 ALCdevice *device;
622 (void)flValue1;
623 (void)flValue2;
624 (void)flValue3;
626 pContext = GetContextRef();
627 if(!pContext) return;
629 device = pContext->Device;
630 if(LookupBuffer(device->BufferMap, buffer) == NULL)
631 alSetError(pContext, AL_INVALID_NAME);
632 else
634 switch(eParam)
636 default:
637 alSetError(pContext, AL_INVALID_ENUM);
638 break;
642 ALCcontext_DecRef(pContext);
646 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
648 ALCcontext *pContext;
649 ALCdevice *device;
651 pContext = GetContextRef();
652 if(!pContext) return;
654 device = pContext->Device;
655 if(!flValues)
656 alSetError(pContext, AL_INVALID_VALUE);
657 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
658 alSetError(pContext, AL_INVALID_NAME);
659 else
661 switch(eParam)
663 default:
664 alSetError(pContext, AL_INVALID_ENUM);
665 break;
669 ALCcontext_DecRef(pContext);
673 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
675 ALCcontext *pContext;
676 ALCdevice *device;
678 (void)lValue;
680 pContext = GetContextRef();
681 if(!pContext) return;
683 device = pContext->Device;
684 if(LookupBuffer(device->BufferMap, buffer) == NULL)
685 alSetError(pContext, AL_INVALID_NAME);
686 else
688 switch(eParam)
690 default:
691 alSetError(pContext, AL_INVALID_ENUM);
692 break;
696 ALCcontext_DecRef(pContext);
700 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
702 ALCcontext *pContext;
703 ALCdevice *device;
705 (void)lValue1;
706 (void)lValue2;
707 (void)lValue3;
709 pContext = GetContextRef();
710 if(!pContext) return;
712 device = pContext->Device;
713 if(LookupBuffer(device->BufferMap, buffer) == NULL)
714 alSetError(pContext, AL_INVALID_NAME);
715 else
717 switch(eParam)
719 default:
720 alSetError(pContext, AL_INVALID_ENUM);
721 break;
725 ALCcontext_DecRef(pContext);
729 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
731 ALCcontext *pContext;
732 ALCdevice *device;
733 ALbuffer *ALBuf;
735 pContext = GetContextRef();
736 if(!pContext) return;
738 device = pContext->Device;
739 if(!plValues)
740 alSetError(pContext, AL_INVALID_VALUE);
741 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
742 alSetError(pContext, AL_INVALID_NAME);
743 else
745 switch(eParam)
747 case AL_LOOP_POINTS_SOFT:
748 WriteLock(&ALBuf->lock);
749 if(ALBuf->ref != 0)
750 alSetError(pContext, AL_INVALID_OPERATION);
751 else if(plValues[0] < 0 || plValues[1] < 0 ||
752 plValues[0] >= plValues[1] || ALBuf->size == 0)
753 alSetError(pContext, AL_INVALID_VALUE);
754 else
756 ALint maxlen = ALBuf->size /
757 FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
758 if(plValues[0] > maxlen || plValues[1] > maxlen)
759 alSetError(pContext, AL_INVALID_VALUE);
760 else
762 ALBuf->LoopStart = plValues[0];
763 ALBuf->LoopEnd = plValues[1];
766 WriteUnlock(&ALBuf->lock);
767 break;
769 default:
770 alSetError(pContext, AL_INVALID_ENUM);
771 break;
775 ALCcontext_DecRef(pContext);
779 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
781 ALCcontext *pContext;
782 ALCdevice *device;
784 pContext = GetContextRef();
785 if(!pContext) return;
787 device = pContext->Device;
788 if(!pflValue)
789 alSetError(pContext, AL_INVALID_VALUE);
790 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
791 alSetError(pContext, AL_INVALID_NAME);
792 else
794 switch(eParam)
796 default:
797 alSetError(pContext, AL_INVALID_ENUM);
798 break;
802 ALCcontext_DecRef(pContext);
806 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
808 ALCcontext *pContext;
809 ALCdevice *device;
811 pContext = GetContextRef();
812 if(!pContext) return;
814 device = pContext->Device;
815 if(!pflValue1 || !pflValue2 || !pflValue3)
816 alSetError(pContext, AL_INVALID_VALUE);
817 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
818 alSetError(pContext, AL_INVALID_NAME);
819 else
821 switch(eParam)
823 default:
824 alSetError(pContext, AL_INVALID_ENUM);
825 break;
829 ALCcontext_DecRef(pContext);
833 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
835 ALCcontext *pContext;
836 ALCdevice *device;
838 pContext = GetContextRef();
839 if(!pContext) return;
841 device = pContext->Device;
842 if(!pflValues)
843 alSetError(pContext, AL_INVALID_VALUE);
844 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
845 alSetError(pContext, AL_INVALID_NAME);
846 else
848 switch(eParam)
850 default:
851 alSetError(pContext, AL_INVALID_ENUM);
852 break;
856 ALCcontext_DecRef(pContext);
860 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
862 ALCcontext *pContext;
863 ALbuffer *pBuffer;
864 ALCdevice *device;
866 pContext = GetContextRef();
867 if(!pContext) return;
869 device = pContext->Device;
870 if(!plValue)
871 alSetError(pContext, AL_INVALID_VALUE);
872 else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
873 alSetError(pContext, AL_INVALID_NAME);
874 else
876 switch(eParam)
878 case AL_FREQUENCY:
879 *plValue = pBuffer->Frequency;
880 break;
882 case AL_BITS:
883 *plValue = BytesFromFmt(pBuffer->FmtType) * 8;
884 break;
886 case AL_CHANNELS:
887 *plValue = ChannelsFromFmt(pBuffer->FmtChannels);
888 break;
890 case AL_SIZE:
891 *plValue = pBuffer->size;
892 break;
894 default:
895 alSetError(pContext, AL_INVALID_ENUM);
896 break;
900 ALCcontext_DecRef(pContext);
904 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
906 ALCcontext *pContext;
907 ALCdevice *device;
909 pContext = GetContextRef();
910 if(!pContext) return;
912 device = pContext->Device;
913 if(!plValue1 || !plValue2 || !plValue3)
914 alSetError(pContext, AL_INVALID_VALUE);
915 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
916 alSetError(pContext, AL_INVALID_NAME);
917 else
919 switch(eParam)
921 default:
922 alSetError(pContext, AL_INVALID_ENUM);
923 break;
927 ALCcontext_DecRef(pContext);
931 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
933 ALCcontext *pContext;
934 ALCdevice *device;
935 ALbuffer *ALBuf;
937 switch(eParam)
939 case AL_FREQUENCY:
940 case AL_BITS:
941 case AL_CHANNELS:
942 case AL_SIZE:
943 alGetBufferi(buffer, eParam, plValues);
944 return;
947 pContext = GetContextRef();
948 if(!pContext) return;
950 device = pContext->Device;
951 if(!plValues)
952 alSetError(pContext, AL_INVALID_VALUE);
953 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
954 alSetError(pContext, AL_INVALID_NAME);
955 else
957 switch(eParam)
959 case AL_LOOP_POINTS_SOFT:
960 ReadLock(&ALBuf->lock);
961 plValues[0] = ALBuf->LoopStart;
962 plValues[1] = ALBuf->LoopEnd;
963 ReadUnlock(&ALBuf->lock);
964 break;
966 default:
967 alSetError(pContext, AL_INVALID_ENUM);
968 break;
972 ALCcontext_DecRef(pContext);
976 typedef ALubyte ALmulaw;
977 typedef ALubyte ALima4;
978 typedef struct {
979 ALbyte b[3];
980 } ALbyte3;
981 typedef struct {
982 ALubyte b[3];
983 } ALubyte3;
985 static __inline ALshort DecodeMuLaw(ALmulaw val)
986 { return muLawDecompressionTable[val]; }
988 static ALmulaw EncodeMuLaw(ALshort val)
990 ALint mant, exp, sign;
992 sign = (val>>8) & 0x80;
993 if(sign)
995 /* -32768 doesn't properly negate on a short; it results in itself.
996 * So clamp to -32767 */
997 val = maxi(val, -32767);
998 val = -val;
1001 val = mini(val, muLawClip);
1002 val += muLawBias;
1004 exp = muLawCompressTable[(val>>7) & 0xff];
1005 mant = (val >> (exp+3)) & 0x0f;
1007 return ~(sign | (exp<<4) | mant);
1010 static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans)
1012 ALint sample[MAXCHANNELS], index[MAXCHANNELS];
1013 ALuint code[MAXCHANNELS];
1014 ALsizei j,k,c;
1016 for(c = 0;c < numchans;c++)
1018 sample[c] = *(src++);
1019 sample[c] |= *(src++) << 8;
1020 sample[c] = (sample[c]^0x8000) - 32768;
1021 index[c] = *(src++);
1022 index[c] |= *(src++) << 8;
1023 index[c] = (index[c]^0x8000) - 32768;
1025 index[c] = clampi(index[c], 0, 88);
1027 dst[c] = sample[c];
1030 j = 1;
1031 while(j < 65)
1033 for(c = 0;c < numchans;c++)
1035 code[c] = *(src++);
1036 code[c] |= *(src++) << 8;
1037 code[c] |= *(src++) << 16;
1038 code[c] |= *(src++) << 24;
1041 for(k = 0;k < 8;k++,j++)
1043 for(c = 0;c < numchans;c++)
1045 int nibble = code[c]&0xf;
1046 code[c] >>= 4;
1048 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
1049 sample[c] = clampi(sample[c], -32768, 32767);
1051 index[c] += IMA4Index_adjust[nibble];
1052 index[c] = clampi(index[c], 0, 88);
1054 dst[j*numchans + c] = sample[c];
1060 static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans)
1062 ALsizei j,k,c;
1064 for(c = 0;c < numchans;c++)
1066 int diff = src[c] - sample[c];
1067 int step = IMAStep_size[index[c]];
1068 int nibble;
1070 nibble = 0;
1071 if(diff < 0)
1073 nibble = 0x8;
1074 diff = -diff;
1077 diff = mini(step*2, diff);
1078 nibble |= (diff*8/step - 1) / 2;
1080 sample[c] += IMA4Codeword[nibble] * step / 8;
1081 sample[c] = clampi(sample[c], -32768, 32767);
1083 index[c] += IMA4Index_adjust[nibble];
1084 index[c] = clampi(index[c], 0, 88);
1086 *(dst++) = sample[c] & 0xff;
1087 *(dst++) = (sample[c]>>8) & 0xff;
1088 *(dst++) = index[c] & 0xff;
1089 *(dst++) = (index[c]>>8) & 0xff;
1092 j = 1;
1093 while(j < 65)
1095 for(c = 0;c < numchans;c++)
1097 for(k = 0;k < 8;k++)
1099 int diff = src[(j+k)*numchans + c] - sample[c];
1100 int step = IMAStep_size[index[c]];
1101 int nibble;
1103 nibble = 0;
1104 if(diff < 0)
1106 nibble = 0x8;
1107 diff = -diff;
1110 diff = mini(step*2, diff);
1111 nibble |= (diff*8/step - 1) / 2;
1113 sample[c] += IMA4Codeword[nibble] * step / 8;
1114 sample[c] = clampi(sample[c], -32768, 32767);
1116 index[c] += IMA4Index_adjust[nibble];
1117 index[c] = clampi(index[c], 0, 88);
1119 if(!(k&1)) *dst = nibble;
1120 else *(dst++) |= nibble<<4;
1123 j += 8;
1127 static const union {
1128 ALuint u;
1129 ALubyte b[sizeof(ALuint)];
1130 } EndianTest = { 1 };
1131 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
1133 static __inline ALint DecodeByte3(ALbyte3 val)
1135 if(IS_LITTLE_ENDIAN)
1136 return (val.b[2]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[0]);
1137 return (val.b[0]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[2]);
1140 static __inline ALbyte3 EncodeByte3(ALint val)
1142 if(IS_LITTLE_ENDIAN)
1144 ALbyte3 ret = {{ val, val>>8, val>>16 }};
1145 return ret;
1147 else
1149 ALbyte3 ret = {{ val>>16, val>>8, val }};
1150 return ret;
1154 static __inline ALint DecodeUByte3(ALubyte3 val)
1156 if(IS_LITTLE_ENDIAN)
1157 return (val.b[2]<<16) | (val.b[1]<<8) | (val.b[0]);
1158 return (val.b[0]<<16) | (val.b[1]<<8) | val.b[2];
1161 static __inline ALubyte3 EncodeUByte3(ALint val)
1163 if(IS_LITTLE_ENDIAN)
1165 ALubyte3 ret = {{ val, val>>8, val>>16 }};
1166 return ret;
1168 else
1170 ALubyte3 ret = {{ val>>16, val>>8, val }};
1171 return ret;
1176 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
1177 { return val; }
1178 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
1179 { return val-128; }
1180 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
1181 { return val>>8; }
1182 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
1183 { return (val>>8)-128; }
1184 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
1185 { return val>>24; }
1186 static __inline ALbyte Conv_ALbyte_ALuint(ALuint val)
1187 { return (val>>24)-128; }
1188 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
1190 if(val > 1.0f) return 127;
1191 if(val < -1.0f) return -128;
1192 return (ALint)(val * 127.0f);
1194 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
1196 if(val > 1.0) return 127;
1197 if(val < -1.0) return -128;
1198 return (ALint)(val * 127.0);
1200 static __inline ALbyte Conv_ALbyte_ALmulaw(ALmulaw val)
1201 { return Conv_ALbyte_ALshort(DecodeMuLaw(val)); }
1202 static __inline ALbyte Conv_ALbyte_ALbyte3(ALbyte3 val)
1203 { return DecodeByte3(val)>>16; }
1204 static __inline ALbyte Conv_ALbyte_ALubyte3(ALubyte3 val)
1205 { return (DecodeUByte3(val)>>16)-128; }
1207 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
1208 { return val+128; }
1209 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
1210 { return val; }
1211 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
1212 { return (val>>8)+128; }
1213 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
1214 { return val>>8; }
1215 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
1216 { return (val>>24)+128; }
1217 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
1218 { return val>>24; }
1219 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
1221 if(val > 1.0f) return 255;
1222 if(val < -1.0f) return 0;
1223 return (ALint)(val * 127.0f) + 128;
1225 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
1227 if(val > 1.0) return 255;
1228 if(val < -1.0) return 0;
1229 return (ALint)(val * 127.0) + 128;
1231 static __inline ALubyte Conv_ALubyte_ALmulaw(ALmulaw val)
1232 { return Conv_ALubyte_ALshort(DecodeMuLaw(val)); }
1233 static __inline ALubyte Conv_ALubyte_ALbyte3(ALbyte3 val)
1234 { return (DecodeByte3(val)>>16)+128; }
1235 static __inline ALubyte Conv_ALubyte_ALubyte3(ALubyte3 val)
1236 { return DecodeUByte3(val)>>16; }
1238 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
1239 { return val<<8; }
1240 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
1241 { return (val-128)<<8; }
1242 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
1243 { return val; }
1244 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
1245 { return val-32768; }
1246 static __inline ALshort Conv_ALshort_ALint(ALint val)
1247 { return val>>16; }
1248 static __inline ALshort Conv_ALshort_ALuint(ALuint val)
1249 { return (val>>16)-32768; }
1250 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
1252 if(val > 1.0f) return 32767;
1253 if(val < -1.0f) return -32768;
1254 return (ALint)(val * 32767.0f);
1256 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
1258 if(val > 1.0) return 32767;
1259 if(val < -1.0) return -32768;
1260 return (ALint)(val * 32767.0);
1262 static __inline ALshort Conv_ALshort_ALmulaw(ALmulaw val)
1263 { return Conv_ALshort_ALshort(DecodeMuLaw(val)); }
1264 static __inline ALshort Conv_ALshort_ALbyte3(ALbyte3 val)
1265 { return DecodeByte3(val)>>8; }
1266 static __inline ALshort Conv_ALshort_ALubyte3(ALubyte3 val)
1267 { return (DecodeUByte3(val)>>8)-32768; }
1269 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
1270 { return (val+128)<<8; }
1271 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
1272 { return val<<8; }
1273 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
1274 { return val+32768; }
1275 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
1276 { return val; }
1277 static __inline ALushort Conv_ALushort_ALint(ALint val)
1278 { return (val>>16)+32768; }
1279 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
1280 { return val>>16; }
1281 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
1283 if(val > 1.0f) return 65535;
1284 if(val < -1.0f) return 0;
1285 return (ALint)(val * 32767.0f) + 32768;
1287 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
1289 if(val > 1.0) return 65535;
1290 if(val < -1.0) return 0;
1291 return (ALint)(val * 32767.0) + 32768;
1293 static __inline ALushort Conv_ALushort_ALmulaw(ALmulaw val)
1294 { return Conv_ALushort_ALshort(DecodeMuLaw(val)); }
1295 static __inline ALushort Conv_ALushort_ALbyte3(ALbyte3 val)
1296 { return (DecodeByte3(val)>>8)+32768; }
1297 static __inline ALushort Conv_ALushort_ALubyte3(ALubyte3 val)
1298 { return DecodeUByte3(val)>>8; }
1300 static __inline ALint Conv_ALint_ALbyte(ALbyte val)
1301 { return val<<24; }
1302 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
1303 { return (val-128)<<24; }
1304 static __inline ALint Conv_ALint_ALshort(ALshort val)
1305 { return val<<16; }
1306 static __inline ALint Conv_ALint_ALushort(ALushort val)
1307 { return (val-32768)<<16; }
1308 static __inline ALint Conv_ALint_ALint(ALint val)
1309 { return val; }
1310 static __inline ALint Conv_ALint_ALuint(ALuint val)
1311 { return val-2147483648u; }
1312 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
1314 if(val > 1.0f) return 2147483647;
1315 if(val < -1.0f) return -2147483647-1;
1316 return (ALint)(val * 2147483647.0);
1318 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
1320 if(val > 1.0) return 2147483647;
1321 if(val < -1.0) return -2147483647-1;
1322 return (ALint)(val * 2147483647.0);
1324 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
1325 { return Conv_ALint_ALshort(DecodeMuLaw(val)); }
1326 static __inline ALint Conv_ALint_ALbyte3(ALbyte3 val)
1327 { return DecodeByte3(val)<<8; }
1328 static __inline ALint Conv_ALint_ALubyte3(ALubyte3 val)
1329 { return (DecodeUByte3(val)-8388608)<<8; }
1331 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
1332 { return (val+128)<<24; }
1333 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
1334 { return val<<24; }
1335 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
1336 { return (val+32768)<<16; }
1337 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
1338 { return val<<16; }
1339 static __inline ALuint Conv_ALuint_ALint(ALint val)
1340 { return val+2147483648u; }
1341 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
1342 { return val; }
1343 static __inline ALuint Conv_ALuint_ALfloat(ALfloat val)
1345 if(val > 1.0f) return 4294967295u;
1346 if(val < -1.0f) return 0;
1347 return (ALint)(val * 2147483647.0) + 2147483648u;
1349 static __inline ALuint Conv_ALuint_ALdouble(ALdouble val)
1351 if(val > 1.0) return 4294967295u;
1352 if(val < -1.0) return 0;
1353 return (ALint)(val * 2147483647.0) + 2147483648u;
1355 static __inline ALuint Conv_ALuint_ALmulaw(ALmulaw val)
1356 { return Conv_ALuint_ALshort(DecodeMuLaw(val)); }
1357 static __inline ALuint Conv_ALuint_ALbyte3(ALbyte3 val)
1358 { return (DecodeByte3(val)+8388608)<<8; }
1359 static __inline ALuint Conv_ALuint_ALubyte3(ALubyte3 val)
1360 { return DecodeUByte3(val)<<8; }
1362 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
1363 { return val * (1.0f/127.0f); }
1364 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
1365 { return (val-128) * (1.0f/127.0f); }
1366 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
1367 { return val * (1.0f/32767.0f); }
1368 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
1369 { return (val-32768) * (1.0f/32767.0f); }
1370 static __inline ALfloat Conv_ALfloat_ALint(ALint val)
1371 { return val * (1.0/2147483647.0); }
1372 static __inline ALfloat Conv_ALfloat_ALuint(ALuint val)
1373 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1374 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
1375 { return (val==val) ? val : 0.0f; }
1376 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1377 { return (val==val) ? val : 0.0; }
1378 static __inline ALfloat Conv_ALfloat_ALmulaw(ALmulaw val)
1379 { return Conv_ALfloat_ALshort(DecodeMuLaw(val)); }
1380 static __inline ALfloat Conv_ALfloat_ALbyte3(ALbyte3 val)
1381 { return DecodeByte3(val) * (1.0/8388607.0); }
1382 static __inline ALfloat Conv_ALfloat_ALubyte3(ALubyte3 val)
1383 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
1385 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
1386 { return val * (1.0/127.0); }
1387 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
1388 { return (val-128) * (1.0/127.0); }
1389 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
1390 { return val * (1.0/32767.0); }
1391 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
1392 { return (val-32768) * (1.0/32767.0); }
1393 static __inline ALdouble Conv_ALdouble_ALint(ALint val)
1394 { return val * (1.0/2147483647.0); }
1395 static __inline ALdouble Conv_ALdouble_ALuint(ALuint val)
1396 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1397 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
1398 { return (val==val) ? val : 0.0f; }
1399 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1400 { return (val==val) ? val : 0.0; }
1401 static __inline ALdouble Conv_ALdouble_ALmulaw(ALmulaw val)
1402 { return Conv_ALdouble_ALshort(DecodeMuLaw(val)); }
1403 static __inline ALdouble Conv_ALdouble_ALbyte3(ALbyte3 val)
1404 { return DecodeByte3(val) * (1.0/8388607.0); }
1405 static __inline ALdouble Conv_ALdouble_ALubyte3(ALubyte3 val)
1406 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
1408 #define DECL_TEMPLATE(T) \
1409 static __inline ALmulaw Conv_ALmulaw_##T(T val) \
1410 { return EncodeMuLaw(Conv_ALshort_##T(val)); }
1412 DECL_TEMPLATE(ALbyte)
1413 DECL_TEMPLATE(ALubyte)
1414 DECL_TEMPLATE(ALshort)
1415 DECL_TEMPLATE(ALushort)
1416 DECL_TEMPLATE(ALint)
1417 DECL_TEMPLATE(ALuint)
1418 DECL_TEMPLATE(ALfloat)
1419 DECL_TEMPLATE(ALdouble)
1420 static __inline ALmulaw Conv_ALmulaw_ALmulaw(ALmulaw val)
1421 { return val; }
1422 DECL_TEMPLATE(ALbyte3)
1423 DECL_TEMPLATE(ALubyte3)
1425 #undef DECL_TEMPLATE
1427 #define DECL_TEMPLATE(T) \
1428 static __inline ALbyte3 Conv_ALbyte3_##T(T val) \
1429 { return EncodeByte3(Conv_ALint_##T(val)>>8); }
1431 DECL_TEMPLATE(ALbyte)
1432 DECL_TEMPLATE(ALubyte)
1433 DECL_TEMPLATE(ALshort)
1434 DECL_TEMPLATE(ALushort)
1435 DECL_TEMPLATE(ALint)
1436 DECL_TEMPLATE(ALuint)
1437 DECL_TEMPLATE(ALfloat)
1438 DECL_TEMPLATE(ALdouble)
1439 DECL_TEMPLATE(ALmulaw)
1440 static __inline ALbyte3 Conv_ALbyte3_ALbyte3(ALbyte3 val)
1441 { return val; }
1442 DECL_TEMPLATE(ALubyte3)
1444 #undef DECL_TEMPLATE
1446 #define DECL_TEMPLATE(T) \
1447 static __inline ALubyte3 Conv_ALubyte3_##T(T val) \
1448 { return EncodeUByte3(Conv_ALuint_##T(val)>>8); }
1450 DECL_TEMPLATE(ALbyte)
1451 DECL_TEMPLATE(ALubyte)
1452 DECL_TEMPLATE(ALshort)
1453 DECL_TEMPLATE(ALushort)
1454 DECL_TEMPLATE(ALint)
1455 DECL_TEMPLATE(ALuint)
1456 DECL_TEMPLATE(ALfloat)
1457 DECL_TEMPLATE(ALdouble)
1458 DECL_TEMPLATE(ALmulaw)
1459 DECL_TEMPLATE(ALbyte3)
1460 static __inline ALubyte3 Conv_ALubyte3_ALubyte3(ALubyte3 val)
1461 { return val; }
1463 #undef DECL_TEMPLATE
1466 #define DECL_TEMPLATE(T1, T2) \
1467 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
1468 ALuint len) \
1470 ALuint i, j; \
1471 for(i = 0;i < len;i++) \
1473 for(j = 0;j < numchans;j++) \
1474 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1478 DECL_TEMPLATE(ALbyte, ALbyte)
1479 DECL_TEMPLATE(ALbyte, ALubyte)
1480 DECL_TEMPLATE(ALbyte, ALshort)
1481 DECL_TEMPLATE(ALbyte, ALushort)
1482 DECL_TEMPLATE(ALbyte, ALint)
1483 DECL_TEMPLATE(ALbyte, ALuint)
1484 DECL_TEMPLATE(ALbyte, ALfloat)
1485 DECL_TEMPLATE(ALbyte, ALdouble)
1486 DECL_TEMPLATE(ALbyte, ALmulaw)
1487 DECL_TEMPLATE(ALbyte, ALbyte3)
1488 DECL_TEMPLATE(ALbyte, ALubyte3)
1490 DECL_TEMPLATE(ALubyte, ALbyte)
1491 DECL_TEMPLATE(ALubyte, ALubyte)
1492 DECL_TEMPLATE(ALubyte, ALshort)
1493 DECL_TEMPLATE(ALubyte, ALushort)
1494 DECL_TEMPLATE(ALubyte, ALint)
1495 DECL_TEMPLATE(ALubyte, ALuint)
1496 DECL_TEMPLATE(ALubyte, ALfloat)
1497 DECL_TEMPLATE(ALubyte, ALdouble)
1498 DECL_TEMPLATE(ALubyte, ALmulaw)
1499 DECL_TEMPLATE(ALubyte, ALbyte3)
1500 DECL_TEMPLATE(ALubyte, ALubyte3)
1502 DECL_TEMPLATE(ALshort, ALbyte)
1503 DECL_TEMPLATE(ALshort, ALubyte)
1504 DECL_TEMPLATE(ALshort, ALshort)
1505 DECL_TEMPLATE(ALshort, ALushort)
1506 DECL_TEMPLATE(ALshort, ALint)
1507 DECL_TEMPLATE(ALshort, ALuint)
1508 DECL_TEMPLATE(ALshort, ALfloat)
1509 DECL_TEMPLATE(ALshort, ALdouble)
1510 DECL_TEMPLATE(ALshort, ALmulaw)
1511 DECL_TEMPLATE(ALshort, ALbyte3)
1512 DECL_TEMPLATE(ALshort, ALubyte3)
1514 DECL_TEMPLATE(ALushort, ALbyte)
1515 DECL_TEMPLATE(ALushort, ALubyte)
1516 DECL_TEMPLATE(ALushort, ALshort)
1517 DECL_TEMPLATE(ALushort, ALushort)
1518 DECL_TEMPLATE(ALushort, ALint)
1519 DECL_TEMPLATE(ALushort, ALuint)
1520 DECL_TEMPLATE(ALushort, ALfloat)
1521 DECL_TEMPLATE(ALushort, ALdouble)
1522 DECL_TEMPLATE(ALushort, ALmulaw)
1523 DECL_TEMPLATE(ALushort, ALbyte3)
1524 DECL_TEMPLATE(ALushort, ALubyte3)
1526 DECL_TEMPLATE(ALint, ALbyte)
1527 DECL_TEMPLATE(ALint, ALubyte)
1528 DECL_TEMPLATE(ALint, ALshort)
1529 DECL_TEMPLATE(ALint, ALushort)
1530 DECL_TEMPLATE(ALint, ALint)
1531 DECL_TEMPLATE(ALint, ALuint)
1532 DECL_TEMPLATE(ALint, ALfloat)
1533 DECL_TEMPLATE(ALint, ALdouble)
1534 DECL_TEMPLATE(ALint, ALmulaw)
1535 DECL_TEMPLATE(ALint, ALbyte3)
1536 DECL_TEMPLATE(ALint, ALubyte3)
1538 DECL_TEMPLATE(ALuint, ALbyte)
1539 DECL_TEMPLATE(ALuint, ALubyte)
1540 DECL_TEMPLATE(ALuint, ALshort)
1541 DECL_TEMPLATE(ALuint, ALushort)
1542 DECL_TEMPLATE(ALuint, ALint)
1543 DECL_TEMPLATE(ALuint, ALuint)
1544 DECL_TEMPLATE(ALuint, ALfloat)
1545 DECL_TEMPLATE(ALuint, ALdouble)
1546 DECL_TEMPLATE(ALuint, ALmulaw)
1547 DECL_TEMPLATE(ALuint, ALbyte3)
1548 DECL_TEMPLATE(ALuint, ALubyte3)
1550 DECL_TEMPLATE(ALfloat, ALbyte)
1551 DECL_TEMPLATE(ALfloat, ALubyte)
1552 DECL_TEMPLATE(ALfloat, ALshort)
1553 DECL_TEMPLATE(ALfloat, ALushort)
1554 DECL_TEMPLATE(ALfloat, ALint)
1555 DECL_TEMPLATE(ALfloat, ALuint)
1556 DECL_TEMPLATE(ALfloat, ALfloat)
1557 DECL_TEMPLATE(ALfloat, ALdouble)
1558 DECL_TEMPLATE(ALfloat, ALmulaw)
1559 DECL_TEMPLATE(ALfloat, ALbyte3)
1560 DECL_TEMPLATE(ALfloat, ALubyte3)
1562 DECL_TEMPLATE(ALdouble, ALbyte)
1563 DECL_TEMPLATE(ALdouble, ALubyte)
1564 DECL_TEMPLATE(ALdouble, ALshort)
1565 DECL_TEMPLATE(ALdouble, ALushort)
1566 DECL_TEMPLATE(ALdouble, ALint)
1567 DECL_TEMPLATE(ALdouble, ALuint)
1568 DECL_TEMPLATE(ALdouble, ALfloat)
1569 DECL_TEMPLATE(ALdouble, ALdouble)
1570 DECL_TEMPLATE(ALdouble, ALmulaw)
1571 DECL_TEMPLATE(ALdouble, ALbyte3)
1572 DECL_TEMPLATE(ALdouble, ALubyte3)
1574 DECL_TEMPLATE(ALmulaw, ALbyte)
1575 DECL_TEMPLATE(ALmulaw, ALubyte)
1576 DECL_TEMPLATE(ALmulaw, ALshort)
1577 DECL_TEMPLATE(ALmulaw, ALushort)
1578 DECL_TEMPLATE(ALmulaw, ALint)
1579 DECL_TEMPLATE(ALmulaw, ALuint)
1580 DECL_TEMPLATE(ALmulaw, ALfloat)
1581 DECL_TEMPLATE(ALmulaw, ALdouble)
1582 DECL_TEMPLATE(ALmulaw, ALmulaw)
1583 DECL_TEMPLATE(ALmulaw, ALbyte3)
1584 DECL_TEMPLATE(ALmulaw, ALubyte3)
1586 DECL_TEMPLATE(ALbyte3, ALbyte)
1587 DECL_TEMPLATE(ALbyte3, ALubyte)
1588 DECL_TEMPLATE(ALbyte3, ALshort)
1589 DECL_TEMPLATE(ALbyte3, ALushort)
1590 DECL_TEMPLATE(ALbyte3, ALint)
1591 DECL_TEMPLATE(ALbyte3, ALuint)
1592 DECL_TEMPLATE(ALbyte3, ALfloat)
1593 DECL_TEMPLATE(ALbyte3, ALdouble)
1594 DECL_TEMPLATE(ALbyte3, ALmulaw)
1595 DECL_TEMPLATE(ALbyte3, ALbyte3)
1596 DECL_TEMPLATE(ALbyte3, ALubyte3)
1598 DECL_TEMPLATE(ALubyte3, ALbyte)
1599 DECL_TEMPLATE(ALubyte3, ALubyte)
1600 DECL_TEMPLATE(ALubyte3, ALshort)
1601 DECL_TEMPLATE(ALubyte3, ALushort)
1602 DECL_TEMPLATE(ALubyte3, ALint)
1603 DECL_TEMPLATE(ALubyte3, ALuint)
1604 DECL_TEMPLATE(ALubyte3, ALfloat)
1605 DECL_TEMPLATE(ALubyte3, ALdouble)
1606 DECL_TEMPLATE(ALubyte3, ALmulaw)
1607 DECL_TEMPLATE(ALubyte3, ALbyte3)
1608 DECL_TEMPLATE(ALubyte3, ALubyte3)
1610 #undef DECL_TEMPLATE
1612 #define DECL_TEMPLATE(T) \
1613 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
1614 ALuint numblocks) \
1616 ALuint i, j; \
1617 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1618 for(i = 0;i < numblocks;i++) \
1620 DecodeIMA4Block(tmp, src, numchans); \
1621 src += 36*numchans; \
1622 for(j = 0;j < 65*numchans;j++) \
1623 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
1627 DECL_TEMPLATE(ALbyte)
1628 DECL_TEMPLATE(ALubyte)
1629 DECL_TEMPLATE(ALshort)
1630 DECL_TEMPLATE(ALushort)
1631 DECL_TEMPLATE(ALint)
1632 DECL_TEMPLATE(ALuint)
1633 DECL_TEMPLATE(ALfloat)
1634 DECL_TEMPLATE(ALdouble)
1635 DECL_TEMPLATE(ALmulaw)
1636 DECL_TEMPLATE(ALbyte3)
1637 DECL_TEMPLATE(ALubyte3)
1639 #undef DECL_TEMPLATE
1641 #define DECL_TEMPLATE(T) \
1642 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
1643 ALuint numblocks) \
1645 ALuint i, j; \
1646 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1647 ALint sample[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1648 ALint index[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1649 for(i = 0;i < numblocks;i++) \
1651 for(j = 0;j < 65*numchans;j++) \
1652 tmp[j] = Conv_ALshort_##T(*(src++)); \
1653 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
1654 dst += 36*numchans; \
1658 DECL_TEMPLATE(ALbyte)
1659 DECL_TEMPLATE(ALubyte)
1660 DECL_TEMPLATE(ALshort)
1661 DECL_TEMPLATE(ALushort)
1662 DECL_TEMPLATE(ALint)
1663 DECL_TEMPLATE(ALuint)
1664 DECL_TEMPLATE(ALfloat)
1665 DECL_TEMPLATE(ALdouble)
1666 DECL_TEMPLATE(ALmulaw)
1667 static void Convert_ALima4_ALima4(ALima4 *dst, const ALima4 *src,
1668 ALuint numchans, ALuint numblocks)
1669 { memcpy(dst, src, numblocks*36*numchans); }
1670 DECL_TEMPLATE(ALbyte3)
1671 DECL_TEMPLATE(ALubyte3)
1673 #undef DECL_TEMPLATE
1675 #define DECL_TEMPLATE(T) \
1676 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
1677 ALsizei numchans, ALsizei len) \
1679 switch(srcType) \
1681 case UserFmtByte: \
1682 Convert_##T##_ALbyte(dst, src, numchans, len); \
1683 break; \
1684 case UserFmtUByte: \
1685 Convert_##T##_ALubyte(dst, src, numchans, len); \
1686 break; \
1687 case UserFmtShort: \
1688 Convert_##T##_ALshort(dst, src, numchans, len); \
1689 break; \
1690 case UserFmtUShort: \
1691 Convert_##T##_ALushort(dst, src, numchans, len); \
1692 break; \
1693 case UserFmtInt: \
1694 Convert_##T##_ALint(dst, src, numchans, len); \
1695 break; \
1696 case UserFmtUInt: \
1697 Convert_##T##_ALuint(dst, src, numchans, len); \
1698 break; \
1699 case UserFmtFloat: \
1700 Convert_##T##_ALfloat(dst, src, numchans, len); \
1701 break; \
1702 case UserFmtDouble: \
1703 Convert_##T##_ALdouble(dst, src, numchans, len); \
1704 break; \
1705 case UserFmtMulaw: \
1706 Convert_##T##_ALmulaw(dst, src, numchans, len); \
1707 break; \
1708 case UserFmtIMA4: \
1709 Convert_##T##_ALima4(dst, src, numchans, len); \
1710 break; \
1711 case UserFmtByte3: \
1712 Convert_##T##_ALbyte3(dst, src, numchans, len); \
1713 break; \
1714 case UserFmtUByte3: \
1715 Convert_##T##_ALubyte3(dst, src, numchans, len); \
1716 break; \
1720 DECL_TEMPLATE(ALbyte)
1721 DECL_TEMPLATE(ALubyte)
1722 DECL_TEMPLATE(ALshort)
1723 DECL_TEMPLATE(ALushort)
1724 DECL_TEMPLATE(ALint)
1725 DECL_TEMPLATE(ALuint)
1726 DECL_TEMPLATE(ALfloat)
1727 DECL_TEMPLATE(ALdouble)
1728 DECL_TEMPLATE(ALmulaw)
1729 DECL_TEMPLATE(ALima4)
1730 DECL_TEMPLATE(ALbyte3)
1731 DECL_TEMPLATE(ALubyte3)
1733 #undef DECL_TEMPLATE
1736 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len)
1738 switch(dstType)
1740 case UserFmtByte:
1741 Convert_ALbyte(dst, src, srcType, numchans, len);
1742 break;
1743 case UserFmtUByte:
1744 Convert_ALubyte(dst, src, srcType, numchans, len);
1745 break;
1746 case UserFmtShort:
1747 Convert_ALshort(dst, src, srcType, numchans, len);
1748 break;
1749 case UserFmtUShort:
1750 Convert_ALushort(dst, src, srcType, numchans, len);
1751 break;
1752 case UserFmtInt:
1753 Convert_ALint(dst, src, srcType, numchans, len);
1754 break;
1755 case UserFmtUInt:
1756 Convert_ALuint(dst, src, srcType, numchans, len);
1757 break;
1758 case UserFmtFloat:
1759 Convert_ALfloat(dst, src, srcType, numchans, len);
1760 break;
1761 case UserFmtDouble:
1762 Convert_ALdouble(dst, src, srcType, numchans, len);
1763 break;
1764 case UserFmtMulaw:
1765 Convert_ALmulaw(dst, src, srcType, numchans, len);
1766 break;
1767 case UserFmtIMA4:
1768 Convert_ALima4(dst, src, srcType, numchans, len);
1769 break;
1770 case UserFmtByte3:
1771 Convert_ALbyte3(dst, src, srcType, numchans, len);
1772 break;
1773 case UserFmtUByte3:
1774 Convert_ALubyte3(dst, src, srcType, numchans, len);
1775 break;
1781 * LoadData
1783 * Loads the specified data into the buffer, using the specified formats.
1784 * Currently, the new format must have the same channel configuration as the
1785 * original format.
1787 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels SrcChannels, enum UserFmtType SrcType, const ALvoid *data, ALboolean storesrc)
1789 ALuint NewChannels, NewBytes;
1790 enum FmtChannels DstChannels;
1791 enum FmtType DstType;
1792 ALuint64 newsize;
1793 ALvoid *temp;
1795 WriteLock(&ALBuf->lock);
1796 if(ALBuf->ref != 0)
1798 WriteUnlock(&ALBuf->lock);
1799 return AL_INVALID_OPERATION;
1802 if(DecomposeFormat(NewFormat, &DstChannels, &DstType) == AL_FALSE ||
1803 (long)SrcChannels != (long)DstChannels)
1805 WriteUnlock(&ALBuf->lock);
1806 return AL_INVALID_ENUM;
1809 NewChannels = ChannelsFromFmt(DstChannels);
1810 NewBytes = BytesFromFmt(DstType);
1812 if(SrcType == UserFmtIMA4)
1814 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
1816 newsize = frames;
1817 newsize *= 65;
1818 newsize *= NewBytes;
1819 newsize *= NewChannels;
1820 if(newsize > INT_MAX)
1822 WriteUnlock(&ALBuf->lock);
1823 return AL_OUT_OF_MEMORY;
1826 temp = realloc(ALBuf->data, newsize);
1827 if(!temp && newsize)
1829 WriteUnlock(&ALBuf->lock);
1830 return AL_OUT_OF_MEMORY;
1832 ALBuf->data = temp;
1833 ALBuf->size = newsize;
1835 if(data != NULL)
1836 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
1838 if(storesrc)
1840 ALBuf->OriginalChannels = SrcChannels;
1841 ALBuf->OriginalType = SrcType;
1842 ALBuf->OriginalSize = frames * 36 * OrigChannels;
1843 ALBuf->OriginalAlign = 36 * OrigChannels;
1846 else
1848 ALuint OrigBytes = BytesFromUserFmt(SrcType);
1849 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
1851 newsize = frames;
1852 newsize *= NewBytes;
1853 newsize *= NewChannels;
1854 if(newsize > INT_MAX)
1856 WriteUnlock(&ALBuf->lock);
1857 return AL_OUT_OF_MEMORY;
1860 temp = realloc(ALBuf->data, newsize);
1861 if(!temp && newsize)
1863 WriteUnlock(&ALBuf->lock);
1864 return AL_OUT_OF_MEMORY;
1866 ALBuf->data = temp;
1867 ALBuf->size = newsize;
1869 if(data != NULL)
1870 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
1872 if(storesrc)
1874 ALBuf->OriginalChannels = SrcChannels;
1875 ALBuf->OriginalType = SrcType;
1876 ALBuf->OriginalSize = frames * OrigBytes * OrigChannels;
1877 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
1881 if(!storesrc)
1883 ALBuf->OriginalChannels = DstChannels;
1884 ALBuf->OriginalType = DstType;
1885 ALBuf->OriginalSize = frames * NewBytes * NewChannels;
1886 ALBuf->OriginalAlign = NewBytes * NewChannels;
1888 ALBuf->Frequency = freq;
1889 ALBuf->FmtChannels = DstChannels;
1890 ALBuf->FmtType = DstType;
1892 ALBuf->LoopStart = 0;
1893 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
1895 WriteUnlock(&ALBuf->lock);
1896 return AL_NO_ERROR;
1900 ALuint BytesFromUserFmt(enum UserFmtType type)
1902 switch(type)
1904 case UserFmtByte: return sizeof(ALbyte);
1905 case UserFmtUByte: return sizeof(ALubyte);
1906 case UserFmtShort: return sizeof(ALshort);
1907 case UserFmtUShort: return sizeof(ALushort);
1908 case UserFmtInt: return sizeof(ALint);
1909 case UserFmtUInt: return sizeof(ALuint);
1910 case UserFmtFloat: return sizeof(ALfloat);
1911 case UserFmtDouble: return sizeof(ALdouble);
1912 case UserFmtByte3: return sizeof(ALbyte3);
1913 case UserFmtUByte3: return sizeof(ALubyte3);
1914 case UserFmtMulaw: return sizeof(ALubyte);
1915 case UserFmtIMA4: break; /* not handled here */
1917 return 0;
1919 ALuint ChannelsFromUserFmt(enum UserFmtChannels chans)
1921 switch(chans)
1923 case UserFmtMono: return 1;
1924 case UserFmtStereo: return 2;
1925 case UserFmtRear: return 2;
1926 case UserFmtQuad: return 4;
1927 case UserFmtX51: return 6;
1928 case UserFmtX61: return 7;
1929 case UserFmtX71: return 8;
1931 return 0;
1933 static ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
1934 enum UserFmtType *type)
1936 static const struct {
1937 ALenum format;
1938 enum UserFmtChannels channels;
1939 enum UserFmtType type;
1940 } list[] = {
1941 { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
1942 { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
1943 { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
1944 { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
1945 { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
1946 { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
1948 { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
1949 { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
1950 { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
1951 { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
1952 { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
1953 { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
1955 { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
1956 { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
1957 { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
1958 { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
1960 { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
1961 { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
1963 { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
1964 { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
1965 { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
1966 { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
1968 { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
1969 { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
1970 { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
1971 { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
1973 { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
1974 { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
1975 { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
1976 { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
1978 { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
1979 { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
1980 { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
1981 { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
1983 ALuint i;
1985 for(i = 0;i < sizeof(list)/sizeof(list[0]);i++)
1987 if(list[i].format == format)
1989 *chans = list[i].channels;
1990 *type = list[i].type;
1991 return AL_TRUE;
1995 return AL_FALSE;
1998 ALuint BytesFromFmt(enum FmtType type)
2000 switch(type)
2002 case FmtByte: return sizeof(ALbyte);
2003 case FmtShort: return sizeof(ALshort);
2004 case FmtFloat: return sizeof(ALfloat);
2006 return 0;
2008 ALuint ChannelsFromFmt(enum FmtChannels chans)
2010 switch(chans)
2012 case FmtMono: return 1;
2013 case FmtStereo: return 2;
2014 case FmtRear: return 2;
2015 case FmtQuad: return 4;
2016 case FmtX51: return 6;
2017 case FmtX61: return 7;
2018 case FmtX71: return 8;
2020 return 0;
2022 static ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
2024 static const struct {
2025 ALenum format;
2026 enum FmtChannels channels;
2027 enum FmtType type;
2028 } list[] = {
2029 { AL_MONO8, FmtMono, FmtByte },
2030 { AL_MONO16, FmtMono, FmtShort },
2031 { AL_MONO32F, FmtMono, FmtFloat },
2033 { AL_STEREO8, FmtStereo, FmtByte },
2034 { AL_STEREO16, FmtStereo, FmtShort },
2035 { AL_STEREO32F, FmtStereo, FmtFloat },
2037 { AL_REAR8, FmtRear, FmtByte },
2038 { AL_REAR16, FmtRear, FmtShort },
2039 { AL_REAR32F, FmtRear, FmtFloat },
2041 { AL_FORMAT_QUAD8_LOKI, FmtQuad, FmtByte },
2042 { AL_FORMAT_QUAD16_LOKI, FmtQuad, FmtShort },
2044 { AL_QUAD8, FmtQuad, FmtByte },
2045 { AL_QUAD16, FmtQuad, FmtShort },
2046 { AL_QUAD32F, FmtQuad, FmtFloat },
2048 { AL_5POINT1_8, FmtX51, FmtByte },
2049 { AL_5POINT1_16, FmtX51, FmtShort },
2050 { AL_5POINT1_32F, FmtX51, FmtFloat },
2052 { AL_6POINT1_8, FmtX61, FmtByte },
2053 { AL_6POINT1_16, FmtX61, FmtShort },
2054 { AL_6POINT1_32F, FmtX61, FmtFloat },
2056 { AL_7POINT1_8, FmtX71, FmtByte },
2057 { AL_7POINT1_16, FmtX71, FmtShort },
2058 { AL_7POINT1_32F, FmtX71, FmtFloat },
2060 ALuint i;
2062 for(i = 0;i < sizeof(list)/sizeof(list[0]);i++)
2064 if(list[i].format == format)
2066 *chans = list[i].channels;
2067 *type = list[i].type;
2068 return AL_TRUE;
2072 return AL_FALSE;
2076 static ALboolean IsValidType(ALenum type)
2078 switch(type)
2080 case AL_BYTE:
2081 case AL_UNSIGNED_BYTE:
2082 case AL_SHORT:
2083 case AL_UNSIGNED_SHORT:
2084 case AL_INT:
2085 case AL_UNSIGNED_INT:
2086 case AL_FLOAT:
2087 case AL_DOUBLE:
2088 case AL_MULAW:
2089 case AL_IMA4:
2090 case AL_BYTE3:
2091 case AL_UNSIGNED_BYTE3:
2092 return AL_TRUE;
2094 return AL_FALSE;
2097 static ALboolean IsValidChannels(ALenum channels)
2099 switch(channels)
2101 case AL_MONO:
2102 case AL_STEREO:
2103 case AL_REAR:
2104 case AL_QUAD:
2105 case AL_5POINT1:
2106 case AL_6POINT1:
2107 case AL_7POINT1:
2108 return AL_TRUE;
2110 return AL_FALSE;
2115 * ReleaseALBuffers()
2117 * INTERNAL: Called to destroy any buffers that still exist on the device
2119 ALvoid ReleaseALBuffers(ALCdevice *device)
2121 ALsizei i;
2122 for(i = 0;i < device->BufferMap.size;i++)
2124 ALbuffer *temp = device->BufferMap.array[i].value;
2125 device->BufferMap.array[i].value = NULL;
2127 free(temp->data);
2129 FreeThunkEntry(temp->buffer);
2130 memset(temp, 0, sizeof(ALbuffer));
2131 free(temp);