Couple small error handling fixups
[openal-soft/openal-hmr.git] / OpenAL32 / alBuffer.c
blob3e2fcaa460c5be912cc81cf5d96abc7d47bbed0f
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);
45 * Global Variables
48 /* IMA ADPCM Stepsize table */
49 static const long IMAStep_size[89] = {
50 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
51 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
52 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
53 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
54 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
55 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
56 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
57 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
58 32767
61 /* IMA4 ADPCM Codeword decode table */
62 static const long IMA4Codeword[16] = {
63 1, 3, 5, 7, 9, 11, 13, 15,
64 -1,-3,-5,-7,-9,-11,-13,-15,
67 /* IMA4 ADPCM Step index adjust decode table */
68 static const long IMA4Index_adjust[16] = {
69 -1,-1,-1,-1, 2, 4, 6, 8,
70 -1,-1,-1,-1, 2, 4, 6, 8
73 /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
74 * signed 16-bit sample */
75 static const ALshort muLawDecompressionTable[256] = {
76 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
77 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
78 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
79 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
80 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
81 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
82 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
83 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
84 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
85 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
86 -876, -844, -812, -780, -748, -716, -684, -652,
87 -620, -588, -556, -524, -492, -460, -428, -396,
88 -372, -356, -340, -324, -308, -292, -276, -260,
89 -244, -228, -212, -196, -180, -164, -148, -132,
90 -120, -112, -104, -96, -88, -80, -72, -64,
91 -56, -48, -40, -32, -24, -16, -8, 0,
92 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
93 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
94 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
95 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
96 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
97 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
98 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
99 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
100 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
101 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
102 876, 844, 812, 780, 748, 716, 684, 652,
103 620, 588, 556, 524, 492, 460, 428, 396,
104 372, 356, 340, 324, 308, 292, 276, 260,
105 244, 228, 212, 196, 180, 164, 148, 132,
106 120, 112, 104, 96, 88, 80, 72, 64,
107 56, 48, 40, 32, 24, 16, 8, 0
110 /* Values used when encoding a muLaw sample */
111 static const int muLawBias = 0x84;
112 static const int muLawClip = 32635;
113 static const char muLawCompressTable[256] = {
114 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
115 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
116 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
117 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
118 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
119 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
120 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
121 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
122 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
123 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
124 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
125 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
126 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
127 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
128 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
129 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
133 /* A quick'n'dirty lookup table to decode an aLaw-encoded byte sample into a
134 * signed 16-bit sample */
135 static const ALshort aLawDecompressionTable[256] = {
136 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
137 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
138 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
139 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
140 -22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,
141 -30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,
142 -11008,-10496,-12032,-11520, -8960, -8448, -9984, -9472,
143 -15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568,
144 -344, -328, -376, -360, -280, -264, -312, -296,
145 -472, -456, -504, -488, -408, -392, -440, -424,
146 -88, -72, -120, -104, -24, -8, -56, -40,
147 -216, -200, -248, -232, -152, -136, -184, -168,
148 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
149 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
150 -688, -656, -752, -720, -560, -528, -624, -592,
151 -944, -912, -1008, -976, -816, -784, -880, -848,
152 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
153 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
154 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
155 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
156 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
157 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
158 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
159 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
160 344, 328, 376, 360, 280, 264, 312, 296,
161 472, 456, 504, 488, 408, 392, 440, 424,
162 88, 72, 120, 104, 24, 8, 56, 40,
163 216, 200, 248, 232, 152, 136, 184, 168,
164 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
165 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
166 688, 656, 752, 720, 560, 528, 624, 592,
167 944, 912, 1008, 976, 816, 784, 880, 848
170 /* Values used when encoding an aLaw sample */
171 static const int aLawClip = 32635;
172 static const char aLawCompressTable[128] = {
173 1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
174 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
175 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
176 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
177 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
178 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
179 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
180 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
184 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
186 ALCcontext *Context;
187 ALsizei cur = 0;
189 Context = GetContextRef();
190 if(!Context) return;
192 al_try
194 ALCdevice *device = Context->Device;
195 ALenum err;
197 CHECK_VALUE(Context, n >= 0);
198 for(cur = 0;cur < n;cur++)
200 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
201 if(!buffer)
202 al_throwerr(Context, AL_OUT_OF_MEMORY);
203 RWLockInit(&buffer->lock);
205 err = NewThunkEntry(&buffer->id);
206 if(err == AL_NO_ERROR)
207 err = InsertUIntMapEntry(&device->BufferMap, buffer->id, buffer);
208 if(err != AL_NO_ERROR)
210 FreeThunkEntry(buffer->id);
211 memset(buffer, 0, sizeof(ALbuffer));
212 free(buffer);
214 al_throwerr(Context, err);
217 buffers[cur] = buffer->id;
220 al_catchany()
222 if(cur > 0)
223 alDeleteBuffers(cur, buffers);
225 al_endtry;
227 ALCcontext_DecRef(Context);
230 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
232 ALCcontext *Context;
233 ALbuffer *ALBuf;
234 ALsizei i;
236 Context = GetContextRef();
237 if(!Context) return;
239 al_try
241 ALCdevice *device = Context->Device;
243 CHECK_VALUE(Context, n >= 0);
244 for(i = 0;i < n;i++)
246 if(!buffers[i])
247 continue;
249 /* Check for valid Buffer ID */
250 if((ALBuf=LookupBuffer(device, buffers[i])) == NULL)
251 al_throwerr(Context, AL_INVALID_NAME);
252 if(ALBuf->ref != 0)
253 al_throwerr(Context, AL_INVALID_OPERATION);
256 for(i = 0;i < n;i++)
258 if((ALBuf=RemoveBuffer(device, buffers[i])) == NULL)
259 continue;
260 FreeThunkEntry(ALBuf->id);
262 free(ALBuf->data);
264 memset(ALBuf, 0, sizeof(*ALBuf));
265 free(ALBuf);
268 al_endtry;
270 ALCcontext_DecRef(Context);
273 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
275 ALCcontext *Context;
276 ALboolean result;
278 Context = GetContextRef();
279 if(!Context) return AL_FALSE;
281 result = ((!buffer || LookupBuffer(Context->Device, buffer)) ?
282 AL_TRUE : AL_FALSE);
284 ALCcontext_DecRef(Context);
286 return result;
290 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
292 enum UserFmtChannels SrcChannels;
293 enum UserFmtType SrcType;
294 ALCcontext *Context;
295 ALuint FrameSize;
296 ALenum NewFormat;
297 ALbuffer *ALBuf;
298 ALenum err;
300 Context = GetContextRef();
301 if(!Context) return;
303 al_try
305 ALCdevice *device = Context->Device;
306 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
307 al_throwerr(Context, AL_INVALID_NAME);
308 CHECK_VALUE(Context, size >= 0 && freq >= 0);
309 if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
310 al_throwerr(Context, AL_INVALID_ENUM);
311 switch(SrcType)
313 case UserFmtByte:
314 case UserFmtUByte:
315 case UserFmtShort:
316 case UserFmtUShort:
317 case UserFmtInt:
318 case UserFmtUInt:
319 case UserFmtFloat:
320 FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
321 CHECK_VALUE(Context, (size%FrameSize) == 0);
323 err = LoadData(ALBuf, freq, format, size/FrameSize,
324 SrcChannels, SrcType, data, AL_TRUE);
325 if(err != AL_NO_ERROR)
326 al_throwerr(Context, err);
327 break;
329 case UserFmtByte3:
330 case UserFmtUByte3:
331 case UserFmtDouble:
332 FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
333 CHECK_VALUE(Context, (size%FrameSize) == 0);
335 NewFormat = AL_FORMAT_MONO_FLOAT32;
336 switch(SrcChannels)
338 case UserFmtMono: NewFormat = AL_FORMAT_MONO_FLOAT32; break;
339 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO_FLOAT32; break;
340 case UserFmtRear: NewFormat = AL_FORMAT_REAR32; break;
341 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD32; break;
342 case UserFmtX51: NewFormat = AL_FORMAT_51CHN32; break;
343 case UserFmtX61: NewFormat = AL_FORMAT_61CHN32; break;
344 case UserFmtX71: NewFormat = AL_FORMAT_71CHN32; break;
346 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
347 SrcChannels, SrcType, data, AL_TRUE);
348 if(err != AL_NO_ERROR)
349 al_throwerr(Context, err);
350 break;
352 case UserFmtMulaw:
353 case UserFmtAlaw:
354 FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
355 CHECK_VALUE(Context, (size%FrameSize) == 0);
357 NewFormat = AL_FORMAT_MONO16;
358 switch(SrcChannels)
360 case UserFmtMono: NewFormat = AL_FORMAT_MONO16; break;
361 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
362 case UserFmtRear: NewFormat = AL_FORMAT_REAR16; break;
363 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
364 case UserFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
365 case UserFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
366 case UserFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
368 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
369 SrcChannels, SrcType, data, AL_TRUE);
370 if(err != AL_NO_ERROR)
371 al_throwerr(Context, err);
372 break;
374 case UserFmtIMA4:
375 /* Here is where things vary:
376 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
377 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
379 FrameSize = ChannelsFromUserFmt(SrcChannels) * 36;
380 CHECK_VALUE(Context, (size%FrameSize) == 0);
382 NewFormat = AL_FORMAT_MONO16;
383 switch(SrcChannels)
385 case UserFmtMono: NewFormat = AL_FORMAT_MONO16; break;
386 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
387 case UserFmtRear: NewFormat = AL_FORMAT_REAR16; break;
388 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
389 case UserFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
390 case UserFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
391 case UserFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
393 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize*65,
394 SrcChannels, SrcType, data, AL_TRUE);
395 if(err != AL_NO_ERROR)
396 al_throwerr(Context, err);
397 break;
400 al_endtry;
402 ALCcontext_DecRef(Context);
405 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
407 enum UserFmtChannels SrcChannels;
408 enum UserFmtType SrcType;
409 ALCcontext *Context;
410 ALbuffer *ALBuf;
412 Context = GetContextRef();
413 if(!Context) return;
415 al_try
417 ALCdevice *device = Context->Device;
418 ALuint original_align;
419 ALuint Channels;
420 ALuint Bytes;
422 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
423 al_throwerr(Context, AL_INVALID_NAME);
424 CHECK_VALUE(Context, length >= 0 && offset >= 0);
425 if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
426 al_throwerr(Context, AL_INVALID_ENUM);
428 WriteLock(&ALBuf->lock);
429 original_align = ((ALBuf->OriginalType == UserFmtIMA4) ?
430 (ChannelsFromUserFmt(ALBuf->OriginalChannels)*36) :
431 FrameSizeFromUserFmt(ALBuf->OriginalChannels,
432 ALBuf->OriginalType));
434 if(SrcChannels != ALBuf->OriginalChannels || SrcType != ALBuf->OriginalType)
436 WriteUnlock(&ALBuf->lock);
437 al_throwerr(Context, AL_INVALID_ENUM);
439 if(offset > ALBuf->OriginalSize || length > ALBuf->OriginalSize-offset ||
440 (offset%original_align) != 0 || (length%original_align) != 0)
442 WriteUnlock(&ALBuf->lock);
443 al_throwerr(Context, AL_INVALID_VALUE);
446 Channels = ChannelsFromFmt(ALBuf->FmtChannels);
447 Bytes = BytesFromFmt(ALBuf->FmtType);
448 /* offset -> byte offset, length -> sample count */
449 if(SrcType == UserFmtIMA4)
451 offset = offset/36*65 * Bytes;
452 length = length/original_align * 65;
454 else
456 ALuint OldBytes = BytesFromUserFmt(SrcType);
457 offset = offset/OldBytes * Bytes;
458 length = length/OldBytes/Channels;
460 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
461 data, SrcType, Channels, length);
462 WriteUnlock(&ALBuf->lock);
464 al_endtry;
466 ALCcontext_DecRef(Context);
470 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer,
471 ALuint samplerate, ALenum internalformat, ALsizei samples,
472 ALenum channels, ALenum type, const ALvoid *data)
474 ALCcontext *Context;
475 ALbuffer *ALBuf;
476 ALenum err;
478 Context = GetContextRef();
479 if(!Context) return;
481 al_try
483 ALCdevice *device = Context->Device;
484 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
485 al_throwerr(Context, AL_INVALID_NAME);
486 CHECK_VALUE(Context, samples >= 0 && samplerate != 0);
487 if(IsValidType(type) == AL_FALSE || IsValidChannels(channels) == AL_FALSE)
488 al_throwerr(Context, AL_INVALID_ENUM);
490 err = LoadData(ALBuf, samplerate, internalformat, samples,
491 channels, type, data, AL_FALSE);
492 if(err != AL_NO_ERROR)
493 al_throwerr(Context, err);
495 al_endtry;
497 ALCcontext_DecRef(Context);
500 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint buffer,
501 ALsizei offset, ALsizei samples,
502 ALenum channels, ALenum type, const ALvoid *data)
504 ALCcontext *Context;
505 ALbuffer *ALBuf;
507 Context = GetContextRef();
508 if(!Context) return;
510 al_try
512 ALCdevice *device = Context->Device;
513 ALuint FrameSize;
515 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
516 al_throwerr(Context, AL_INVALID_NAME);
517 CHECK_VALUE(Context, samples >= 0 && offset >= 0);
518 if(IsValidType(type) == AL_FALSE)
519 al_throwerr(Context, AL_INVALID_ENUM);
521 WriteLock(&ALBuf->lock);
522 FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
523 if(channels != (ALenum)ALBuf->FmtChannels)
525 WriteUnlock(&ALBuf->lock);
526 al_throwerr(Context, AL_INVALID_ENUM);
528 else if(offset > ALBuf->SampleLen || samples > ALBuf->SampleLen-offset)
530 WriteUnlock(&ALBuf->lock);
531 al_throwerr(Context,AL_INVALID_VALUE);
534 /* offset -> byte offset */
535 offset *= FrameSize;
536 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
537 data, type, ChannelsFromFmt(ALBuf->FmtChannels), samples);
538 WriteUnlock(&ALBuf->lock);
540 al_endtry;
542 ALCcontext_DecRef(Context);
545 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer,
546 ALsizei offset, ALsizei samples,
547 ALenum channels, ALenum type, ALvoid *data)
549 ALCcontext *Context;
550 ALbuffer *ALBuf;
552 Context = GetContextRef();
553 if(!Context) return;
555 al_try
557 ALCdevice *device = Context->Device;
558 ALuint FrameSize;
560 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
561 al_throwerr(Context, AL_INVALID_NAME);
562 CHECK_VALUE(Context, samples >= 0 && offset >= 0);
563 if(IsValidType(type) == AL_FALSE)
564 al_throwerr(Context, AL_INVALID_ENUM);
566 ReadLock(&ALBuf->lock);
567 FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
568 if(channels != (ALenum)ALBuf->FmtChannels)
570 ReadUnlock(&ALBuf->lock);
571 al_throwerr(Context, AL_INVALID_ENUM);
573 if(offset > ALBuf->SampleLen || samples > ALBuf->SampleLen-offset)
575 ReadUnlock(&ALBuf->lock);
576 al_throwerr(Context,AL_INVALID_VALUE);
578 if(type == UserFmtIMA4 && (samples%65) != 0)
580 ReadUnlock(&ALBuf->lock);
581 al_throwerr(Context, AL_INVALID_VALUE);
584 /* offset -> byte offset */
585 offset *= FrameSize;
586 ConvertData(data, type, &((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
587 ChannelsFromFmt(ALBuf->FmtChannels), samples);
588 ReadUnlock(&ALBuf->lock);
590 al_endtry;
592 ALCcontext_DecRef(Context);
595 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format)
597 enum FmtChannels DstChannels;
598 enum FmtType DstType;
599 ALCcontext *Context;
600 ALboolean ret;
602 Context = GetContextRef();
603 if(!Context) return AL_FALSE;
605 ret = DecomposeFormat(format, &DstChannels, &DstType);
607 ALCcontext_DecRef(Context);
609 return ret;
613 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat value)
615 ALCcontext *Context;
617 (void)value;
619 Context = GetContextRef();
620 if(!Context) return;
622 al_try
624 ALCdevice *device = Context->Device;
625 if(LookupBuffer(device, buffer) == NULL)
626 al_throwerr(Context, AL_INVALID_NAME);
628 switch(param)
630 default:
631 al_throwerr(Context, AL_INVALID_ENUM);
634 al_endtry;
636 ALCcontext_DecRef(Context);
640 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3)
642 ALCcontext *Context;
644 (void)value1;
645 (void)value2;
646 (void)value3;
648 Context = GetContextRef();
649 if(!Context) return;
651 al_try
653 ALCdevice *device = Context->Device;
654 if(LookupBuffer(device, buffer) == NULL)
655 al_throwerr(Context, AL_INVALID_NAME);
657 switch(param)
659 default:
660 al_throwerr(Context, AL_INVALID_ENUM);
663 al_endtry;
665 ALCcontext_DecRef(Context);
669 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
671 ALCcontext *Context;
673 Context = GetContextRef();
674 if(!Context) return;
676 al_try
678 ALCdevice *device = Context->Device;
679 if(LookupBuffer(device, buffer) == NULL)
680 al_throwerr(Context, AL_INVALID_NAME);
682 CHECK_VALUE(Context, values);
683 switch(param)
685 default:
686 al_throwerr(Context, AL_INVALID_ENUM);
689 al_endtry;
691 ALCcontext_DecRef(Context);
695 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
697 ALCcontext *Context;
699 (void)value;
701 Context = GetContextRef();
702 if(!Context) return;
704 al_try
706 ALCdevice *device = Context->Device;
707 if(LookupBuffer(device, buffer) == NULL)
708 al_throwerr(Context, AL_INVALID_NAME);
710 switch(param)
712 default:
713 al_throwerr(Context, AL_INVALID_ENUM);
716 al_endtry;
718 ALCcontext_DecRef(Context);
722 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum param, ALint value1, ALint value2, ALint value3)
724 ALCcontext *Context;
726 (void)value1;
727 (void)value2;
728 (void)value3;
730 Context = GetContextRef();
731 if(!Context) return;
733 al_try
735 ALCdevice *device = Context->Device;
736 if(LookupBuffer(device, buffer) == NULL)
737 al_throwerr(Context, AL_INVALID_NAME);
739 switch(param)
741 default:
742 al_throwerr(Context, AL_INVALID_ENUM);
745 al_endtry;
747 ALCcontext_DecRef(Context);
751 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
753 ALCcontext *Context;
754 ALbuffer *ALBuf;
756 Context = GetContextRef();
757 if(!Context) return;
759 al_try
761 ALCdevice *device = Context->Device;
762 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
763 al_throwerr(Context, AL_INVALID_NAME);
765 CHECK_VALUE(Context, values);
766 switch(param)
768 case AL_LOOP_POINTS_SOFT:
769 WriteLock(&ALBuf->lock);
770 if(ALBuf->ref != 0)
772 WriteUnlock(&ALBuf->lock);
773 al_throwerr(Context, AL_INVALID_OPERATION);
775 if(values[0] >= values[1] || values[0] < 0 ||
776 values[1] > ALBuf->SampleLen)
778 WriteUnlock(&ALBuf->lock);
779 al_throwerr(Context, AL_INVALID_VALUE);
782 ALBuf->LoopStart = values[0];
783 ALBuf->LoopEnd = values[1];
784 WriteUnlock(&ALBuf->lock);
785 break;
787 default:
788 al_throwerr(Context, AL_INVALID_ENUM);
791 al_endtry;
793 ALCcontext_DecRef(Context);
797 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
799 ALCcontext *Context;
800 ALbuffer *Buffer;
802 Context = GetContextRef();
803 if(!Context) return;
805 al_try
807 ALCdevice *device = Context->Device;
808 if((Buffer=LookupBuffer(device, buffer)) == NULL)
809 al_throwerr(Context, AL_INVALID_NAME);
811 CHECK_VALUE(Context, value);
812 switch(param)
814 case AL_SEC_LENGTH_SOFT:
815 ReadLock(&Buffer->lock);
816 if(Buffer->SampleLen != 0)
817 *value = Buffer->SampleLen / (ALfloat)Buffer->Frequency;
818 else
819 *value = 0.0f;
820 ReadUnlock(&Buffer->lock);
821 break;
823 default:
824 al_throwerr(Context, AL_INVALID_ENUM);
827 al_endtry;
829 ALCcontext_DecRef(Context);
833 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
835 ALCcontext *Context;
837 Context = GetContextRef();
838 if(!Context) return;
840 al_try
842 ALCdevice *device = Context->Device;
843 if(LookupBuffer(device, buffer) == NULL)
844 al_throwerr(Context, AL_INVALID_NAME);
846 CHECK_VALUE(Context, value1 && value2 && value3);
847 switch(param)
849 default:
850 al_throwerr(Context, AL_INVALID_ENUM);
853 al_endtry;
855 ALCcontext_DecRef(Context);
859 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
861 ALCcontext *Context;
863 switch(param)
865 case AL_SEC_LENGTH_SOFT:
866 alGetBufferf(buffer, param, values);
867 return;
870 Context = GetContextRef();
871 if(!Context) return;
873 al_try
875 ALCdevice *device = Context->Device;
876 if(LookupBuffer(device, buffer) == NULL)
877 al_throwerr(Context, AL_INVALID_NAME);
879 CHECK_VALUE(Context, values);
880 switch(param)
882 default:
883 al_throwerr(Context, AL_INVALID_ENUM);
886 al_endtry;
888 ALCcontext_DecRef(Context);
892 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
894 ALCcontext *Context;
895 ALbuffer *Buffer;
897 Context = GetContextRef();
898 if(!Context) return;
900 al_try
902 ALCdevice *device = Context->Device;
903 if((Buffer=LookupBuffer(device, buffer)) == NULL)
904 al_throwerr(Context, AL_INVALID_NAME);
906 CHECK_VALUE(Context, value);
907 switch(param)
909 case AL_FREQUENCY:
910 *value = Buffer->Frequency;
911 break;
913 case AL_BITS:
914 *value = BytesFromFmt(Buffer->FmtType) * 8;
915 break;
917 case AL_CHANNELS:
918 *value = ChannelsFromFmt(Buffer->FmtChannels);
919 break;
921 case AL_SIZE:
922 ReadLock(&Buffer->lock);
923 *value = Buffer->SampleLen * FrameSizeFromFmt(Buffer->FmtChannels,
924 Buffer->FmtType);
925 ReadUnlock(&Buffer->lock);
926 break;
928 case AL_INTERNAL_FORMAT_SOFT:
929 *value = Buffer->Format;
930 break;
932 case AL_BYTE_LENGTH_SOFT:
933 *value = Buffer->OriginalSize;
934 break;
936 case AL_SAMPLE_LENGTH_SOFT:
937 *value = Buffer->SampleLen;
938 break;
940 default:
941 al_throwerr(Context, AL_INVALID_ENUM);
944 al_endtry;
946 ALCcontext_DecRef(Context);
950 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
952 ALCcontext *Context;
954 Context = GetContextRef();
955 if(!Context) return;
957 al_try
959 ALCdevice *device = Context->Device;
960 if(LookupBuffer(device, buffer) == NULL)
961 al_throwerr(Context, AL_INVALID_NAME);
963 CHECK_VALUE(Context, value1 && value2 && value3);
964 switch(param)
966 default:
967 ; al_throwerr(Context, AL_INVALID_ENUM);
970 al_endtry;
972 ALCcontext_DecRef(Context);
976 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
978 ALCcontext *Context;
979 ALbuffer *ALBuf;
981 switch(param)
983 case AL_FREQUENCY:
984 case AL_BITS:
985 case AL_CHANNELS:
986 case AL_SIZE:
987 case AL_INTERNAL_FORMAT_SOFT:
988 case AL_BYTE_LENGTH_SOFT:
989 case AL_SAMPLE_LENGTH_SOFT:
990 alGetBufferi(buffer, param, values);
991 return;
994 Context = GetContextRef();
995 if(!Context) return;
997 al_try
999 ALCdevice *device = Context->Device;
1000 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
1001 al_throwerr(Context, AL_INVALID_NAME);
1003 CHECK_VALUE(Context, values);
1004 switch(param)
1006 case AL_LOOP_POINTS_SOFT:
1007 ReadLock(&ALBuf->lock);
1008 values[0] = ALBuf->LoopStart;
1009 values[1] = ALBuf->LoopEnd;
1010 ReadUnlock(&ALBuf->lock);
1011 break;
1013 default:
1014 al_throwerr(Context, AL_INVALID_ENUM);
1017 al_endtry;
1019 ALCcontext_DecRef(Context);
1023 typedef ALubyte ALmulaw;
1024 typedef ALubyte ALalaw;
1025 typedef ALubyte ALima4;
1026 typedef struct {
1027 ALbyte b[3];
1028 } ALbyte3;
1029 extern ALbyte ALbyte3_size_is_not_3[(sizeof(ALbyte3)==sizeof(ALbyte[3]))?1:-1];
1030 typedef struct {
1031 ALubyte b[3];
1032 } ALubyte3;
1033 extern ALbyte ALubyte3_size_is_not_3[(sizeof(ALubyte3)==sizeof(ALubyte[3]))?1:-1];
1035 static __inline ALshort DecodeMuLaw(ALmulaw val)
1036 { return muLawDecompressionTable[val]; }
1038 static ALmulaw EncodeMuLaw(ALshort val)
1040 ALint mant, exp, sign;
1042 sign = (val>>8) & 0x80;
1043 if(sign)
1045 /* -32768 doesn't properly negate on a short; it results in itself.
1046 * So clamp to -32767 */
1047 val = maxi(val, -32767);
1048 val = -val;
1051 val = mini(val, muLawClip);
1052 val += muLawBias;
1054 exp = muLawCompressTable[(val>>7) & 0xff];
1055 mant = (val >> (exp+3)) & 0x0f;
1057 return ~(sign | (exp<<4) | mant);
1060 static __inline ALshort DecodeALaw(ALalaw val)
1061 { return aLawDecompressionTable[val]; }
1063 static ALalaw EncodeALaw(ALshort val)
1065 ALint mant, exp, sign;
1067 sign = ((~val) >> 8) & 0x80;
1068 if(!sign)
1070 val = maxi(val, -32767);
1071 val = -val;
1073 val = mini(val, aLawClip);
1075 if(val >= 256)
1077 exp = aLawCompressTable[(val>>8) & 0x7f];
1078 mant = (val >> (exp+3)) & 0x0f;
1080 else
1082 exp = 0;
1083 mant = val >> 4;
1086 return ((exp<<4) | mant) ^ (sign^0x55);
1089 static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans)
1091 ALint sample[MAXCHANNELS], index[MAXCHANNELS];
1092 ALuint code[MAXCHANNELS];
1093 ALsizei j,k,c;
1095 for(c = 0;c < numchans;c++)
1097 sample[c] = *(src++);
1098 sample[c] |= *(src++) << 8;
1099 sample[c] = (sample[c]^0x8000) - 32768;
1100 index[c] = *(src++);
1101 index[c] |= *(src++) << 8;
1102 index[c] = (index[c]^0x8000) - 32768;
1104 index[c] = clampi(index[c], 0, 88);
1106 dst[c] = sample[c];
1109 j = 1;
1110 while(j < 65)
1112 for(c = 0;c < numchans;c++)
1114 code[c] = *(src++);
1115 code[c] |= *(src++) << 8;
1116 code[c] |= *(src++) << 16;
1117 code[c] |= *(src++) << 24;
1120 for(k = 0;k < 8;k++,j++)
1122 for(c = 0;c < numchans;c++)
1124 int nibble = code[c]&0xf;
1125 code[c] >>= 4;
1127 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
1128 sample[c] = clampi(sample[c], -32768, 32767);
1130 index[c] += IMA4Index_adjust[nibble];
1131 index[c] = clampi(index[c], 0, 88);
1133 dst[j*numchans + c] = sample[c];
1139 static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans)
1141 ALsizei j,k,c;
1143 for(c = 0;c < numchans;c++)
1145 int diff = src[c] - sample[c];
1146 int step = IMAStep_size[index[c]];
1147 int nibble;
1149 nibble = 0;
1150 if(diff < 0)
1152 nibble = 0x8;
1153 diff = -diff;
1156 diff = mini(step*2, diff);
1157 nibble |= (diff*8/step - 1) / 2;
1159 sample[c] += IMA4Codeword[nibble] * step / 8;
1160 sample[c] = clampi(sample[c], -32768, 32767);
1162 index[c] += IMA4Index_adjust[nibble];
1163 index[c] = clampi(index[c], 0, 88);
1165 *(dst++) = sample[c] & 0xff;
1166 *(dst++) = (sample[c]>>8) & 0xff;
1167 *(dst++) = index[c] & 0xff;
1168 *(dst++) = (index[c]>>8) & 0xff;
1171 j = 1;
1172 while(j < 65)
1174 for(c = 0;c < numchans;c++)
1176 for(k = 0;k < 8;k++)
1178 int diff = src[(j+k)*numchans + c] - sample[c];
1179 int step = IMAStep_size[index[c]];
1180 int nibble;
1182 nibble = 0;
1183 if(diff < 0)
1185 nibble = 0x8;
1186 diff = -diff;
1189 diff = mini(step*2, diff);
1190 nibble |= (diff*8/step - 1) / 2;
1192 sample[c] += IMA4Codeword[nibble] * step / 8;
1193 sample[c] = clampi(sample[c], -32768, 32767);
1195 index[c] += IMA4Index_adjust[nibble];
1196 index[c] = clampi(index[c], 0, 88);
1198 if(!(k&1)) *dst = nibble;
1199 else *(dst++) |= nibble<<4;
1202 j += 8;
1207 static __inline ALint DecodeByte3(ALbyte3 val)
1209 if(IS_LITTLE_ENDIAN)
1210 return (val.b[2]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[0]);
1211 return (val.b[0]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[2]);
1214 static __inline ALbyte3 EncodeByte3(ALint val)
1216 if(IS_LITTLE_ENDIAN)
1218 ALbyte3 ret = {{ val, val>>8, val>>16 }};
1219 return ret;
1221 else
1223 ALbyte3 ret = {{ val>>16, val>>8, val }};
1224 return ret;
1228 static __inline ALint DecodeUByte3(ALubyte3 val)
1230 if(IS_LITTLE_ENDIAN)
1231 return (val.b[2]<<16) | (val.b[1]<<8) | (val.b[0]);
1232 return (val.b[0]<<16) | (val.b[1]<<8) | val.b[2];
1235 static __inline ALubyte3 EncodeUByte3(ALint val)
1237 if(IS_LITTLE_ENDIAN)
1239 ALubyte3 ret = {{ val, val>>8, val>>16 }};
1240 return ret;
1242 else
1244 ALubyte3 ret = {{ val>>16, val>>8, val }};
1245 return ret;
1250 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
1251 { return val; }
1252 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
1253 { return val-128; }
1254 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
1255 { return val>>8; }
1256 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
1257 { return (val>>8)-128; }
1258 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
1259 { return val>>24; }
1260 static __inline ALbyte Conv_ALbyte_ALuint(ALuint val)
1261 { return (val>>24)-128; }
1262 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
1264 if(val > 1.0f) return 127;
1265 if(val < -1.0f) return -128;
1266 return (ALint)(val * 127.0f);
1268 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
1270 if(val > 1.0) return 127;
1271 if(val < -1.0) return -128;
1272 return (ALint)(val * 127.0);
1274 static __inline ALbyte Conv_ALbyte_ALmulaw(ALmulaw val)
1275 { return Conv_ALbyte_ALshort(DecodeMuLaw(val)); }
1276 static __inline ALbyte Conv_ALbyte_ALalaw(ALalaw val)
1277 { return Conv_ALbyte_ALshort(DecodeALaw(val)); }
1278 static __inline ALbyte Conv_ALbyte_ALbyte3(ALbyte3 val)
1279 { return DecodeByte3(val)>>16; }
1280 static __inline ALbyte Conv_ALbyte_ALubyte3(ALubyte3 val)
1281 { return (DecodeUByte3(val)>>16)-128; }
1283 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
1284 { return val+128; }
1285 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
1286 { return val; }
1287 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
1288 { return (val>>8)+128; }
1289 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
1290 { return val>>8; }
1291 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
1292 { return (val>>24)+128; }
1293 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
1294 { return val>>24; }
1295 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
1297 if(val > 1.0f) return 255;
1298 if(val < -1.0f) return 0;
1299 return (ALint)(val * 127.0f) + 128;
1301 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
1303 if(val > 1.0) return 255;
1304 if(val < -1.0) return 0;
1305 return (ALint)(val * 127.0) + 128;
1307 static __inline ALubyte Conv_ALubyte_ALmulaw(ALmulaw val)
1308 { return Conv_ALubyte_ALshort(DecodeMuLaw(val)); }
1309 static __inline ALubyte Conv_ALubyte_ALalaw(ALalaw val)
1310 { return Conv_ALubyte_ALshort(DecodeALaw(val)); }
1311 static __inline ALubyte Conv_ALubyte_ALbyte3(ALbyte3 val)
1312 { return (DecodeByte3(val)>>16)+128; }
1313 static __inline ALubyte Conv_ALubyte_ALubyte3(ALubyte3 val)
1314 { return DecodeUByte3(val)>>16; }
1316 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
1317 { return val<<8; }
1318 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
1319 { return (val-128)<<8; }
1320 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
1321 { return val; }
1322 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
1323 { return val-32768; }
1324 static __inline ALshort Conv_ALshort_ALint(ALint val)
1325 { return val>>16; }
1326 static __inline ALshort Conv_ALshort_ALuint(ALuint val)
1327 { return (val>>16)-32768; }
1328 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
1330 if(val > 1.0f) return 32767;
1331 if(val < -1.0f) return -32768;
1332 return (ALint)(val * 32767.0f);
1334 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
1336 if(val > 1.0) return 32767;
1337 if(val < -1.0) return -32768;
1338 return (ALint)(val * 32767.0);
1340 static __inline ALshort Conv_ALshort_ALmulaw(ALmulaw val)
1341 { return Conv_ALshort_ALshort(DecodeMuLaw(val)); }
1342 static __inline ALshort Conv_ALshort_ALalaw(ALalaw val)
1343 { return Conv_ALshort_ALshort(DecodeALaw(val)); }
1344 static __inline ALshort Conv_ALshort_ALbyte3(ALbyte3 val)
1345 { return DecodeByte3(val)>>8; }
1346 static __inline ALshort Conv_ALshort_ALubyte3(ALubyte3 val)
1347 { return (DecodeUByte3(val)>>8)-32768; }
1349 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
1350 { return (val+128)<<8; }
1351 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
1352 { return val<<8; }
1353 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
1354 { return val+32768; }
1355 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
1356 { return val; }
1357 static __inline ALushort Conv_ALushort_ALint(ALint val)
1358 { return (val>>16)+32768; }
1359 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
1360 { return val>>16; }
1361 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
1363 if(val > 1.0f) return 65535;
1364 if(val < -1.0f) return 0;
1365 return (ALint)(val * 32767.0f) + 32768;
1367 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
1369 if(val > 1.0) return 65535;
1370 if(val < -1.0) return 0;
1371 return (ALint)(val * 32767.0) + 32768;
1373 static __inline ALushort Conv_ALushort_ALmulaw(ALmulaw val)
1374 { return Conv_ALushort_ALshort(DecodeMuLaw(val)); }
1375 static __inline ALushort Conv_ALushort_ALalaw(ALalaw val)
1376 { return Conv_ALushort_ALshort(DecodeALaw(val)); }
1377 static __inline ALushort Conv_ALushort_ALbyte3(ALbyte3 val)
1378 { return (DecodeByte3(val)>>8)+32768; }
1379 static __inline ALushort Conv_ALushort_ALubyte3(ALubyte3 val)
1380 { return DecodeUByte3(val)>>8; }
1382 static __inline ALint Conv_ALint_ALbyte(ALbyte val)
1383 { return val<<24; }
1384 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
1385 { return (val-128)<<24; }
1386 static __inline ALint Conv_ALint_ALshort(ALshort val)
1387 { return val<<16; }
1388 static __inline ALint Conv_ALint_ALushort(ALushort val)
1389 { return (val-32768)<<16; }
1390 static __inline ALint Conv_ALint_ALint(ALint val)
1391 { return val; }
1392 static __inline ALint Conv_ALint_ALuint(ALuint val)
1393 { return val-2147483648u; }
1394 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
1396 if(val > 1.0f) return 2147483647;
1397 if(val < -1.0f) return -2147483647-1;
1398 return (ALint)(val * 2147483647.0);
1400 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
1402 if(val > 1.0) return 2147483647;
1403 if(val < -1.0) return -2147483647-1;
1404 return (ALint)(val * 2147483647.0);
1406 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
1407 { return Conv_ALint_ALshort(DecodeMuLaw(val)); }
1408 static __inline ALint Conv_ALint_ALalaw(ALalaw val)
1409 { return Conv_ALint_ALshort(DecodeALaw(val)); }
1410 static __inline ALint Conv_ALint_ALbyte3(ALbyte3 val)
1411 { return DecodeByte3(val)<<8; }
1412 static __inline ALint Conv_ALint_ALubyte3(ALubyte3 val)
1413 { return (DecodeUByte3(val)-8388608)<<8; }
1415 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
1416 { return (val+128)<<24; }
1417 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
1418 { return val<<24; }
1419 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
1420 { return (val+32768)<<16; }
1421 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
1422 { return val<<16; }
1423 static __inline ALuint Conv_ALuint_ALint(ALint val)
1424 { return val+2147483648u; }
1425 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
1426 { return val; }
1427 static __inline ALuint Conv_ALuint_ALfloat(ALfloat val)
1429 if(val > 1.0f) return 4294967295u;
1430 if(val < -1.0f) return 0;
1431 return (ALint)(val * 2147483647.0) + 2147483648u;
1433 static __inline ALuint Conv_ALuint_ALdouble(ALdouble val)
1435 if(val > 1.0) return 4294967295u;
1436 if(val < -1.0) return 0;
1437 return (ALint)(val * 2147483647.0) + 2147483648u;
1439 static __inline ALuint Conv_ALuint_ALmulaw(ALmulaw val)
1440 { return Conv_ALuint_ALshort(DecodeMuLaw(val)); }
1441 static __inline ALuint Conv_ALuint_ALalaw(ALalaw val)
1442 { return Conv_ALuint_ALshort(DecodeALaw(val)); }
1443 static __inline ALuint Conv_ALuint_ALbyte3(ALbyte3 val)
1444 { return (DecodeByte3(val)+8388608)<<8; }
1445 static __inline ALuint Conv_ALuint_ALubyte3(ALubyte3 val)
1446 { return DecodeUByte3(val)<<8; }
1448 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
1449 { return val * (1.0f/127.0f); }
1450 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
1451 { return (val-128) * (1.0f/127.0f); }
1452 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
1453 { return val * (1.0f/32767.0f); }
1454 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
1455 { return (val-32768) * (1.0f/32767.0f); }
1456 static __inline ALfloat Conv_ALfloat_ALint(ALint val)
1457 { return (ALfloat)(val * (1.0/2147483647.0)); }
1458 static __inline ALfloat Conv_ALfloat_ALuint(ALuint val)
1459 { return (ALfloat)((ALint)(val-2147483648u) * (1.0/2147483647.0)); }
1460 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
1461 { return (val==val) ? val : 0.0f; }
1462 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1463 { return (val==val) ? (ALfloat)val : 0.0f; }
1464 static __inline ALfloat Conv_ALfloat_ALmulaw(ALmulaw val)
1465 { return Conv_ALfloat_ALshort(DecodeMuLaw(val)); }
1466 static __inline ALfloat Conv_ALfloat_ALalaw(ALalaw val)
1467 { return Conv_ALfloat_ALshort(DecodeALaw(val)); }
1468 static __inline ALfloat Conv_ALfloat_ALbyte3(ALbyte3 val)
1469 { return (ALfloat)(DecodeByte3(val) * (1.0/8388607.0)); }
1470 static __inline ALfloat Conv_ALfloat_ALubyte3(ALubyte3 val)
1471 { return (ALfloat)((DecodeUByte3(val)-8388608) * (1.0/8388607.0)); }
1473 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
1474 { return val * (1.0/127.0); }
1475 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
1476 { return (val-128) * (1.0/127.0); }
1477 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
1478 { return val * (1.0/32767.0); }
1479 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
1480 { return (val-32768) * (1.0/32767.0); }
1481 static __inline ALdouble Conv_ALdouble_ALint(ALint val)
1482 { return val * (1.0/2147483647.0); }
1483 static __inline ALdouble Conv_ALdouble_ALuint(ALuint val)
1484 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1485 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
1486 { return (val==val) ? val : 0.0f; }
1487 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1488 { return (val==val) ? val : 0.0; }
1489 static __inline ALdouble Conv_ALdouble_ALmulaw(ALmulaw val)
1490 { return Conv_ALdouble_ALshort(DecodeMuLaw(val)); }
1491 static __inline ALdouble Conv_ALdouble_ALalaw(ALalaw val)
1492 { return Conv_ALdouble_ALshort(DecodeALaw(val)); }
1493 static __inline ALdouble Conv_ALdouble_ALbyte3(ALbyte3 val)
1494 { return DecodeByte3(val) * (1.0/8388607.0); }
1495 static __inline ALdouble Conv_ALdouble_ALubyte3(ALubyte3 val)
1496 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
1498 #define DECL_TEMPLATE(T) \
1499 static __inline ALmulaw Conv_ALmulaw_##T(T val) \
1500 { return EncodeMuLaw(Conv_ALshort_##T(val)); }
1502 DECL_TEMPLATE(ALbyte)
1503 DECL_TEMPLATE(ALubyte)
1504 DECL_TEMPLATE(ALshort)
1505 DECL_TEMPLATE(ALushort)
1506 DECL_TEMPLATE(ALint)
1507 DECL_TEMPLATE(ALuint)
1508 DECL_TEMPLATE(ALfloat)
1509 DECL_TEMPLATE(ALdouble)
1510 static __inline ALmulaw Conv_ALmulaw_ALmulaw(ALmulaw val)
1511 { return val; }
1512 DECL_TEMPLATE(ALalaw)
1513 DECL_TEMPLATE(ALbyte3)
1514 DECL_TEMPLATE(ALubyte3)
1516 #undef DECL_TEMPLATE
1518 #define DECL_TEMPLATE(T) \
1519 static __inline ALalaw Conv_ALalaw_##T(T val) \
1520 { return EncodeALaw(Conv_ALshort_##T(val)); }
1522 DECL_TEMPLATE(ALbyte)
1523 DECL_TEMPLATE(ALubyte)
1524 DECL_TEMPLATE(ALshort)
1525 DECL_TEMPLATE(ALushort)
1526 DECL_TEMPLATE(ALint)
1527 DECL_TEMPLATE(ALuint)
1528 DECL_TEMPLATE(ALfloat)
1529 DECL_TEMPLATE(ALdouble)
1530 DECL_TEMPLATE(ALmulaw)
1531 static __inline ALalaw Conv_ALalaw_ALalaw(ALalaw val)
1532 { return val; }
1533 DECL_TEMPLATE(ALbyte3)
1534 DECL_TEMPLATE(ALubyte3)
1536 #undef DECL_TEMPLATE
1538 #define DECL_TEMPLATE(T) \
1539 static __inline ALbyte3 Conv_ALbyte3_##T(T val) \
1540 { return EncodeByte3(Conv_ALint_##T(val)>>8); }
1542 DECL_TEMPLATE(ALbyte)
1543 DECL_TEMPLATE(ALubyte)
1544 DECL_TEMPLATE(ALshort)
1545 DECL_TEMPLATE(ALushort)
1546 DECL_TEMPLATE(ALint)
1547 DECL_TEMPLATE(ALuint)
1548 DECL_TEMPLATE(ALfloat)
1549 DECL_TEMPLATE(ALdouble)
1550 DECL_TEMPLATE(ALmulaw)
1551 DECL_TEMPLATE(ALalaw)
1552 static __inline ALbyte3 Conv_ALbyte3_ALbyte3(ALbyte3 val)
1553 { return val; }
1554 DECL_TEMPLATE(ALubyte3)
1556 #undef DECL_TEMPLATE
1558 #define DECL_TEMPLATE(T) \
1559 static __inline ALubyte3 Conv_ALubyte3_##T(T val) \
1560 { return EncodeUByte3(Conv_ALuint_##T(val)>>8); }
1562 DECL_TEMPLATE(ALbyte)
1563 DECL_TEMPLATE(ALubyte)
1564 DECL_TEMPLATE(ALshort)
1565 DECL_TEMPLATE(ALushort)
1566 DECL_TEMPLATE(ALint)
1567 DECL_TEMPLATE(ALuint)
1568 DECL_TEMPLATE(ALfloat)
1569 DECL_TEMPLATE(ALdouble)
1570 DECL_TEMPLATE(ALmulaw)
1571 DECL_TEMPLATE(ALalaw)
1572 DECL_TEMPLATE(ALbyte3)
1573 static __inline ALubyte3 Conv_ALubyte3_ALubyte3(ALubyte3 val)
1574 { return val; }
1576 #undef DECL_TEMPLATE
1579 #define DECL_TEMPLATE(T1, T2) \
1580 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
1581 ALuint len) \
1583 ALuint i, j; \
1584 for(i = 0;i < len;i++) \
1586 for(j = 0;j < numchans;j++) \
1587 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1591 DECL_TEMPLATE(ALbyte, ALbyte)
1592 DECL_TEMPLATE(ALbyte, ALubyte)
1593 DECL_TEMPLATE(ALbyte, ALshort)
1594 DECL_TEMPLATE(ALbyte, ALushort)
1595 DECL_TEMPLATE(ALbyte, ALint)
1596 DECL_TEMPLATE(ALbyte, ALuint)
1597 DECL_TEMPLATE(ALbyte, ALfloat)
1598 DECL_TEMPLATE(ALbyte, ALdouble)
1599 DECL_TEMPLATE(ALbyte, ALmulaw)
1600 DECL_TEMPLATE(ALbyte, ALalaw)
1601 DECL_TEMPLATE(ALbyte, ALbyte3)
1602 DECL_TEMPLATE(ALbyte, ALubyte3)
1604 DECL_TEMPLATE(ALubyte, ALbyte)
1605 DECL_TEMPLATE(ALubyte, ALubyte)
1606 DECL_TEMPLATE(ALubyte, ALshort)
1607 DECL_TEMPLATE(ALubyte, ALushort)
1608 DECL_TEMPLATE(ALubyte, ALint)
1609 DECL_TEMPLATE(ALubyte, ALuint)
1610 DECL_TEMPLATE(ALubyte, ALfloat)
1611 DECL_TEMPLATE(ALubyte, ALdouble)
1612 DECL_TEMPLATE(ALubyte, ALmulaw)
1613 DECL_TEMPLATE(ALubyte, ALalaw)
1614 DECL_TEMPLATE(ALubyte, ALbyte3)
1615 DECL_TEMPLATE(ALubyte, ALubyte3)
1617 DECL_TEMPLATE(ALshort, ALbyte)
1618 DECL_TEMPLATE(ALshort, ALubyte)
1619 DECL_TEMPLATE(ALshort, ALshort)
1620 DECL_TEMPLATE(ALshort, ALushort)
1621 DECL_TEMPLATE(ALshort, ALint)
1622 DECL_TEMPLATE(ALshort, ALuint)
1623 DECL_TEMPLATE(ALshort, ALfloat)
1624 DECL_TEMPLATE(ALshort, ALdouble)
1625 DECL_TEMPLATE(ALshort, ALmulaw)
1626 DECL_TEMPLATE(ALshort, ALalaw)
1627 DECL_TEMPLATE(ALshort, ALbyte3)
1628 DECL_TEMPLATE(ALshort, ALubyte3)
1630 DECL_TEMPLATE(ALushort, ALbyte)
1631 DECL_TEMPLATE(ALushort, ALubyte)
1632 DECL_TEMPLATE(ALushort, ALshort)
1633 DECL_TEMPLATE(ALushort, ALushort)
1634 DECL_TEMPLATE(ALushort, ALint)
1635 DECL_TEMPLATE(ALushort, ALuint)
1636 DECL_TEMPLATE(ALushort, ALfloat)
1637 DECL_TEMPLATE(ALushort, ALdouble)
1638 DECL_TEMPLATE(ALushort, ALmulaw)
1639 DECL_TEMPLATE(ALushort, ALalaw)
1640 DECL_TEMPLATE(ALushort, ALbyte3)
1641 DECL_TEMPLATE(ALushort, ALubyte3)
1643 DECL_TEMPLATE(ALint, ALbyte)
1644 DECL_TEMPLATE(ALint, ALubyte)
1645 DECL_TEMPLATE(ALint, ALshort)
1646 DECL_TEMPLATE(ALint, ALushort)
1647 DECL_TEMPLATE(ALint, ALint)
1648 DECL_TEMPLATE(ALint, ALuint)
1649 DECL_TEMPLATE(ALint, ALfloat)
1650 DECL_TEMPLATE(ALint, ALdouble)
1651 DECL_TEMPLATE(ALint, ALmulaw)
1652 DECL_TEMPLATE(ALint, ALalaw)
1653 DECL_TEMPLATE(ALint, ALbyte3)
1654 DECL_TEMPLATE(ALint, ALubyte3)
1656 DECL_TEMPLATE(ALuint, ALbyte)
1657 DECL_TEMPLATE(ALuint, ALubyte)
1658 DECL_TEMPLATE(ALuint, ALshort)
1659 DECL_TEMPLATE(ALuint, ALushort)
1660 DECL_TEMPLATE(ALuint, ALint)
1661 DECL_TEMPLATE(ALuint, ALuint)
1662 DECL_TEMPLATE(ALuint, ALfloat)
1663 DECL_TEMPLATE(ALuint, ALdouble)
1664 DECL_TEMPLATE(ALuint, ALmulaw)
1665 DECL_TEMPLATE(ALuint, ALalaw)
1666 DECL_TEMPLATE(ALuint, ALbyte3)
1667 DECL_TEMPLATE(ALuint, ALubyte3)
1669 DECL_TEMPLATE(ALfloat, ALbyte)
1670 DECL_TEMPLATE(ALfloat, ALubyte)
1671 DECL_TEMPLATE(ALfloat, ALshort)
1672 DECL_TEMPLATE(ALfloat, ALushort)
1673 DECL_TEMPLATE(ALfloat, ALint)
1674 DECL_TEMPLATE(ALfloat, ALuint)
1675 DECL_TEMPLATE(ALfloat, ALfloat)
1676 DECL_TEMPLATE(ALfloat, ALdouble)
1677 DECL_TEMPLATE(ALfloat, ALmulaw)
1678 DECL_TEMPLATE(ALfloat, ALalaw)
1679 DECL_TEMPLATE(ALfloat, ALbyte3)
1680 DECL_TEMPLATE(ALfloat, ALubyte3)
1682 DECL_TEMPLATE(ALdouble, ALbyte)
1683 DECL_TEMPLATE(ALdouble, ALubyte)
1684 DECL_TEMPLATE(ALdouble, ALshort)
1685 DECL_TEMPLATE(ALdouble, ALushort)
1686 DECL_TEMPLATE(ALdouble, ALint)
1687 DECL_TEMPLATE(ALdouble, ALuint)
1688 DECL_TEMPLATE(ALdouble, ALfloat)
1689 DECL_TEMPLATE(ALdouble, ALdouble)
1690 DECL_TEMPLATE(ALdouble, ALmulaw)
1691 DECL_TEMPLATE(ALdouble, ALalaw)
1692 DECL_TEMPLATE(ALdouble, ALbyte3)
1693 DECL_TEMPLATE(ALdouble, ALubyte3)
1695 DECL_TEMPLATE(ALmulaw, ALbyte)
1696 DECL_TEMPLATE(ALmulaw, ALubyte)
1697 DECL_TEMPLATE(ALmulaw, ALshort)
1698 DECL_TEMPLATE(ALmulaw, ALushort)
1699 DECL_TEMPLATE(ALmulaw, ALint)
1700 DECL_TEMPLATE(ALmulaw, ALuint)
1701 DECL_TEMPLATE(ALmulaw, ALfloat)
1702 DECL_TEMPLATE(ALmulaw, ALdouble)
1703 DECL_TEMPLATE(ALmulaw, ALmulaw)
1704 DECL_TEMPLATE(ALmulaw, ALalaw)
1705 DECL_TEMPLATE(ALmulaw, ALbyte3)
1706 DECL_TEMPLATE(ALmulaw, ALubyte3)
1708 DECL_TEMPLATE(ALalaw, ALbyte)
1709 DECL_TEMPLATE(ALalaw, ALubyte)
1710 DECL_TEMPLATE(ALalaw, ALshort)
1711 DECL_TEMPLATE(ALalaw, ALushort)
1712 DECL_TEMPLATE(ALalaw, ALint)
1713 DECL_TEMPLATE(ALalaw, ALuint)
1714 DECL_TEMPLATE(ALalaw, ALfloat)
1715 DECL_TEMPLATE(ALalaw, ALdouble)
1716 DECL_TEMPLATE(ALalaw, ALmulaw)
1717 DECL_TEMPLATE(ALalaw, ALalaw)
1718 DECL_TEMPLATE(ALalaw, ALbyte3)
1719 DECL_TEMPLATE(ALalaw, ALubyte3)
1721 DECL_TEMPLATE(ALbyte3, ALbyte)
1722 DECL_TEMPLATE(ALbyte3, ALubyte)
1723 DECL_TEMPLATE(ALbyte3, ALshort)
1724 DECL_TEMPLATE(ALbyte3, ALushort)
1725 DECL_TEMPLATE(ALbyte3, ALint)
1726 DECL_TEMPLATE(ALbyte3, ALuint)
1727 DECL_TEMPLATE(ALbyte3, ALfloat)
1728 DECL_TEMPLATE(ALbyte3, ALdouble)
1729 DECL_TEMPLATE(ALbyte3, ALmulaw)
1730 DECL_TEMPLATE(ALbyte3, ALalaw)
1731 DECL_TEMPLATE(ALbyte3, ALbyte3)
1732 DECL_TEMPLATE(ALbyte3, ALubyte3)
1734 DECL_TEMPLATE(ALubyte3, ALbyte)
1735 DECL_TEMPLATE(ALubyte3, ALubyte)
1736 DECL_TEMPLATE(ALubyte3, ALshort)
1737 DECL_TEMPLATE(ALubyte3, ALushort)
1738 DECL_TEMPLATE(ALubyte3, ALint)
1739 DECL_TEMPLATE(ALubyte3, ALuint)
1740 DECL_TEMPLATE(ALubyte3, ALfloat)
1741 DECL_TEMPLATE(ALubyte3, ALdouble)
1742 DECL_TEMPLATE(ALubyte3, ALmulaw)
1743 DECL_TEMPLATE(ALubyte3, ALalaw)
1744 DECL_TEMPLATE(ALubyte3, ALbyte3)
1745 DECL_TEMPLATE(ALubyte3, ALubyte3)
1747 #undef DECL_TEMPLATE
1749 #define DECL_TEMPLATE(T) \
1750 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
1751 ALuint len) \
1753 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1754 ALuint i, j, k; \
1756 i = 0; \
1757 while(i < len) \
1759 DecodeIMA4Block(tmp, src, numchans); \
1760 src += 36*numchans; \
1762 for(j = 0;j < 65 && i < len;j++,i++) \
1764 for(k = 0;k < numchans;k++) \
1765 *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
1770 DECL_TEMPLATE(ALbyte)
1771 DECL_TEMPLATE(ALubyte)
1772 DECL_TEMPLATE(ALshort)
1773 DECL_TEMPLATE(ALushort)
1774 DECL_TEMPLATE(ALint)
1775 DECL_TEMPLATE(ALuint)
1776 DECL_TEMPLATE(ALfloat)
1777 DECL_TEMPLATE(ALdouble)
1778 DECL_TEMPLATE(ALmulaw)
1779 DECL_TEMPLATE(ALalaw)
1780 DECL_TEMPLATE(ALbyte3)
1781 DECL_TEMPLATE(ALubyte3)
1783 #undef DECL_TEMPLATE
1785 #define DECL_TEMPLATE(T) \
1786 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
1787 ALuint len) \
1789 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1790 ALint sample[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1791 ALint index[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1792 ALuint i, j; \
1794 for(i = 0;i < len;i += 65) \
1796 for(j = 0;j < 65*numchans;j++) \
1797 tmp[j] = Conv_ALshort_##T(*(src++)); \
1798 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
1799 dst += 36*numchans; \
1803 DECL_TEMPLATE(ALbyte)
1804 DECL_TEMPLATE(ALubyte)
1805 DECL_TEMPLATE(ALshort)
1806 DECL_TEMPLATE(ALushort)
1807 DECL_TEMPLATE(ALint)
1808 DECL_TEMPLATE(ALuint)
1809 DECL_TEMPLATE(ALfloat)
1810 DECL_TEMPLATE(ALdouble)
1811 DECL_TEMPLATE(ALmulaw)
1812 DECL_TEMPLATE(ALalaw)
1813 static void Convert_ALima4_ALima4(ALima4 *dst, const ALima4 *src,
1814 ALuint numchans, ALuint numblocks)
1815 { memcpy(dst, src, numblocks*36*numchans); }
1816 DECL_TEMPLATE(ALbyte3)
1817 DECL_TEMPLATE(ALubyte3)
1819 #undef DECL_TEMPLATE
1821 #define DECL_TEMPLATE(T) \
1822 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
1823 ALsizei numchans, ALsizei len) \
1825 switch(srcType) \
1827 case UserFmtByte: \
1828 Convert_##T##_ALbyte(dst, src, numchans, len); \
1829 break; \
1830 case UserFmtUByte: \
1831 Convert_##T##_ALubyte(dst, src, numchans, len); \
1832 break; \
1833 case UserFmtShort: \
1834 Convert_##T##_ALshort(dst, src, numchans, len); \
1835 break; \
1836 case UserFmtUShort: \
1837 Convert_##T##_ALushort(dst, src, numchans, len); \
1838 break; \
1839 case UserFmtInt: \
1840 Convert_##T##_ALint(dst, src, numchans, len); \
1841 break; \
1842 case UserFmtUInt: \
1843 Convert_##T##_ALuint(dst, src, numchans, len); \
1844 break; \
1845 case UserFmtFloat: \
1846 Convert_##T##_ALfloat(dst, src, numchans, len); \
1847 break; \
1848 case UserFmtDouble: \
1849 Convert_##T##_ALdouble(dst, src, numchans, len); \
1850 break; \
1851 case UserFmtMulaw: \
1852 Convert_##T##_ALmulaw(dst, src, numchans, len); \
1853 break; \
1854 case UserFmtAlaw: \
1855 Convert_##T##_ALalaw(dst, src, numchans, len); \
1856 break; \
1857 case UserFmtIMA4: \
1858 Convert_##T##_ALima4(dst, src, numchans, len); \
1859 break; \
1860 case UserFmtByte3: \
1861 Convert_##T##_ALbyte3(dst, src, numchans, len); \
1862 break; \
1863 case UserFmtUByte3: \
1864 Convert_##T##_ALubyte3(dst, src, numchans, len); \
1865 break; \
1869 DECL_TEMPLATE(ALbyte)
1870 DECL_TEMPLATE(ALubyte)
1871 DECL_TEMPLATE(ALshort)
1872 DECL_TEMPLATE(ALushort)
1873 DECL_TEMPLATE(ALint)
1874 DECL_TEMPLATE(ALuint)
1875 DECL_TEMPLATE(ALfloat)
1876 DECL_TEMPLATE(ALdouble)
1877 DECL_TEMPLATE(ALmulaw)
1878 DECL_TEMPLATE(ALalaw)
1879 DECL_TEMPLATE(ALima4)
1880 DECL_TEMPLATE(ALbyte3)
1881 DECL_TEMPLATE(ALubyte3)
1883 #undef DECL_TEMPLATE
1886 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len)
1888 switch(dstType)
1890 case UserFmtByte:
1891 Convert_ALbyte(dst, src, srcType, numchans, len);
1892 break;
1893 case UserFmtUByte:
1894 Convert_ALubyte(dst, src, srcType, numchans, len);
1895 break;
1896 case UserFmtShort:
1897 Convert_ALshort(dst, src, srcType, numchans, len);
1898 break;
1899 case UserFmtUShort:
1900 Convert_ALushort(dst, src, srcType, numchans, len);
1901 break;
1902 case UserFmtInt:
1903 Convert_ALint(dst, src, srcType, numchans, len);
1904 break;
1905 case UserFmtUInt:
1906 Convert_ALuint(dst, src, srcType, numchans, len);
1907 break;
1908 case UserFmtFloat:
1909 Convert_ALfloat(dst, src, srcType, numchans, len);
1910 break;
1911 case UserFmtDouble:
1912 Convert_ALdouble(dst, src, srcType, numchans, len);
1913 break;
1914 case UserFmtMulaw:
1915 Convert_ALmulaw(dst, src, srcType, numchans, len);
1916 break;
1917 case UserFmtAlaw:
1918 Convert_ALalaw(dst, src, srcType, numchans, len);
1919 break;
1920 case UserFmtIMA4:
1921 Convert_ALima4(dst, src, srcType, numchans, len);
1922 break;
1923 case UserFmtByte3:
1924 Convert_ALbyte3(dst, src, srcType, numchans, len);
1925 break;
1926 case UserFmtUByte3:
1927 Convert_ALubyte3(dst, src, srcType, numchans, len);
1928 break;
1934 * LoadData
1936 * Loads the specified data into the buffer, using the specified formats.
1937 * Currently, the new format must have the same channel configuration as the
1938 * original format.
1940 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels SrcChannels, enum UserFmtType SrcType, const ALvoid *data, ALboolean storesrc)
1942 ALuint NewChannels, NewBytes;
1943 enum FmtChannels DstChannels;
1944 enum FmtType DstType;
1945 ALuint64 newsize;
1946 ALvoid *temp;
1948 if(DecomposeFormat(NewFormat, &DstChannels, &DstType) == AL_FALSE ||
1949 (long)SrcChannels != (long)DstChannels)
1950 return AL_INVALID_ENUM;
1952 NewChannels = ChannelsFromFmt(DstChannels);
1953 NewBytes = BytesFromFmt(DstType);
1955 newsize = frames;
1956 newsize *= NewBytes;
1957 newsize *= NewChannels;
1958 if(newsize > INT_MAX)
1959 return AL_OUT_OF_MEMORY;
1961 WriteLock(&ALBuf->lock);
1962 if(ALBuf->ref != 0)
1964 WriteUnlock(&ALBuf->lock);
1965 return AL_INVALID_OPERATION;
1968 temp = realloc(ALBuf->data, (size_t)newsize);
1969 if(!temp && newsize)
1971 WriteUnlock(&ALBuf->lock);
1972 return AL_OUT_OF_MEMORY;
1974 ALBuf->data = temp;
1976 if(data != NULL)
1977 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
1979 if(storesrc)
1981 ALBuf->OriginalChannels = SrcChannels;
1982 ALBuf->OriginalType = SrcType;
1983 if(SrcType == UserFmtIMA4)
1984 ALBuf->OriginalSize = frames / 65 * 36 * ChannelsFromUserFmt(SrcChannels);
1985 else
1986 ALBuf->OriginalSize = frames * FrameSizeFromUserFmt(SrcChannels, SrcType);
1988 else
1990 ALBuf->OriginalChannels = DstChannels;
1991 ALBuf->OriginalType = DstType;
1992 ALBuf->OriginalSize = frames * NewBytes * NewChannels;
1995 ALBuf->Frequency = freq;
1996 ALBuf->FmtChannels = DstChannels;
1997 ALBuf->FmtType = DstType;
1998 ALBuf->Format = NewFormat;
2000 ALBuf->SampleLen = frames;
2001 ALBuf->LoopStart = 0;
2002 ALBuf->LoopEnd = ALBuf->SampleLen;
2004 WriteUnlock(&ALBuf->lock);
2005 return AL_NO_ERROR;
2009 ALuint BytesFromUserFmt(enum UserFmtType type)
2011 switch(type)
2013 case UserFmtByte: return sizeof(ALbyte);
2014 case UserFmtUByte: return sizeof(ALubyte);
2015 case UserFmtShort: return sizeof(ALshort);
2016 case UserFmtUShort: return sizeof(ALushort);
2017 case UserFmtInt: return sizeof(ALint);
2018 case UserFmtUInt: return sizeof(ALuint);
2019 case UserFmtFloat: return sizeof(ALfloat);
2020 case UserFmtDouble: return sizeof(ALdouble);
2021 case UserFmtByte3: return sizeof(ALbyte3);
2022 case UserFmtUByte3: return sizeof(ALubyte3);
2023 case UserFmtMulaw: return sizeof(ALubyte);
2024 case UserFmtAlaw: return sizeof(ALubyte);
2025 case UserFmtIMA4: break; /* not handled here */
2027 return 0;
2029 ALuint ChannelsFromUserFmt(enum UserFmtChannels chans)
2031 switch(chans)
2033 case UserFmtMono: return 1;
2034 case UserFmtStereo: return 2;
2035 case UserFmtRear: return 2;
2036 case UserFmtQuad: return 4;
2037 case UserFmtX51: return 6;
2038 case UserFmtX61: return 7;
2039 case UserFmtX71: return 8;
2041 return 0;
2043 static ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
2044 enum UserFmtType *type)
2046 static const struct {
2047 ALenum format;
2048 enum UserFmtChannels channels;
2049 enum UserFmtType type;
2050 } list[] = {
2051 { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
2052 { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
2053 { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
2054 { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
2055 { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
2056 { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
2057 { AL_FORMAT_MONO_ALAW_EXT, UserFmtMono, UserFmtAlaw },
2059 { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
2060 { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
2061 { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
2062 { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
2063 { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
2064 { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
2065 { AL_FORMAT_STEREO_ALAW_EXT, UserFmtStereo, UserFmtAlaw },
2067 { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
2068 { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
2069 { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
2070 { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
2072 { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
2073 { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
2075 { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
2076 { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
2077 { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
2078 { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
2080 { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
2081 { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
2082 { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
2083 { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
2085 { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
2086 { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
2087 { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
2088 { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
2090 { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
2091 { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
2092 { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
2093 { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
2095 ALuint i;
2097 for(i = 0;i < COUNTOF(list);i++)
2099 if(list[i].format == format)
2101 *chans = list[i].channels;
2102 *type = list[i].type;
2103 return AL_TRUE;
2107 return AL_FALSE;
2110 ALuint BytesFromFmt(enum FmtType type)
2112 switch(type)
2114 case FmtByte: return sizeof(ALbyte);
2115 case FmtShort: return sizeof(ALshort);
2116 case FmtFloat: return sizeof(ALfloat);
2118 return 0;
2120 ALuint ChannelsFromFmt(enum FmtChannels chans)
2122 switch(chans)
2124 case FmtMono: return 1;
2125 case FmtStereo: return 2;
2126 case FmtRear: return 2;
2127 case FmtQuad: return 4;
2128 case FmtX51: return 6;
2129 case FmtX61: return 7;
2130 case FmtX71: return 8;
2132 return 0;
2134 static ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
2136 static const struct {
2137 ALenum format;
2138 enum FmtChannels channels;
2139 enum FmtType type;
2140 } list[] = {
2141 { AL_MONO8_SOFT, FmtMono, FmtByte },
2142 { AL_MONO16_SOFT, FmtMono, FmtShort },
2143 { AL_MONO32F_SOFT, FmtMono, FmtFloat },
2145 { AL_STEREO8_SOFT, FmtStereo, FmtByte },
2146 { AL_STEREO16_SOFT, FmtStereo, FmtShort },
2147 { AL_STEREO32F_SOFT, FmtStereo, FmtFloat },
2149 { AL_REAR8_SOFT, FmtRear, FmtByte },
2150 { AL_REAR16_SOFT, FmtRear, FmtShort },
2151 { AL_REAR32F_SOFT, FmtRear, FmtFloat },
2153 { AL_FORMAT_QUAD8_LOKI, FmtQuad, FmtByte },
2154 { AL_FORMAT_QUAD16_LOKI, FmtQuad, FmtShort },
2156 { AL_QUAD8_SOFT, FmtQuad, FmtByte },
2157 { AL_QUAD16_SOFT, FmtQuad, FmtShort },
2158 { AL_QUAD32F_SOFT, FmtQuad, FmtFloat },
2160 { AL_5POINT1_8_SOFT, FmtX51, FmtByte },
2161 { AL_5POINT1_16_SOFT, FmtX51, FmtShort },
2162 { AL_5POINT1_32F_SOFT, FmtX51, FmtFloat },
2164 { AL_6POINT1_8_SOFT, FmtX61, FmtByte },
2165 { AL_6POINT1_16_SOFT, FmtX61, FmtShort },
2166 { AL_6POINT1_32F_SOFT, FmtX61, FmtFloat },
2168 { AL_7POINT1_8_SOFT, FmtX71, FmtByte },
2169 { AL_7POINT1_16_SOFT, FmtX71, FmtShort },
2170 { AL_7POINT1_32F_SOFT, FmtX71, FmtFloat },
2172 ALuint i;
2174 for(i = 0;i < COUNTOF(list);i++)
2176 if(list[i].format == format)
2178 *chans = list[i].channels;
2179 *type = list[i].type;
2180 return AL_TRUE;
2184 return AL_FALSE;
2188 static ALboolean IsValidType(ALenum type)
2190 switch(type)
2192 case AL_BYTE_SOFT:
2193 case AL_UNSIGNED_BYTE_SOFT:
2194 case AL_SHORT_SOFT:
2195 case AL_UNSIGNED_SHORT_SOFT:
2196 case AL_INT_SOFT:
2197 case AL_UNSIGNED_INT_SOFT:
2198 case AL_FLOAT_SOFT:
2199 case AL_DOUBLE_SOFT:
2200 case AL_BYTE3_SOFT:
2201 case AL_UNSIGNED_BYTE3_SOFT:
2202 return AL_TRUE;
2204 return AL_FALSE;
2207 static ALboolean IsValidChannels(ALenum channels)
2209 switch(channels)
2211 case AL_MONO_SOFT:
2212 case AL_STEREO_SOFT:
2213 case AL_REAR_SOFT:
2214 case AL_QUAD_SOFT:
2215 case AL_5POINT1_SOFT:
2216 case AL_6POINT1_SOFT:
2217 case AL_7POINT1_SOFT:
2218 return AL_TRUE;
2220 return AL_FALSE;
2225 * ReleaseALBuffers()
2227 * INTERNAL: Called to destroy any buffers that still exist on the device
2229 ALvoid ReleaseALBuffers(ALCdevice *device)
2231 ALsizei i;
2232 for(i = 0;i < device->BufferMap.size;i++)
2234 ALbuffer *temp = device->BufferMap.array[i].value;
2235 device->BufferMap.array[i].value = NULL;
2237 free(temp->data);
2239 FreeThunkEntry(temp->id);
2240 memset(temp, 0, sizeof(ALbuffer));
2241 free(temp);