Don't return unsupported effects from alGetEnumValue
[openal-soft.git] / OpenAL32 / alBuffer.c
blobb7650ec26c5e89a6ddf3dff0910e0e918856e489
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 "alMain.h"
27 #include "AL/al.h"
28 #include "AL/alc.h"
29 #include "alError.h"
30 #include "alBuffer.h"
31 #include "alDatabuffer.h"
32 #include "alThunk.h"
35 static ALenum LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat);
36 static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len);
37 static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len);
38 static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len);
39 static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len);
40 static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len);
42 DECL_VERIFIER(Buffer, ALbuffer, buffer)
45 * Global Variables
48 static const long g_IMAStep_size[89]={ // IMA ADPCM Stepsize table
49 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
50 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
51 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
52 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
53 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
54 15289,16818,18500,20350,22358,24633,27086,29794,32767
57 static const long g_IMACodeword_4[16]={ // IMA4 ADPCM Codeword decode table
58 1, 3, 5, 7, 9, 11, 13, 15,
59 -1,-3,-5,-7,-9,-11,-13,-15,
62 static const long g_IMAIndex_adjust_4[16]={ // IMA4 ADPCM Step index adjust decode table
63 -1,-1,-1,-1, 2, 4, 6, 8,
64 -1,-1,-1,-1, 2, 4, 6, 8
67 static const ALshort muLawDecompressionTable[256] = {
68 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
69 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
70 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
71 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
72 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
73 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
74 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
75 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
76 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
77 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
78 -876, -844, -812, -780, -748, -716, -684, -652,
79 -620, -588, -556, -524, -492, -460, -428, -396,
80 -372, -356, -340, -324, -308, -292, -276, -260,
81 -244, -228, -212, -196, -180, -164, -148, -132,
82 -120, -112, -104, -96, -88, -80, -72, -64,
83 -56, -48, -40, -32, -24, -16, -8, 0,
84 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
85 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
86 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
87 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
88 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
89 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
90 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
91 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
92 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
93 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
94 876, 844, 812, 780, 748, 716, 684, 652,
95 620, 588, 556, 524, 492, 460, 428, 396,
96 372, 356, 340, 324, 308, 292, 276, 260,
97 244, 228, 212, 196, 180, 164, 148, 132,
98 120, 112, 104, 96, 88, 80, 72, 64,
99 56, 48, 40, 32, 24, 16, 8, 0
103 * alGenBuffers(ALsizei n, ALuint *puiBuffers)
105 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by puiBuffers
107 ALAPI ALvoid ALAPIENTRY alGenBuffers(ALsizei n,ALuint *puiBuffers)
109 ALCcontext *Context;
110 ALsizei i=0;
112 Context = GetContextSuspended();
113 if(!Context) return;
115 // Check that we are actually generation some Buffers
116 if (n > 0)
118 ALCdevice *device = Context->Device;
120 // Check the pointer is valid (and points to enough memory to store Buffer Names)
121 if (!IsBadWritePtr((void*)puiBuffers, n * sizeof(ALuint)))
123 ALbuffer **list = &device->BufferList;
124 while(*list)
125 list = &(*list)->next;
127 // Create all the new Buffers
128 while(i < n)
130 *list = calloc(1, sizeof(ALbuffer));
131 if(!(*list))
133 alDeleteBuffers(i, puiBuffers);
134 alSetError(Context, AL_OUT_OF_MEMORY);
135 break;
138 puiBuffers[i] = (ALuint)ALTHUNK_ADDENTRY(*list);
139 (*list)->buffer = puiBuffers[i];
141 device->BufferCount++;
142 i++;
144 list = &(*list)->next;
147 else
149 // Pointer does not point to enough memory to write Buffer names
150 alSetError(Context, AL_INVALID_VALUE);
154 ProcessContext(Context);
158 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
160 * Deletes the n AL Buffers pointed to by puiBuffers
162 ALAPI ALvoid ALAPIENTRY alDeleteBuffers(ALsizei n, const ALuint *puiBuffers)
164 ALCcontext *Context;
165 ALbuffer *ALBuf;
166 ALsizei i;
167 ALboolean bFailed = AL_FALSE;
169 Context = GetContextSuspended();
170 if(!Context) return;
172 // Check we are actually Deleting some Buffers
173 if (n >= 0)
175 ALCdevice *device = Context->Device;
177 // Check that all the buffers are valid and can actually be deleted
178 for (i = 0; i < n; i++)
180 if(!puiBuffers[i])
181 continue;
183 // Check for valid Buffer ID (can be NULL buffer)
184 if((ALBuf=VerifyBuffer(device->BufferList, puiBuffers[i])) != NULL)
186 if(ALBuf->refcount != 0)
188 // Buffer still in use, cannot be deleted
189 alSetError(Context, AL_INVALID_OPERATION);
190 bFailed = AL_TRUE;
191 break;
194 else
196 // Invalid Buffer
197 alSetError(Context, AL_INVALID_NAME);
198 bFailed = AL_TRUE;
199 break;
203 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
204 if (!bFailed)
206 for (i = 0; i < n; i++)
208 if((ALBuf=VerifyBuffer(device->BufferList, puiBuffers[i])) != NULL)
210 ALbuffer **list = &device->BufferList;
212 while(*list && *list != ALBuf)
213 list = &(*list)->next;
215 if(*list)
216 *list = (*list)->next;
218 // Release the memory used to store audio data
219 free(ALBuf->data);
221 // Release buffer structure
222 ALTHUNK_REMOVEENTRY(puiBuffers[i]);
223 memset(ALBuf, 0, sizeof(ALbuffer));
224 device->BufferCount--;
225 free(ALBuf);
230 else
231 alSetError(Context, AL_INVALID_VALUE);
233 ProcessContext(Context);
237 * alIsBuffer(ALuint uiBuffer)
239 * Checks if ulBuffer is a valid Buffer Name
241 ALAPI ALboolean ALAPIENTRY alIsBuffer(ALuint uiBuffer)
243 ALCcontext *Context;
244 ALboolean result = AL_TRUE;
246 Context = GetContextSuspended();
247 if(!Context) return AL_FALSE;
249 if(uiBuffer)
250 result = (VerifyBuffer(Context->Device->BufferList, uiBuffer) ?
251 AL_TRUE : AL_FALSE);
253 ProcessContext(Context);
255 return result;
259 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
261 * Fill buffer with audio data
263 ALAPI ALvoid ALAPIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
265 ALCcontext *Context;
266 ALCdevice *device;
267 ALbuffer *ALBuf;
268 ALvoid *temp;
269 ALenum err;
271 Context = GetContextSuspended();
272 if(!Context) return;
274 device = Context->Device;
275 if((ALBuf=VerifyBuffer(device->BufferList, buffer)) != NULL)
277 if(Context->SampleSource)
279 ALuint offset;
281 if(Context->SampleSource->state == MAPPED)
283 alSetError(Context, AL_INVALID_OPERATION);
284 ProcessContext(Context);
285 return;
288 offset = (ALuint)data;
289 data = Context->SampleSource->data + offset;
292 if ((ALBuf->refcount==0)&&(data))
294 switch(format)
296 case AL_FORMAT_MONO8:
297 case AL_FORMAT_MONO16:
298 case AL_FORMAT_MONO_FLOAT32:
299 case AL_FORMAT_MONO_DOUBLE_EXT:
300 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO_FLOAT32);
301 if(err != AL_NO_ERROR)
302 alSetError(Context, err);
303 break;
305 case AL_FORMAT_STEREO8:
306 case AL_FORMAT_STEREO16:
307 case AL_FORMAT_STEREO_FLOAT32:
308 case AL_FORMAT_STEREO_DOUBLE_EXT:
309 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO_FLOAT32);
310 if(err != AL_NO_ERROR)
311 alSetError(Context, err);
312 break;
314 case AL_FORMAT_REAR8:
315 case AL_FORMAT_REAR16:
316 case AL_FORMAT_REAR32: {
317 ALuint NewFormat = AL_FORMAT_QUAD32;
318 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
319 ALuint NewBytes = aluBytesFromFormat(NewFormat);
320 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
321 ((format==AL_FORMAT_REAR16) ? 2 :
322 4));
324 if((size%(OrigBytes*2)) != 0)
326 alSetError(Context, AL_INVALID_VALUE);
327 break;
330 size /= OrigBytes;
331 size *= 2;
333 // Samples are converted here
334 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size) * NewBytes);
335 if(temp)
337 ALBuf->data = temp;
338 ConvertDataRear(ALBuf->data, data, OrigBytes, size);
340 ALBuf->format = NewFormat;
341 ALBuf->eOriginalFormat = format;
342 ALBuf->size = size*NewBytes;
343 ALBuf->frequency = freq;
345 else
346 alSetError(Context, AL_OUT_OF_MEMORY);
347 } break;
349 case AL_FORMAT_QUAD8_LOKI:
350 case AL_FORMAT_QUAD16_LOKI:
351 case AL_FORMAT_QUAD8:
352 case AL_FORMAT_QUAD16:
353 case AL_FORMAT_QUAD32:
354 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_QUAD32);
355 if(err != AL_NO_ERROR)
356 alSetError(Context, err);
357 break;
359 case AL_FORMAT_51CHN8:
360 case AL_FORMAT_51CHN16:
361 case AL_FORMAT_51CHN32:
362 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_51CHN32);
363 if(err != AL_NO_ERROR)
364 alSetError(Context, err);
365 break;
367 case AL_FORMAT_61CHN8:
368 case AL_FORMAT_61CHN16:
369 case AL_FORMAT_61CHN32:
370 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_61CHN32);
371 if(err != AL_NO_ERROR)
372 alSetError(Context, err);
373 break;
375 case AL_FORMAT_71CHN8:
376 case AL_FORMAT_71CHN16:
377 case AL_FORMAT_71CHN32:
378 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_71CHN32);
379 if(err != AL_NO_ERROR)
380 alSetError(Context, err);
381 break;
383 case AL_FORMAT_MONO_IMA4:
384 case AL_FORMAT_STEREO_IMA4: {
385 int OrigChans = ((format==AL_FORMAT_MONO_IMA4) ? 1 : 2);
386 ALuint NewFormat = ((OrigChans==1) ? AL_FORMAT_MONO_FLOAT32 :
387 AL_FORMAT_STEREO_FLOAT32);
388 ALuint NewBytes = aluBytesFromFormat(NewFormat);
390 // Here is where things vary:
391 // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
392 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
393 if((size%(36*OrigChans)) != 0)
395 alSetError(Context, AL_INVALID_VALUE);
396 break;
399 size /= 36;
400 size *= 65;
402 // Allocate extra padding samples
403 temp = realloc(ALBuf->data, (BUFFER_PADDING*OrigChans + size)*NewBytes);
404 if(temp)
406 ALBuf->data = temp;
407 ConvertDataIMA4(ALBuf->data, data, OrigChans, size/65);
409 ALBuf->format = NewFormat;
410 ALBuf->eOriginalFormat = format;
411 ALBuf->size = size*NewBytes;
412 ALBuf->frequency = freq;
414 else
415 alSetError(Context, AL_OUT_OF_MEMORY);
416 } break;
418 case AL_FORMAT_MONO_MULAW:
419 case AL_FORMAT_STEREO_MULAW:
420 case AL_FORMAT_QUAD_MULAW:
421 case AL_FORMAT_51CHN_MULAW:
422 case AL_FORMAT_61CHN_MULAW:
423 case AL_FORMAT_71CHN_MULAW: {
424 int Channels = ((format==AL_FORMAT_MONO_MULAW) ? 1 :
425 ((format==AL_FORMAT_STEREO_MULAW) ? 2 :
426 ((format==AL_FORMAT_QUAD_MULAW) ? 4 :
427 ((format==AL_FORMAT_51CHN_MULAW) ? 6 :
428 ((format==AL_FORMAT_61CHN_MULAW) ? 7 : 8)))));
429 ALuint NewFormat = ((Channels==1) ? AL_FORMAT_MONO_FLOAT32 :
430 ((Channels==2) ? AL_FORMAT_STEREO_FLOAT32 :
431 ((Channels==4) ? AL_FORMAT_QUAD32 :
432 ((Channels==6) ? AL_FORMAT_51CHN32 :
433 ((Channels==7) ? AL_FORMAT_61CHN32 :
434 AL_FORMAT_71CHN32)))));
435 ALuint NewBytes = aluBytesFromFormat(NewFormat);
437 if((size%(1*Channels)) != 0)
439 alSetError(Context, AL_INVALID_VALUE);
440 break;
443 // Allocate extra padding samples
444 temp = realloc(ALBuf->data, (BUFFER_PADDING*Channels + size)*NewBytes);
445 if(temp)
447 ALBuf->data = temp;
448 ConvertDataMULaw(ALBuf->data, data, size);
450 ALBuf->format = NewFormat;
451 ALBuf->eOriginalFormat = format;
452 ALBuf->size = size*NewBytes;
453 ALBuf->frequency = freq;
455 else
456 alSetError(Context, AL_OUT_OF_MEMORY);
457 } break;
459 case AL_FORMAT_REAR_MULAW: {
460 int OrigChans = 2;
461 ALuint NewFormat = AL_FORMAT_QUAD32;
462 ALuint NewBytes = aluBytesFromFormat(NewFormat);
463 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
465 if((size%(1*OrigChans)) != 0)
467 alSetError(Context, AL_INVALID_VALUE);
468 break;
471 size *= 2;
473 // Allocate extra padding samples
474 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size)*NewBytes);
475 if(temp)
477 ALBuf->data = temp;
478 ConvertDataMULawRear(ALBuf->data, data, size);
480 ALBuf->format = NewFormat;
481 ALBuf->eOriginalFormat = format;
482 ALBuf->size = size*NewBytes;
483 ALBuf->frequency = freq;
485 else
486 alSetError(Context, AL_OUT_OF_MEMORY);
487 } break;
489 default:
490 alSetError(Context, AL_INVALID_ENUM);
491 break;
494 else
496 // Buffer is in use, or data is a NULL pointer
497 alSetError(Context, AL_INVALID_VALUE);
500 else
502 // Invalid Buffer Name
503 alSetError(Context, AL_INVALID_NAME);
506 ProcessContext(Context);
510 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
512 * Fill buffer with audio data
514 ALvoid ALAPIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
516 ALCcontext *Context;
517 ALCdevice *device;
518 ALbuffer *ALBuf;
520 Context = GetContextSuspended();
521 if(!Context) return;
523 device = Context->Device;
524 if((ALBuf=VerifyBuffer(device->BufferList, buffer)) != NULL)
526 if(Context->SampleSource)
528 ALuint offset;
530 if(Context->SampleSource->state == MAPPED)
532 alSetError(Context, AL_INVALID_OPERATION);
533 ProcessContext(Context);
534 return;
537 offset = (ALuint)data;
538 data = Context->SampleSource->data + offset;
541 if(ALBuf->data == NULL)
543 // buffer does not have any data
544 alSetError(Context, AL_INVALID_NAME);
546 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
548 // data is NULL or offset/length is negative
549 alSetError(Context, AL_INVALID_VALUE);
551 else
553 switch(format)
555 case AL_FORMAT_REAR8:
556 case AL_FORMAT_REAR16:
557 case AL_FORMAT_REAR32: {
558 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
559 ((format==AL_FORMAT_REAR16) ? 2 :
560 4));
561 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
563 if(ALBuf->eOriginalFormat != AL_FORMAT_REAR8 &&
564 ALBuf->eOriginalFormat != AL_FORMAT_REAR16 &&
565 ALBuf->eOriginalFormat != AL_FORMAT_REAR32)
567 alSetError(Context, AL_INVALID_ENUM);
568 break;
571 if(ALBuf->size/4/NewBytes < (ALuint)offset+length)
573 alSetError(Context, AL_INVALID_VALUE);
574 break;
577 ConvertDataRear(&ALBuf->data[offset*4], data, OrigBytes, length*2);
578 } break;
580 case AL_FORMAT_MONO_IMA4:
581 case AL_FORMAT_STEREO_IMA4: {
582 int Channels = aluChannelsFromFormat(ALBuf->format);
583 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
585 if(ALBuf->eOriginalFormat != format)
587 alSetError(Context, AL_INVALID_ENUM);
588 break;
591 if((offset%65) != 0 || (length%65) != 0 ||
592 ALBuf->size/Channels/Bytes < (ALuint)offset+length)
594 alSetError(Context, AL_INVALID_VALUE);
595 break;
598 ConvertDataIMA4(&ALBuf->data[offset*Channels], data, Channels, length/65*Channels);
599 } break;
601 case AL_FORMAT_MONO_MULAW:
602 case AL_FORMAT_STEREO_MULAW:
603 case AL_FORMAT_QUAD_MULAW:
604 case AL_FORMAT_REAR_MULAW:
605 case AL_FORMAT_51CHN_MULAW:
606 case AL_FORMAT_61CHN_MULAW:
607 case AL_FORMAT_71CHN_MULAW: {
608 int Channels = aluChannelsFromFormat(ALBuf->format);
609 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
611 if(ALBuf->eOriginalFormat != format)
613 alSetError(Context, AL_INVALID_ENUM);
614 break;
617 if(ALBuf->size/Channels/Bytes < (ALuint)offset+length)
619 alSetError(Context, AL_INVALID_VALUE);
620 break;
623 if(ALBuf->eOriginalFormat == AL_FORMAT_REAR_MULAW)
624 ConvertDataMULawRear(&ALBuf->data[offset*Channels], data, length*2);
625 else
626 ConvertDataMULaw(&ALBuf->data[offset*Channels], data, length*Channels);
627 } break;
629 default: {
630 ALuint Channels = aluChannelsFromFormat(format);
631 ALuint Bytes = aluBytesFromFormat(format);
632 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
634 if(Channels != aluChannelsFromFormat(ALBuf->format))
636 alSetError(Context, AL_INVALID_ENUM);
637 break;
640 if(ALBuf->size/Channels/NewBytes < (ALuint)offset+length)
642 alSetError(Context, AL_INVALID_VALUE);
643 break;
646 ConvertData(&ALBuf->data[offset*Channels], data, Bytes, length*Channels);
647 } break;
651 else
653 // Invalid Buffer Name
654 alSetError(Context, AL_INVALID_NAME);
657 ProcessContext(Context);
661 ALAPI void ALAPIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
663 ALCcontext *pContext;
664 ALCdevice *device;
666 (void)flValue;
668 pContext = GetContextSuspended();
669 if(!pContext) return;
671 device = pContext->Device;
672 if(VerifyBuffer(device->BufferList, buffer) != NULL)
674 switch(eParam)
676 default:
677 alSetError(pContext, AL_INVALID_ENUM);
678 break;
681 else
683 alSetError(pContext, AL_INVALID_NAME);
686 ProcessContext(pContext);
690 ALAPI void ALAPIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
692 ALCcontext *pContext;
693 ALCdevice *device;
695 (void)flValue1;
696 (void)flValue2;
697 (void)flValue3;
699 pContext = GetContextSuspended();
700 if(!pContext) return;
702 device = pContext->Device;
703 if(VerifyBuffer(device->BufferList, buffer) != NULL)
705 switch(eParam)
707 default:
708 alSetError(pContext, AL_INVALID_ENUM);
709 break;
712 else
714 alSetError(pContext, AL_INVALID_NAME);
717 ProcessContext(pContext);
721 ALAPI void ALAPIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
723 ALCcontext *pContext;
724 ALCdevice *device;
726 (void)flValues;
728 pContext = GetContextSuspended();
729 if(!pContext) return;
731 device = pContext->Device;
732 if(VerifyBuffer(device->BufferList, buffer) != NULL)
734 switch(eParam)
736 default:
737 alSetError(pContext, AL_INVALID_ENUM);
738 break;
741 else
743 alSetError(pContext, AL_INVALID_NAME);
746 ProcessContext(pContext);
750 ALAPI void ALAPIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
752 ALCcontext *pContext;
753 ALCdevice *device;
755 (void)lValue;
757 pContext = GetContextSuspended();
758 if(!pContext) return;
760 device = pContext->Device;
761 if(VerifyBuffer(device->BufferList, buffer) != NULL)
763 switch(eParam)
765 default:
766 alSetError(pContext, AL_INVALID_ENUM);
767 break;
770 else
772 alSetError(pContext, AL_INVALID_NAME);
775 ProcessContext(pContext);
779 ALAPI void ALAPIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
781 ALCcontext *pContext;
782 ALCdevice *device;
784 (void)lValue1;
785 (void)lValue2;
786 (void)lValue3;
788 pContext = GetContextSuspended();
789 if(!pContext) return;
791 device = pContext->Device;
792 if(VerifyBuffer(device->BufferList, buffer) != NULL)
794 switch(eParam)
796 default:
797 alSetError(pContext, AL_INVALID_ENUM);
798 break;
801 else
803 alSetError(pContext, AL_INVALID_NAME);
806 ProcessContext(pContext);
810 ALAPI void ALAPIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
812 ALCcontext *pContext;
813 ALCdevice *device;
815 (void)plValues;
817 pContext = GetContextSuspended();
818 if(!pContext) return;
820 device = pContext->Device;
821 if(VerifyBuffer(device->BufferList, buffer) != NULL)
823 switch(eParam)
825 default:
826 alSetError(pContext, AL_INVALID_ENUM);
827 break;
830 else
832 alSetError(pContext, AL_INVALID_NAME);
835 ProcessContext(pContext);
839 ALAPI ALvoid ALAPIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
841 ALCcontext *pContext;
842 ALCdevice *device;
844 pContext = GetContextSuspended();
845 if(!pContext) return;
847 if (pflValue)
849 device = pContext->Device;
850 if(VerifyBuffer(device->BufferList, buffer) != NULL)
852 switch(eParam)
854 default:
855 alSetError(pContext, AL_INVALID_ENUM);
856 break;
859 else
861 alSetError(pContext, AL_INVALID_NAME);
864 else
866 alSetError(pContext, AL_INVALID_VALUE);
869 ProcessContext(pContext);
873 ALAPI void ALAPIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
875 ALCcontext *pContext;
876 ALCdevice *device;
878 pContext = GetContextSuspended();
879 if(!pContext) return;
881 if ((pflValue1) && (pflValue2) && (pflValue3))
883 device = pContext->Device;
884 if(VerifyBuffer(device->BufferList, buffer) != NULL)
886 switch(eParam)
888 default:
889 alSetError(pContext, AL_INVALID_ENUM);
890 break;
893 else
895 alSetError(pContext, AL_INVALID_NAME);
898 else
900 alSetError(pContext, AL_INVALID_VALUE);
903 ProcessContext(pContext);
907 ALAPI void ALAPIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
909 ALCcontext *pContext;
910 ALCdevice *device;
912 pContext = GetContextSuspended();
913 if(!pContext) return;
915 if (pflValues)
917 device = pContext->Device;
918 if(VerifyBuffer(device->BufferList, buffer) != NULL)
920 switch(eParam)
922 default:
923 alSetError(pContext, AL_INVALID_ENUM);
924 break;
927 else
929 alSetError(pContext, AL_INVALID_NAME);
932 else
934 alSetError(pContext, AL_INVALID_VALUE);
937 ProcessContext(pContext);
941 ALAPI ALvoid ALAPIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
943 ALCcontext *pContext;
944 ALbuffer *pBuffer;
945 ALCdevice *device;
947 pContext = GetContextSuspended();
948 if(!pContext) return;
950 if (plValue)
952 device = pContext->Device;
953 if((pBuffer=VerifyBuffer(device->BufferList, buffer)) != NULL)
955 switch (eParam)
957 case AL_FREQUENCY:
958 *plValue = pBuffer->frequency;
959 break;
961 case AL_BITS:
962 *plValue = aluBytesFromFormat(pBuffer->format) * 8;
963 break;
965 case AL_CHANNELS:
966 *plValue = aluChannelsFromFormat(pBuffer->format);
967 break;
969 case AL_SIZE:
970 *plValue = pBuffer->size;
971 break;
973 default:
974 alSetError(pContext, AL_INVALID_ENUM);
975 break;
978 else
980 alSetError(pContext, AL_INVALID_NAME);
983 else
985 alSetError(pContext, AL_INVALID_VALUE);
988 ProcessContext(pContext);
992 ALAPI void ALAPIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
994 ALCcontext *pContext;
995 ALCdevice *device;
997 pContext = GetContextSuspended();
998 if(!pContext) return;
1000 if ((plValue1) && (plValue2) && (plValue3))
1002 device = pContext->Device;
1003 if(VerifyBuffer(device->BufferList, buffer) != NULL)
1005 switch(eParam)
1007 default:
1008 alSetError(pContext, AL_INVALID_ENUM);
1009 break;
1012 else
1014 alSetError(pContext, AL_INVALID_NAME);
1017 else
1019 alSetError(pContext, AL_INVALID_VALUE);
1022 ProcessContext(pContext);
1026 ALAPI void ALAPIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
1028 ALCcontext *pContext;
1029 ALCdevice *device;
1031 pContext = GetContextSuspended();
1032 if(!pContext) return;
1034 if (plValues)
1036 device = pContext->Device;
1037 if(VerifyBuffer(device->BufferList, buffer) != NULL)
1039 switch (eParam)
1041 case AL_FREQUENCY:
1042 case AL_BITS:
1043 case AL_CHANNELS:
1044 case AL_SIZE:
1045 alGetBufferi(buffer, eParam, plValues);
1046 break;
1048 default:
1049 alSetError(pContext, AL_INVALID_ENUM);
1050 break;
1053 else
1055 alSetError(pContext, AL_INVALID_NAME);
1058 else
1060 alSetError(pContext, AL_INVALID_VALUE);
1063 ProcessContext(pContext);
1067 * LoadData
1069 * Loads the specified data into the buffer, using the specified formats.
1070 * Currently, the new format must be 32-bit float, and must have the same
1071 * channel configuration as the original format. This does NOT handle
1072 * compressed formats (eg. IMA4).
1074 static ALenum LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
1076 ALuint NewBytes = aluBytesFromFormat(NewFormat);
1077 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
1078 ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
1079 ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
1080 ALvoid *temp;
1082 assert(NewBytes == 4);
1083 assert(NewChannels == OrigChannels);
1085 if ((size%(OrigBytes*OrigChannels)) != 0)
1086 return AL_INVALID_VALUE;
1088 // Samples are converted here
1089 size /= OrigBytes;
1090 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size) * NewBytes);
1091 if(!temp) return AL_OUT_OF_MEMORY;
1092 ALBuf->data = temp;
1093 ConvertData(ALBuf->data, data, OrigBytes, size);
1095 ALBuf->format = NewFormat;
1096 ALBuf->eOriginalFormat = OrigFormat;
1097 ALBuf->size = size*NewBytes;
1098 ALBuf->frequency = freq;
1100 return AL_NO_ERROR;
1103 static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1105 ALsizei i;
1106 ALint smp;
1107 switch(origBytes)
1109 case 1:
1110 for(i = 0;i < len;i++)
1112 smp = ((ALubyte*)src)[i];
1113 dst[i] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1115 break;
1117 case 2:
1118 for(i = 0;i < len;i++)
1120 smp = ((ALshort*)src)[i];
1121 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1123 break;
1125 case 4:
1126 for(i = 0;i < len;i++)
1127 dst[i] = ((ALfloat*)src)[i];
1128 break;
1130 case 8:
1131 for(i = 0;i < len;i++)
1132 dst[i] = ((ALdouble*)src)[i];
1133 break;
1135 default:
1136 assert(0);
1140 static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1142 ALsizei i;
1143 ALint smp;
1144 switch(origBytes)
1146 case 1:
1147 for(i = 0;i < len;i+=4)
1149 dst[i+0] = 0;
1150 dst[i+1] = 0;
1151 smp = ((ALubyte*)src)[i/2+0];
1152 dst[i+2] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1153 smp = ((ALubyte*)src)[i/2+1];
1154 dst[i+3] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1156 break;
1158 case 2:
1159 for(i = 0;i < len;i+=4)
1161 dst[i+0] = 0;
1162 dst[i+1] = 0;
1163 smp = ((ALshort*)src)[i/2+0];
1164 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1165 smp = ((ALshort*)src)[i/2+1];
1166 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1168 break;
1170 case 4:
1171 for(i = 0;i < len;i+=4)
1173 dst[i+0] = 0;
1174 dst[i+1] = 0;
1175 dst[i+2] = ((ALfloat*)src)[i/2+0];
1176 dst[i+3] = ((ALfloat*)src)[i/2+1];
1178 break;
1180 default:
1181 assert(0);
1185 static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len)
1187 const ALuint *IMAData;
1188 ALint Sample[2],Index[2];
1189 ALuint IMACode[2];
1190 ALsizei i,j,k,c;
1192 assert(origChans <= 2);
1194 IMAData = src;
1195 for(i = 0;i < len/origChans;i++)
1197 for(c = 0;c < origChans;c++)
1199 Sample[c] = ((ALshort*)IMAData)[0];
1200 Index[c] = ((ALshort*)IMAData)[1];
1202 Index[c] = ((Index[c]<0) ? 0 : Index[c]);
1203 Index[c] = ((Index[c]>88) ? 88 : Index[c]);
1205 dst[i*65*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1207 IMAData++;
1210 for(j = 1;j < 65;j += 8)
1212 for(c = 0;c < origChans;c++)
1213 IMACode[c] = *(IMAData++);
1215 for(k = 0;k < 8;k++)
1217 for(c = 0;c < origChans;c++)
1219 Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
1220 Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
1222 if(Sample[c] < -32768) Sample[c] = -32768;
1223 else if(Sample[c] > 32767) Sample[c] = 32767;
1225 if(Index[c]<0) Index[c] = 0;
1226 else if(Index[c]>88) Index[c] = 88;
1228 dst[(i*65+j+k)*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1229 IMACode[c] >>= 4;
1236 static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len)
1238 ALsizei i;
1239 ALint smp;
1240 for(i = 0;i < len;i++)
1242 smp = muLawDecompressionTable[((ALubyte*)src)[i]];
1243 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1247 static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len)
1249 ALsizei i;
1250 ALint smp;
1251 for(i = 0;i < len;i+=4)
1253 dst[i+0] = 0;
1254 dst[i+1] = 0;
1255 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+0]];
1256 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1257 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+1]];
1258 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1263 * ReleaseALBuffers()
1265 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1267 ALvoid ReleaseALBuffers(ALCdevice *device)
1269 ALbuffer *ALBuffer;
1270 ALbuffer *ALBufferTemp;
1272 ALBuffer = device->BufferList;
1273 while(ALBuffer)
1275 // Release sample data
1276 free(ALBuffer->data);
1278 // Release Buffer structure
1279 ALBufferTemp = ALBuffer;
1280 ALBuffer = ALBuffer->next;
1281 memset(ALBufferTemp, 0, sizeof(ALbuffer));
1282 free(ALBufferTemp);
1284 device->BufferList = NULL;
1285 device->BufferCount = 0;