Release 1.15.1
[openal-soft.git] / OpenAL32 / alBuffer.c
blob29f3e61747591a76002ce1ecd4e5e68369568e7b
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 "alu.h"
30 #include "alError.h"
31 #include "alBuffer.h"
32 #include "alThunk.h"
35 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels chans, enum UserFmtType type, const ALvoid *data, ALboolean storesrc);
36 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len);
37 static ALboolean IsValidType(ALenum type);
38 static ALboolean IsValidChannels(ALenum channels);
39 static ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans, enum UserFmtType *type);
40 static ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type);
44 * Global Variables
47 /* IMA ADPCM Stepsize table */
48 static const int IMAStep_size[89] = {
49 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
50 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
51 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
52 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
53 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
54 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
55 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
56 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
57 32767
60 /* IMA4 ADPCM Codeword decode table */
61 static const int IMA4Codeword[16] = {
62 1, 3, 5, 7, 9, 11, 13, 15,
63 -1,-3,-5,-7,-9,-11,-13,-15,
66 /* IMA4 ADPCM Step index adjust decode table */
67 static const int IMA4Index_adjust[16] = {
68 -1,-1,-1,-1, 2, 4, 6, 8,
69 -1,-1,-1,-1, 2, 4, 6, 8
72 /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
73 * signed 16-bit sample */
74 static const ALshort muLawDecompressionTable[256] = {
75 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
76 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
77 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
78 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
79 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
80 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
81 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
82 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
83 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
84 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
85 -876, -844, -812, -780, -748, -716, -684, -652,
86 -620, -588, -556, -524, -492, -460, -428, -396,
87 -372, -356, -340, -324, -308, -292, -276, -260,
88 -244, -228, -212, -196, -180, -164, -148, -132,
89 -120, -112, -104, -96, -88, -80, -72, -64,
90 -56, -48, -40, -32, -24, -16, -8, 0,
91 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
92 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
93 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
94 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
95 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
96 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
97 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
98 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
99 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
100 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
101 876, 844, 812, 780, 748, 716, 684, 652,
102 620, 588, 556, 524, 492, 460, 428, 396,
103 372, 356, 340, 324, 308, 292, 276, 260,
104 244, 228, 212, 196, 180, 164, 148, 132,
105 120, 112, 104, 96, 88, 80, 72, 64,
106 56, 48, 40, 32, 24, 16, 8, 0
109 /* Values used when encoding a muLaw sample */
110 static const int muLawBias = 0x84;
111 static const int muLawClip = 32635;
112 static const char muLawCompressTable[256] = {
113 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
114 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
115 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
116 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
117 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
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 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
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
132 /* A quick'n'dirty lookup table to decode an aLaw-encoded byte sample into a
133 * signed 16-bit sample */
134 static const ALshort aLawDecompressionTable[256] = {
135 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
136 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
137 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
138 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
139 -22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,
140 -30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,
141 -11008,-10496,-12032,-11520, -8960, -8448, -9984, -9472,
142 -15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568,
143 -344, -328, -376, -360, -280, -264, -312, -296,
144 -472, -456, -504, -488, -408, -392, -440, -424,
145 -88, -72, -120, -104, -24, -8, -56, -40,
146 -216, -200, -248, -232, -152, -136, -184, -168,
147 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
148 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
149 -688, -656, -752, -720, -560, -528, -624, -592,
150 -944, -912, -1008, -976, -816, -784, -880, -848,
151 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
152 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
153 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
154 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
155 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
156 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
157 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
158 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
159 344, 328, 376, 360, 280, 264, 312, 296,
160 472, 456, 504, 488, 408, 392, 440, 424,
161 88, 72, 120, 104, 24, 8, 56, 40,
162 216, 200, 248, 232, 152, 136, 184, 168,
163 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
164 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
165 688, 656, 752, 720, 560, 528, 624, 592,
166 944, 912, 1008, 976, 816, 784, 880, 848
169 /* Values used when encoding an aLaw sample */
170 static const int aLawClip = 32635;
171 static const char aLawCompressTable[128] = {
172 1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
173 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
174 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
175 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
176 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
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
183 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
185 ALCcontext *Context;
186 ALsizei cur = 0;
188 Context = GetContextRef();
189 if(!Context) return;
191 al_try
193 ALCdevice *device = Context->Device;
194 ALenum err;
196 CHECK_VALUE(Context, n >= 0);
197 for(cur = 0;cur < n;cur++)
199 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
200 if(!buffer)
201 al_throwerr(Context, AL_OUT_OF_MEMORY);
202 RWLockInit(&buffer->lock);
204 err = NewThunkEntry(&buffer->id);
205 if(err == AL_NO_ERROR)
206 err = InsertUIntMapEntry(&device->BufferMap, buffer->id, buffer);
207 if(err != AL_NO_ERROR)
209 FreeThunkEntry(buffer->id);
210 memset(buffer, 0, sizeof(ALbuffer));
211 free(buffer);
213 al_throwerr(Context, err);
216 buffers[cur] = buffer->id;
219 al_catchany()
221 if(cur > 0)
222 alDeleteBuffers(cur, buffers);
224 al_endtry;
226 ALCcontext_DecRef(Context);
229 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
231 ALCcontext *Context;
232 ALbuffer *ALBuf;
233 ALsizei i;
235 Context = GetContextRef();
236 if(!Context) return;
238 al_try
240 ALCdevice *device = Context->Device;
242 CHECK_VALUE(Context, n >= 0);
243 for(i = 0;i < n;i++)
245 if(!buffers[i])
246 continue;
248 /* Check for valid Buffer ID */
249 if((ALBuf=LookupBuffer(device, buffers[i])) == NULL)
250 al_throwerr(Context, AL_INVALID_NAME);
251 if(ALBuf->ref != 0)
252 al_throwerr(Context, AL_INVALID_OPERATION);
255 for(i = 0;i < n;i++)
257 if((ALBuf=RemoveBuffer(device, buffers[i])) == NULL)
258 continue;
259 FreeThunkEntry(ALBuf->id);
261 free(ALBuf->data);
263 memset(ALBuf, 0, sizeof(*ALBuf));
264 free(ALBuf);
267 al_endtry;
269 ALCcontext_DecRef(Context);
272 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
274 ALCcontext *Context;
275 ALboolean result;
277 Context = GetContextRef();
278 if(!Context) return AL_FALSE;
280 result = ((!buffer || LookupBuffer(Context->Device, buffer)) ?
281 AL_TRUE : AL_FALSE);
283 ALCcontext_DecRef(Context);
285 return result;
289 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
291 enum UserFmtChannels SrcChannels;
292 enum UserFmtType SrcType;
293 ALCcontext *Context;
294 ALuint FrameSize;
295 ALenum NewFormat;
296 ALbuffer *ALBuf;
297 ALenum err;
299 Context = GetContextRef();
300 if(!Context) return;
302 al_try
304 ALCdevice *device = Context->Device;
305 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
306 al_throwerr(Context, AL_INVALID_NAME);
307 CHECK_VALUE(Context, size >= 0 && freq >= 0);
308 if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
309 al_throwerr(Context, AL_INVALID_ENUM);
310 switch(SrcType)
312 case UserFmtByte:
313 case UserFmtUByte:
314 case UserFmtShort:
315 case UserFmtUShort:
316 case UserFmtFloat:
317 FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
318 CHECK_VALUE(Context, (size%FrameSize) == 0);
320 err = LoadData(ALBuf, freq, format, size/FrameSize,
321 SrcChannels, SrcType, data, AL_TRUE);
322 if(err != AL_NO_ERROR)
323 al_throwerr(Context, err);
324 break;
326 case UserFmtInt:
327 case UserFmtUInt:
328 case UserFmtByte3:
329 case UserFmtUByte3:
330 case UserFmtDouble:
331 FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
332 CHECK_VALUE(Context, (size%FrameSize) == 0);
334 NewFormat = AL_FORMAT_MONO_FLOAT32;
335 switch(SrcChannels)
337 case UserFmtMono: NewFormat = AL_FORMAT_MONO_FLOAT32; break;
338 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO_FLOAT32; break;
339 case UserFmtRear: NewFormat = AL_FORMAT_REAR32; break;
340 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD32; break;
341 case UserFmtX51: NewFormat = AL_FORMAT_51CHN32; break;
342 case UserFmtX61: NewFormat = AL_FORMAT_61CHN32; break;
343 case UserFmtX71: NewFormat = AL_FORMAT_71CHN32; break;
345 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
346 SrcChannels, SrcType, data, AL_TRUE);
347 if(err != AL_NO_ERROR)
348 al_throwerr(Context, err);
349 break;
351 case UserFmtMulaw:
352 case UserFmtAlaw:
353 FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
354 CHECK_VALUE(Context, (size%FrameSize) == 0);
356 NewFormat = AL_FORMAT_MONO16;
357 switch(SrcChannels)
359 case UserFmtMono: NewFormat = AL_FORMAT_MONO16; break;
360 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
361 case UserFmtRear: NewFormat = AL_FORMAT_REAR16; break;
362 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
363 case UserFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
364 case UserFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
365 case UserFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
367 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
368 SrcChannels, SrcType, data, AL_TRUE);
369 if(err != AL_NO_ERROR)
370 al_throwerr(Context, err);
371 break;
373 case UserFmtIMA4:
374 /* Here is where things vary:
375 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
376 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
378 FrameSize = ChannelsFromUserFmt(SrcChannels) * 36;
379 CHECK_VALUE(Context, (size%FrameSize) == 0);
381 NewFormat = AL_FORMAT_MONO16;
382 switch(SrcChannels)
384 case UserFmtMono: NewFormat = AL_FORMAT_MONO16; break;
385 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
386 case UserFmtRear: NewFormat = AL_FORMAT_REAR16; break;
387 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
388 case UserFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
389 case UserFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
390 case UserFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
392 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize*65,
393 SrcChannels, SrcType, data, AL_TRUE);
394 if(err != AL_NO_ERROR)
395 al_throwerr(Context, err);
396 break;
399 al_endtry;
401 ALCcontext_DecRef(Context);
404 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
406 enum UserFmtChannels SrcChannels;
407 enum UserFmtType SrcType;
408 ALCcontext *Context;
409 ALbuffer *ALBuf;
411 Context = GetContextRef();
412 if(!Context) return;
414 al_try
416 ALCdevice *device = Context->Device;
417 ALuint original_align;
418 ALuint Channels;
419 ALuint Bytes;
421 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
422 al_throwerr(Context, AL_INVALID_NAME);
423 CHECK_VALUE(Context, length >= 0 && offset >= 0);
424 if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
425 al_throwerr(Context, AL_INVALID_ENUM);
427 WriteLock(&ALBuf->lock);
428 original_align = ((ALBuf->OriginalType == UserFmtIMA4) ?
429 (ChannelsFromUserFmt(ALBuf->OriginalChannels)*36) :
430 FrameSizeFromUserFmt(ALBuf->OriginalChannels,
431 ALBuf->OriginalType));
433 if(SrcChannels != ALBuf->OriginalChannels || SrcType != ALBuf->OriginalType)
435 WriteUnlock(&ALBuf->lock);
436 al_throwerr(Context, AL_INVALID_ENUM);
438 if(offset > ALBuf->OriginalSize || length > ALBuf->OriginalSize-offset ||
439 (offset%original_align) != 0 || (length%original_align) != 0)
441 WriteUnlock(&ALBuf->lock);
442 al_throwerr(Context, AL_INVALID_VALUE);
445 Channels = ChannelsFromFmt(ALBuf->FmtChannels);
446 Bytes = BytesFromFmt(ALBuf->FmtType);
447 /* offset -> byte offset, length -> sample count */
448 if(SrcType == UserFmtIMA4)
450 offset = offset/36*65 * Bytes;
451 length = length/original_align * 65;
453 else
455 ALuint OldBytes = BytesFromUserFmt(SrcType);
456 offset = offset/OldBytes * Bytes;
457 length = length/OldBytes/Channels;
459 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
460 data, SrcType, Channels, length);
461 WriteUnlock(&ALBuf->lock);
463 al_endtry;
465 ALCcontext_DecRef(Context);
469 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer,
470 ALuint samplerate, ALenum internalformat, ALsizei samples,
471 ALenum channels, ALenum type, const ALvoid *data)
473 ALCcontext *Context;
474 ALbuffer *ALBuf;
475 ALenum err;
477 Context = GetContextRef();
478 if(!Context) return;
480 al_try
482 ALCdevice *device = Context->Device;
483 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
484 al_throwerr(Context, AL_INVALID_NAME);
485 CHECK_VALUE(Context, samples >= 0 && samplerate != 0);
486 if(IsValidType(type) == AL_FALSE || IsValidChannels(channels) == AL_FALSE)
487 al_throwerr(Context, AL_INVALID_ENUM);
489 err = LoadData(ALBuf, samplerate, internalformat, samples,
490 channels, type, data, AL_FALSE);
491 if(err != AL_NO_ERROR)
492 al_throwerr(Context, err);
494 al_endtry;
496 ALCcontext_DecRef(Context);
499 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint buffer,
500 ALsizei offset, ALsizei samples,
501 ALenum channels, ALenum type, const ALvoid *data)
503 ALCcontext *Context;
504 ALbuffer *ALBuf;
506 Context = GetContextRef();
507 if(!Context) return;
509 al_try
511 ALCdevice *device = Context->Device;
512 ALuint FrameSize;
514 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
515 al_throwerr(Context, AL_INVALID_NAME);
516 CHECK_VALUE(Context, samples >= 0 && offset >= 0);
517 if(IsValidType(type) == AL_FALSE)
518 al_throwerr(Context, AL_INVALID_ENUM);
520 WriteLock(&ALBuf->lock);
521 FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
522 if(channels != (ALenum)ALBuf->FmtChannels)
524 WriteUnlock(&ALBuf->lock);
525 al_throwerr(Context, AL_INVALID_ENUM);
527 else if(offset > ALBuf->SampleLen || samples > ALBuf->SampleLen-offset)
529 WriteUnlock(&ALBuf->lock);
530 al_throwerr(Context,AL_INVALID_VALUE);
533 /* offset -> byte offset */
534 offset *= FrameSize;
535 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
536 data, type, ChannelsFromFmt(ALBuf->FmtChannels), samples);
537 WriteUnlock(&ALBuf->lock);
539 al_endtry;
541 ALCcontext_DecRef(Context);
544 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer,
545 ALsizei offset, ALsizei samples,
546 ALenum channels, ALenum type, ALvoid *data)
548 ALCcontext *Context;
549 ALbuffer *ALBuf;
551 Context = GetContextRef();
552 if(!Context) return;
554 al_try
556 ALCdevice *device = Context->Device;
557 ALuint FrameSize;
559 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
560 al_throwerr(Context, AL_INVALID_NAME);
561 CHECK_VALUE(Context, samples >= 0 && offset >= 0);
562 if(IsValidType(type) == AL_FALSE)
563 al_throwerr(Context, AL_INVALID_ENUM);
565 ReadLock(&ALBuf->lock);
566 FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
567 if(channels != (ALenum)ALBuf->FmtChannels)
569 ReadUnlock(&ALBuf->lock);
570 al_throwerr(Context, AL_INVALID_ENUM);
572 if(offset > ALBuf->SampleLen || samples > ALBuf->SampleLen-offset)
574 ReadUnlock(&ALBuf->lock);
575 al_throwerr(Context,AL_INVALID_VALUE);
577 if(type == UserFmtIMA4 && (samples%65) != 0)
579 ReadUnlock(&ALBuf->lock);
580 al_throwerr(Context, AL_INVALID_VALUE);
583 /* offset -> byte offset */
584 offset *= FrameSize;
585 ConvertData(data, type, &((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
586 ChannelsFromFmt(ALBuf->FmtChannels), samples);
587 ReadUnlock(&ALBuf->lock);
589 al_endtry;
591 ALCcontext_DecRef(Context);
594 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format)
596 enum FmtChannels DstChannels;
597 enum FmtType DstType;
598 ALCcontext *Context;
599 ALboolean ret;
601 Context = GetContextRef();
602 if(!Context) return AL_FALSE;
604 ret = DecomposeFormat(format, &DstChannels, &DstType);
606 ALCcontext_DecRef(Context);
608 return ret;
612 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat value)
614 ALCcontext *Context;
616 (void)value;
618 Context = GetContextRef();
619 if(!Context) return;
621 al_try
623 ALCdevice *device = Context->Device;
624 if(LookupBuffer(device, buffer) == NULL)
625 al_throwerr(Context, AL_INVALID_NAME);
627 switch(param)
629 default:
630 al_throwerr(Context, AL_INVALID_ENUM);
633 al_endtry;
635 ALCcontext_DecRef(Context);
639 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3)
641 ALCcontext *Context;
643 (void)value1;
644 (void)value2;
645 (void)value3;
647 Context = GetContextRef();
648 if(!Context) return;
650 al_try
652 ALCdevice *device = Context->Device;
653 if(LookupBuffer(device, buffer) == NULL)
654 al_throwerr(Context, AL_INVALID_NAME);
656 switch(param)
658 default:
659 al_throwerr(Context, AL_INVALID_ENUM);
662 al_endtry;
664 ALCcontext_DecRef(Context);
668 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
670 ALCcontext *Context;
672 Context = GetContextRef();
673 if(!Context) return;
675 al_try
677 ALCdevice *device = Context->Device;
678 if(LookupBuffer(device, buffer) == NULL)
679 al_throwerr(Context, AL_INVALID_NAME);
681 CHECK_VALUE(Context, values);
682 switch(param)
684 default:
685 al_throwerr(Context, AL_INVALID_ENUM);
688 al_endtry;
690 ALCcontext_DecRef(Context);
694 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
696 ALCcontext *Context;
698 (void)value;
700 Context = GetContextRef();
701 if(!Context) return;
703 al_try
705 ALCdevice *device = Context->Device;
706 if(LookupBuffer(device, buffer) == NULL)
707 al_throwerr(Context, AL_INVALID_NAME);
709 switch(param)
711 default:
712 al_throwerr(Context, AL_INVALID_ENUM);
715 al_endtry;
717 ALCcontext_DecRef(Context);
721 AL_API void AL_APIENTRY alBuffer3i(ALuint buffer, ALenum param, ALint value1, ALint value2, ALint value3)
723 ALCcontext *Context;
725 (void)value1;
726 (void)value2;
727 (void)value3;
729 Context = GetContextRef();
730 if(!Context) return;
732 al_try
734 ALCdevice *device = Context->Device;
735 if(LookupBuffer(device, buffer) == NULL)
736 al_throwerr(Context, AL_INVALID_NAME);
738 switch(param)
740 default:
741 al_throwerr(Context, AL_INVALID_ENUM);
744 al_endtry;
746 ALCcontext_DecRef(Context);
750 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
752 ALCcontext *Context;
753 ALbuffer *ALBuf;
755 Context = GetContextRef();
756 if(!Context) return;
758 al_try
760 ALCdevice *device = Context->Device;
761 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
762 al_throwerr(Context, AL_INVALID_NAME);
764 CHECK_VALUE(Context, values);
765 switch(param)
767 case AL_LOOP_POINTS_SOFT:
768 WriteLock(&ALBuf->lock);
769 if(ALBuf->ref != 0)
771 WriteUnlock(&ALBuf->lock);
772 al_throwerr(Context, AL_INVALID_OPERATION);
774 if(values[0] >= values[1] || values[0] < 0 ||
775 values[1] > ALBuf->SampleLen)
777 WriteUnlock(&ALBuf->lock);
778 al_throwerr(Context, AL_INVALID_VALUE);
781 ALBuf->LoopStart = values[0];
782 ALBuf->LoopEnd = values[1];
783 WriteUnlock(&ALBuf->lock);
784 break;
786 default:
787 al_throwerr(Context, AL_INVALID_ENUM);
790 al_endtry;
792 ALCcontext_DecRef(Context);
796 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
798 ALCcontext *Context;
799 ALbuffer *Buffer;
801 Context = GetContextRef();
802 if(!Context) return;
804 al_try
806 ALCdevice *device = Context->Device;
807 if((Buffer=LookupBuffer(device, buffer)) == NULL)
808 al_throwerr(Context, AL_INVALID_NAME);
810 CHECK_VALUE(Context, value);
811 switch(param)
813 case AL_SEC_LENGTH_SOFT:
814 ReadLock(&Buffer->lock);
815 if(Buffer->SampleLen != 0)
816 *value = Buffer->SampleLen / (ALfloat)Buffer->Frequency;
817 else
818 *value = 0.0f;
819 ReadUnlock(&Buffer->lock);
820 break;
822 default:
823 al_throwerr(Context, AL_INVALID_ENUM);
826 al_endtry;
828 ALCcontext_DecRef(Context);
832 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
834 ALCcontext *Context;
836 Context = GetContextRef();
837 if(!Context) return;
839 al_try
841 ALCdevice *device = Context->Device;
842 if(LookupBuffer(device, buffer) == NULL)
843 al_throwerr(Context, AL_INVALID_NAME);
845 CHECK_VALUE(Context, value1 && value2 && value3);
846 switch(param)
848 default:
849 al_throwerr(Context, AL_INVALID_ENUM);
852 al_endtry;
854 ALCcontext_DecRef(Context);
858 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
860 ALCcontext *Context;
862 switch(param)
864 case AL_SEC_LENGTH_SOFT:
865 alGetBufferf(buffer, param, values);
866 return;
869 Context = GetContextRef();
870 if(!Context) return;
872 al_try
874 ALCdevice *device = Context->Device;
875 if(LookupBuffer(device, buffer) == NULL)
876 al_throwerr(Context, AL_INVALID_NAME);
878 CHECK_VALUE(Context, values);
879 switch(param)
881 default:
882 al_throwerr(Context, AL_INVALID_ENUM);
885 al_endtry;
887 ALCcontext_DecRef(Context);
891 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
893 ALCcontext *Context;
894 ALbuffer *Buffer;
896 Context = GetContextRef();
897 if(!Context) return;
899 al_try
901 ALCdevice *device = Context->Device;
902 if((Buffer=LookupBuffer(device, buffer)) == NULL)
903 al_throwerr(Context, AL_INVALID_NAME);
905 CHECK_VALUE(Context, value);
906 switch(param)
908 case AL_FREQUENCY:
909 *value = Buffer->Frequency;
910 break;
912 case AL_BITS:
913 *value = BytesFromFmt(Buffer->FmtType) * 8;
914 break;
916 case AL_CHANNELS:
917 *value = ChannelsFromFmt(Buffer->FmtChannels);
918 break;
920 case AL_SIZE:
921 ReadLock(&Buffer->lock);
922 *value = Buffer->SampleLen * FrameSizeFromFmt(Buffer->FmtChannels,
923 Buffer->FmtType);
924 ReadUnlock(&Buffer->lock);
925 break;
927 case AL_INTERNAL_FORMAT_SOFT:
928 *value = Buffer->Format;
929 break;
931 case AL_BYTE_LENGTH_SOFT:
932 *value = Buffer->OriginalSize;
933 break;
935 case AL_SAMPLE_LENGTH_SOFT:
936 *value = Buffer->SampleLen;
937 break;
939 default:
940 al_throwerr(Context, AL_INVALID_ENUM);
943 al_endtry;
945 ALCcontext_DecRef(Context);
949 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
951 ALCcontext *Context;
953 Context = GetContextRef();
954 if(!Context) return;
956 al_try
958 ALCdevice *device = Context->Device;
959 if(LookupBuffer(device, buffer) == NULL)
960 al_throwerr(Context, AL_INVALID_NAME);
962 CHECK_VALUE(Context, value1 && value2 && value3);
963 switch(param)
965 default:
966 al_throwerr(Context, AL_INVALID_ENUM);
969 al_endtry;
971 ALCcontext_DecRef(Context);
975 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
977 ALCcontext *Context;
978 ALbuffer *ALBuf;
980 switch(param)
982 case AL_FREQUENCY:
983 case AL_BITS:
984 case AL_CHANNELS:
985 case AL_SIZE:
986 case AL_INTERNAL_FORMAT_SOFT:
987 case AL_BYTE_LENGTH_SOFT:
988 case AL_SAMPLE_LENGTH_SOFT:
989 alGetBufferi(buffer, param, values);
990 return;
993 Context = GetContextRef();
994 if(!Context) return;
996 al_try
998 ALCdevice *device = Context->Device;
999 if((ALBuf=LookupBuffer(device, buffer)) == NULL)
1000 al_throwerr(Context, AL_INVALID_NAME);
1002 CHECK_VALUE(Context, values);
1003 switch(param)
1005 case AL_LOOP_POINTS_SOFT:
1006 ReadLock(&ALBuf->lock);
1007 values[0] = ALBuf->LoopStart;
1008 values[1] = ALBuf->LoopEnd;
1009 ReadUnlock(&ALBuf->lock);
1010 break;
1012 default:
1013 al_throwerr(Context, AL_INVALID_ENUM);
1016 al_endtry;
1018 ALCcontext_DecRef(Context);
1022 typedef ALubyte ALmulaw;
1023 typedef ALubyte ALalaw;
1024 typedef ALubyte ALima4;
1025 typedef struct {
1026 ALbyte b[3];
1027 } ALbyte3;
1028 extern ALbyte ALbyte3_size_is_not_3[(sizeof(ALbyte3)==sizeof(ALbyte[3]))?1:-1];
1029 typedef struct {
1030 ALubyte b[3];
1031 } ALubyte3;
1032 extern ALbyte ALubyte3_size_is_not_3[(sizeof(ALubyte3)==sizeof(ALubyte[3]))?1:-1];
1034 static __inline ALshort DecodeMuLaw(ALmulaw val)
1035 { return muLawDecompressionTable[val]; }
1037 static ALmulaw EncodeMuLaw(ALshort val)
1039 ALint mant, exp, sign;
1041 sign = (val>>8) & 0x80;
1042 if(sign)
1044 /* -32768 doesn't properly negate on a short; it results in itself.
1045 * So clamp to -32767 */
1046 val = maxi(val, -32767);
1047 val = -val;
1050 val = mini(val, muLawClip);
1051 val += muLawBias;
1053 exp = muLawCompressTable[(val>>7) & 0xff];
1054 mant = (val >> (exp+3)) & 0x0f;
1056 return ~(sign | (exp<<4) | mant);
1059 static __inline ALshort DecodeALaw(ALalaw val)
1060 { return aLawDecompressionTable[val]; }
1062 static ALalaw EncodeALaw(ALshort val)
1064 ALint mant, exp, sign;
1066 sign = ((~val) >> 8) & 0x80;
1067 if(!sign)
1069 val = maxi(val, -32767);
1070 val = -val;
1072 val = mini(val, aLawClip);
1074 if(val >= 256)
1076 exp = aLawCompressTable[(val>>8) & 0x7f];
1077 mant = (val >> (exp+3)) & 0x0f;
1079 else
1081 exp = 0;
1082 mant = val >> 4;
1085 return ((exp<<4) | mant) ^ (sign^0x55);
1088 static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans)
1090 ALint sample[MaxChannels], index[MaxChannels];
1091 ALuint code[MaxChannels];
1092 ALsizei j,k,c;
1094 for(c = 0;c < numchans;c++)
1096 sample[c] = *(src++);
1097 sample[c] |= *(src++) << 8;
1098 sample[c] = (sample[c]^0x8000) - 32768;
1099 index[c] = *(src++);
1100 index[c] |= *(src++) << 8;
1101 index[c] = (index[c]^0x8000) - 32768;
1103 index[c] = clampi(index[c], 0, 88);
1105 dst[c] = sample[c];
1108 j = 1;
1109 while(j < 65)
1111 for(c = 0;c < numchans;c++)
1113 code[c] = *(src++);
1114 code[c] |= *(src++) << 8;
1115 code[c] |= *(src++) << 16;
1116 code[c] |= *(src++) << 24;
1119 for(k = 0;k < 8;k++,j++)
1121 for(c = 0;c < numchans;c++)
1123 int nibble = code[c]&0xf;
1124 code[c] >>= 4;
1126 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
1127 sample[c] = clampi(sample[c], -32768, 32767);
1129 index[c] += IMA4Index_adjust[nibble];
1130 index[c] = clampi(index[c], 0, 88);
1132 dst[j*numchans + c] = sample[c];
1138 static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans)
1140 ALsizei j,k,c;
1142 for(c = 0;c < numchans;c++)
1144 int diff = src[c] - sample[c];
1145 int step = IMAStep_size[index[c]];
1146 int nibble;
1148 nibble = 0;
1149 if(diff < 0)
1151 nibble = 0x8;
1152 diff = -diff;
1155 diff = mini(step*2, diff);
1156 nibble |= (diff*8/step - 1) / 2;
1158 sample[c] += IMA4Codeword[nibble] * step / 8;
1159 sample[c] = clampi(sample[c], -32768, 32767);
1161 index[c] += IMA4Index_adjust[nibble];
1162 index[c] = clampi(index[c], 0, 88);
1164 *(dst++) = sample[c] & 0xff;
1165 *(dst++) = (sample[c]>>8) & 0xff;
1166 *(dst++) = index[c] & 0xff;
1167 *(dst++) = (index[c]>>8) & 0xff;
1170 j = 1;
1171 while(j < 65)
1173 for(c = 0;c < numchans;c++)
1175 for(k = 0;k < 8;k++)
1177 int diff = src[(j+k)*numchans + c] - sample[c];
1178 int step = IMAStep_size[index[c]];
1179 int nibble;
1181 nibble = 0;
1182 if(diff < 0)
1184 nibble = 0x8;
1185 diff = -diff;
1188 diff = mini(step*2, diff);
1189 nibble |= (diff*8/step - 1) / 2;
1191 sample[c] += IMA4Codeword[nibble] * step / 8;
1192 sample[c] = clampi(sample[c], -32768, 32767);
1194 index[c] += IMA4Index_adjust[nibble];
1195 index[c] = clampi(index[c], 0, 88);
1197 if(!(k&1)) *dst = nibble;
1198 else *(dst++) |= nibble<<4;
1201 j += 8;
1206 static __inline ALint DecodeByte3(ALbyte3 val)
1208 if(IS_LITTLE_ENDIAN)
1209 return (val.b[2]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[0]);
1210 return (val.b[0]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[2]);
1213 static __inline ALbyte3 EncodeByte3(ALint val)
1215 if(IS_LITTLE_ENDIAN)
1217 ALbyte3 ret = {{ val, val>>8, val>>16 }};
1218 return ret;
1220 else
1222 ALbyte3 ret = {{ val>>16, val>>8, val }};
1223 return ret;
1227 static __inline ALint DecodeUByte3(ALubyte3 val)
1229 if(IS_LITTLE_ENDIAN)
1230 return (val.b[2]<<16) | (val.b[1]<<8) | (val.b[0]);
1231 return (val.b[0]<<16) | (val.b[1]<<8) | val.b[2];
1234 static __inline ALubyte3 EncodeUByte3(ALint val)
1236 if(IS_LITTLE_ENDIAN)
1238 ALubyte3 ret = {{ val, val>>8, val>>16 }};
1239 return ret;
1241 else
1243 ALubyte3 ret = {{ val>>16, val>>8, val }};
1244 return ret;
1249 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
1250 { return val; }
1251 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
1252 { return val-128; }
1253 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
1254 { return val>>8; }
1255 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
1256 { return (val>>8)-128; }
1257 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
1258 { return val>>24; }
1259 static __inline ALbyte Conv_ALbyte_ALuint(ALuint val)
1260 { return (val>>24)-128; }
1261 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
1263 if(val > 1.0f) return 127;
1264 if(val < -1.0f) return -128;
1265 return (ALint)(val * 127.0f);
1267 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
1269 if(val > 1.0) return 127;
1270 if(val < -1.0) return -128;
1271 return (ALint)(val * 127.0);
1273 static __inline ALbyte Conv_ALbyte_ALmulaw(ALmulaw val)
1274 { return Conv_ALbyte_ALshort(DecodeMuLaw(val)); }
1275 static __inline ALbyte Conv_ALbyte_ALalaw(ALalaw val)
1276 { return Conv_ALbyte_ALshort(DecodeALaw(val)); }
1277 static __inline ALbyte Conv_ALbyte_ALbyte3(ALbyte3 val)
1278 { return DecodeByte3(val)>>16; }
1279 static __inline ALbyte Conv_ALbyte_ALubyte3(ALubyte3 val)
1280 { return (DecodeUByte3(val)>>16)-128; }
1282 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
1283 { return val+128; }
1284 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
1285 { return val; }
1286 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
1287 { return (val>>8)+128; }
1288 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
1289 { return val>>8; }
1290 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
1291 { return (val>>24)+128; }
1292 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
1293 { return val>>24; }
1294 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
1296 if(val > 1.0f) return 255;
1297 if(val < -1.0f) return 0;
1298 return (ALint)(val * 127.0f) + 128;
1300 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
1302 if(val > 1.0) return 255;
1303 if(val < -1.0) return 0;
1304 return (ALint)(val * 127.0) + 128;
1306 static __inline ALubyte Conv_ALubyte_ALmulaw(ALmulaw val)
1307 { return Conv_ALubyte_ALshort(DecodeMuLaw(val)); }
1308 static __inline ALubyte Conv_ALubyte_ALalaw(ALalaw val)
1309 { return Conv_ALubyte_ALshort(DecodeALaw(val)); }
1310 static __inline ALubyte Conv_ALubyte_ALbyte3(ALbyte3 val)
1311 { return (DecodeByte3(val)>>16)+128; }
1312 static __inline ALubyte Conv_ALubyte_ALubyte3(ALubyte3 val)
1313 { return DecodeUByte3(val)>>16; }
1315 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
1316 { return val<<8; }
1317 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
1318 { return (val-128)<<8; }
1319 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
1320 { return val; }
1321 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
1322 { return val-32768; }
1323 static __inline ALshort Conv_ALshort_ALint(ALint val)
1324 { return val>>16; }
1325 static __inline ALshort Conv_ALshort_ALuint(ALuint val)
1326 { return (val>>16)-32768; }
1327 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
1329 if(val > 1.0f) return 32767;
1330 if(val < -1.0f) return -32768;
1331 return (ALint)(val * 32767.0f);
1333 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
1335 if(val > 1.0) return 32767;
1336 if(val < -1.0) return -32768;
1337 return (ALint)(val * 32767.0);
1339 static __inline ALshort Conv_ALshort_ALmulaw(ALmulaw val)
1340 { return Conv_ALshort_ALshort(DecodeMuLaw(val)); }
1341 static __inline ALshort Conv_ALshort_ALalaw(ALalaw val)
1342 { return Conv_ALshort_ALshort(DecodeALaw(val)); }
1343 static __inline ALshort Conv_ALshort_ALbyte3(ALbyte3 val)
1344 { return DecodeByte3(val)>>8; }
1345 static __inline ALshort Conv_ALshort_ALubyte3(ALubyte3 val)
1346 { return (DecodeUByte3(val)>>8)-32768; }
1348 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
1349 { return (val+128)<<8; }
1350 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
1351 { return val<<8; }
1352 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
1353 { return val+32768; }
1354 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
1355 { return val; }
1356 static __inline ALushort Conv_ALushort_ALint(ALint val)
1357 { return (val>>16)+32768; }
1358 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
1359 { return val>>16; }
1360 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
1362 if(val > 1.0f) return 65535;
1363 if(val < -1.0f) return 0;
1364 return (ALint)(val * 32767.0f) + 32768;
1366 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
1368 if(val > 1.0) return 65535;
1369 if(val < -1.0) return 0;
1370 return (ALint)(val * 32767.0) + 32768;
1372 static __inline ALushort Conv_ALushort_ALmulaw(ALmulaw val)
1373 { return Conv_ALushort_ALshort(DecodeMuLaw(val)); }
1374 static __inline ALushort Conv_ALushort_ALalaw(ALalaw val)
1375 { return Conv_ALushort_ALshort(DecodeALaw(val)); }
1376 static __inline ALushort Conv_ALushort_ALbyte3(ALbyte3 val)
1377 { return (DecodeByte3(val)>>8)+32768; }
1378 static __inline ALushort Conv_ALushort_ALubyte3(ALubyte3 val)
1379 { return DecodeUByte3(val)>>8; }
1381 static __inline ALint Conv_ALint_ALbyte(ALbyte val)
1382 { return val<<24; }
1383 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
1384 { return (val-128)<<24; }
1385 static __inline ALint Conv_ALint_ALshort(ALshort val)
1386 { return val<<16; }
1387 static __inline ALint Conv_ALint_ALushort(ALushort val)
1388 { return (val-32768)<<16; }
1389 static __inline ALint Conv_ALint_ALint(ALint val)
1390 { return val; }
1391 static __inline ALint Conv_ALint_ALuint(ALuint val)
1392 { return val-2147483648u; }
1393 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
1395 if(val > 1.0f) return 2147483647;
1396 if(val < -1.0f) return -2147483647-1;
1397 return (ALint)(val * 2147483647.0);
1399 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
1401 if(val > 1.0) return 2147483647;
1402 if(val < -1.0) return -2147483647-1;
1403 return (ALint)(val * 2147483647.0);
1405 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
1406 { return Conv_ALint_ALshort(DecodeMuLaw(val)); }
1407 static __inline ALint Conv_ALint_ALalaw(ALalaw val)
1408 { return Conv_ALint_ALshort(DecodeALaw(val)); }
1409 static __inline ALint Conv_ALint_ALbyte3(ALbyte3 val)
1410 { return DecodeByte3(val)<<8; }
1411 static __inline ALint Conv_ALint_ALubyte3(ALubyte3 val)
1412 { return (DecodeUByte3(val)-8388608)<<8; }
1414 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
1415 { return (val+128)<<24; }
1416 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
1417 { return val<<24; }
1418 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
1419 { return (val+32768)<<16; }
1420 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
1421 { return val<<16; }
1422 static __inline ALuint Conv_ALuint_ALint(ALint val)
1423 { return val+2147483648u; }
1424 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
1425 { return val; }
1426 static __inline ALuint Conv_ALuint_ALfloat(ALfloat val)
1428 if(val > 1.0f) return 4294967295u;
1429 if(val < -1.0f) return 0;
1430 return (ALint)(val * 2147483647.0) + 2147483648u;
1432 static __inline ALuint Conv_ALuint_ALdouble(ALdouble val)
1434 if(val > 1.0) return 4294967295u;
1435 if(val < -1.0) return 0;
1436 return (ALint)(val * 2147483647.0) + 2147483648u;
1438 static __inline ALuint Conv_ALuint_ALmulaw(ALmulaw val)
1439 { return Conv_ALuint_ALshort(DecodeMuLaw(val)); }
1440 static __inline ALuint Conv_ALuint_ALalaw(ALalaw val)
1441 { return Conv_ALuint_ALshort(DecodeALaw(val)); }
1442 static __inline ALuint Conv_ALuint_ALbyte3(ALbyte3 val)
1443 { return (DecodeByte3(val)+8388608)<<8; }
1444 static __inline ALuint Conv_ALuint_ALubyte3(ALubyte3 val)
1445 { return DecodeUByte3(val)<<8; }
1447 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
1448 { return val * (1.0f/127.0f); }
1449 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
1450 { return (val-128) * (1.0f/127.0f); }
1451 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
1452 { return val * (1.0f/32767.0f); }
1453 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
1454 { return (val-32768) * (1.0f/32767.0f); }
1455 static __inline ALfloat Conv_ALfloat_ALint(ALint val)
1456 { return (ALfloat)(val * (1.0/2147483647.0)); }
1457 static __inline ALfloat Conv_ALfloat_ALuint(ALuint val)
1458 { return (ALfloat)((ALint)(val-2147483648u) * (1.0/2147483647.0)); }
1459 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
1460 { return (val==val) ? val : 0.0f; }
1461 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1462 { return (val==val) ? (ALfloat)val : 0.0f; }
1463 static __inline ALfloat Conv_ALfloat_ALmulaw(ALmulaw val)
1464 { return Conv_ALfloat_ALshort(DecodeMuLaw(val)); }
1465 static __inline ALfloat Conv_ALfloat_ALalaw(ALalaw val)
1466 { return Conv_ALfloat_ALshort(DecodeALaw(val)); }
1467 static __inline ALfloat Conv_ALfloat_ALbyte3(ALbyte3 val)
1468 { return (ALfloat)(DecodeByte3(val) * (1.0/8388607.0)); }
1469 static __inline ALfloat Conv_ALfloat_ALubyte3(ALubyte3 val)
1470 { return (ALfloat)((DecodeUByte3(val)-8388608) * (1.0/8388607.0)); }
1472 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
1473 { return val * (1.0/127.0); }
1474 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
1475 { return (val-128) * (1.0/127.0); }
1476 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
1477 { return val * (1.0/32767.0); }
1478 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
1479 { return (val-32768) * (1.0/32767.0); }
1480 static __inline ALdouble Conv_ALdouble_ALint(ALint val)
1481 { return val * (1.0/2147483647.0); }
1482 static __inline ALdouble Conv_ALdouble_ALuint(ALuint val)
1483 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1484 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
1485 { return (val==val) ? val : 0.0f; }
1486 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1487 { return (val==val) ? val : 0.0; }
1488 static __inline ALdouble Conv_ALdouble_ALmulaw(ALmulaw val)
1489 { return Conv_ALdouble_ALshort(DecodeMuLaw(val)); }
1490 static __inline ALdouble Conv_ALdouble_ALalaw(ALalaw val)
1491 { return Conv_ALdouble_ALshort(DecodeALaw(val)); }
1492 static __inline ALdouble Conv_ALdouble_ALbyte3(ALbyte3 val)
1493 { return DecodeByte3(val) * (1.0/8388607.0); }
1494 static __inline ALdouble Conv_ALdouble_ALubyte3(ALubyte3 val)
1495 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
1497 #define DECL_TEMPLATE(T) \
1498 static __inline ALmulaw Conv_ALmulaw_##T(T val) \
1499 { return EncodeMuLaw(Conv_ALshort_##T(val)); }
1501 DECL_TEMPLATE(ALbyte)
1502 DECL_TEMPLATE(ALubyte)
1503 DECL_TEMPLATE(ALshort)
1504 DECL_TEMPLATE(ALushort)
1505 DECL_TEMPLATE(ALint)
1506 DECL_TEMPLATE(ALuint)
1507 DECL_TEMPLATE(ALfloat)
1508 DECL_TEMPLATE(ALdouble)
1509 static __inline ALmulaw Conv_ALmulaw_ALmulaw(ALmulaw val)
1510 { return val; }
1511 DECL_TEMPLATE(ALalaw)
1512 DECL_TEMPLATE(ALbyte3)
1513 DECL_TEMPLATE(ALubyte3)
1515 #undef DECL_TEMPLATE
1517 #define DECL_TEMPLATE(T) \
1518 static __inline ALalaw Conv_ALalaw_##T(T val) \
1519 { return EncodeALaw(Conv_ALshort_##T(val)); }
1521 DECL_TEMPLATE(ALbyte)
1522 DECL_TEMPLATE(ALubyte)
1523 DECL_TEMPLATE(ALshort)
1524 DECL_TEMPLATE(ALushort)
1525 DECL_TEMPLATE(ALint)
1526 DECL_TEMPLATE(ALuint)
1527 DECL_TEMPLATE(ALfloat)
1528 DECL_TEMPLATE(ALdouble)
1529 DECL_TEMPLATE(ALmulaw)
1530 static __inline ALalaw Conv_ALalaw_ALalaw(ALalaw val)
1531 { return val; }
1532 DECL_TEMPLATE(ALbyte3)
1533 DECL_TEMPLATE(ALubyte3)
1535 #undef DECL_TEMPLATE
1537 #define DECL_TEMPLATE(T) \
1538 static __inline ALbyte3 Conv_ALbyte3_##T(T val) \
1539 { return EncodeByte3(Conv_ALint_##T(val)>>8); }
1541 DECL_TEMPLATE(ALbyte)
1542 DECL_TEMPLATE(ALubyte)
1543 DECL_TEMPLATE(ALshort)
1544 DECL_TEMPLATE(ALushort)
1545 DECL_TEMPLATE(ALint)
1546 DECL_TEMPLATE(ALuint)
1547 DECL_TEMPLATE(ALfloat)
1548 DECL_TEMPLATE(ALdouble)
1549 DECL_TEMPLATE(ALmulaw)
1550 DECL_TEMPLATE(ALalaw)
1551 static __inline ALbyte3 Conv_ALbyte3_ALbyte3(ALbyte3 val)
1552 { return val; }
1553 DECL_TEMPLATE(ALubyte3)
1555 #undef DECL_TEMPLATE
1557 #define DECL_TEMPLATE(T) \
1558 static __inline ALubyte3 Conv_ALubyte3_##T(T val) \
1559 { return EncodeUByte3(Conv_ALuint_##T(val)>>8); }
1561 DECL_TEMPLATE(ALbyte)
1562 DECL_TEMPLATE(ALubyte)
1563 DECL_TEMPLATE(ALshort)
1564 DECL_TEMPLATE(ALushort)
1565 DECL_TEMPLATE(ALint)
1566 DECL_TEMPLATE(ALuint)
1567 DECL_TEMPLATE(ALfloat)
1568 DECL_TEMPLATE(ALdouble)
1569 DECL_TEMPLATE(ALmulaw)
1570 DECL_TEMPLATE(ALalaw)
1571 DECL_TEMPLATE(ALbyte3)
1572 static __inline ALubyte3 Conv_ALubyte3_ALubyte3(ALubyte3 val)
1573 { return val; }
1575 #undef DECL_TEMPLATE
1578 #define DECL_TEMPLATE(T1, T2) \
1579 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
1580 ALuint len) \
1582 ALuint i, j; \
1583 for(i = 0;i < len;i++) \
1585 for(j = 0;j < numchans;j++) \
1586 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1590 DECL_TEMPLATE(ALbyte, ALbyte)
1591 DECL_TEMPLATE(ALbyte, ALubyte)
1592 DECL_TEMPLATE(ALbyte, ALshort)
1593 DECL_TEMPLATE(ALbyte, ALushort)
1594 DECL_TEMPLATE(ALbyte, ALint)
1595 DECL_TEMPLATE(ALbyte, ALuint)
1596 DECL_TEMPLATE(ALbyte, ALfloat)
1597 DECL_TEMPLATE(ALbyte, ALdouble)
1598 DECL_TEMPLATE(ALbyte, ALmulaw)
1599 DECL_TEMPLATE(ALbyte, ALalaw)
1600 DECL_TEMPLATE(ALbyte, ALbyte3)
1601 DECL_TEMPLATE(ALbyte, ALubyte3)
1603 DECL_TEMPLATE(ALubyte, ALbyte)
1604 DECL_TEMPLATE(ALubyte, ALubyte)
1605 DECL_TEMPLATE(ALubyte, ALshort)
1606 DECL_TEMPLATE(ALubyte, ALushort)
1607 DECL_TEMPLATE(ALubyte, ALint)
1608 DECL_TEMPLATE(ALubyte, ALuint)
1609 DECL_TEMPLATE(ALubyte, ALfloat)
1610 DECL_TEMPLATE(ALubyte, ALdouble)
1611 DECL_TEMPLATE(ALubyte, ALmulaw)
1612 DECL_TEMPLATE(ALubyte, ALalaw)
1613 DECL_TEMPLATE(ALubyte, ALbyte3)
1614 DECL_TEMPLATE(ALubyte, ALubyte3)
1616 DECL_TEMPLATE(ALshort, ALbyte)
1617 DECL_TEMPLATE(ALshort, ALubyte)
1618 DECL_TEMPLATE(ALshort, ALshort)
1619 DECL_TEMPLATE(ALshort, ALushort)
1620 DECL_TEMPLATE(ALshort, ALint)
1621 DECL_TEMPLATE(ALshort, ALuint)
1622 DECL_TEMPLATE(ALshort, ALfloat)
1623 DECL_TEMPLATE(ALshort, ALdouble)
1624 DECL_TEMPLATE(ALshort, ALmulaw)
1625 DECL_TEMPLATE(ALshort, ALalaw)
1626 DECL_TEMPLATE(ALshort, ALbyte3)
1627 DECL_TEMPLATE(ALshort, ALubyte3)
1629 DECL_TEMPLATE(ALushort, ALbyte)
1630 DECL_TEMPLATE(ALushort, ALubyte)
1631 DECL_TEMPLATE(ALushort, ALshort)
1632 DECL_TEMPLATE(ALushort, ALushort)
1633 DECL_TEMPLATE(ALushort, ALint)
1634 DECL_TEMPLATE(ALushort, ALuint)
1635 DECL_TEMPLATE(ALushort, ALfloat)
1636 DECL_TEMPLATE(ALushort, ALdouble)
1637 DECL_TEMPLATE(ALushort, ALmulaw)
1638 DECL_TEMPLATE(ALushort, ALalaw)
1639 DECL_TEMPLATE(ALushort, ALbyte3)
1640 DECL_TEMPLATE(ALushort, ALubyte3)
1642 DECL_TEMPLATE(ALint, ALbyte)
1643 DECL_TEMPLATE(ALint, ALubyte)
1644 DECL_TEMPLATE(ALint, ALshort)
1645 DECL_TEMPLATE(ALint, ALushort)
1646 DECL_TEMPLATE(ALint, ALint)
1647 DECL_TEMPLATE(ALint, ALuint)
1648 DECL_TEMPLATE(ALint, ALfloat)
1649 DECL_TEMPLATE(ALint, ALdouble)
1650 DECL_TEMPLATE(ALint, ALmulaw)
1651 DECL_TEMPLATE(ALint, ALalaw)
1652 DECL_TEMPLATE(ALint, ALbyte3)
1653 DECL_TEMPLATE(ALint, ALubyte3)
1655 DECL_TEMPLATE(ALuint, ALbyte)
1656 DECL_TEMPLATE(ALuint, ALubyte)
1657 DECL_TEMPLATE(ALuint, ALshort)
1658 DECL_TEMPLATE(ALuint, ALushort)
1659 DECL_TEMPLATE(ALuint, ALint)
1660 DECL_TEMPLATE(ALuint, ALuint)
1661 DECL_TEMPLATE(ALuint, ALfloat)
1662 DECL_TEMPLATE(ALuint, ALdouble)
1663 DECL_TEMPLATE(ALuint, ALmulaw)
1664 DECL_TEMPLATE(ALuint, ALalaw)
1665 DECL_TEMPLATE(ALuint, ALbyte3)
1666 DECL_TEMPLATE(ALuint, ALubyte3)
1668 DECL_TEMPLATE(ALfloat, ALbyte)
1669 DECL_TEMPLATE(ALfloat, ALubyte)
1670 DECL_TEMPLATE(ALfloat, ALshort)
1671 DECL_TEMPLATE(ALfloat, ALushort)
1672 DECL_TEMPLATE(ALfloat, ALint)
1673 DECL_TEMPLATE(ALfloat, ALuint)
1674 DECL_TEMPLATE(ALfloat, ALfloat)
1675 DECL_TEMPLATE(ALfloat, ALdouble)
1676 DECL_TEMPLATE(ALfloat, ALmulaw)
1677 DECL_TEMPLATE(ALfloat, ALalaw)
1678 DECL_TEMPLATE(ALfloat, ALbyte3)
1679 DECL_TEMPLATE(ALfloat, ALubyte3)
1681 DECL_TEMPLATE(ALdouble, ALbyte)
1682 DECL_TEMPLATE(ALdouble, ALubyte)
1683 DECL_TEMPLATE(ALdouble, ALshort)
1684 DECL_TEMPLATE(ALdouble, ALushort)
1685 DECL_TEMPLATE(ALdouble, ALint)
1686 DECL_TEMPLATE(ALdouble, ALuint)
1687 DECL_TEMPLATE(ALdouble, ALfloat)
1688 DECL_TEMPLATE(ALdouble, ALdouble)
1689 DECL_TEMPLATE(ALdouble, ALmulaw)
1690 DECL_TEMPLATE(ALdouble, ALalaw)
1691 DECL_TEMPLATE(ALdouble, ALbyte3)
1692 DECL_TEMPLATE(ALdouble, ALubyte3)
1694 DECL_TEMPLATE(ALmulaw, ALbyte)
1695 DECL_TEMPLATE(ALmulaw, ALubyte)
1696 DECL_TEMPLATE(ALmulaw, ALshort)
1697 DECL_TEMPLATE(ALmulaw, ALushort)
1698 DECL_TEMPLATE(ALmulaw, ALint)
1699 DECL_TEMPLATE(ALmulaw, ALuint)
1700 DECL_TEMPLATE(ALmulaw, ALfloat)
1701 DECL_TEMPLATE(ALmulaw, ALdouble)
1702 DECL_TEMPLATE(ALmulaw, ALmulaw)
1703 DECL_TEMPLATE(ALmulaw, ALalaw)
1704 DECL_TEMPLATE(ALmulaw, ALbyte3)
1705 DECL_TEMPLATE(ALmulaw, ALubyte3)
1707 DECL_TEMPLATE(ALalaw, ALbyte)
1708 DECL_TEMPLATE(ALalaw, ALubyte)
1709 DECL_TEMPLATE(ALalaw, ALshort)
1710 DECL_TEMPLATE(ALalaw, ALushort)
1711 DECL_TEMPLATE(ALalaw, ALint)
1712 DECL_TEMPLATE(ALalaw, ALuint)
1713 DECL_TEMPLATE(ALalaw, ALfloat)
1714 DECL_TEMPLATE(ALalaw, ALdouble)
1715 DECL_TEMPLATE(ALalaw, ALmulaw)
1716 DECL_TEMPLATE(ALalaw, ALalaw)
1717 DECL_TEMPLATE(ALalaw, ALbyte3)
1718 DECL_TEMPLATE(ALalaw, ALubyte3)
1720 DECL_TEMPLATE(ALbyte3, ALbyte)
1721 DECL_TEMPLATE(ALbyte3, ALubyte)
1722 DECL_TEMPLATE(ALbyte3, ALshort)
1723 DECL_TEMPLATE(ALbyte3, ALushort)
1724 DECL_TEMPLATE(ALbyte3, ALint)
1725 DECL_TEMPLATE(ALbyte3, ALuint)
1726 DECL_TEMPLATE(ALbyte3, ALfloat)
1727 DECL_TEMPLATE(ALbyte3, ALdouble)
1728 DECL_TEMPLATE(ALbyte3, ALmulaw)
1729 DECL_TEMPLATE(ALbyte3, ALalaw)
1730 DECL_TEMPLATE(ALbyte3, ALbyte3)
1731 DECL_TEMPLATE(ALbyte3, ALubyte3)
1733 DECL_TEMPLATE(ALubyte3, ALbyte)
1734 DECL_TEMPLATE(ALubyte3, ALubyte)
1735 DECL_TEMPLATE(ALubyte3, ALshort)
1736 DECL_TEMPLATE(ALubyte3, ALushort)
1737 DECL_TEMPLATE(ALubyte3, ALint)
1738 DECL_TEMPLATE(ALubyte3, ALuint)
1739 DECL_TEMPLATE(ALubyte3, ALfloat)
1740 DECL_TEMPLATE(ALubyte3, ALdouble)
1741 DECL_TEMPLATE(ALubyte3, ALmulaw)
1742 DECL_TEMPLATE(ALubyte3, ALalaw)
1743 DECL_TEMPLATE(ALubyte3, ALbyte3)
1744 DECL_TEMPLATE(ALubyte3, ALubyte3)
1746 #undef DECL_TEMPLATE
1748 #define DECL_TEMPLATE(T) \
1749 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
1750 ALuint len) \
1752 ALshort tmp[65*MaxChannels]; /* Max samples an IMA4 frame can be */ \
1753 ALuint i, j, k; \
1755 i = 0; \
1756 while(i < len) \
1758 DecodeIMA4Block(tmp, src, numchans); \
1759 src += 36*numchans; \
1761 for(j = 0;j < 65 && i < len;j++,i++) \
1763 for(k = 0;k < numchans;k++) \
1764 *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
1769 DECL_TEMPLATE(ALbyte)
1770 DECL_TEMPLATE(ALubyte)
1771 DECL_TEMPLATE(ALshort)
1772 DECL_TEMPLATE(ALushort)
1773 DECL_TEMPLATE(ALint)
1774 DECL_TEMPLATE(ALuint)
1775 DECL_TEMPLATE(ALfloat)
1776 DECL_TEMPLATE(ALdouble)
1777 DECL_TEMPLATE(ALmulaw)
1778 DECL_TEMPLATE(ALalaw)
1779 DECL_TEMPLATE(ALbyte3)
1780 DECL_TEMPLATE(ALubyte3)
1782 #undef DECL_TEMPLATE
1784 #define DECL_TEMPLATE(T) \
1785 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
1786 ALuint len) \
1788 ALshort tmp[65*MaxChannels]; /* Max samples an IMA4 frame can be */ \
1789 ALint sample[MaxChannels] = {0,0,0,0,0,0,0,0}; \
1790 ALint index[MaxChannels] = {0,0,0,0,0,0,0,0}; \
1791 ALuint i, j; \
1793 for(i = 0;i < len;i += 65) \
1795 for(j = 0;j < 65*numchans;j++) \
1796 tmp[j] = Conv_ALshort_##T(*(src++)); \
1797 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
1798 dst += 36*numchans; \
1802 DECL_TEMPLATE(ALbyte)
1803 DECL_TEMPLATE(ALubyte)
1804 DECL_TEMPLATE(ALshort)
1805 DECL_TEMPLATE(ALushort)
1806 DECL_TEMPLATE(ALint)
1807 DECL_TEMPLATE(ALuint)
1808 DECL_TEMPLATE(ALfloat)
1809 DECL_TEMPLATE(ALdouble)
1810 DECL_TEMPLATE(ALmulaw)
1811 DECL_TEMPLATE(ALalaw)
1812 static void Convert_ALima4_ALima4(ALima4 *dst, const ALima4 *src,
1813 ALuint numchans, ALuint numblocks)
1814 { memcpy(dst, src, numblocks*36*numchans); }
1815 DECL_TEMPLATE(ALbyte3)
1816 DECL_TEMPLATE(ALubyte3)
1818 #undef DECL_TEMPLATE
1820 #define DECL_TEMPLATE(T) \
1821 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
1822 ALsizei numchans, ALsizei len) \
1824 switch(srcType) \
1826 case UserFmtByte: \
1827 Convert_##T##_ALbyte(dst, src, numchans, len); \
1828 break; \
1829 case UserFmtUByte: \
1830 Convert_##T##_ALubyte(dst, src, numchans, len); \
1831 break; \
1832 case UserFmtShort: \
1833 Convert_##T##_ALshort(dst, src, numchans, len); \
1834 break; \
1835 case UserFmtUShort: \
1836 Convert_##T##_ALushort(dst, src, numchans, len); \
1837 break; \
1838 case UserFmtInt: \
1839 Convert_##T##_ALint(dst, src, numchans, len); \
1840 break; \
1841 case UserFmtUInt: \
1842 Convert_##T##_ALuint(dst, src, numchans, len); \
1843 break; \
1844 case UserFmtFloat: \
1845 Convert_##T##_ALfloat(dst, src, numchans, len); \
1846 break; \
1847 case UserFmtDouble: \
1848 Convert_##T##_ALdouble(dst, src, numchans, len); \
1849 break; \
1850 case UserFmtMulaw: \
1851 Convert_##T##_ALmulaw(dst, src, numchans, len); \
1852 break; \
1853 case UserFmtAlaw: \
1854 Convert_##T##_ALalaw(dst, src, numchans, len); \
1855 break; \
1856 case UserFmtIMA4: \
1857 Convert_##T##_ALima4(dst, src, numchans, len); \
1858 break; \
1859 case UserFmtByte3: \
1860 Convert_##T##_ALbyte3(dst, src, numchans, len); \
1861 break; \
1862 case UserFmtUByte3: \
1863 Convert_##T##_ALubyte3(dst, src, numchans, len); \
1864 break; \
1868 DECL_TEMPLATE(ALbyte)
1869 DECL_TEMPLATE(ALubyte)
1870 DECL_TEMPLATE(ALshort)
1871 DECL_TEMPLATE(ALushort)
1872 DECL_TEMPLATE(ALint)
1873 DECL_TEMPLATE(ALuint)
1874 DECL_TEMPLATE(ALfloat)
1875 DECL_TEMPLATE(ALdouble)
1876 DECL_TEMPLATE(ALmulaw)
1877 DECL_TEMPLATE(ALalaw)
1878 DECL_TEMPLATE(ALima4)
1879 DECL_TEMPLATE(ALbyte3)
1880 DECL_TEMPLATE(ALubyte3)
1882 #undef DECL_TEMPLATE
1885 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len)
1887 switch(dstType)
1889 case UserFmtByte:
1890 Convert_ALbyte(dst, src, srcType, numchans, len);
1891 break;
1892 case UserFmtUByte:
1893 Convert_ALubyte(dst, src, srcType, numchans, len);
1894 break;
1895 case UserFmtShort:
1896 Convert_ALshort(dst, src, srcType, numchans, len);
1897 break;
1898 case UserFmtUShort:
1899 Convert_ALushort(dst, src, srcType, numchans, len);
1900 break;
1901 case UserFmtInt:
1902 Convert_ALint(dst, src, srcType, numchans, len);
1903 break;
1904 case UserFmtUInt:
1905 Convert_ALuint(dst, src, srcType, numchans, len);
1906 break;
1907 case UserFmtFloat:
1908 Convert_ALfloat(dst, src, srcType, numchans, len);
1909 break;
1910 case UserFmtDouble:
1911 Convert_ALdouble(dst, src, srcType, numchans, len);
1912 break;
1913 case UserFmtMulaw:
1914 Convert_ALmulaw(dst, src, srcType, numchans, len);
1915 break;
1916 case UserFmtAlaw:
1917 Convert_ALalaw(dst, src, srcType, numchans, len);
1918 break;
1919 case UserFmtIMA4:
1920 Convert_ALima4(dst, src, srcType, numchans, len);
1921 break;
1922 case UserFmtByte3:
1923 Convert_ALbyte3(dst, src, srcType, numchans, len);
1924 break;
1925 case UserFmtUByte3:
1926 Convert_ALubyte3(dst, src, srcType, numchans, len);
1927 break;
1933 * LoadData
1935 * Loads the specified data into the buffer, using the specified formats.
1936 * Currently, the new format must have the same channel configuration as the
1937 * original format.
1939 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels SrcChannels, enum UserFmtType SrcType, const ALvoid *data, ALboolean storesrc)
1941 ALuint NewChannels, NewBytes;
1942 enum FmtChannels DstChannels;
1943 enum FmtType DstType;
1944 ALuint64 newsize;
1945 ALvoid *temp;
1947 if(DecomposeFormat(NewFormat, &DstChannels, &DstType) == AL_FALSE ||
1948 (long)SrcChannels != (long)DstChannels)
1949 return AL_INVALID_ENUM;
1951 NewChannels = ChannelsFromFmt(DstChannels);
1952 NewBytes = BytesFromFmt(DstType);
1954 newsize = frames;
1955 newsize *= NewBytes;
1956 newsize *= NewChannels;
1957 if(newsize > INT_MAX)
1958 return AL_OUT_OF_MEMORY;
1960 WriteLock(&ALBuf->lock);
1961 if(ALBuf->ref != 0)
1963 WriteUnlock(&ALBuf->lock);
1964 return AL_INVALID_OPERATION;
1967 temp = realloc(ALBuf->data, (size_t)newsize);
1968 if(!temp && newsize)
1970 WriteUnlock(&ALBuf->lock);
1971 return AL_OUT_OF_MEMORY;
1973 ALBuf->data = temp;
1975 if(data != NULL)
1976 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
1978 if(storesrc)
1980 ALBuf->OriginalChannels = SrcChannels;
1981 ALBuf->OriginalType = SrcType;
1982 if(SrcType == UserFmtIMA4)
1983 ALBuf->OriginalSize = frames / 65 * 36 * ChannelsFromUserFmt(SrcChannels);
1984 else
1985 ALBuf->OriginalSize = frames * FrameSizeFromUserFmt(SrcChannels, SrcType);
1987 else
1989 ALBuf->OriginalChannels = DstChannels;
1990 ALBuf->OriginalType = DstType;
1991 ALBuf->OriginalSize = frames * NewBytes * NewChannels;
1994 ALBuf->Frequency = freq;
1995 ALBuf->FmtChannels = DstChannels;
1996 ALBuf->FmtType = DstType;
1997 ALBuf->Format = NewFormat;
1999 ALBuf->SampleLen = frames;
2000 ALBuf->LoopStart = 0;
2001 ALBuf->LoopEnd = ALBuf->SampleLen;
2003 WriteUnlock(&ALBuf->lock);
2004 return AL_NO_ERROR;
2008 ALuint BytesFromUserFmt(enum UserFmtType type)
2010 switch(type)
2012 case UserFmtByte: return sizeof(ALbyte);
2013 case UserFmtUByte: return sizeof(ALubyte);
2014 case UserFmtShort: return sizeof(ALshort);
2015 case UserFmtUShort: return sizeof(ALushort);
2016 case UserFmtInt: return sizeof(ALint);
2017 case UserFmtUInt: return sizeof(ALuint);
2018 case UserFmtFloat: return sizeof(ALfloat);
2019 case UserFmtDouble: return sizeof(ALdouble);
2020 case UserFmtByte3: return sizeof(ALbyte3);
2021 case UserFmtUByte3: return sizeof(ALubyte3);
2022 case UserFmtMulaw: return sizeof(ALubyte);
2023 case UserFmtAlaw: return sizeof(ALubyte);
2024 case UserFmtIMA4: break; /* not handled here */
2026 return 0;
2028 ALuint ChannelsFromUserFmt(enum UserFmtChannels chans)
2030 switch(chans)
2032 case UserFmtMono: return 1;
2033 case UserFmtStereo: return 2;
2034 case UserFmtRear: return 2;
2035 case UserFmtQuad: return 4;
2036 case UserFmtX51: return 6;
2037 case UserFmtX61: return 7;
2038 case UserFmtX71: return 8;
2040 return 0;
2042 static ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
2043 enum UserFmtType *type)
2045 static const struct {
2046 ALenum format;
2047 enum UserFmtChannels channels;
2048 enum UserFmtType type;
2049 } list[] = {
2050 { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
2051 { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
2052 { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
2053 { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
2054 { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
2055 { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
2056 { AL_FORMAT_MONO_ALAW_EXT, UserFmtMono, UserFmtAlaw },
2058 { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
2059 { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
2060 { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
2061 { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
2062 { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
2063 { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
2064 { AL_FORMAT_STEREO_ALAW_EXT, UserFmtStereo, UserFmtAlaw },
2066 { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
2067 { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
2068 { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
2069 { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
2071 { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
2072 { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
2074 { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
2075 { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
2076 { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
2077 { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
2079 { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
2080 { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
2081 { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
2082 { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
2084 { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
2085 { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
2086 { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
2087 { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
2089 { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
2090 { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
2091 { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
2092 { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
2094 ALuint i;
2096 for(i = 0;i < COUNTOF(list);i++)
2098 if(list[i].format == format)
2100 *chans = list[i].channels;
2101 *type = list[i].type;
2102 return AL_TRUE;
2106 return AL_FALSE;
2109 ALuint BytesFromFmt(enum FmtType type)
2111 switch(type)
2113 case FmtByte: return sizeof(ALbyte);
2114 case FmtShort: return sizeof(ALshort);
2115 case FmtFloat: return sizeof(ALfloat);
2117 return 0;
2119 ALuint ChannelsFromFmt(enum FmtChannels chans)
2121 switch(chans)
2123 case FmtMono: return 1;
2124 case FmtStereo: return 2;
2125 case FmtRear: return 2;
2126 case FmtQuad: return 4;
2127 case FmtX51: return 6;
2128 case FmtX61: return 7;
2129 case FmtX71: return 8;
2131 return 0;
2133 static ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
2135 static const struct {
2136 ALenum format;
2137 enum FmtChannels channels;
2138 enum FmtType type;
2139 } list[] = {
2140 { AL_MONO8_SOFT, FmtMono, FmtByte },
2141 { AL_MONO16_SOFT, FmtMono, FmtShort },
2142 { AL_MONO32F_SOFT, FmtMono, FmtFloat },
2144 { AL_STEREO8_SOFT, FmtStereo, FmtByte },
2145 { AL_STEREO16_SOFT, FmtStereo, FmtShort },
2146 { AL_STEREO32F_SOFT, FmtStereo, FmtFloat },
2148 { AL_REAR8_SOFT, FmtRear, FmtByte },
2149 { AL_REAR16_SOFT, FmtRear, FmtShort },
2150 { AL_REAR32F_SOFT, FmtRear, FmtFloat },
2152 { AL_FORMAT_QUAD8_LOKI, FmtQuad, FmtByte },
2153 { AL_FORMAT_QUAD16_LOKI, FmtQuad, FmtShort },
2155 { AL_QUAD8_SOFT, FmtQuad, FmtByte },
2156 { AL_QUAD16_SOFT, FmtQuad, FmtShort },
2157 { AL_QUAD32F_SOFT, FmtQuad, FmtFloat },
2159 { AL_5POINT1_8_SOFT, FmtX51, FmtByte },
2160 { AL_5POINT1_16_SOFT, FmtX51, FmtShort },
2161 { AL_5POINT1_32F_SOFT, FmtX51, FmtFloat },
2163 { AL_6POINT1_8_SOFT, FmtX61, FmtByte },
2164 { AL_6POINT1_16_SOFT, FmtX61, FmtShort },
2165 { AL_6POINT1_32F_SOFT, FmtX61, FmtFloat },
2167 { AL_7POINT1_8_SOFT, FmtX71, FmtByte },
2168 { AL_7POINT1_16_SOFT, FmtX71, FmtShort },
2169 { AL_7POINT1_32F_SOFT, FmtX71, FmtFloat },
2171 ALuint i;
2173 for(i = 0;i < COUNTOF(list);i++)
2175 if(list[i].format == format)
2177 *chans = list[i].channels;
2178 *type = list[i].type;
2179 return AL_TRUE;
2183 return AL_FALSE;
2187 static ALboolean IsValidType(ALenum type)
2189 switch(type)
2191 case AL_BYTE_SOFT:
2192 case AL_UNSIGNED_BYTE_SOFT:
2193 case AL_SHORT_SOFT:
2194 case AL_UNSIGNED_SHORT_SOFT:
2195 case AL_INT_SOFT:
2196 case AL_UNSIGNED_INT_SOFT:
2197 case AL_FLOAT_SOFT:
2198 case AL_DOUBLE_SOFT:
2199 case AL_BYTE3_SOFT:
2200 case AL_UNSIGNED_BYTE3_SOFT:
2201 return AL_TRUE;
2203 return AL_FALSE;
2206 static ALboolean IsValidChannels(ALenum channels)
2208 switch(channels)
2210 case AL_MONO_SOFT:
2211 case AL_STEREO_SOFT:
2212 case AL_REAR_SOFT:
2213 case AL_QUAD_SOFT:
2214 case AL_5POINT1_SOFT:
2215 case AL_6POINT1_SOFT:
2216 case AL_7POINT1_SOFT:
2217 return AL_TRUE;
2219 return AL_FALSE;
2224 * ReleaseALBuffers()
2226 * INTERNAL: Called to destroy any buffers that still exist on the device
2228 ALvoid ReleaseALBuffers(ALCdevice *device)
2230 ALsizei i;
2231 for(i = 0;i < device->BufferMap.size;i++)
2233 ALbuffer *temp = device->BufferMap.array[i].value;
2234 device->BufferMap.array[i].value = NULL;
2236 free(temp->data);
2238 FreeThunkEntry(temp->id);
2239 memset(temp, 0, sizeof(ALbuffer));
2240 free(temp);