Get rid of more alIs* calls
[openal-soft.git] / OpenAL32 / alBuffer.c
blobb1f21b3e4efebfd153eaa711be779e62afe2b3e5
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 assert(aluBytesFromFormat(NewFormat) == 2);
326 if((size%(OrigBytes*2)) != 0)
328 alSetError(Context, AL_INVALID_VALUE);
329 break;
332 size /= OrigBytes;
333 size *= 2;
335 // Samples are converted here
336 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size) * NewBytes);
337 if(temp)
339 ALBuf->data = temp;
340 ConvertDataRear(ALBuf->data, data, OrigBytes, size);
342 ALBuf->format = NewFormat;
343 ALBuf->eOriginalFormat = format;
344 ALBuf->size = size*NewBytes;
345 ALBuf->frequency = freq;
347 else
348 alSetError(Context, AL_OUT_OF_MEMORY);
349 } break;
351 case AL_FORMAT_QUAD8_LOKI:
352 case AL_FORMAT_QUAD16_LOKI:
353 case AL_FORMAT_QUAD8:
354 case AL_FORMAT_QUAD16:
355 case AL_FORMAT_QUAD32:
356 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_QUAD32);
357 if(err != AL_NO_ERROR)
358 alSetError(Context, err);
359 break;
361 case AL_FORMAT_51CHN8:
362 case AL_FORMAT_51CHN16:
363 case AL_FORMAT_51CHN32:
364 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_51CHN32);
365 if(err != AL_NO_ERROR)
366 alSetError(Context, err);
367 break;
369 case AL_FORMAT_61CHN8:
370 case AL_FORMAT_61CHN16:
371 case AL_FORMAT_61CHN32:
372 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_61CHN32);
373 if(err != AL_NO_ERROR)
374 alSetError(Context, err);
375 break;
377 case AL_FORMAT_71CHN8:
378 case AL_FORMAT_71CHN16:
379 case AL_FORMAT_71CHN32:
380 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_71CHN32);
381 if(err != AL_NO_ERROR)
382 alSetError(Context, err);
383 break;
385 case AL_FORMAT_MONO_IMA4:
386 case AL_FORMAT_STEREO_IMA4: {
387 int OrigChans = ((format==AL_FORMAT_MONO_IMA4) ? 1 : 2);
388 ALuint NewFormat = ((OrigChans==1) ? AL_FORMAT_MONO_FLOAT32 :
389 AL_FORMAT_STEREO_FLOAT32);
390 ALuint NewBytes = aluBytesFromFormat(NewFormat);
392 // Here is where things vary:
393 // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
394 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
395 if((size%(36*OrigChans)) != 0)
397 alSetError(Context, AL_INVALID_VALUE);
398 break;
401 size /= 36;
402 size *= 65;
404 // Allocate extra padding samples
405 temp = realloc(ALBuf->data, (BUFFER_PADDING*OrigChans + size)*NewBytes);
406 if(temp)
408 ALBuf->data = temp;
409 ConvertDataIMA4(ALBuf->data, data, OrigChans, size/65);
411 ALBuf->format = NewFormat;
412 ALBuf->eOriginalFormat = format;
413 ALBuf->size = size*NewBytes;
414 ALBuf->frequency = freq;
416 else
417 alSetError(Context, AL_OUT_OF_MEMORY);
418 } break;
420 case AL_FORMAT_MONO_MULAW:
421 case AL_FORMAT_STEREO_MULAW:
422 case AL_FORMAT_QUAD_MULAW:
423 case AL_FORMAT_51CHN_MULAW:
424 case AL_FORMAT_61CHN_MULAW:
425 case AL_FORMAT_71CHN_MULAW: {
426 int Channels = ((format==AL_FORMAT_MONO_MULAW) ? 1 :
427 ((format==AL_FORMAT_STEREO_MULAW) ? 2 :
428 ((format==AL_FORMAT_QUAD_MULAW) ? 4 :
429 ((format==AL_FORMAT_51CHN_MULAW) ? 6 :
430 ((format==AL_FORMAT_61CHN_MULAW) ? 7 : 8)))));
431 ALuint NewFormat = ((Channels==1) ? AL_FORMAT_MONO_FLOAT32 :
432 ((Channels==2) ? AL_FORMAT_STEREO_FLOAT32 :
433 ((Channels==4) ? AL_FORMAT_QUAD32 :
434 ((Channels==6) ? AL_FORMAT_51CHN32 :
435 ((Channels==7) ? AL_FORMAT_61CHN32 :
436 AL_FORMAT_71CHN32)))));
437 ALuint NewBytes = aluBytesFromFormat(NewFormat);
439 if((size%(1*Channels)) != 0)
441 alSetError(Context, AL_INVALID_VALUE);
442 break;
445 // Allocate extra padding samples
446 temp = realloc(ALBuf->data, (BUFFER_PADDING*Channels + size)*NewBytes);
447 if(temp)
449 ALBuf->data = temp;
450 ConvertDataMULaw(ALBuf->data, data, size);
452 ALBuf->format = NewFormat;
453 ALBuf->eOriginalFormat = format;
454 ALBuf->size = size*NewBytes;
455 ALBuf->frequency = freq;
457 else
458 alSetError(Context, AL_OUT_OF_MEMORY);
459 } break;
461 case AL_FORMAT_REAR_MULAW: {
462 int OrigChans = 2;
463 ALuint NewFormat = AL_FORMAT_QUAD32;
464 ALuint NewBytes = aluBytesFromFormat(NewFormat);
465 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
467 if((size%(1*OrigChans)) != 0)
469 alSetError(Context, AL_INVALID_VALUE);
470 break;
473 size *= 2;
475 // Allocate extra padding samples
476 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size)*NewBytes);
477 if(temp)
479 ALBuf->data = temp;
480 ConvertDataMULawRear(ALBuf->data, data, size);
482 ALBuf->format = NewFormat;
483 ALBuf->eOriginalFormat = format;
484 ALBuf->size = size*NewBytes;
485 ALBuf->frequency = freq;
487 else
488 alSetError(Context, AL_OUT_OF_MEMORY);
489 } break;
491 default:
492 alSetError(Context, AL_INVALID_ENUM);
493 break;
496 else
498 // Buffer is in use, or data is a NULL pointer
499 alSetError(Context, AL_INVALID_VALUE);
502 else
504 // Invalid Buffer Name
505 alSetError(Context, AL_INVALID_NAME);
508 ProcessContext(Context);
512 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
514 * Fill buffer with audio data
516 ALvoid ALAPIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
518 ALCcontext *Context;
519 ALCdevice *device;
520 ALbuffer *ALBuf;
522 Context = GetContextSuspended();
523 if(!Context) return;
525 device = Context->Device;
526 if((ALBuf=VerifyBuffer(device->BufferList, buffer)) != NULL)
528 if(Context->SampleSource)
530 ALuint offset;
532 if(Context->SampleSource->state == MAPPED)
534 alSetError(Context, AL_INVALID_OPERATION);
535 ProcessContext(Context);
536 return;
539 offset = (ALuint)data;
540 data = Context->SampleSource->data + offset;
543 if(ALBuf->data == NULL)
545 // buffer does not have any data
546 alSetError(Context, AL_INVALID_NAME);
548 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
550 // data is NULL or offset/length is negative
551 alSetError(Context, AL_INVALID_VALUE);
553 else
555 switch(format)
557 case AL_FORMAT_REAR8:
558 case AL_FORMAT_REAR16:
559 case AL_FORMAT_REAR32: {
560 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
561 ((format==AL_FORMAT_REAR16) ? 2 :
562 4));
563 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
565 if(ALBuf->eOriginalFormat != AL_FORMAT_REAR8 &&
566 ALBuf->eOriginalFormat != AL_FORMAT_REAR16 &&
567 ALBuf->eOriginalFormat != AL_FORMAT_REAR32)
569 alSetError(Context, AL_INVALID_ENUM);
570 break;
573 if(ALBuf->size/4/NewBytes < (ALuint)offset+length)
575 alSetError(Context, AL_INVALID_VALUE);
576 break;
579 ConvertDataRear(&ALBuf->data[offset*4], data, OrigBytes, length*2);
580 } break;
582 case AL_FORMAT_MONO_IMA4:
583 case AL_FORMAT_STEREO_IMA4: {
584 int Channels = aluChannelsFromFormat(ALBuf->format);
585 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
587 if(ALBuf->eOriginalFormat != format)
589 alSetError(Context, AL_INVALID_ENUM);
590 break;
593 if((offset%65) != 0 || (length%65) != 0 ||
594 ALBuf->size/Channels/Bytes < (ALuint)offset+length)
596 alSetError(Context, AL_INVALID_VALUE);
597 break;
600 ConvertDataIMA4(&ALBuf->data[offset*Channels], data, Channels, length/65*Channels);
601 } break;
603 case AL_FORMAT_MONO_MULAW:
604 case AL_FORMAT_STEREO_MULAW:
605 case AL_FORMAT_QUAD_MULAW:
606 case AL_FORMAT_REAR_MULAW:
607 case AL_FORMAT_51CHN_MULAW:
608 case AL_FORMAT_61CHN_MULAW:
609 case AL_FORMAT_71CHN_MULAW: {
610 int Channels = aluChannelsFromFormat(ALBuf->format);
611 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
613 if(ALBuf->eOriginalFormat != format)
615 alSetError(Context, AL_INVALID_ENUM);
616 break;
619 if(ALBuf->size/Channels/Bytes < (ALuint)offset+length)
621 alSetError(Context, AL_INVALID_VALUE);
622 break;
625 if(ALBuf->eOriginalFormat == AL_FORMAT_REAR_MULAW)
626 ConvertDataMULawRear(&ALBuf->data[offset*Channels], data, length*2);
627 else
628 ConvertDataMULaw(&ALBuf->data[offset*Channels], data, length*Channels);
629 } break;
631 default: {
632 ALuint Channels = aluChannelsFromFormat(format);
633 ALuint Bytes = aluBytesFromFormat(format);
634 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
636 if(Channels != aluChannelsFromFormat(ALBuf->format))
638 alSetError(Context, AL_INVALID_ENUM);
639 break;
642 if(ALBuf->size/Channels/NewBytes < (ALuint)offset+length)
644 alSetError(Context, AL_INVALID_VALUE);
645 break;
648 ConvertData(&ALBuf->data[offset*Channels], data, Bytes, length*Channels);
649 } break;
653 else
655 // Invalid Buffer Name
656 alSetError(Context, AL_INVALID_NAME);
659 ProcessContext(Context);
663 ALAPI void ALAPIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
665 ALCcontext *pContext;
666 ALCdevice *device;
668 (void)flValue;
670 pContext = GetContextSuspended();
671 if(!pContext) return;
673 device = pContext->Device;
674 if(VerifyBuffer(device->BufferList, buffer) != NULL)
676 switch(eParam)
678 default:
679 alSetError(pContext, AL_INVALID_ENUM);
680 break;
683 else
685 alSetError(pContext, AL_INVALID_NAME);
688 ProcessContext(pContext);
692 ALAPI void ALAPIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
694 ALCcontext *pContext;
695 ALCdevice *device;
697 (void)flValue1;
698 (void)flValue2;
699 (void)flValue3;
701 pContext = GetContextSuspended();
702 if(!pContext) return;
704 device = pContext->Device;
705 if(VerifyBuffer(device->BufferList, buffer) != NULL)
707 switch(eParam)
709 default:
710 alSetError(pContext, AL_INVALID_ENUM);
711 break;
714 else
716 alSetError(pContext, AL_INVALID_NAME);
719 ProcessContext(pContext);
723 ALAPI void ALAPIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
725 ALCcontext *pContext;
726 ALCdevice *device;
728 (void)flValues;
730 pContext = GetContextSuspended();
731 if(!pContext) return;
733 device = pContext->Device;
734 if(VerifyBuffer(device->BufferList, buffer) != NULL)
736 switch(eParam)
738 default:
739 alSetError(pContext, AL_INVALID_ENUM);
740 break;
743 else
745 alSetError(pContext, AL_INVALID_NAME);
748 ProcessContext(pContext);
752 ALAPI void ALAPIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
754 ALCcontext *pContext;
755 ALCdevice *device;
757 (void)lValue;
759 pContext = GetContextSuspended();
760 if(!pContext) return;
762 device = pContext->Device;
763 if(VerifyBuffer(device->BufferList, buffer) != NULL)
765 switch(eParam)
767 default:
768 alSetError(pContext, AL_INVALID_ENUM);
769 break;
772 else
774 alSetError(pContext, AL_INVALID_NAME);
777 ProcessContext(pContext);
781 ALAPI void ALAPIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
783 ALCcontext *pContext;
784 ALCdevice *device;
786 (void)lValue1;
787 (void)lValue2;
788 (void)lValue3;
790 pContext = GetContextSuspended();
791 if(!pContext) return;
793 device = pContext->Device;
794 if(VerifyBuffer(device->BufferList, buffer) != NULL)
796 switch(eParam)
798 default:
799 alSetError(pContext, AL_INVALID_ENUM);
800 break;
803 else
805 alSetError(pContext, AL_INVALID_NAME);
808 ProcessContext(pContext);
812 ALAPI void ALAPIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
814 ALCcontext *pContext;
815 ALCdevice *device;
817 (void)plValues;
819 pContext = GetContextSuspended();
820 if(!pContext) return;
822 device = pContext->Device;
823 if(VerifyBuffer(device->BufferList, buffer) != NULL)
825 switch(eParam)
827 default:
828 alSetError(pContext, AL_INVALID_ENUM);
829 break;
832 else
834 alSetError(pContext, AL_INVALID_NAME);
837 ProcessContext(pContext);
841 ALAPI ALvoid ALAPIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
843 ALCcontext *pContext;
844 ALCdevice *device;
846 pContext = GetContextSuspended();
847 if(!pContext) return;
849 if (pflValue)
851 device = pContext->Device;
852 if(VerifyBuffer(device->BufferList, buffer) != NULL)
854 switch(eParam)
856 default:
857 alSetError(pContext, AL_INVALID_ENUM);
858 break;
861 else
863 alSetError(pContext, AL_INVALID_NAME);
866 else
868 alSetError(pContext, AL_INVALID_VALUE);
871 ProcessContext(pContext);
875 ALAPI void ALAPIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
877 ALCcontext *pContext;
878 ALCdevice *device;
880 pContext = GetContextSuspended();
881 if(!pContext) return;
883 if ((pflValue1) && (pflValue2) && (pflValue3))
885 device = pContext->Device;
886 if(VerifyBuffer(device->BufferList, buffer) != NULL)
888 switch(eParam)
890 default:
891 alSetError(pContext, AL_INVALID_ENUM);
892 break;
895 else
897 alSetError(pContext, AL_INVALID_NAME);
900 else
902 alSetError(pContext, AL_INVALID_VALUE);
905 ProcessContext(pContext);
909 ALAPI void ALAPIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
911 ALCcontext *pContext;
912 ALCdevice *device;
914 pContext = GetContextSuspended();
915 if(!pContext) return;
917 if (pflValues)
919 device = pContext->Device;
920 if(VerifyBuffer(device->BufferList, buffer) != NULL)
922 switch(eParam)
924 default:
925 alSetError(pContext, AL_INVALID_ENUM);
926 break;
929 else
931 alSetError(pContext, AL_INVALID_NAME);
934 else
936 alSetError(pContext, AL_INVALID_VALUE);
939 ProcessContext(pContext);
943 ALAPI ALvoid ALAPIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
945 ALCcontext *pContext;
946 ALbuffer *pBuffer;
947 ALCdevice *device;
949 pContext = GetContextSuspended();
950 if(!pContext) return;
952 if (plValue)
954 device = pContext->Device;
955 if((pBuffer=VerifyBuffer(device->BufferList, buffer)) != NULL)
957 switch (eParam)
959 case AL_FREQUENCY:
960 *plValue = pBuffer->frequency;
961 break;
963 case AL_BITS:
964 *plValue = aluBytesFromFormat(pBuffer->format) * 8;
965 break;
967 case AL_CHANNELS:
968 *plValue = aluChannelsFromFormat(pBuffer->format);
969 break;
971 case AL_SIZE:
972 *plValue = pBuffer->size;
973 break;
975 default:
976 alSetError(pContext, AL_INVALID_ENUM);
977 break;
980 else
982 alSetError(pContext, AL_INVALID_NAME);
985 else
987 alSetError(pContext, AL_INVALID_VALUE);
990 ProcessContext(pContext);
994 ALAPI void ALAPIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
996 ALCcontext *pContext;
997 ALCdevice *device;
999 pContext = GetContextSuspended();
1000 if(!pContext) return;
1002 if ((plValue1) && (plValue2) && (plValue3))
1004 device = pContext->Device;
1005 if(VerifyBuffer(device->BufferList, buffer) != NULL)
1007 switch(eParam)
1009 default:
1010 alSetError(pContext, AL_INVALID_ENUM);
1011 break;
1014 else
1016 alSetError(pContext, AL_INVALID_NAME);
1019 else
1021 alSetError(pContext, AL_INVALID_VALUE);
1024 ProcessContext(pContext);
1028 ALAPI void ALAPIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
1030 ALCcontext *pContext;
1031 ALCdevice *device;
1033 pContext = GetContextSuspended();
1034 if(!pContext) return;
1036 if (plValues)
1038 device = pContext->Device;
1039 if(VerifyBuffer(device->BufferList, buffer) != NULL)
1041 switch (eParam)
1043 case AL_FREQUENCY:
1044 case AL_BITS:
1045 case AL_CHANNELS:
1046 case AL_SIZE:
1047 alGetBufferi(buffer, eParam, plValues);
1048 break;
1050 default:
1051 alSetError(pContext, AL_INVALID_ENUM);
1052 break;
1055 else
1057 alSetError(pContext, AL_INVALID_NAME);
1060 else
1062 alSetError(pContext, AL_INVALID_VALUE);
1065 ProcessContext(pContext);
1069 * LoadData
1071 * Loads the specified data into the buffer, using the specified formats.
1072 * Currently, the new format must be 32-bit float, and must have the same
1073 * channel configuration as the original format. This does NOT handle
1074 * compressed formats (eg. IMA4).
1076 static ALenum LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
1078 ALuint NewBytes = aluBytesFromFormat(NewFormat);
1079 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
1080 ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
1081 ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
1082 ALvoid *temp;
1084 assert(NewBytes == 4);
1085 assert(NewChannels == OrigChannels);
1087 if ((size%(OrigBytes*OrigChannels)) != 0)
1088 return AL_INVALID_VALUE;
1090 // Samples are converted here
1091 size /= OrigBytes;
1092 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size) * NewBytes);
1093 if(!temp) return AL_OUT_OF_MEMORY;
1094 ALBuf->data = temp;
1095 ConvertData(ALBuf->data, data, OrigBytes, size);
1097 ALBuf->format = NewFormat;
1098 ALBuf->eOriginalFormat = OrigFormat;
1099 ALBuf->size = size*NewBytes;
1100 ALBuf->frequency = freq;
1102 return AL_NO_ERROR;
1105 static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1107 ALsizei i;
1108 ALint smp;
1109 switch(origBytes)
1111 case 1:
1112 for(i = 0;i < len;i++)
1114 smp = ((ALubyte*)src)[i];
1115 dst[i] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1117 break;
1119 case 2:
1120 for(i = 0;i < len;i++)
1122 smp = ((ALshort*)src)[i];
1123 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1125 break;
1127 case 4:
1128 for(i = 0;i < len;i++)
1129 dst[i] = ((ALfloat*)src)[i];
1130 break;
1132 case 8:
1133 for(i = 0;i < len;i++)
1134 dst[i] = ((ALdouble*)src)[i];
1135 break;
1137 default:
1138 assert(0);
1142 static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1144 ALsizei i;
1145 ALint smp;
1146 switch(origBytes)
1148 case 1:
1149 for(i = 0;i < len;i+=4)
1151 dst[i+0] = 0;
1152 dst[i+1] = 0;
1153 smp = ((ALubyte*)src)[i/2+0];
1154 dst[i+2] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1155 smp = ((ALubyte*)src)[i/2+1];
1156 dst[i+3] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1158 break;
1160 case 2:
1161 for(i = 0;i < len;i+=4)
1163 dst[i+0] = 0;
1164 dst[i+1] = 0;
1165 smp = ((ALshort*)src)[i/2+0];
1166 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1167 smp = ((ALshort*)src)[i/2+1];
1168 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1170 break;
1172 case 4:
1173 for(i = 0;i < len;i+=4)
1175 dst[i+0] = 0;
1176 dst[i+1] = 0;
1177 dst[i+2] = ((ALfloat*)src)[i/2+0];
1178 dst[i+3] = ((ALfloat*)src)[i/2+1];
1180 break;
1182 default:
1183 assert(0);
1187 static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len)
1189 const ALuint *IMAData;
1190 ALint Sample[2],Index[2];
1191 ALuint IMACode[2];
1192 ALsizei i,j,k,c;
1194 assert(origChans <= 2);
1196 IMAData = src;
1197 for(i = 0;i < len/origChans;i++)
1199 for(c = 0;c < origChans;c++)
1201 Sample[c] = ((ALshort*)IMAData)[0];
1202 Index[c] = ((ALshort*)IMAData)[1];
1204 Index[c] = ((Index[c]<0) ? 0 : Index[c]);
1205 Index[c] = ((Index[c]>88) ? 88 : Index[c]);
1207 dst[i*65*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1209 IMAData++;
1212 for(j = 1;j < 65;j += 8)
1214 for(c = 0;c < origChans;c++)
1215 IMACode[c] = *(IMAData++);
1217 for(k = 0;k < 8;k++)
1219 for(c = 0;c < origChans;c++)
1221 Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
1222 Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
1224 if(Sample[c] < -32768) Sample[c] = -32768;
1225 else if(Sample[c] > 32767) Sample[c] = 32767;
1227 if(Index[c]<0) Index[c] = 0;
1228 else if(Index[c]>88) Index[c] = 88;
1230 dst[(i*65+j+k)*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1231 IMACode[c] >>= 4;
1238 static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len)
1240 ALsizei i;
1241 ALint smp;
1242 for(i = 0;i < len;i++)
1244 smp = muLawDecompressionTable[((ALubyte*)src)[i]];
1245 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1249 static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len)
1251 ALsizei i;
1252 ALint smp;
1253 for(i = 0;i < len;i+=4)
1255 dst[i+0] = 0;
1256 dst[i+1] = 0;
1257 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+0]];
1258 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1259 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+1]];
1260 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1265 * ReleaseALBuffers()
1267 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1269 ALvoid ReleaseALBuffers(ALCdevice *device)
1271 ALbuffer *ALBuffer;
1272 ALbuffer *ALBufferTemp;
1274 ALBuffer = device->BufferList;
1275 while(ALBuffer)
1277 // Release sample data
1278 free(ALBuffer->data);
1280 // Release Buffer structure
1281 ALBufferTemp = ALBuffer;
1282 ALBuffer = ALBuffer->next;
1283 memset(ALBufferTemp, 0, sizeof(ALbuffer));
1284 free(ALBufferTemp);
1286 device->BufferList = NULL;
1287 device->BufferCount = 0;