Store the buffer's original data size and block alignment
[openal-soft/android/lowlatency.git] / OpenAL32 / alBuffer.c
blob82c0808b590d2d1f8cfa2a7f544163bad16bd5aa
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 #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
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 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
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;
119 ALenum err;
121 // Check the pointer is valid (and points to enough memory to store Buffer Names)
122 if(!IsBadWritePtr((void*)buffers, n * sizeof(ALuint)))
124 // Create all the new Buffers
125 while(i < n)
127 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
128 if(!buffer)
130 alSetError(Context, AL_OUT_OF_MEMORY);
131 alDeleteBuffers(i, buffers);
132 break;
135 buffer->buffer = (ALuint)ALTHUNK_ADDENTRY(buffer);
136 err = InsertUIntMapEntry(&device->BufferMap, buffer->buffer,
137 buffer);
138 if(err != AL_NO_ERROR)
140 ALTHUNK_REMOVEENTRY(buffer->buffer);
141 memset(buffer, 0, sizeof(ALbuffer));
142 free(buffer);
144 alSetError(Context, err);
145 alDeleteBuffers(i, buffers);
146 break;
148 buffers[i++] = buffer->buffer;
151 else
153 // Pointer does not point to enough memory to write Buffer names
154 alSetError(Context, AL_INVALID_VALUE);
158 ProcessContext(Context);
162 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
164 * Deletes the n AL Buffers pointed to by puiBuffers
166 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *puiBuffers)
168 ALCcontext *Context;
169 ALbuffer *ALBuf;
170 ALsizei i;
171 ALboolean bFailed = AL_FALSE;
173 Context = GetContextSuspended();
174 if(!Context) return;
176 // Check we are actually Deleting some Buffers
177 if (n >= 0)
179 ALCdevice *device = Context->Device;
181 // Check that all the buffers are valid and can actually be deleted
182 for (i = 0; i < n; i++)
184 if(!puiBuffers[i])
185 continue;
187 // Check for valid Buffer ID (can be NULL buffer)
188 if((ALBuf=LookupBuffer(device->BufferMap, puiBuffers[i])) != NULL)
190 if(ALBuf->refcount != 0)
192 // Buffer still in use, cannot be deleted
193 alSetError(Context, AL_INVALID_OPERATION);
194 bFailed = AL_TRUE;
195 break;
198 else
200 // Invalid Buffer
201 alSetError(Context, AL_INVALID_NAME);
202 bFailed = AL_TRUE;
203 break;
207 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
208 if (!bFailed)
210 for (i = 0; i < n; i++)
212 if((ALBuf=LookupBuffer(device->BufferMap, puiBuffers[i])) != NULL)
214 // Release the memory used to store audio data
215 free(ALBuf->data);
217 // Release buffer structure
218 RemoveUIntMapKey(&device->BufferMap, ALBuf->buffer);
219 ALTHUNK_REMOVEENTRY(ALBuf->buffer);
221 memset(ALBuf, 0, sizeof(ALbuffer));
222 free(ALBuf);
227 else
228 alSetError(Context, AL_INVALID_VALUE);
230 ProcessContext(Context);
234 * alIsBuffer(ALuint uiBuffer)
236 * Checks if ulBuffer is a valid Buffer Name
238 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint uiBuffer)
240 ALCcontext *Context;
241 ALboolean result = AL_TRUE;
243 Context = GetContextSuspended();
244 if(!Context) return AL_FALSE;
246 if(uiBuffer)
247 result = (LookupBuffer(Context->Device->BufferMap, uiBuffer) ?
248 AL_TRUE : AL_FALSE);
250 ProcessContext(Context);
252 return result;
256 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
258 * Fill buffer with audio data
260 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
262 ALCcontext *Context;
263 ALCdevice *device;
264 ALbuffer *ALBuf;
265 ALvoid *temp;
266 ALenum err;
268 Context = GetContextSuspended();
269 if(!Context) return;
271 device = Context->Device;
272 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) != NULL)
274 if(Context->SampleSource)
276 ALintptrEXT offset;
278 if(Context->SampleSource->state == MAPPED)
280 alSetError(Context, AL_INVALID_OPERATION);
281 ProcessContext(Context);
282 return;
285 offset = (const ALubyte*)data - (ALubyte*)NULL;
286 data = Context->SampleSource->data + offset;
289 if ((ALBuf->refcount==0)&&(data))
291 switch(format)
293 case AL_FORMAT_MONO8:
294 case AL_FORMAT_MONO16:
295 case AL_FORMAT_MONO_FLOAT32:
296 case AL_FORMAT_MONO_DOUBLE_EXT:
297 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO_FLOAT32);
298 if(err != AL_NO_ERROR)
299 alSetError(Context, err);
300 break;
302 case AL_FORMAT_STEREO8:
303 case AL_FORMAT_STEREO16:
304 case AL_FORMAT_STEREO_FLOAT32:
305 case AL_FORMAT_STEREO_DOUBLE_EXT:
306 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO_FLOAT32);
307 if(err != AL_NO_ERROR)
308 alSetError(Context, err);
309 break;
311 case AL_FORMAT_REAR8:
312 case AL_FORMAT_REAR16:
313 case AL_FORMAT_REAR32: {
314 ALenum NewFormat = AL_FORMAT_QUAD32;
315 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
316 ALuint NewBytes = aluBytesFromFormat(NewFormat);
317 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
318 ((format==AL_FORMAT_REAR16) ? 2 :
319 4));
320 ALsizei newsize;
322 if((size%(OrigBytes*2)) != 0)
324 alSetError(Context, AL_INVALID_VALUE);
325 break;
328 newsize = size / OrigBytes;
329 newsize *= 2;
331 // Samples are converted here
332 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + newsize) * NewBytes);
333 if(temp)
335 ALBuf->data = temp;
336 ConvertDataRear(ALBuf->data, data, OrigBytes, newsize);
338 ALBuf->format = NewFormat;
339 ALBuf->eOriginalFormat = format;
340 ALBuf->size = newsize*NewBytes;
341 ALBuf->frequency = freq;
343 ALBuf->OriginalSize = size;
344 ALBuf->OriginalAlign = OrigBytes * 2;
346 else
347 alSetError(Context, AL_OUT_OF_MEMORY);
348 } break;
350 case AL_FORMAT_QUAD8_LOKI:
351 case AL_FORMAT_QUAD16_LOKI:
352 case AL_FORMAT_QUAD8:
353 case AL_FORMAT_QUAD16:
354 case AL_FORMAT_QUAD32:
355 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_QUAD32);
356 if(err != AL_NO_ERROR)
357 alSetError(Context, err);
358 break;
360 case AL_FORMAT_51CHN8:
361 case AL_FORMAT_51CHN16:
362 case AL_FORMAT_51CHN32:
363 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_51CHN32);
364 if(err != AL_NO_ERROR)
365 alSetError(Context, err);
366 break;
368 case AL_FORMAT_61CHN8:
369 case AL_FORMAT_61CHN16:
370 case AL_FORMAT_61CHN32:
371 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_61CHN32);
372 if(err != AL_NO_ERROR)
373 alSetError(Context, err);
374 break;
376 case AL_FORMAT_71CHN8:
377 case AL_FORMAT_71CHN16:
378 case AL_FORMAT_71CHN32:
379 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_71CHN32);
380 if(err != AL_NO_ERROR)
381 alSetError(Context, err);
382 break;
384 case AL_FORMAT_MONO_IMA4:
385 case AL_FORMAT_STEREO_IMA4: {
386 int OrigChans = ((format==AL_FORMAT_MONO_IMA4) ? 1 : 2);
387 ALenum NewFormat = ((OrigChans==1) ? AL_FORMAT_MONO_FLOAT32 :
388 AL_FORMAT_STEREO_FLOAT32);
389 ALuint NewBytes = aluBytesFromFormat(NewFormat);
390 ALsizei newsize;
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 newsize = size / 36;
402 newsize *= 65;
404 // Allocate extra padding samples
405 temp = realloc(ALBuf->data, (BUFFER_PADDING*OrigChans + newsize)*NewBytes);
406 if(temp)
408 ALBuf->data = temp;
409 ConvertDataIMA4(ALBuf->data, data, OrigChans, newsize/65);
411 ALBuf->format = NewFormat;
412 ALBuf->eOriginalFormat = format;
413 ALBuf->size = newsize*NewBytes;
414 ALBuf->frequency = freq;
416 ALBuf->OriginalSize = size;
417 ALBuf->OriginalAlign = 36 * OrigChans;
419 else
420 alSetError(Context, AL_OUT_OF_MEMORY);
421 } break;
423 case AL_FORMAT_MONO_MULAW:
424 case AL_FORMAT_STEREO_MULAW:
425 case AL_FORMAT_QUAD_MULAW:
426 case AL_FORMAT_51CHN_MULAW:
427 case AL_FORMAT_61CHN_MULAW:
428 case AL_FORMAT_71CHN_MULAW: {
429 int Channels = ((format==AL_FORMAT_MONO_MULAW) ? 1 :
430 ((format==AL_FORMAT_STEREO_MULAW) ? 2 :
431 ((format==AL_FORMAT_QUAD_MULAW) ? 4 :
432 ((format==AL_FORMAT_51CHN_MULAW) ? 6 :
433 ((format==AL_FORMAT_61CHN_MULAW) ? 7 : 8)))));
434 ALenum NewFormat = ((Channels==1) ? AL_FORMAT_MONO_FLOAT32 :
435 ((Channels==2) ? AL_FORMAT_STEREO_FLOAT32 :
436 ((Channels==4) ? AL_FORMAT_QUAD32 :
437 ((Channels==6) ? AL_FORMAT_51CHN32 :
438 ((Channels==7) ? AL_FORMAT_61CHN32 :
439 AL_FORMAT_71CHN32)))));
440 ALuint NewBytes = aluBytesFromFormat(NewFormat);
442 if((size%(1*Channels)) != 0)
444 alSetError(Context, AL_INVALID_VALUE);
445 break;
448 // Allocate extra padding samples
449 temp = realloc(ALBuf->data, (BUFFER_PADDING*Channels + size)*NewBytes);
450 if(temp)
452 ALBuf->data = temp;
453 ConvertDataMULaw(ALBuf->data, data, size);
455 ALBuf->format = NewFormat;
456 ALBuf->eOriginalFormat = format;
457 ALBuf->size = size*NewBytes;
458 ALBuf->frequency = freq;
460 ALBuf->OriginalSize = size;
461 ALBuf->OriginalAlign = 1 * Channels;
463 else
464 alSetError(Context, AL_OUT_OF_MEMORY);
465 } break;
467 case AL_FORMAT_REAR_MULAW: {
468 int OrigChans = 2;
469 ALenum NewFormat = AL_FORMAT_QUAD32;
470 ALuint NewBytes = aluBytesFromFormat(NewFormat);
471 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
472 ALsizei newsize;
474 if((size%(1*OrigChans)) != 0)
476 alSetError(Context, AL_INVALID_VALUE);
477 break;
480 newsize = size * 2;
482 // Allocate extra padding samples
483 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + newsize)*NewBytes);
484 if(temp)
486 ALBuf->data = temp;
487 ConvertDataMULawRear(ALBuf->data, data, newsize);
489 ALBuf->format = NewFormat;
490 ALBuf->eOriginalFormat = format;
491 ALBuf->size = newsize*NewBytes;
492 ALBuf->frequency = freq;
494 ALBuf->OriginalSize = size;
495 ALBuf->OriginalAlign = 1 * OrigChans;
497 else
498 alSetError(Context, AL_OUT_OF_MEMORY);
499 } break;
501 default:
502 alSetError(Context, AL_INVALID_ENUM);
503 break;
506 else
508 // Buffer is in use, or data is a NULL pointer
509 alSetError(Context, AL_INVALID_VALUE);
512 else
514 // Invalid Buffer Name
515 alSetError(Context, AL_INVALID_NAME);
518 ProcessContext(Context);
522 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
524 * Fill buffer with audio data
526 AL_API ALvoid AL_APIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
528 ALCcontext *Context;
529 ALCdevice *device;
530 ALbuffer *ALBuf;
532 Context = GetContextSuspended();
533 if(!Context) return;
535 device = Context->Device;
536 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) != NULL)
538 if(Context->SampleSource)
540 ALintptrEXT offset;
542 if(Context->SampleSource->state == MAPPED)
544 alSetError(Context, AL_INVALID_OPERATION);
545 ProcessContext(Context);
546 return;
549 offset = (const ALubyte*)data - (ALubyte*)NULL;
550 data = Context->SampleSource->data + offset;
553 if(ALBuf->data == NULL)
555 // buffer does not have any data
556 alSetError(Context, AL_INVALID_NAME);
558 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
560 // data is NULL or offset/length is negative
561 alSetError(Context, AL_INVALID_VALUE);
563 else
565 switch(format)
567 case AL_FORMAT_REAR8:
568 case AL_FORMAT_REAR16:
569 case AL_FORMAT_REAR32: {
570 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
571 ((format==AL_FORMAT_REAR16) ? 2 :
572 4));
573 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
575 if(ALBuf->eOriginalFormat != AL_FORMAT_REAR8 &&
576 ALBuf->eOriginalFormat != AL_FORMAT_REAR16 &&
577 ALBuf->eOriginalFormat != AL_FORMAT_REAR32)
579 alSetError(Context, AL_INVALID_ENUM);
580 break;
583 if(ALBuf->size/4/NewBytes < (ALuint)offset+length)
585 alSetError(Context, AL_INVALID_VALUE);
586 break;
589 ConvertDataRear(&ALBuf->data[offset*4], data, OrigBytes, length*2);
590 } break;
592 case AL_FORMAT_MONO_IMA4:
593 case AL_FORMAT_STEREO_IMA4: {
594 int Channels = aluChannelsFromFormat(ALBuf->format);
595 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
597 if(ALBuf->eOriginalFormat != format)
599 alSetError(Context, AL_INVALID_ENUM);
600 break;
603 if((offset%65) != 0 || (length%65) != 0 ||
604 ALBuf->size/Channels/Bytes < (ALuint)offset+length)
606 alSetError(Context, AL_INVALID_VALUE);
607 break;
610 ConvertDataIMA4(&ALBuf->data[offset*Channels], data, Channels, length/65*Channels);
611 } break;
613 case AL_FORMAT_MONO_MULAW:
614 case AL_FORMAT_STEREO_MULAW:
615 case AL_FORMAT_QUAD_MULAW:
616 case AL_FORMAT_REAR_MULAW:
617 case AL_FORMAT_51CHN_MULAW:
618 case AL_FORMAT_61CHN_MULAW:
619 case AL_FORMAT_71CHN_MULAW: {
620 int Channels = aluChannelsFromFormat(ALBuf->format);
621 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
623 if(ALBuf->eOriginalFormat != format)
625 alSetError(Context, AL_INVALID_ENUM);
626 break;
629 if(ALBuf->size/Channels/Bytes < (ALuint)offset+length)
631 alSetError(Context, AL_INVALID_VALUE);
632 break;
635 if(ALBuf->eOriginalFormat == AL_FORMAT_REAR_MULAW)
636 ConvertDataMULawRear(&ALBuf->data[offset*Channels], data, length*2);
637 else
638 ConvertDataMULaw(&ALBuf->data[offset*Channels], data, length*Channels);
639 } break;
641 default: {
642 ALuint Channels = aluChannelsFromFormat(format);
643 ALuint Bytes = aluBytesFromFormat(format);
644 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
646 if(Channels != aluChannelsFromFormat(ALBuf->format))
648 alSetError(Context, AL_INVALID_ENUM);
649 break;
652 if(ALBuf->size/Channels/NewBytes < (ALuint)offset+length)
654 alSetError(Context, AL_INVALID_VALUE);
655 break;
658 ConvertData(&ALBuf->data[offset*Channels], data, Bytes, length*Channels);
659 } break;
663 else
665 // Invalid Buffer Name
666 alSetError(Context, AL_INVALID_NAME);
669 ProcessContext(Context);
673 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
675 ALCcontext *pContext;
676 ALCdevice *device;
678 (void)flValue;
680 pContext = GetContextSuspended();
681 if(!pContext) return;
683 device = pContext->Device;
684 if(LookupBuffer(device->BufferMap, buffer) != NULL)
686 switch(eParam)
688 default:
689 alSetError(pContext, AL_INVALID_ENUM);
690 break;
693 else
695 alSetError(pContext, AL_INVALID_NAME);
698 ProcessContext(pContext);
702 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
704 ALCcontext *pContext;
705 ALCdevice *device;
707 (void)flValue1;
708 (void)flValue2;
709 (void)flValue3;
711 pContext = GetContextSuspended();
712 if(!pContext) return;
714 device = pContext->Device;
715 if(LookupBuffer(device->BufferMap, buffer) != NULL)
717 switch(eParam)
719 default:
720 alSetError(pContext, AL_INVALID_ENUM);
721 break;
724 else
726 alSetError(pContext, AL_INVALID_NAME);
729 ProcessContext(pContext);
733 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
735 ALCcontext *pContext;
736 ALCdevice *device;
738 (void)flValues;
740 pContext = GetContextSuspended();
741 if(!pContext) return;
743 device = pContext->Device;
744 if(LookupBuffer(device->BufferMap, buffer) != NULL)
746 switch(eParam)
748 default:
749 alSetError(pContext, AL_INVALID_ENUM);
750 break;
753 else
755 alSetError(pContext, AL_INVALID_NAME);
758 ProcessContext(pContext);
762 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
764 ALCcontext *pContext;
765 ALCdevice *device;
767 (void)lValue;
769 pContext = GetContextSuspended();
770 if(!pContext) return;
772 device = pContext->Device;
773 if(LookupBuffer(device->BufferMap, buffer) != NULL)
775 switch(eParam)
777 default:
778 alSetError(pContext, AL_INVALID_ENUM);
779 break;
782 else
784 alSetError(pContext, AL_INVALID_NAME);
787 ProcessContext(pContext);
791 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
793 ALCcontext *pContext;
794 ALCdevice *device;
796 (void)lValue1;
797 (void)lValue2;
798 (void)lValue3;
800 pContext = GetContextSuspended();
801 if(!pContext) return;
803 device = pContext->Device;
804 if(LookupBuffer(device->BufferMap, buffer) != NULL)
806 switch(eParam)
808 default:
809 alSetError(pContext, AL_INVALID_ENUM);
810 break;
813 else
815 alSetError(pContext, AL_INVALID_NAME);
818 ProcessContext(pContext);
822 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
824 ALCcontext *pContext;
825 ALCdevice *device;
827 (void)plValues;
829 pContext = GetContextSuspended();
830 if(!pContext) return;
832 device = pContext->Device;
833 if(LookupBuffer(device->BufferMap, buffer) != NULL)
835 switch(eParam)
837 default:
838 alSetError(pContext, AL_INVALID_ENUM);
839 break;
842 else
844 alSetError(pContext, AL_INVALID_NAME);
847 ProcessContext(pContext);
851 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
853 ALCcontext *pContext;
854 ALCdevice *device;
856 pContext = GetContextSuspended();
857 if(!pContext) return;
859 if (pflValue)
861 device = pContext->Device;
862 if(LookupBuffer(device->BufferMap, buffer) != NULL)
864 switch(eParam)
866 default:
867 alSetError(pContext, AL_INVALID_ENUM);
868 break;
871 else
873 alSetError(pContext, AL_INVALID_NAME);
876 else
878 alSetError(pContext, AL_INVALID_VALUE);
881 ProcessContext(pContext);
885 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
887 ALCcontext *pContext;
888 ALCdevice *device;
890 pContext = GetContextSuspended();
891 if(!pContext) return;
893 if ((pflValue1) && (pflValue2) && (pflValue3))
895 device = pContext->Device;
896 if(LookupBuffer(device->BufferMap, buffer) != NULL)
898 switch(eParam)
900 default:
901 alSetError(pContext, AL_INVALID_ENUM);
902 break;
905 else
907 alSetError(pContext, AL_INVALID_NAME);
910 else
912 alSetError(pContext, AL_INVALID_VALUE);
915 ProcessContext(pContext);
919 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
921 ALCcontext *pContext;
922 ALCdevice *device;
924 pContext = GetContextSuspended();
925 if(!pContext) return;
927 if (pflValues)
929 device = pContext->Device;
930 if(LookupBuffer(device->BufferMap, buffer) != NULL)
932 switch(eParam)
934 default:
935 alSetError(pContext, AL_INVALID_ENUM);
936 break;
939 else
941 alSetError(pContext, AL_INVALID_NAME);
944 else
946 alSetError(pContext, AL_INVALID_VALUE);
949 ProcessContext(pContext);
953 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
955 ALCcontext *pContext;
956 ALbuffer *pBuffer;
957 ALCdevice *device;
959 pContext = GetContextSuspended();
960 if(!pContext) return;
962 if (plValue)
964 device = pContext->Device;
965 if((pBuffer=LookupBuffer(device->BufferMap, buffer)) != NULL)
967 switch (eParam)
969 case AL_FREQUENCY:
970 *plValue = pBuffer->frequency;
971 break;
973 case AL_BITS:
974 *plValue = aluBytesFromFormat(pBuffer->format) * 8;
975 break;
977 case AL_CHANNELS:
978 *plValue = aluChannelsFromFormat(pBuffer->format);
979 break;
981 case AL_SIZE:
982 *plValue = pBuffer->size;
983 break;
985 default:
986 alSetError(pContext, AL_INVALID_ENUM);
987 break;
990 else
992 alSetError(pContext, AL_INVALID_NAME);
995 else
997 alSetError(pContext, AL_INVALID_VALUE);
1000 ProcessContext(pContext);
1004 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
1006 ALCcontext *pContext;
1007 ALCdevice *device;
1009 pContext = GetContextSuspended();
1010 if(!pContext) return;
1012 if ((plValue1) && (plValue2) && (plValue3))
1014 device = pContext->Device;
1015 if(LookupBuffer(device->BufferMap, buffer) != NULL)
1017 switch(eParam)
1019 default:
1020 alSetError(pContext, AL_INVALID_ENUM);
1021 break;
1024 else
1026 alSetError(pContext, AL_INVALID_NAME);
1029 else
1031 alSetError(pContext, AL_INVALID_VALUE);
1034 ProcessContext(pContext);
1038 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
1040 ALCcontext *pContext;
1041 ALCdevice *device;
1043 pContext = GetContextSuspended();
1044 if(!pContext) return;
1046 if (plValues)
1048 device = pContext->Device;
1049 if(LookupBuffer(device->BufferMap, buffer) != NULL)
1051 switch (eParam)
1053 case AL_FREQUENCY:
1054 case AL_BITS:
1055 case AL_CHANNELS:
1056 case AL_SIZE:
1057 alGetBufferi(buffer, eParam, plValues);
1058 break;
1060 default:
1061 alSetError(pContext, AL_INVALID_ENUM);
1062 break;
1065 else
1067 alSetError(pContext, AL_INVALID_NAME);
1070 else
1072 alSetError(pContext, AL_INVALID_VALUE);
1075 ProcessContext(pContext);
1079 * LoadData
1081 * Loads the specified data into the buffer, using the specified formats.
1082 * Currently, the new format must be 32-bit float, and must have the same
1083 * channel configuration as the original format. This does NOT handle
1084 * compressed formats (eg. IMA4).
1086 static ALenum LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
1088 ALuint NewBytes = aluBytesFromFormat(NewFormat);
1089 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
1090 ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
1091 ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
1092 ALsizei newsize;
1093 ALvoid *temp;
1095 assert(NewBytes == 4);
1096 assert(NewChannels == OrigChannels);
1098 if ((size%(OrigBytes*OrigChannels)) != 0)
1099 return AL_INVALID_VALUE;
1101 // Samples are converted here
1102 newsize = size / OrigBytes;
1103 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + newsize) * NewBytes);
1104 if(!temp) return AL_OUT_OF_MEMORY;
1106 ALBuf->data = temp;
1107 ConvertData(ALBuf->data, data, OrigBytes, newsize);
1109 ALBuf->format = NewFormat;
1110 ALBuf->eOriginalFormat = OrigFormat;
1111 ALBuf->size = newsize*NewBytes;
1112 ALBuf->frequency = freq;
1114 ALBuf->OriginalSize = size;
1115 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
1117 return AL_NO_ERROR;
1120 static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1122 ALsizei i;
1123 ALint smp;
1124 switch(origBytes)
1126 case 1:
1127 for(i = 0;i < len;i++)
1129 smp = ((ALubyte*)src)[i];
1130 dst[i] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1132 break;
1134 case 2:
1135 for(i = 0;i < len;i++)
1137 smp = ((ALshort*)src)[i];
1138 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1140 break;
1142 case 4:
1143 for(i = 0;i < len;i++)
1144 dst[i] = ((ALfloat*)src)[i];
1145 break;
1147 case 8:
1148 for(i = 0;i < len;i++)
1149 dst[i] = ((ALdouble*)src)[i];
1150 break;
1152 default:
1153 assert(0);
1157 static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1159 ALsizei i;
1160 ALint smp;
1161 switch(origBytes)
1163 case 1:
1164 for(i = 0;i < len;i+=4)
1166 dst[i+0] = 0;
1167 dst[i+1] = 0;
1168 smp = ((ALubyte*)src)[i/2+0];
1169 dst[i+2] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1170 smp = ((ALubyte*)src)[i/2+1];
1171 dst[i+3] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1173 break;
1175 case 2:
1176 for(i = 0;i < len;i+=4)
1178 dst[i+0] = 0;
1179 dst[i+1] = 0;
1180 smp = ((ALshort*)src)[i/2+0];
1181 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1182 smp = ((ALshort*)src)[i/2+1];
1183 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1185 break;
1187 case 4:
1188 for(i = 0;i < len;i+=4)
1190 dst[i+0] = 0;
1191 dst[i+1] = 0;
1192 dst[i+2] = ((ALfloat*)src)[i/2+0];
1193 dst[i+3] = ((ALfloat*)src)[i/2+1];
1195 break;
1197 default:
1198 assert(0);
1202 static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len)
1204 const ALuint *IMAData;
1205 ALint Sample[2],Index[2];
1206 ALuint IMACode[2];
1207 ALsizei i,j,k,c;
1209 IMAData = src;
1210 for(i = 0;i < len/origChans;i++)
1212 for(c = 0;c < origChans;c++)
1214 Sample[c] = ((ALshort*)IMAData)[0];
1215 Index[c] = ((ALshort*)IMAData)[1];
1217 Index[c] = ((Index[c]<0) ? 0 : Index[c]);
1218 Index[c] = ((Index[c]>88) ? 88 : Index[c]);
1220 dst[i*65*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1222 IMAData++;
1225 for(j = 1;j < 65;j += 8)
1227 for(c = 0;c < origChans;c++)
1228 IMACode[c] = *(IMAData++);
1230 for(k = 0;k < 8;k++)
1232 for(c = 0;c < origChans;c++)
1234 Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
1235 Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
1237 if(Sample[c] < -32768) Sample[c] = -32768;
1238 else if(Sample[c] > 32767) Sample[c] = 32767;
1240 if(Index[c]<0) Index[c] = 0;
1241 else if(Index[c]>88) Index[c] = 88;
1243 dst[(i*65+j+k)*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1244 IMACode[c] >>= 4;
1251 static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len)
1253 ALsizei i;
1254 ALint smp;
1255 for(i = 0;i < len;i++)
1257 smp = muLawDecompressionTable[((ALubyte*)src)[i]];
1258 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1262 static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len)
1264 ALsizei i;
1265 ALint smp;
1266 for(i = 0;i < len;i+=4)
1268 dst[i+0] = 0;
1269 dst[i+1] = 0;
1270 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+0]];
1271 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1272 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+1]];
1273 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1278 * ReleaseALBuffers()
1280 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1282 ALvoid ReleaseALBuffers(ALCdevice *device)
1284 ALsizei i;
1285 for(i = 0;i < device->BufferMap.size;i++)
1287 ALbuffer *temp = device->BufferMap.array[i].value;
1288 device->BufferMap.array[i].value = NULL;
1290 // Release sample data
1291 free(temp->data);
1293 // Release Buffer structure
1294 ALTHUNK_REMOVEENTRY(temp->buffer);
1295 memset(temp, 0, sizeof(ALbuffer));
1296 free(temp);