annotate OpenAL32/alBuffer.c @ 33:3caceef436ea tip

formatting for web
author Robert McIntyre <rlm@mit.edu>
date Sat, 11 Feb 2012 12:25:55 -0700
parents f9476ff7637e
children
rev   line source
rlm@0 1 /**
rlm@0 2 * OpenAL cross platform audio library
rlm@0 3 * Copyright (C) 1999-2007 by authors.
rlm@0 4 * This library is free software; you can redistribute it and/or
rlm@0 5 * modify it under the terms of the GNU Library General Public
rlm@0 6 * License as published by the Free Software Foundation; either
rlm@0 7 * version 2 of the License, or (at your option) any later version.
rlm@0 8 *
rlm@0 9 * This library is distributed in the hope that it will be useful,
rlm@0 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@0 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rlm@0 12 * Library General Public License for more details.
rlm@0 13 *
rlm@0 14 * You should have received a copy of the GNU Library General Public
rlm@0 15 * License along with this library; if not, write to the
rlm@0 16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rlm@0 17 * Boston, MA 02111-1307, USA.
rlm@0 18 * Or go to http://www.gnu.org/copyleft/lgpl.html
rlm@0 19 */
rlm@0 20
rlm@0 21 #include "config.h"
rlm@0 22
rlm@0 23 #include <stdlib.h>
rlm@0 24 #include <stdio.h>
rlm@0 25 #include <assert.h>
rlm@0 26 #include <limits.h>
rlm@0 27
rlm@0 28 #include "alMain.h"
rlm@0 29 #include "AL/al.h"
rlm@0 30 #include "AL/alc.h"
rlm@0 31 #include "alError.h"
rlm@0 32 #include "alBuffer.h"
rlm@0 33 #include "alThunk.h"
rlm@0 34
rlm@0 35
rlm@0 36 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels chans, enum UserFmtType type, const ALvoid *data, ALboolean storesrc);
rlm@0 37 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len);
rlm@0 38 static ALboolean IsValidType(ALenum type);
rlm@0 39 static ALboolean IsValidChannels(ALenum channels);
rlm@0 40
rlm@0 41 #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
rlm@0 42
rlm@0 43
rlm@0 44 /*
rlm@0 45 * Global Variables
rlm@0 46 */
rlm@0 47
rlm@0 48 /* IMA ADPCM Stepsize table */
rlm@0 49 static const long IMAStep_size[89] = {
rlm@0 50 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
rlm@0 51 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
rlm@0 52 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
rlm@0 53 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
rlm@0 54 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
rlm@0 55 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
rlm@0 56 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
rlm@0 57 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
rlm@0 58 32767
rlm@0 59 };
rlm@0 60
rlm@0 61 /* IMA4 ADPCM Codeword decode table */
rlm@0 62 static const long IMA4Codeword[16] = {
rlm@0 63 1, 3, 5, 7, 9, 11, 13, 15,
rlm@0 64 -1,-3,-5,-7,-9,-11,-13,-15,
rlm@0 65 };
rlm@0 66
rlm@0 67 /* IMA4 ADPCM Step index adjust decode table */
rlm@0 68 static const long IMA4Index_adjust[16] = {
rlm@0 69 -1,-1,-1,-1, 2, 4, 6, 8,
rlm@0 70 -1,-1,-1,-1, 2, 4, 6, 8
rlm@0 71 };
rlm@0 72
rlm@0 73 /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
rlm@0 74 * signed 16-bit sample */
rlm@0 75 static const ALshort muLawDecompressionTable[256] = {
rlm@0 76 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
rlm@0 77 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
rlm@0 78 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
rlm@0 79 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
rlm@0 80 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
rlm@0 81 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
rlm@0 82 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
rlm@0 83 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
rlm@0 84 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
rlm@0 85 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
rlm@0 86 -876, -844, -812, -780, -748, -716, -684, -652,
rlm@0 87 -620, -588, -556, -524, -492, -460, -428, -396,
rlm@0 88 -372, -356, -340, -324, -308, -292, -276, -260,
rlm@0 89 -244, -228, -212, -196, -180, -164, -148, -132,
rlm@0 90 -120, -112, -104, -96, -88, -80, -72, -64,
rlm@0 91 -56, -48, -40, -32, -24, -16, -8, 0,
rlm@0 92 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
rlm@0 93 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
rlm@0 94 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
rlm@0 95 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
rlm@0 96 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
rlm@0 97 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
rlm@0 98 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
rlm@0 99 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
rlm@0 100 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
rlm@0 101 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
rlm@0 102 876, 844, 812, 780, 748, 716, 684, 652,
rlm@0 103 620, 588, 556, 524, 492, 460, 428, 396,
rlm@0 104 372, 356, 340, 324, 308, 292, 276, 260,
rlm@0 105 244, 228, 212, 196, 180, 164, 148, 132,
rlm@0 106 120, 112, 104, 96, 88, 80, 72, 64,
rlm@0 107 56, 48, 40, 32, 24, 16, 8, 0
rlm@0 108 };
rlm@0 109
rlm@0 110 /* Values used when encoding a muLaw sample */
rlm@0 111 static const int muLawBias = 0x84;
rlm@0 112 static const int muLawClip = 32635;
rlm@0 113 static const char muLawCompressTable[256] =
rlm@0 114 {
rlm@0 115 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
rlm@0 116 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
rlm@0 117 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
rlm@0 118 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
rlm@0 119 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
rlm@0 120 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
rlm@0 121 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
rlm@0 122 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
rlm@0 123 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
rlm@0 124 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
rlm@0 125 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
rlm@0 126 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
rlm@0 127 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
rlm@0 128 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
rlm@0 129 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
rlm@0 130 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
rlm@0 131 };
rlm@0 132
rlm@0 133 /*
rlm@0 134 * alGenBuffers(ALsizei n, ALuint *buffers)
rlm@0 135 *
rlm@0 136 * Generates n AL Buffers, and stores the Buffers Names in the array pointed
rlm@0 137 * to by buffers
rlm@0 138 */
rlm@0 139 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
rlm@0 140 {
rlm@0 141 ALCcontext *Context;
rlm@0 142 ALsizei i=0;
rlm@0 143
rlm@0 144 Context = GetLockedContext();
rlm@0 145 if(!Context) return;
rlm@0 146
rlm@0 147 /* Check that we are actually generating some Buffers */
rlm@0 148 if(n < 0 || IsBadWritePtr((void*)buffers, n * sizeof(ALuint)))
rlm@0 149 alSetError(Context, AL_INVALID_VALUE);
rlm@0 150 else
rlm@0 151 {
rlm@0 152 ALCdevice *device = Context->Device;
rlm@0 153 ALenum err;
rlm@0 154
rlm@0 155 // Create all the new Buffers
rlm@0 156 while(i < n)
rlm@0 157 {
rlm@0 158 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
rlm@0 159 if(!buffer)
rlm@0 160 {
rlm@0 161 alSetError(Context, AL_OUT_OF_MEMORY);
rlm@0 162 alDeleteBuffers(i, buffers);
rlm@0 163 break;
rlm@0 164 }
rlm@0 165
rlm@0 166 err = NewThunkEntry(&buffer->buffer);
rlm@0 167 if(err == AL_NO_ERROR)
rlm@0 168 err = InsertUIntMapEntry(&device->BufferMap, buffer->buffer, buffer);
rlm@0 169 if(err != AL_NO_ERROR)
rlm@0 170 {
rlm@0 171 FreeThunkEntry(buffer->buffer);
rlm@0 172 memset(buffer, 0, sizeof(ALbuffer));
rlm@0 173 free(buffer);
rlm@0 174
rlm@0 175 alSetError(Context, err);
rlm@0 176 alDeleteBuffers(i, buffers);
rlm@0 177 break;
rlm@0 178 }
rlm@0 179 buffers[i++] = buffer->buffer;
rlm@0 180 }
rlm@0 181 }
rlm@0 182
rlm@0 183 UnlockContext(Context);
rlm@0 184 }
rlm@0 185
rlm@0 186 /*
rlm@0 187 * alDeleteBuffers(ALsizei n, ALuint *buffers)
rlm@0 188 *
rlm@0 189 * Deletes the n AL Buffers pointed to by buffers
rlm@0 190 */
rlm@0 191 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
rlm@0 192 {
rlm@0 193 ALCcontext *Context;
rlm@0 194 ALCdevice *device;
rlm@0 195 ALboolean Failed;
rlm@0 196 ALbuffer *ALBuf;
rlm@0 197 ALsizei i;
rlm@0 198
rlm@0 199 Context = GetLockedContext();
rlm@0 200 if(!Context) return;
rlm@0 201
rlm@0 202 Failed = AL_TRUE;
rlm@0 203 device = Context->Device;
rlm@0 204 /* Check we are actually Deleting some Buffers */
rlm@0 205 if(n < 0)
rlm@0 206 alSetError(Context, AL_INVALID_VALUE);
rlm@0 207 else
rlm@0 208 {
rlm@0 209 Failed = AL_FALSE;
rlm@0 210
rlm@0 211 /* Check that all the buffers are valid and can actually be deleted */
rlm@0 212 for(i = 0;i < n;i++)
rlm@0 213 {
rlm@0 214 if(!buffers[i])
rlm@0 215 continue;
rlm@0 216
rlm@0 217 /* Check for valid Buffer ID */
rlm@0 218 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
rlm@0 219 {
rlm@0 220 alSetError(Context, AL_INVALID_NAME);
rlm@0 221 Failed = AL_TRUE;
rlm@0 222 break;
rlm@0 223 }
rlm@0 224 else if(ALBuf->refcount != 0)
rlm@0 225 {
rlm@0 226 /* Buffer still in use, cannot be deleted */
rlm@0 227 alSetError(Context, AL_INVALID_OPERATION);
rlm@0 228 Failed = AL_TRUE;
rlm@0 229 break;
rlm@0 230 }
rlm@0 231 }
rlm@0 232 }
rlm@0 233
rlm@0 234 /* If all the Buffers were valid (and have Reference Counts of 0), then we
rlm@0 235 * can delete them */
rlm@0 236 if(!Failed)
rlm@0 237 {
rlm@0 238 for(i = 0;i < n;i++)
rlm@0 239 {
rlm@0 240 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
rlm@0 241 continue;
rlm@0 242
rlm@0 243 /* Release the memory used to store audio data */
rlm@0 244 free(ALBuf->data);
rlm@0 245
rlm@0 246 /* Release buffer structure */
rlm@0 247 RemoveUIntMapKey(&device->BufferMap, ALBuf->buffer);
rlm@0 248 FreeThunkEntry(ALBuf->buffer);
rlm@0 249
rlm@0 250 memset(ALBuf, 0, sizeof(ALbuffer));
rlm@0 251 free(ALBuf);
rlm@0 252 }
rlm@0 253 }
rlm@0 254
rlm@0 255 UnlockContext(Context);
rlm@0 256 }
rlm@0 257
rlm@0 258 /*
rlm@0 259 * alIsBuffer(ALuint buffer)
rlm@0 260 *
rlm@0 261 * Checks if buffer is a valid Buffer Name
rlm@0 262 */
rlm@0 263 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
rlm@0 264 {
rlm@0 265 ALCcontext *Context;
rlm@0 266 ALboolean result;
rlm@0 267
rlm@0 268 Context = GetLockedContext();
rlm@0 269 if(!Context) return AL_FALSE;
rlm@0 270
rlm@0 271 result = ((!buffer || LookupBuffer(Context->Device->BufferMap, buffer)) ?
rlm@0 272 AL_TRUE : AL_FALSE);
rlm@0 273
rlm@0 274 UnlockContext(Context);
rlm@0 275
rlm@0 276 return result;
rlm@0 277 }
rlm@0 278
rlm@0 279 /*
rlm@0 280 * alBufferData(ALuint buffer, ALenum format, const ALvoid *data,
rlm@0 281 * ALsizei size, ALsizei freq)
rlm@0 282 *
rlm@0 283 * Fill buffer with audio data
rlm@0 284 */
rlm@0 285 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
rlm@0 286 {
rlm@0 287 enum UserFmtChannels SrcChannels;
rlm@0 288 enum UserFmtType SrcType;
rlm@0 289 ALCcontext *Context;
rlm@0 290 ALCdevice *device;
rlm@0 291 ALbuffer *ALBuf;
rlm@0 292 ALenum err;
rlm@0 293
rlm@0 294 Context = GetLockedContext();
rlm@0 295 if(!Context) return;
rlm@0 296
rlm@0 297 device = Context->Device;
rlm@0 298 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
rlm@0 299 alSetError(Context, AL_INVALID_NAME);
rlm@0 300 else if(ALBuf->refcount != 0)
rlm@0 301 alSetError(Context, AL_INVALID_VALUE);
rlm@0 302 else if(size < 0 || freq < 0)
rlm@0 303 alSetError(Context, AL_INVALID_VALUE);
rlm@0 304 else if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
rlm@0 305 alSetError(Context, AL_INVALID_ENUM);
rlm@0 306 else switch(SrcType)
rlm@0 307 {
rlm@0 308 case UserFmtByte:
rlm@0 309 case UserFmtUByte:
rlm@0 310 case UserFmtShort:
rlm@0 311 case UserFmtUShort:
rlm@0 312 case UserFmtInt:
rlm@0 313 case UserFmtUInt:
rlm@0 314 case UserFmtFloat: {
rlm@0 315 ALuint FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
rlm@0 316 if((size%FrameSize) != 0)
rlm@0 317 err = AL_INVALID_VALUE;
rlm@0 318 else
rlm@0 319 err = LoadData(ALBuf, freq, format, size/FrameSize,
rlm@0 320 SrcChannels, SrcType, data, AL_TRUE);
rlm@0 321 if(err != AL_NO_ERROR)
rlm@0 322 alSetError(Context, err);
rlm@0 323 } break;
rlm@0 324
rlm@0 325 case UserFmtByte3:
rlm@0 326 case UserFmtUByte3:
rlm@0 327 case UserFmtDouble: {
rlm@0 328 ALuint FrameSize = FrameSizeFromUserFmt(SrcChannels, SrcType);
rlm@0 329 ALenum NewFormat = AL_FORMAT_MONO_FLOAT32;
rlm@0 330 switch(SrcChannels)
rlm@0 331 {
rlm@0 332 case UserFmtMono: NewFormat = AL_FORMAT_MONO_FLOAT32; break;
rlm@0 333 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO_FLOAT32; break;
rlm@0 334 case UserFmtRear: NewFormat = AL_FORMAT_REAR32; break;
rlm@0 335 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD32; break;
rlm@0 336 case UserFmtX51: NewFormat = AL_FORMAT_51CHN32; break;
rlm@0 337 case UserFmtX61: NewFormat = AL_FORMAT_61CHN32; break;
rlm@0 338 case UserFmtX71: NewFormat = AL_FORMAT_71CHN32; break;
rlm@0 339 }
rlm@0 340 if((size%FrameSize) != 0)
rlm@0 341 err = AL_INVALID_VALUE;
rlm@0 342 else
rlm@0 343 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
rlm@0 344 SrcChannels, SrcType, data, AL_TRUE);
rlm@0 345 if(err != AL_NO_ERROR)
rlm@0 346 alSetError(Context, err);
rlm@0 347 } break;
rlm@0 348
rlm@0 349 case UserFmtMulaw:
rlm@0 350 case UserFmtIMA4: {
rlm@0 351 /* Here is where things vary:
rlm@0 352 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
rlm@0 353 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
rlm@0 354 */
rlm@0 355 ALuint FrameSize = (SrcType == UserFmtIMA4) ?
rlm@0 356 (ChannelsFromUserFmt(SrcChannels) * 36) :
rlm@0 357 FrameSizeFromUserFmt(SrcChannels, SrcType);
rlm@0 358 ALenum NewFormat = AL_FORMAT_MONO16;
rlm@0 359 switch(SrcChannels)
rlm@0 360 {
rlm@0 361 case UserFmtMono: NewFormat = AL_FORMAT_MONO16; break;
rlm@0 362 case UserFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
rlm@0 363 case UserFmtRear: NewFormat = AL_FORMAT_REAR16; break;
rlm@0 364 case UserFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
rlm@0 365 case UserFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
rlm@0 366 case UserFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
rlm@0 367 case UserFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
rlm@0 368 }
rlm@0 369 if((size%FrameSize) != 0)
rlm@0 370 err = AL_INVALID_VALUE;
rlm@0 371 else
rlm@0 372 err = LoadData(ALBuf, freq, NewFormat, size/FrameSize,
rlm@0 373 SrcChannels, SrcType, data, AL_TRUE);
rlm@0 374 if(err != AL_NO_ERROR)
rlm@0 375 alSetError(Context, err);
rlm@0 376 } break;
rlm@0 377 }
rlm@0 378
rlm@0 379 UnlockContext(Context);
rlm@0 380 }
rlm@0 381
rlm@0 382 /*
rlm@0 383 * alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data,
rlm@0 384 * ALsizei offset, ALsizei length)
rlm@0 385 *
rlm@0 386 * Update buffer's audio data
rlm@0 387 */
rlm@0 388 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
rlm@0 389 {
rlm@0 390 enum UserFmtChannels SrcChannels;
rlm@0 391 enum UserFmtType SrcType;
rlm@0 392 ALCcontext *Context;
rlm@0 393 ALCdevice *device;
rlm@0 394 ALbuffer *ALBuf;
rlm@0 395
rlm@0 396 Context = GetLockedContext();
rlm@0 397 if(!Context) return;
rlm@0 398
rlm@0 399 device = Context->Device;
rlm@0 400 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
rlm@0 401 alSetError(Context, AL_INVALID_NAME);
rlm@0 402 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
rlm@0 403 alSetError(Context, AL_INVALID_VALUE);
rlm@0 404 else if(DecomposeUserFormat(format, &SrcChannels, &SrcType) == AL_FALSE ||
rlm@0 405 SrcChannels != ALBuf->OriginalChannels ||
rlm@0 406 SrcType != ALBuf->OriginalType)
rlm@0 407 alSetError(Context, AL_INVALID_ENUM);
rlm@0 408 else if(offset > ALBuf->OriginalSize ||
rlm@0 409 length > ALBuf->OriginalSize-offset ||
rlm@0 410 (offset%ALBuf->OriginalAlign) != 0 ||
rlm@0 411 (length%ALBuf->OriginalAlign) != 0)
rlm@0 412 alSetError(Context, AL_INVALID_VALUE);
rlm@0 413 else
rlm@0 414 {
rlm@0 415 ALuint Channels = ChannelsFromFmt(ALBuf->FmtChannels);
rlm@0 416 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
rlm@0 417 if(SrcType == UserFmtIMA4)
rlm@0 418 {
rlm@0 419 /* offset -> byte offset, length -> block count */
rlm@0 420 offset /= 36;
rlm@0 421 offset *= 65;
rlm@0 422 offset *= Bytes;
rlm@0 423 length /= ALBuf->OriginalAlign;
rlm@0 424 }
rlm@0 425 else
rlm@0 426 {
rlm@0 427 ALuint OldBytes = BytesFromUserFmt(SrcType);
rlm@0 428
rlm@0 429 offset /= OldBytes;
rlm@0 430 offset *= Bytes;
rlm@0 431 length /= OldBytes * Channels;
rlm@0 432 }
rlm@0 433 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
rlm@0 434 data, SrcType, Channels, length);
rlm@0 435 }
rlm@0 436
rlm@0 437 UnlockContext(Context);
rlm@0 438 }
rlm@0 439
rlm@0 440
rlm@0 441 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer,
rlm@0 442 ALuint samplerate, ALenum internalformat, ALsizei frames,
rlm@0 443 ALenum channels, ALenum type, const ALvoid *data)
rlm@0 444 {
rlm@0 445 ALCcontext *Context;
rlm@0 446 ALCdevice *device;
rlm@0 447 ALbuffer *ALBuf;
rlm@0 448 ALenum err;
rlm@0 449
rlm@0 450 Context = GetLockedContext();
rlm@0 451 if(!Context) return;
rlm@0 452
rlm@0 453 device = Context->Device;
rlm@0 454 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
rlm@0 455 alSetError(Context, AL_INVALID_NAME);
rlm@0 456 else if(ALBuf->refcount != 0)
rlm@0 457 alSetError(Context, AL_INVALID_VALUE);
rlm@0 458 else if(frames < 0 || samplerate == 0)
rlm@0 459 alSetError(Context, AL_INVALID_VALUE);
rlm@0 460 else if(IsValidType(type) == AL_FALSE || IsValidChannels(channels) == AL_FALSE)
rlm@0 461 alSetError(Context, AL_INVALID_ENUM);
rlm@0 462 else
rlm@0 463 {
rlm@0 464 err = AL_NO_ERROR;
rlm@0 465 if(type == UserFmtIMA4)
rlm@0 466 {
rlm@0 467 if((frames%65) == 0) frames /= 65;
rlm@0 468 else err = AL_INVALID_VALUE;
rlm@0 469 }
rlm@0 470 if(err == AL_NO_ERROR)
rlm@0 471 err = LoadData(ALBuf, samplerate, internalformat, frames,
rlm@0 472 channels, type, data, AL_FALSE);
rlm@0 473 if(err != AL_NO_ERROR)
rlm@0 474 alSetError(Context, err);
rlm@0 475 }
rlm@0 476
rlm@0 477 UnlockContext(Context);
rlm@0 478 }
rlm@0 479
rlm@0 480 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint buffer,
rlm@0 481 ALsizei offset, ALsizei frames,
rlm@0 482 ALenum channels, ALenum type, const ALvoid *data)
rlm@0 483 {
rlm@0 484 ALCcontext *Context;
rlm@0 485 ALCdevice *device;
rlm@0 486 ALbuffer *ALBuf;
rlm@0 487
rlm@0 488 Context = GetLockedContext();
rlm@0 489 if(!Context) return;
rlm@0 490
rlm@0 491 device = Context->Device;
rlm@0 492 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
rlm@0 493 alSetError(Context, AL_INVALID_NAME);
rlm@0 494 else if(frames < 0 || offset < 0 || (frames > 0 && data == NULL))
rlm@0 495 alSetError(Context, AL_INVALID_VALUE);
rlm@0 496 else if(channels != (ALenum)ALBuf->FmtChannels ||
rlm@0 497 IsValidType(type) == AL_FALSE)
rlm@0 498 alSetError(Context, AL_INVALID_ENUM);
rlm@0 499 else
rlm@0 500 {
rlm@0 501 ALuint FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
rlm@0 502 ALuint FrameCount = ALBuf->size / FrameSize;
rlm@0 503 if((ALuint)offset > FrameCount || (ALuint)frames > FrameCount-offset)
rlm@0 504 alSetError(Context, AL_INVALID_VALUE);
rlm@0 505 else if(type == UserFmtIMA4 && (frames%65) != 0)
rlm@0 506 alSetError(Context, AL_INVALID_VALUE);
rlm@0 507 else
rlm@0 508 {
rlm@0 509 /* offset -> byte offset */
rlm@0 510 offset *= FrameSize;
rlm@0 511 /* frames -> IMA4 block count */
rlm@0 512 if(type == UserFmtIMA4) frames /= 65;
rlm@0 513 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
rlm@0 514 data, type,
rlm@0 515 ChannelsFromFmt(ALBuf->FmtChannels), frames);
rlm@0 516 }
rlm@0 517 }
rlm@0 518
rlm@0 519 UnlockContext(Context);
rlm@0 520 }
rlm@0 521
rlm@0 522 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer,
rlm@0 523 ALsizei offset, ALsizei frames,
rlm@0 524 ALenum channels, ALenum type, ALvoid *data)
rlm@0 525 {
rlm@0 526 ALCcontext *Context;
rlm@0 527 ALCdevice *device;
rlm@0 528 ALbuffer *ALBuf;
rlm@0 529
rlm@0 530 Context = GetLockedContext();
rlm@0 531 if(!Context) return;
rlm@0 532
rlm@0 533 device = Context->Device;
rlm@0 534 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
rlm@0 535 alSetError(Context, AL_INVALID_NAME);
rlm@0 536 else if(frames < 0 || offset < 0 || (frames > 0 && data == NULL))
rlm@0 537 alSetError(Context, AL_INVALID_VALUE);
rlm@0 538 else if(channels != (ALenum)ALBuf->FmtChannels ||
rlm@0 539 IsValidType(type) == AL_FALSE)
rlm@0 540 alSetError(Context, AL_INVALID_ENUM);
rlm@0 541 else
rlm@0 542 {
rlm@0 543 ALuint FrameSize = FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
rlm@0 544 ALuint FrameCount = ALBuf->size / FrameSize;
rlm@0 545 if((ALuint)offset > FrameCount || (ALuint)frames > FrameCount-offset)
rlm@0 546 alSetError(Context, AL_INVALID_VALUE);
rlm@0 547 else if(type == UserFmtIMA4 && (frames%65) != 0)
rlm@0 548 alSetError(Context, AL_INVALID_VALUE);
rlm@0 549 else
rlm@0 550 {
rlm@0 551 /* offset -> byte offset */
rlm@0 552 offset *= FrameSize;
rlm@0 553 /* frames -> IMA4 block count */
rlm@0 554 if(type == UserFmtIMA4) frames /= 65;
rlm@0 555 ConvertData(data, type,
rlm@0 556 &((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
rlm@0 557 ChannelsFromFmt(ALBuf->FmtChannels), frames);
rlm@0 558 }
rlm@0 559 }
rlm@0 560
rlm@0 561 UnlockContext(Context);
rlm@0 562 }
rlm@0 563
rlm@0 564 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format)
rlm@0 565 {
rlm@0 566 enum FmtChannels DstChannels;
rlm@0 567 enum FmtType DstType;
rlm@0 568 ALCcontext *Context;
rlm@0 569 ALboolean ret;
rlm@0 570
rlm@0 571 Context = GetLockedContext();
rlm@0 572 if(!Context) return AL_FALSE;
rlm@0 573
rlm@0 574 ret = DecomposeFormat(format, &DstChannels, &DstType);
rlm@0 575
rlm@0 576 UnlockContext(Context);
rlm@0 577
rlm@0 578 return ret;
rlm@0 579 }
rlm@0 580
rlm@0 581
rlm@0 582 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
rlm@0 583 {
rlm@0 584 ALCcontext *pContext;
rlm@0 585 ALCdevice *device;
rlm@0 586
rlm@0 587 (void)flValue;
rlm@0 588
rlm@0 589 pContext = GetLockedContext();
rlm@0 590 if(!pContext) return;
rlm@0 591
rlm@0 592 device = pContext->Device;
rlm@0 593 if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 594 alSetError(pContext, AL_INVALID_NAME);
rlm@0 595 else
rlm@0 596 {
rlm@0 597 switch(eParam)
rlm@0 598 {
rlm@0 599 default:
rlm@0 600 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 601 break;
rlm@0 602 }
rlm@0 603 }
rlm@0 604
rlm@0 605 UnlockContext(pContext);
rlm@0 606 }
rlm@0 607
rlm@0 608
rlm@0 609 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
rlm@0 610 {
rlm@0 611 ALCcontext *pContext;
rlm@0 612 ALCdevice *device;
rlm@0 613
rlm@0 614 (void)flValue1;
rlm@0 615 (void)flValue2;
rlm@0 616 (void)flValue3;
rlm@0 617
rlm@0 618 pContext = GetLockedContext();
rlm@0 619 if(!pContext) return;
rlm@0 620
rlm@0 621 device = pContext->Device;
rlm@0 622 if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 623 alSetError(pContext, AL_INVALID_NAME);
rlm@0 624 else
rlm@0 625 {
rlm@0 626 switch(eParam)
rlm@0 627 {
rlm@0 628 default:
rlm@0 629 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 630 break;
rlm@0 631 }
rlm@0 632 }
rlm@0 633
rlm@0 634 UnlockContext(pContext);
rlm@0 635 }
rlm@0 636
rlm@0 637
rlm@0 638 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
rlm@0 639 {
rlm@0 640 ALCcontext *pContext;
rlm@0 641 ALCdevice *device;
rlm@0 642
rlm@0 643 pContext = GetLockedContext();
rlm@0 644 if(!pContext) return;
rlm@0 645
rlm@0 646 device = pContext->Device;
rlm@0 647 if(!flValues)
rlm@0 648 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 649 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 650 alSetError(pContext, AL_INVALID_NAME);
rlm@0 651 else
rlm@0 652 {
rlm@0 653 switch(eParam)
rlm@0 654 {
rlm@0 655 default:
rlm@0 656 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 657 break;
rlm@0 658 }
rlm@0 659 }
rlm@0 660
rlm@0 661 UnlockContext(pContext);
rlm@0 662 }
rlm@0 663
rlm@0 664
rlm@0 665 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
rlm@0 666 {
rlm@0 667 ALCcontext *pContext;
rlm@0 668 ALCdevice *device;
rlm@0 669
rlm@0 670 (void)lValue;
rlm@0 671
rlm@0 672 pContext = GetLockedContext();
rlm@0 673 if(!pContext) return;
rlm@0 674
rlm@0 675 device = pContext->Device;
rlm@0 676 if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 677 alSetError(pContext, AL_INVALID_NAME);
rlm@0 678 else
rlm@0 679 {
rlm@0 680 switch(eParam)
rlm@0 681 {
rlm@0 682 default:
rlm@0 683 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 684 break;
rlm@0 685 }
rlm@0 686 }
rlm@0 687
rlm@0 688 UnlockContext(pContext);
rlm@0 689 }
rlm@0 690
rlm@0 691
rlm@0 692 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
rlm@0 693 {
rlm@0 694 ALCcontext *pContext;
rlm@0 695 ALCdevice *device;
rlm@0 696
rlm@0 697 (void)lValue1;
rlm@0 698 (void)lValue2;
rlm@0 699 (void)lValue3;
rlm@0 700
rlm@0 701 pContext = GetLockedContext();
rlm@0 702 if(!pContext) return;
rlm@0 703
rlm@0 704 device = pContext->Device;
rlm@0 705 if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 706 alSetError(pContext, AL_INVALID_NAME);
rlm@0 707 else
rlm@0 708 {
rlm@0 709 switch(eParam)
rlm@0 710 {
rlm@0 711 default:
rlm@0 712 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 713 break;
rlm@0 714 }
rlm@0 715 }
rlm@0 716
rlm@0 717 UnlockContext(pContext);
rlm@0 718 }
rlm@0 719
rlm@0 720
rlm@0 721 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
rlm@0 722 {
rlm@0 723 ALCcontext *pContext;
rlm@0 724 ALCdevice *device;
rlm@0 725 ALbuffer *ALBuf;
rlm@0 726
rlm@0 727 pContext = GetLockedContext();
rlm@0 728 if(!pContext) return;
rlm@0 729
rlm@0 730 device = pContext->Device;
rlm@0 731 if(!plValues)
rlm@0 732 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 733 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
rlm@0 734 alSetError(pContext, AL_INVALID_NAME);
rlm@0 735 else
rlm@0 736 {
rlm@0 737 switch(eParam)
rlm@0 738 {
rlm@0 739 case AL_LOOP_POINTS_SOFT:
rlm@0 740 if(ALBuf->refcount > 0)
rlm@0 741 alSetError(pContext, AL_INVALID_OPERATION);
rlm@0 742 else if(plValues[0] < 0 || plValues[1] < 0 ||
rlm@0 743 plValues[0] >= plValues[1] || ALBuf->size == 0)
rlm@0 744 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 745 else
rlm@0 746 {
rlm@0 747 ALint maxlen = ALBuf->size /
rlm@0 748 FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
rlm@0 749 if(plValues[0] > maxlen || plValues[1] > maxlen)
rlm@0 750 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 751 else
rlm@0 752 {
rlm@0 753 ALBuf->LoopStart = plValues[0];
rlm@0 754 ALBuf->LoopEnd = plValues[1];
rlm@0 755 }
rlm@0 756 }
rlm@0 757 break;
rlm@0 758
rlm@0 759 default:
rlm@0 760 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 761 break;
rlm@0 762 }
rlm@0 763 }
rlm@0 764
rlm@0 765 UnlockContext(pContext);
rlm@0 766 }
rlm@0 767
rlm@0 768
rlm@0 769 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
rlm@0 770 {
rlm@0 771 ALCcontext *pContext;
rlm@0 772 ALCdevice *device;
rlm@0 773
rlm@0 774 pContext = GetLockedContext();
rlm@0 775 if(!pContext) return;
rlm@0 776
rlm@0 777 device = pContext->Device;
rlm@0 778 if(!pflValue)
rlm@0 779 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 780 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 781 alSetError(pContext, AL_INVALID_NAME);
rlm@0 782 else
rlm@0 783 {
rlm@0 784 switch(eParam)
rlm@0 785 {
rlm@0 786 default:
rlm@0 787 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 788 break;
rlm@0 789 }
rlm@0 790 }
rlm@0 791
rlm@0 792 UnlockContext(pContext);
rlm@0 793 }
rlm@0 794
rlm@0 795
rlm@0 796 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
rlm@0 797 {
rlm@0 798 ALCcontext *pContext;
rlm@0 799 ALCdevice *device;
rlm@0 800
rlm@0 801 pContext = GetLockedContext();
rlm@0 802 if(!pContext) return;
rlm@0 803
rlm@0 804 device = pContext->Device;
rlm@0 805 if(!pflValue1 || !pflValue2 || !pflValue3)
rlm@0 806 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 807 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 808 alSetError(pContext, AL_INVALID_NAME);
rlm@0 809 else
rlm@0 810 {
rlm@0 811 switch(eParam)
rlm@0 812 {
rlm@0 813 default:
rlm@0 814 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 815 break;
rlm@0 816 }
rlm@0 817 }
rlm@0 818
rlm@0 819 UnlockContext(pContext);
rlm@0 820 }
rlm@0 821
rlm@0 822
rlm@0 823 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
rlm@0 824 {
rlm@0 825 ALCcontext *pContext;
rlm@0 826 ALCdevice *device;
rlm@0 827
rlm@0 828 pContext = GetLockedContext();
rlm@0 829 if(!pContext) return;
rlm@0 830
rlm@0 831 device = pContext->Device;
rlm@0 832 if(!pflValues)
rlm@0 833 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 834 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 835 alSetError(pContext, AL_INVALID_NAME);
rlm@0 836 else
rlm@0 837 {
rlm@0 838 switch(eParam)
rlm@0 839 {
rlm@0 840 default:
rlm@0 841 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 842 break;
rlm@0 843 }
rlm@0 844 }
rlm@0 845
rlm@0 846 UnlockContext(pContext);
rlm@0 847 }
rlm@0 848
rlm@0 849
rlm@0 850 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
rlm@0 851 {
rlm@0 852 ALCcontext *pContext;
rlm@0 853 ALbuffer *pBuffer;
rlm@0 854 ALCdevice *device;
rlm@0 855
rlm@0 856 pContext = GetLockedContext();
rlm@0 857 if(!pContext) return;
rlm@0 858
rlm@0 859 device = pContext->Device;
rlm@0 860 if(!plValue)
rlm@0 861 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 862 else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
rlm@0 863 alSetError(pContext, AL_INVALID_NAME);
rlm@0 864 else
rlm@0 865 {
rlm@0 866 switch(eParam)
rlm@0 867 {
rlm@0 868 case AL_FREQUENCY:
rlm@0 869 *plValue = pBuffer->Frequency;
rlm@0 870 break;
rlm@0 871
rlm@0 872 case AL_BITS:
rlm@0 873 *plValue = BytesFromFmt(pBuffer->FmtType) * 8;
rlm@0 874 break;
rlm@0 875
rlm@0 876 case AL_CHANNELS:
rlm@0 877 *plValue = ChannelsFromFmt(pBuffer->FmtChannels);
rlm@0 878 break;
rlm@0 879
rlm@0 880 case AL_SIZE:
rlm@0 881 *plValue = pBuffer->size;
rlm@0 882 break;
rlm@0 883
rlm@0 884 default:
rlm@0 885 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 886 break;
rlm@0 887 }
rlm@0 888 }
rlm@0 889
rlm@0 890 UnlockContext(pContext);
rlm@0 891 }
rlm@0 892
rlm@0 893
rlm@0 894 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
rlm@0 895 {
rlm@0 896 ALCcontext *pContext;
rlm@0 897 ALCdevice *device;
rlm@0 898
rlm@0 899 pContext = GetLockedContext();
rlm@0 900 if(!pContext) return;
rlm@0 901
rlm@0 902 device = pContext->Device;
rlm@0 903 if(!plValue1 || !plValue2 || !plValue3)
rlm@0 904 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 905 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
rlm@0 906 alSetError(pContext, AL_INVALID_NAME);
rlm@0 907 else
rlm@0 908 {
rlm@0 909 switch(eParam)
rlm@0 910 {
rlm@0 911 default:
rlm@0 912 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 913 break;
rlm@0 914 }
rlm@0 915 }
rlm@0 916
rlm@0 917 UnlockContext(pContext);
rlm@0 918 }
rlm@0 919
rlm@0 920
rlm@0 921 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
rlm@0 922 {
rlm@0 923 ALCcontext *pContext;
rlm@0 924 ALCdevice *device;
rlm@0 925 ALbuffer *ALBuf;
rlm@0 926
rlm@0 927 switch(eParam)
rlm@0 928 {
rlm@0 929 case AL_FREQUENCY:
rlm@0 930 case AL_BITS:
rlm@0 931 case AL_CHANNELS:
rlm@0 932 case AL_SIZE:
rlm@0 933 alGetBufferi(buffer, eParam, plValues);
rlm@0 934 return;
rlm@0 935 }
rlm@0 936
rlm@0 937 pContext = GetLockedContext();
rlm@0 938 if(!pContext) return;
rlm@0 939
rlm@0 940 device = pContext->Device;
rlm@0 941 if(!plValues)
rlm@0 942 alSetError(pContext, AL_INVALID_VALUE);
rlm@0 943 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
rlm@0 944 alSetError(pContext, AL_INVALID_NAME);
rlm@0 945 else
rlm@0 946 {
rlm@0 947 switch(eParam)
rlm@0 948 {
rlm@0 949 case AL_LOOP_POINTS_SOFT:
rlm@0 950 plValues[0] = ALBuf->LoopStart;
rlm@0 951 plValues[1] = ALBuf->LoopEnd;
rlm@0 952 break;
rlm@0 953
rlm@0 954 default:
rlm@0 955 alSetError(pContext, AL_INVALID_ENUM);
rlm@0 956 break;
rlm@0 957 }
rlm@0 958 }
rlm@0 959
rlm@0 960 UnlockContext(pContext);
rlm@0 961 }
rlm@0 962
rlm@0 963
rlm@0 964 typedef ALubyte ALmulaw;
rlm@0 965 typedef ALubyte ALima4;
rlm@0 966 typedef struct {
rlm@0 967 ALbyte b[3];
rlm@0 968 } ALbyte3;
rlm@0 969 typedef struct {
rlm@0 970 ALubyte b[3];
rlm@0 971 } ALubyte3;
rlm@0 972
rlm@0 973 static __inline ALshort DecodeMuLaw(ALmulaw val)
rlm@0 974 { return muLawDecompressionTable[val]; }
rlm@0 975
rlm@0 976 static ALmulaw EncodeMuLaw(ALshort val)
rlm@0 977 {
rlm@0 978 ALint mant, exp, sign;
rlm@0 979
rlm@0 980 sign = (val>>8) & 0x80;
rlm@0 981 if(sign)
rlm@0 982 {
rlm@0 983 /* -32768 doesn't properly negate on a short; it results in itself.
rlm@0 984 * So clamp to -32767 */
rlm@0 985 val = maxi(val, -32767);
rlm@0 986 val = -val;
rlm@0 987 }
rlm@0 988
rlm@0 989 val = mini(val, muLawClip);
rlm@0 990 val += muLawBias;
rlm@0 991
rlm@0 992 exp = muLawCompressTable[(val>>7) & 0xff];
rlm@0 993 mant = (val >> (exp+3)) & 0x0f;
rlm@0 994
rlm@0 995 return ~(sign | (exp<<4) | mant);
rlm@0 996 }
rlm@0 997
rlm@0 998 static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans)
rlm@0 999 {
rlm@0 1000 ALint sample[MAXCHANNELS], index[MAXCHANNELS];
rlm@0 1001 ALuint code[MAXCHANNELS];
rlm@0 1002 ALsizei j,k,c;
rlm@0 1003
rlm@0 1004 for(c = 0;c < numchans;c++)
rlm@0 1005 {
rlm@0 1006 sample[c] = *(src++);
rlm@0 1007 sample[c] |= *(src++) << 8;
rlm@0 1008 sample[c] = (sample[c]^0x8000) - 32768;
rlm@0 1009 index[c] = *(src++);
rlm@0 1010 index[c] |= *(src++) << 8;
rlm@0 1011 index[c] = (index[c]^0x8000) - 32768;
rlm@0 1012
rlm@0 1013 index[c] = clampi(index[c], 0, 88);
rlm@0 1014
rlm@0 1015 dst[c] = sample[c];
rlm@0 1016 }
rlm@0 1017
rlm@0 1018 j = 1;
rlm@0 1019 while(j < 65)
rlm@0 1020 {
rlm@0 1021 for(c = 0;c < numchans;c++)
rlm@0 1022 {
rlm@0 1023 code[c] = *(src++);
rlm@0 1024 code[c] |= *(src++) << 8;
rlm@0 1025 code[c] |= *(src++) << 16;
rlm@0 1026 code[c] |= *(src++) << 24;
rlm@0 1027 }
rlm@0 1028
rlm@0 1029 for(k = 0;k < 8;k++,j++)
rlm@0 1030 {
rlm@0 1031 for(c = 0;c < numchans;c++)
rlm@0 1032 {
rlm@0 1033 int nibble = code[c]&0xf;
rlm@0 1034 code[c] >>= 4;
rlm@0 1035
rlm@0 1036 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
rlm@0 1037 sample[c] = clampi(sample[c], -32768, 32767);
rlm@0 1038
rlm@0 1039 index[c] += IMA4Index_adjust[nibble];
rlm@0 1040 index[c] = clampi(index[c], 0, 88);
rlm@0 1041
rlm@0 1042 dst[j*numchans + c] = sample[c];
rlm@0 1043 }
rlm@0 1044 }
rlm@0 1045 }
rlm@0 1046 }
rlm@0 1047
rlm@0 1048 static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans)
rlm@0 1049 {
rlm@0 1050 ALsizei j,k,c;
rlm@0 1051
rlm@0 1052 for(c = 0;c < numchans;c++)
rlm@0 1053 {
rlm@0 1054 int diff = src[c] - sample[c];
rlm@0 1055 int step = IMAStep_size[index[c]];
rlm@0 1056 int nibble;
rlm@0 1057
rlm@0 1058 nibble = 0;
rlm@0 1059 if(diff < 0)
rlm@0 1060 {
rlm@0 1061 nibble = 0x8;
rlm@0 1062 diff = -diff;
rlm@0 1063 }
rlm@0 1064
rlm@0 1065 diff = mini(step*2, diff);
rlm@0 1066 nibble |= (diff*8/step - 1) / 2;
rlm@0 1067
rlm@0 1068 sample[c] += IMA4Codeword[nibble] * step / 8;
rlm@0 1069 sample[c] = clampi(sample[c], -32768, 32767);
rlm@0 1070
rlm@0 1071 index[c] += IMA4Index_adjust[nibble];
rlm@0 1072 index[c] = clampi(index[c], 0, 88);
rlm@0 1073
rlm@0 1074 *(dst++) = sample[c] & 0xff;
rlm@0 1075 *(dst++) = (sample[c]>>8) & 0xff;
rlm@0 1076 *(dst++) = index[c] & 0xff;
rlm@0 1077 *(dst++) = (index[c]>>8) & 0xff;
rlm@0 1078 }
rlm@0 1079
rlm@0 1080 j = 1;
rlm@0 1081 while(j < 65)
rlm@0 1082 {
rlm@0 1083 for(c = 0;c < numchans;c++)
rlm@0 1084 {
rlm@0 1085 for(k = 0;k < 8;k++)
rlm@0 1086 {
rlm@0 1087 int diff = src[(j+k)*numchans + c] - sample[c];
rlm@0 1088 int step = IMAStep_size[index[c]];
rlm@0 1089 int nibble;
rlm@0 1090
rlm@0 1091 nibble = 0;
rlm@0 1092 if(diff < 0)
rlm@0 1093 {
rlm@0 1094 nibble = 0x8;
rlm@0 1095 diff = -diff;
rlm@0 1096 }
rlm@0 1097
rlm@0 1098 diff = mini(step*2, diff);
rlm@0 1099 nibble |= (diff*8/step - 1) / 2;
rlm@0 1100
rlm@0 1101 sample[c] += IMA4Codeword[nibble] * step / 8;
rlm@0 1102 sample[c] = clampi(sample[c], -32768, 32767);
rlm@0 1103
rlm@0 1104 index[c] += IMA4Index_adjust[nibble];
rlm@0 1105 index[c] = clampi(index[c], 0, 88);
rlm@0 1106
rlm@0 1107 if(!(k&1)) *dst = nibble;
rlm@0 1108 else *(dst++) |= nibble<<4;
rlm@0 1109 }
rlm@0 1110 }
rlm@0 1111 j += 8;
rlm@0 1112 }
rlm@0 1113 }
rlm@0 1114
rlm@0 1115 static const union {
rlm@0 1116 ALuint u;
rlm@0 1117 ALubyte b[sizeof(ALuint)];
rlm@0 1118 } EndianTest = { 1 };
rlm@0 1119 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
rlm@0 1120
rlm@0 1121 static __inline ALint DecodeByte3(ALbyte3 val)
rlm@0 1122 {
rlm@0 1123 if(IS_LITTLE_ENDIAN)
rlm@0 1124 return (val.b[2]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[0]);
rlm@0 1125 return (val.b[0]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[2]);
rlm@0 1126 }
rlm@0 1127
rlm@0 1128 static __inline ALbyte3 EncodeByte3(ALint val)
rlm@0 1129 {
rlm@0 1130 if(IS_LITTLE_ENDIAN)
rlm@0 1131 {
rlm@0 1132 ALbyte3 ret = {{ val, val>>8, val>>16 }};
rlm@0 1133 return ret;
rlm@0 1134 }
rlm@0 1135 else
rlm@0 1136 {
rlm@0 1137 ALbyte3 ret = {{ val>>16, val>>8, val }};
rlm@0 1138 return ret;
rlm@0 1139 }
rlm@0 1140 }
rlm@0 1141
rlm@0 1142 static __inline ALint DecodeUByte3(ALubyte3 val)
rlm@0 1143 {
rlm@0 1144 if(IS_LITTLE_ENDIAN)
rlm@0 1145 return (val.b[2]<<16) | (val.b[1]<<8) | (val.b[0]);
rlm@0 1146 return (val.b[0]<<16) | (val.b[1]<<8) | val.b[2];
rlm@0 1147 }
rlm@0 1148
rlm@0 1149 static __inline ALubyte3 EncodeUByte3(ALint val)
rlm@0 1150 {
rlm@0 1151 if(IS_LITTLE_ENDIAN)
rlm@0 1152 {
rlm@0 1153 ALubyte3 ret = {{ val, val>>8, val>>16 }};
rlm@0 1154 return ret;
rlm@0 1155 }
rlm@0 1156 else
rlm@0 1157 {
rlm@0 1158 ALubyte3 ret = {{ val>>16, val>>8, val }};
rlm@0 1159 return ret;
rlm@0 1160 }
rlm@0 1161 }
rlm@0 1162
rlm@0 1163
rlm@0 1164 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
rlm@0 1165 { return val; }
rlm@0 1166 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
rlm@0 1167 { return val-128; }
rlm@0 1168 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
rlm@0 1169 { return val>>8; }
rlm@0 1170 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
rlm@0 1171 { return (val>>8)-128; }
rlm@0 1172 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
rlm@0 1173 { return val>>24; }
rlm@0 1174 static __inline ALbyte Conv_ALbyte_ALuint(ALuint val)
rlm@0 1175 { return (val>>24)-128; }
rlm@0 1176 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
rlm@0 1177 {
rlm@0 1178 if(val > 1.0f) return 127;
rlm@0 1179 if(val < -1.0f) return -128;
rlm@0 1180 return (ALint)(val * 127.0f);
rlm@0 1181 }
rlm@0 1182 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
rlm@0 1183 {
rlm@0 1184 if(val > 1.0) return 127;
rlm@0 1185 if(val < -1.0) return -128;
rlm@0 1186 return (ALint)(val * 127.0);
rlm@0 1187 }
rlm@0 1188 static __inline ALbyte Conv_ALbyte_ALmulaw(ALmulaw val)
rlm@0 1189 { return Conv_ALbyte_ALshort(DecodeMuLaw(val)); }
rlm@0 1190 static __inline ALbyte Conv_ALbyte_ALbyte3(ALbyte3 val)
rlm@0 1191 { return DecodeByte3(val)>>16; }
rlm@0 1192 static __inline ALbyte Conv_ALbyte_ALubyte3(ALubyte3 val)
rlm@0 1193 { return (DecodeUByte3(val)>>16)-128; }
rlm@0 1194
rlm@0 1195 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
rlm@0 1196 { return val+128; }
rlm@0 1197 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
rlm@0 1198 { return val; }
rlm@0 1199 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
rlm@0 1200 { return (val>>8)+128; }
rlm@0 1201 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
rlm@0 1202 { return val>>8; }
rlm@0 1203 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
rlm@0 1204 { return (val>>24)+128; }
rlm@0 1205 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
rlm@0 1206 { return val>>24; }
rlm@0 1207 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
rlm@0 1208 {
rlm@0 1209 if(val > 1.0f) return 255;
rlm@0 1210 if(val < -1.0f) return 0;
rlm@0 1211 return (ALint)(val * 127.0f) + 128;
rlm@0 1212 }
rlm@0 1213 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
rlm@0 1214 {
rlm@0 1215 if(val > 1.0) return 255;
rlm@0 1216 if(val < -1.0) return 0;
rlm@0 1217 return (ALint)(val * 127.0) + 128;
rlm@0 1218 }
rlm@0 1219 static __inline ALubyte Conv_ALubyte_ALmulaw(ALmulaw val)
rlm@0 1220 { return Conv_ALubyte_ALshort(DecodeMuLaw(val)); }
rlm@0 1221 static __inline ALubyte Conv_ALubyte_ALbyte3(ALbyte3 val)
rlm@0 1222 { return (DecodeByte3(val)>>16)+128; }
rlm@0 1223 static __inline ALubyte Conv_ALubyte_ALubyte3(ALubyte3 val)
rlm@0 1224 { return DecodeUByte3(val)>>16; }
rlm@0 1225
rlm@0 1226 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
rlm@0 1227 { return val<<8; }
rlm@0 1228 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
rlm@0 1229 { return (val-128)<<8; }
rlm@0 1230 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
rlm@0 1231 { return val; }
rlm@0 1232 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
rlm@0 1233 { return val-32768; }
rlm@0 1234 static __inline ALshort Conv_ALshort_ALint(ALint val)
rlm@0 1235 { return val>>16; }
rlm@0 1236 static __inline ALshort Conv_ALshort_ALuint(ALuint val)
rlm@0 1237 { return (val>>16)-32768; }
rlm@0 1238 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
rlm@0 1239 {
rlm@0 1240 if(val > 1.0f) return 32767;
rlm@0 1241 if(val < -1.0f) return -32768;
rlm@0 1242 return (ALint)(val * 32767.0f);
rlm@0 1243 }
rlm@0 1244 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
rlm@0 1245 {
rlm@0 1246 if(val > 1.0) return 32767;
rlm@0 1247 if(val < -1.0) return -32768;
rlm@0 1248 return (ALint)(val * 32767.0);
rlm@0 1249 }
rlm@0 1250 static __inline ALshort Conv_ALshort_ALmulaw(ALmulaw val)
rlm@0 1251 { return Conv_ALshort_ALshort(DecodeMuLaw(val)); }
rlm@0 1252 static __inline ALshort Conv_ALshort_ALbyte3(ALbyte3 val)
rlm@0 1253 { return DecodeByte3(val)>>8; }
rlm@0 1254 static __inline ALshort Conv_ALshort_ALubyte3(ALubyte3 val)
rlm@0 1255 { return (DecodeUByte3(val)>>8)-32768; }
rlm@0 1256
rlm@0 1257 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
rlm@0 1258 { return (val+128)<<8; }
rlm@0 1259 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
rlm@0 1260 { return val<<8; }
rlm@0 1261 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
rlm@0 1262 { return val+32768; }
rlm@0 1263 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
rlm@0 1264 { return val; }
rlm@0 1265 static __inline ALushort Conv_ALushort_ALint(ALint val)
rlm@0 1266 { return (val>>16)+32768; }
rlm@0 1267 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
rlm@0 1268 { return val>>16; }
rlm@0 1269 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
rlm@0 1270 {
rlm@0 1271 if(val > 1.0f) return 65535;
rlm@0 1272 if(val < -1.0f) return 0;
rlm@0 1273 return (ALint)(val * 32767.0f) + 32768;
rlm@0 1274 }
rlm@0 1275 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
rlm@0 1276 {
rlm@0 1277 if(val > 1.0) return 65535;
rlm@0 1278 if(val < -1.0) return 0;
rlm@0 1279 return (ALint)(val * 32767.0) + 32768;
rlm@0 1280 }
rlm@0 1281 static __inline ALushort Conv_ALushort_ALmulaw(ALmulaw val)
rlm@0 1282 { return Conv_ALushort_ALshort(DecodeMuLaw(val)); }
rlm@0 1283 static __inline ALushort Conv_ALushort_ALbyte3(ALbyte3 val)
rlm@0 1284 { return (DecodeByte3(val)>>8)+32768; }
rlm@0 1285 static __inline ALushort Conv_ALushort_ALubyte3(ALubyte3 val)
rlm@0 1286 { return DecodeUByte3(val)>>8; }
rlm@0 1287
rlm@0 1288 static __inline ALint Conv_ALint_ALbyte(ALbyte val)
rlm@0 1289 { return val<<24; }
rlm@0 1290 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
rlm@0 1291 { return (val-128)<<24; }
rlm@0 1292 static __inline ALint Conv_ALint_ALshort(ALshort val)
rlm@0 1293 { return val<<16; }
rlm@0 1294 static __inline ALint Conv_ALint_ALushort(ALushort val)
rlm@0 1295 { return (val-32768)<<16; }
rlm@0 1296 static __inline ALint Conv_ALint_ALint(ALint val)
rlm@0 1297 { return val; }
rlm@0 1298 static __inline ALint Conv_ALint_ALuint(ALuint val)
rlm@0 1299 { return val-2147483648u; }
rlm@0 1300 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
rlm@0 1301 {
rlm@0 1302 if(val > 1.0f) return 2147483647;
rlm@0 1303 if(val < -1.0f) return -2147483647-1;
rlm@0 1304 return (ALint)(val * 2147483647.0);
rlm@0 1305 }
rlm@0 1306 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
rlm@0 1307 {
rlm@0 1308 if(val > 1.0) return 2147483647;
rlm@0 1309 if(val < -1.0) return -2147483647-1;
rlm@0 1310 return (ALint)(val * 2147483647.0);
rlm@0 1311 }
rlm@0 1312 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
rlm@0 1313 { return Conv_ALint_ALshort(DecodeMuLaw(val)); }
rlm@0 1314 static __inline ALint Conv_ALint_ALbyte3(ALbyte3 val)
rlm@0 1315 { return DecodeByte3(val)<<8; }
rlm@0 1316 static __inline ALint Conv_ALint_ALubyte3(ALubyte3 val)
rlm@0 1317 { return (DecodeUByte3(val)-8388608)<<8; }
rlm@0 1318
rlm@0 1319 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
rlm@0 1320 { return (val+128)<<24; }
rlm@0 1321 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
rlm@0 1322 { return val<<24; }
rlm@0 1323 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
rlm@0 1324 { return (val+32768)<<16; }
rlm@0 1325 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
rlm@0 1326 { return val<<16; }
rlm@0 1327 static __inline ALuint Conv_ALuint_ALint(ALint val)
rlm@0 1328 { return val+2147483648u; }
rlm@0 1329 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
rlm@0 1330 { return val; }
rlm@0 1331 static __inline ALuint Conv_ALuint_ALfloat(ALfloat val)
rlm@0 1332 {
rlm@0 1333 if(val > 1.0f) return 4294967295u;
rlm@0 1334 if(val < -1.0f) return 0;
rlm@0 1335 return (ALint)(val * 2147483647.0) + 2147483648u;
rlm@0 1336 }
rlm@0 1337 static __inline ALuint Conv_ALuint_ALdouble(ALdouble val)
rlm@0 1338 {
rlm@0 1339 if(val > 1.0) return 4294967295u;
rlm@0 1340 if(val < -1.0) return 0;
rlm@0 1341 return (ALint)(val * 2147483647.0) + 2147483648u;
rlm@0 1342 }
rlm@0 1343 static __inline ALuint Conv_ALuint_ALmulaw(ALmulaw val)
rlm@0 1344 { return Conv_ALuint_ALshort(DecodeMuLaw(val)); }
rlm@0 1345 static __inline ALuint Conv_ALuint_ALbyte3(ALbyte3 val)
rlm@0 1346 { return (DecodeByte3(val)+8388608)<<8; }
rlm@0 1347 static __inline ALuint Conv_ALuint_ALubyte3(ALubyte3 val)
rlm@0 1348 { return DecodeUByte3(val)<<8; }
rlm@0 1349
rlm@0 1350 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
rlm@0 1351 { return val * (1.0f/127.0f); }
rlm@0 1352 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
rlm@0 1353 { return (val-128) * (1.0f/127.0f); }
rlm@0 1354 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
rlm@0 1355 { return val * (1.0f/32767.0f); }
rlm@0 1356 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
rlm@0 1357 { return (val-32768) * (1.0f/32767.0f); }
rlm@0 1358 static __inline ALfloat Conv_ALfloat_ALint(ALint val)
rlm@0 1359 { return val * (1.0/2147483647.0); }
rlm@0 1360 static __inline ALfloat Conv_ALfloat_ALuint(ALuint val)
rlm@0 1361 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
rlm@0 1362 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
rlm@0 1363 { return (val==val) ? val : 0.0f; }
rlm@0 1364 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
rlm@0 1365 { return (val==val) ? val : 0.0; }
rlm@0 1366 static __inline ALfloat Conv_ALfloat_ALmulaw(ALmulaw val)
rlm@0 1367 { return Conv_ALfloat_ALshort(DecodeMuLaw(val)); }
rlm@0 1368 static __inline ALfloat Conv_ALfloat_ALbyte3(ALbyte3 val)
rlm@0 1369 { return DecodeByte3(val) * (1.0/8388607.0); }
rlm@0 1370 static __inline ALfloat Conv_ALfloat_ALubyte3(ALubyte3 val)
rlm@0 1371 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
rlm@0 1372
rlm@0 1373 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
rlm@0 1374 { return val * (1.0/127.0); }
rlm@0 1375 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
rlm@0 1376 { return (val-128) * (1.0/127.0); }
rlm@0 1377 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
rlm@0 1378 { return val * (1.0/32767.0); }
rlm@0 1379 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
rlm@0 1380 { return (val-32768) * (1.0/32767.0); }
rlm@0 1381 static __inline ALdouble Conv_ALdouble_ALint(ALint val)
rlm@0 1382 { return val * (1.0/2147483647.0); }
rlm@0 1383 static __inline ALdouble Conv_ALdouble_ALuint(ALuint val)
rlm@0 1384 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
rlm@0 1385 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
rlm@0 1386 { return (val==val) ? val : 0.0f; }
rlm@0 1387 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
rlm@0 1388 { return (val==val) ? val : 0.0; }
rlm@0 1389 static __inline ALdouble Conv_ALdouble_ALmulaw(ALmulaw val)
rlm@0 1390 { return Conv_ALdouble_ALshort(DecodeMuLaw(val)); }
rlm@0 1391 static __inline ALdouble Conv_ALdouble_ALbyte3(ALbyte3 val)
rlm@0 1392 { return DecodeByte3(val) * (1.0/8388607.0); }
rlm@0 1393 static __inline ALdouble Conv_ALdouble_ALubyte3(ALubyte3 val)
rlm@0 1394 { return (DecodeUByte3(val)-8388608) * (1.0/8388607.0); }
rlm@0 1395
rlm@0 1396 #define DECL_TEMPLATE(T) \
rlm@0 1397 static __inline ALmulaw Conv_ALmulaw_##T(T val) \
rlm@0 1398 { return EncodeMuLaw(Conv_ALshort_##T(val)); }
rlm@0 1399
rlm@0 1400 DECL_TEMPLATE(ALbyte)
rlm@0 1401 DECL_TEMPLATE(ALubyte)
rlm@0 1402 DECL_TEMPLATE(ALshort)
rlm@0 1403 DECL_TEMPLATE(ALushort)
rlm@0 1404 DECL_TEMPLATE(ALint)
rlm@0 1405 DECL_TEMPLATE(ALuint)
rlm@0 1406 DECL_TEMPLATE(ALfloat)
rlm@0 1407 DECL_TEMPLATE(ALdouble)
rlm@0 1408 static __inline ALmulaw Conv_ALmulaw_ALmulaw(ALmulaw val)
rlm@0 1409 { return val; }
rlm@0 1410 DECL_TEMPLATE(ALbyte3)
rlm@0 1411 DECL_TEMPLATE(ALubyte3)
rlm@0 1412
rlm@0 1413 #undef DECL_TEMPLATE
rlm@0 1414
rlm@0 1415 #define DECL_TEMPLATE(T) \
rlm@0 1416 static __inline ALbyte3 Conv_ALbyte3_##T(T val) \
rlm@0 1417 { return EncodeByte3(Conv_ALint_##T(val)>>8); }
rlm@0 1418
rlm@0 1419 DECL_TEMPLATE(ALbyte)
rlm@0 1420 DECL_TEMPLATE(ALubyte)
rlm@0 1421 DECL_TEMPLATE(ALshort)
rlm@0 1422 DECL_TEMPLATE(ALushort)
rlm@0 1423 DECL_TEMPLATE(ALint)
rlm@0 1424 DECL_TEMPLATE(ALuint)
rlm@0 1425 DECL_TEMPLATE(ALfloat)
rlm@0 1426 DECL_TEMPLATE(ALdouble)
rlm@0 1427 DECL_TEMPLATE(ALmulaw)
rlm@0 1428 static __inline ALbyte3 Conv_ALbyte3_ALbyte3(ALbyte3 val)
rlm@0 1429 { return val; }
rlm@0 1430 DECL_TEMPLATE(ALubyte3)
rlm@0 1431
rlm@0 1432 #undef DECL_TEMPLATE
rlm@0 1433
rlm@0 1434 #define DECL_TEMPLATE(T) \
rlm@0 1435 static __inline ALubyte3 Conv_ALubyte3_##T(T val) \
rlm@0 1436 { return EncodeUByte3(Conv_ALuint_##T(val)>>8); }
rlm@0 1437
rlm@0 1438 DECL_TEMPLATE(ALbyte)
rlm@0 1439 DECL_TEMPLATE(ALubyte)
rlm@0 1440 DECL_TEMPLATE(ALshort)
rlm@0 1441 DECL_TEMPLATE(ALushort)
rlm@0 1442 DECL_TEMPLATE(ALint)
rlm@0 1443 DECL_TEMPLATE(ALuint)
rlm@0 1444 DECL_TEMPLATE(ALfloat)
rlm@0 1445 DECL_TEMPLATE(ALdouble)
rlm@0 1446 DECL_TEMPLATE(ALmulaw)
rlm@0 1447 DECL_TEMPLATE(ALbyte3)
rlm@0 1448 static __inline ALubyte3 Conv_ALubyte3_ALubyte3(ALubyte3 val)
rlm@0 1449 { return val; }
rlm@0 1450
rlm@0 1451 #undef DECL_TEMPLATE
rlm@0 1452
rlm@0 1453
rlm@0 1454 #define DECL_TEMPLATE(T1, T2) \
rlm@0 1455 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
rlm@0 1456 ALuint len) \
rlm@0 1457 { \
rlm@0 1458 ALuint i, j; \
rlm@0 1459 for(i = 0;i < len;i++) \
rlm@0 1460 { \
rlm@0 1461 for(j = 0;j < numchans;j++) \
rlm@0 1462 *(dst++) = Conv_##T1##_##T2(*(src++)); \
rlm@0 1463 } \
rlm@0 1464 }
rlm@0 1465
rlm@0 1466 DECL_TEMPLATE(ALbyte, ALbyte)
rlm@0 1467 DECL_TEMPLATE(ALbyte, ALubyte)
rlm@0 1468 DECL_TEMPLATE(ALbyte, ALshort)
rlm@0 1469 DECL_TEMPLATE(ALbyte, ALushort)
rlm@0 1470 DECL_TEMPLATE(ALbyte, ALint)
rlm@0 1471 DECL_TEMPLATE(ALbyte, ALuint)
rlm@0 1472 DECL_TEMPLATE(ALbyte, ALfloat)
rlm@0 1473 DECL_TEMPLATE(ALbyte, ALdouble)
rlm@0 1474 DECL_TEMPLATE(ALbyte, ALmulaw)
rlm@0 1475 DECL_TEMPLATE(ALbyte, ALbyte3)
rlm@0 1476 DECL_TEMPLATE(ALbyte, ALubyte3)
rlm@0 1477
rlm@0 1478 DECL_TEMPLATE(ALubyte, ALbyte)
rlm@0 1479 DECL_TEMPLATE(ALubyte, ALubyte)
rlm@0 1480 DECL_TEMPLATE(ALubyte, ALshort)
rlm@0 1481 DECL_TEMPLATE(ALubyte, ALushort)
rlm@0 1482 DECL_TEMPLATE(ALubyte, ALint)
rlm@0 1483 DECL_TEMPLATE(ALubyte, ALuint)
rlm@0 1484 DECL_TEMPLATE(ALubyte, ALfloat)
rlm@0 1485 DECL_TEMPLATE(ALubyte, ALdouble)
rlm@0 1486 DECL_TEMPLATE(ALubyte, ALmulaw)
rlm@0 1487 DECL_TEMPLATE(ALubyte, ALbyte3)
rlm@0 1488 DECL_TEMPLATE(ALubyte, ALubyte3)
rlm@0 1489
rlm@0 1490 DECL_TEMPLATE(ALshort, ALbyte)
rlm@0 1491 DECL_TEMPLATE(ALshort, ALubyte)
rlm@0 1492 DECL_TEMPLATE(ALshort, ALshort)
rlm@0 1493 DECL_TEMPLATE(ALshort, ALushort)
rlm@0 1494 DECL_TEMPLATE(ALshort, ALint)
rlm@0 1495 DECL_TEMPLATE(ALshort, ALuint)
rlm@0 1496 DECL_TEMPLATE(ALshort, ALfloat)
rlm@0 1497 DECL_TEMPLATE(ALshort, ALdouble)
rlm@0 1498 DECL_TEMPLATE(ALshort, ALmulaw)
rlm@0 1499 DECL_TEMPLATE(ALshort, ALbyte3)
rlm@0 1500 DECL_TEMPLATE(ALshort, ALubyte3)
rlm@0 1501
rlm@0 1502 DECL_TEMPLATE(ALushort, ALbyte)
rlm@0 1503 DECL_TEMPLATE(ALushort, ALubyte)
rlm@0 1504 DECL_TEMPLATE(ALushort, ALshort)
rlm@0 1505 DECL_TEMPLATE(ALushort, ALushort)
rlm@0 1506 DECL_TEMPLATE(ALushort, ALint)
rlm@0 1507 DECL_TEMPLATE(ALushort, ALuint)
rlm@0 1508 DECL_TEMPLATE(ALushort, ALfloat)
rlm@0 1509 DECL_TEMPLATE(ALushort, ALdouble)
rlm@0 1510 DECL_TEMPLATE(ALushort, ALmulaw)
rlm@0 1511 DECL_TEMPLATE(ALushort, ALbyte3)
rlm@0 1512 DECL_TEMPLATE(ALushort, ALubyte3)
rlm@0 1513
rlm@0 1514 DECL_TEMPLATE(ALint, ALbyte)
rlm@0 1515 DECL_TEMPLATE(ALint, ALubyte)
rlm@0 1516 DECL_TEMPLATE(ALint, ALshort)
rlm@0 1517 DECL_TEMPLATE(ALint, ALushort)
rlm@0 1518 DECL_TEMPLATE(ALint, ALint)
rlm@0 1519 DECL_TEMPLATE(ALint, ALuint)
rlm@0 1520 DECL_TEMPLATE(ALint, ALfloat)
rlm@0 1521 DECL_TEMPLATE(ALint, ALdouble)
rlm@0 1522 DECL_TEMPLATE(ALint, ALmulaw)
rlm@0 1523 DECL_TEMPLATE(ALint, ALbyte3)
rlm@0 1524 DECL_TEMPLATE(ALint, ALubyte3)
rlm@0 1525
rlm@0 1526 DECL_TEMPLATE(ALuint, ALbyte)
rlm@0 1527 DECL_TEMPLATE(ALuint, ALubyte)
rlm@0 1528 DECL_TEMPLATE(ALuint, ALshort)
rlm@0 1529 DECL_TEMPLATE(ALuint, ALushort)
rlm@0 1530 DECL_TEMPLATE(ALuint, ALint)
rlm@0 1531 DECL_TEMPLATE(ALuint, ALuint)
rlm@0 1532 DECL_TEMPLATE(ALuint, ALfloat)
rlm@0 1533 DECL_TEMPLATE(ALuint, ALdouble)
rlm@0 1534 DECL_TEMPLATE(ALuint, ALmulaw)
rlm@0 1535 DECL_TEMPLATE(ALuint, ALbyte3)
rlm@0 1536 DECL_TEMPLATE(ALuint, ALubyte3)
rlm@0 1537
rlm@0 1538 DECL_TEMPLATE(ALfloat, ALbyte)
rlm@0 1539 DECL_TEMPLATE(ALfloat, ALubyte)
rlm@0 1540 DECL_TEMPLATE(ALfloat, ALshort)
rlm@0 1541 DECL_TEMPLATE(ALfloat, ALushort)
rlm@0 1542 DECL_TEMPLATE(ALfloat, ALint)
rlm@0 1543 DECL_TEMPLATE(ALfloat, ALuint)
rlm@0 1544 DECL_TEMPLATE(ALfloat, ALfloat)
rlm@0 1545 DECL_TEMPLATE(ALfloat, ALdouble)
rlm@0 1546 DECL_TEMPLATE(ALfloat, ALmulaw)
rlm@0 1547 DECL_TEMPLATE(ALfloat, ALbyte3)
rlm@0 1548 DECL_TEMPLATE(ALfloat, ALubyte3)
rlm@0 1549
rlm@0 1550 DECL_TEMPLATE(ALdouble, ALbyte)
rlm@0 1551 DECL_TEMPLATE(ALdouble, ALubyte)
rlm@0 1552 DECL_TEMPLATE(ALdouble, ALshort)
rlm@0 1553 DECL_TEMPLATE(ALdouble, ALushort)
rlm@0 1554 DECL_TEMPLATE(ALdouble, ALint)
rlm@0 1555 DECL_TEMPLATE(ALdouble, ALuint)
rlm@0 1556 DECL_TEMPLATE(ALdouble, ALfloat)
rlm@0 1557 DECL_TEMPLATE(ALdouble, ALdouble)
rlm@0 1558 DECL_TEMPLATE(ALdouble, ALmulaw)
rlm@0 1559 DECL_TEMPLATE(ALdouble, ALbyte3)
rlm@0 1560 DECL_TEMPLATE(ALdouble, ALubyte3)
rlm@0 1561
rlm@0 1562 DECL_TEMPLATE(ALmulaw, ALbyte)
rlm@0 1563 DECL_TEMPLATE(ALmulaw, ALubyte)
rlm@0 1564 DECL_TEMPLATE(ALmulaw, ALshort)
rlm@0 1565 DECL_TEMPLATE(ALmulaw, ALushort)
rlm@0 1566 DECL_TEMPLATE(ALmulaw, ALint)
rlm@0 1567 DECL_TEMPLATE(ALmulaw, ALuint)
rlm@0 1568 DECL_TEMPLATE(ALmulaw, ALfloat)
rlm@0 1569 DECL_TEMPLATE(ALmulaw, ALdouble)
rlm@0 1570 DECL_TEMPLATE(ALmulaw, ALmulaw)
rlm@0 1571 DECL_TEMPLATE(ALmulaw, ALbyte3)
rlm@0 1572 DECL_TEMPLATE(ALmulaw, ALubyte3)
rlm@0 1573
rlm@0 1574 DECL_TEMPLATE(ALbyte3, ALbyte)
rlm@0 1575 DECL_TEMPLATE(ALbyte3, ALubyte)
rlm@0 1576 DECL_TEMPLATE(ALbyte3, ALshort)
rlm@0 1577 DECL_TEMPLATE(ALbyte3, ALushort)
rlm@0 1578 DECL_TEMPLATE(ALbyte3, ALint)
rlm@0 1579 DECL_TEMPLATE(ALbyte3, ALuint)
rlm@0 1580 DECL_TEMPLATE(ALbyte3, ALfloat)
rlm@0 1581 DECL_TEMPLATE(ALbyte3, ALdouble)
rlm@0 1582 DECL_TEMPLATE(ALbyte3, ALmulaw)
rlm@0 1583 DECL_TEMPLATE(ALbyte3, ALbyte3)
rlm@0 1584 DECL_TEMPLATE(ALbyte3, ALubyte3)
rlm@0 1585
rlm@0 1586 DECL_TEMPLATE(ALubyte3, ALbyte)
rlm@0 1587 DECL_TEMPLATE(ALubyte3, ALubyte)
rlm@0 1588 DECL_TEMPLATE(ALubyte3, ALshort)
rlm@0 1589 DECL_TEMPLATE(ALubyte3, ALushort)
rlm@0 1590 DECL_TEMPLATE(ALubyte3, ALint)
rlm@0 1591 DECL_TEMPLATE(ALubyte3, ALuint)
rlm@0 1592 DECL_TEMPLATE(ALubyte3, ALfloat)
rlm@0 1593 DECL_TEMPLATE(ALubyte3, ALdouble)
rlm@0 1594 DECL_TEMPLATE(ALubyte3, ALmulaw)
rlm@0 1595 DECL_TEMPLATE(ALubyte3, ALbyte3)
rlm@0 1596 DECL_TEMPLATE(ALubyte3, ALubyte3)
rlm@0 1597
rlm@0 1598 #undef DECL_TEMPLATE
rlm@0 1599
rlm@0 1600 #define DECL_TEMPLATE(T) \
rlm@0 1601 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
rlm@0 1602 ALuint numblocks) \
rlm@0 1603 { \
rlm@0 1604 ALuint i, j; \
rlm@0 1605 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
rlm@0 1606 for(i = 0;i < numblocks;i++) \
rlm@0 1607 { \
rlm@0 1608 DecodeIMA4Block(tmp, src, numchans); \
rlm@0 1609 src += 36*numchans; \
rlm@0 1610 for(j = 0;j < 65*numchans;j++) \
rlm@0 1611 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
rlm@0 1612 } \
rlm@0 1613 }
rlm@0 1614
rlm@0 1615 DECL_TEMPLATE(ALbyte)
rlm@0 1616 DECL_TEMPLATE(ALubyte)
rlm@0 1617 DECL_TEMPLATE(ALshort)
rlm@0 1618 DECL_TEMPLATE(ALushort)
rlm@0 1619 DECL_TEMPLATE(ALint)
rlm@0 1620 DECL_TEMPLATE(ALuint)
rlm@0 1621 DECL_TEMPLATE(ALfloat)
rlm@0 1622 DECL_TEMPLATE(ALdouble)
rlm@0 1623 DECL_TEMPLATE(ALmulaw)
rlm@0 1624 DECL_TEMPLATE(ALbyte3)
rlm@0 1625 DECL_TEMPLATE(ALubyte3)
rlm@0 1626
rlm@0 1627 #undef DECL_TEMPLATE
rlm@0 1628
rlm@0 1629 #define DECL_TEMPLATE(T) \
rlm@0 1630 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
rlm@0 1631 ALuint numblocks) \
rlm@0 1632 { \
rlm@0 1633 ALuint i, j; \
rlm@0 1634 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
rlm@0 1635 ALint sample[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
rlm@0 1636 ALint index[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
rlm@0 1637 for(i = 0;i < numblocks;i++) \
rlm@0 1638 { \
rlm@0 1639 for(j = 0;j < 65*numchans;j++) \
rlm@0 1640 tmp[j] = Conv_ALshort_##T(*(src++)); \
rlm@0 1641 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
rlm@0 1642 dst += 36*numchans; \
rlm@0 1643 } \
rlm@0 1644 }
rlm@0 1645
rlm@0 1646 DECL_TEMPLATE(ALbyte)
rlm@0 1647 DECL_TEMPLATE(ALubyte)
rlm@0 1648 DECL_TEMPLATE(ALshort)
rlm@0 1649 DECL_TEMPLATE(ALushort)
rlm@0 1650 DECL_TEMPLATE(ALint)
rlm@0 1651 DECL_TEMPLATE(ALuint)
rlm@0 1652 DECL_TEMPLATE(ALfloat)
rlm@0 1653 DECL_TEMPLATE(ALdouble)
rlm@0 1654 DECL_TEMPLATE(ALmulaw)
rlm@0 1655 static void Convert_ALima4_ALima4(ALima4 *dst, const ALima4 *src,
rlm@0 1656 ALuint numchans, ALuint numblocks)
rlm@0 1657 { memcpy(dst, src, numblocks*36*numchans); }
rlm@0 1658 DECL_TEMPLATE(ALbyte3)
rlm@0 1659 DECL_TEMPLATE(ALubyte3)
rlm@0 1660
rlm@0 1661 #undef DECL_TEMPLATE
rlm@0 1662
rlm@0 1663 #define DECL_TEMPLATE(T) \
rlm@0 1664 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
rlm@0 1665 ALsizei numchans, ALsizei len) \
rlm@0 1666 { \
rlm@0 1667 switch(srcType) \
rlm@0 1668 { \
rlm@0 1669 case UserFmtByte: \
rlm@0 1670 Convert_##T##_ALbyte(dst, src, numchans, len); \
rlm@0 1671 break; \
rlm@0 1672 case UserFmtUByte: \
rlm@0 1673 Convert_##T##_ALubyte(dst, src, numchans, len); \
rlm@0 1674 break; \
rlm@0 1675 case UserFmtShort: \
rlm@0 1676 Convert_##T##_ALshort(dst, src, numchans, len); \
rlm@0 1677 break; \
rlm@0 1678 case UserFmtUShort: \
rlm@0 1679 Convert_##T##_ALushort(dst, src, numchans, len); \
rlm@0 1680 break; \
rlm@0 1681 case UserFmtInt: \
rlm@0 1682 Convert_##T##_ALint(dst, src, numchans, len); \
rlm@0 1683 break; \
rlm@0 1684 case UserFmtUInt: \
rlm@0 1685 Convert_##T##_ALuint(dst, src, numchans, len); \
rlm@0 1686 break; \
rlm@0 1687 case UserFmtFloat: \
rlm@0 1688 Convert_##T##_ALfloat(dst, src, numchans, len); \
rlm@0 1689 break; \
rlm@0 1690 case UserFmtDouble: \
rlm@0 1691 Convert_##T##_ALdouble(dst, src, numchans, len); \
rlm@0 1692 break; \
rlm@0 1693 case UserFmtMulaw: \
rlm@0 1694 Convert_##T##_ALmulaw(dst, src, numchans, len); \
rlm@0 1695 break; \
rlm@0 1696 case UserFmtIMA4: \
rlm@0 1697 Convert_##T##_ALima4(dst, src, numchans, len); \
rlm@0 1698 break; \
rlm@0 1699 case UserFmtByte3: \
rlm@0 1700 Convert_##T##_ALbyte3(dst, src, numchans, len); \
rlm@0 1701 break; \
rlm@0 1702 case UserFmtUByte3: \
rlm@0 1703 Convert_##T##_ALubyte3(dst, src, numchans, len); \
rlm@0 1704 break; \
rlm@0 1705 } \
rlm@0 1706 }
rlm@0 1707
rlm@0 1708 DECL_TEMPLATE(ALbyte)
rlm@0 1709 DECL_TEMPLATE(ALubyte)
rlm@0 1710 DECL_TEMPLATE(ALshort)
rlm@0 1711 DECL_TEMPLATE(ALushort)
rlm@0 1712 DECL_TEMPLATE(ALint)
rlm@0 1713 DECL_TEMPLATE(ALuint)
rlm@0 1714 DECL_TEMPLATE(ALfloat)
rlm@0 1715 DECL_TEMPLATE(ALdouble)
rlm@0 1716 DECL_TEMPLATE(ALmulaw)
rlm@0 1717 DECL_TEMPLATE(ALima4)
rlm@0 1718 DECL_TEMPLATE(ALbyte3)
rlm@0 1719 DECL_TEMPLATE(ALubyte3)
rlm@0 1720
rlm@0 1721 #undef DECL_TEMPLATE
rlm@0 1722
rlm@0 1723
rlm@0 1724 static void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len)
rlm@0 1725 {
rlm@0 1726 switch(dstType)
rlm@0 1727 {
rlm@0 1728 case UserFmtByte:
rlm@0 1729 Convert_ALbyte(dst, src, srcType, numchans, len);
rlm@0 1730 break;
rlm@0 1731 case UserFmtUByte:
rlm@0 1732 Convert_ALubyte(dst, src, srcType, numchans, len);
rlm@0 1733 break;
rlm@0 1734 case UserFmtShort:
rlm@0 1735 Convert_ALshort(dst, src, srcType, numchans, len);
rlm@0 1736 break;
rlm@0 1737 case UserFmtUShort:
rlm@0 1738 Convert_ALushort(dst, src, srcType, numchans, len);
rlm@0 1739 break;
rlm@0 1740 case UserFmtInt:
rlm@0 1741 Convert_ALint(dst, src, srcType, numchans, len);
rlm@0 1742 break;
rlm@0 1743 case UserFmtUInt:
rlm@0 1744 Convert_ALuint(dst, src, srcType, numchans, len);
rlm@0 1745 break;
rlm@0 1746 case UserFmtFloat:
rlm@0 1747 Convert_ALfloat(dst, src, srcType, numchans, len);
rlm@0 1748 break;
rlm@0 1749 case UserFmtDouble:
rlm@0 1750 Convert_ALdouble(dst, src, srcType, numchans, len);
rlm@0 1751 break;
rlm@0 1752 case UserFmtMulaw:
rlm@0 1753 Convert_ALmulaw(dst, src, srcType, numchans, len);
rlm@0 1754 break;
rlm@0 1755 case UserFmtIMA4:
rlm@0 1756 Convert_ALima4(dst, src, srcType, numchans, len);
rlm@0 1757 break;
rlm@0 1758 case UserFmtByte3:
rlm@0 1759 Convert_ALbyte3(dst, src, srcType, numchans, len);
rlm@0 1760 break;
rlm@0 1761 case UserFmtUByte3:
rlm@0 1762 Convert_ALubyte3(dst, src, srcType, numchans, len);
rlm@0 1763 break;
rlm@0 1764 }
rlm@0 1765 }
rlm@0 1766
rlm@0 1767
rlm@0 1768 /*
rlm@0 1769 * LoadData
rlm@0 1770 *
rlm@0 1771 * Loads the specified data into the buffer, using the specified formats.
rlm@0 1772 * Currently, the new format must have the same channel configuration as the
rlm@0 1773 * original format.
rlm@0 1774 */
rlm@0 1775 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum UserFmtChannels SrcChannels, enum UserFmtType SrcType, const ALvoid *data, ALboolean storesrc)
rlm@0 1776 {
rlm@0 1777 ALuint NewChannels, NewBytes;
rlm@0 1778 enum FmtChannels DstChannels;
rlm@0 1779 enum FmtType DstType;
rlm@0 1780 ALuint64 newsize;
rlm@0 1781 ALvoid *temp;
rlm@0 1782
rlm@0 1783 if(DecomposeFormat(NewFormat, &DstChannels, &DstType) == AL_FALSE ||
rlm@0 1784 (long)SrcChannels != (long)DstChannels)
rlm@0 1785 return AL_INVALID_ENUM;
rlm@0 1786
rlm@0 1787 NewChannels = ChannelsFromFmt(DstChannels);
rlm@0 1788 NewBytes = BytesFromFmt(DstType);
rlm@0 1789
rlm@0 1790 if(SrcType == UserFmtIMA4)
rlm@0 1791 {
rlm@0 1792 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
rlm@0 1793
rlm@0 1794 newsize = frames;
rlm@0 1795 newsize *= 65;
rlm@0 1796 newsize *= NewBytes;
rlm@0 1797 newsize *= NewChannels;
rlm@0 1798 if(newsize > INT_MAX)
rlm@0 1799 return AL_OUT_OF_MEMORY;
rlm@0 1800
rlm@0 1801 temp = realloc(ALBuf->data, newsize);
rlm@0 1802 if(!temp && newsize) return AL_OUT_OF_MEMORY;
rlm@0 1803 ALBuf->data = temp;
rlm@0 1804 ALBuf->size = newsize;
rlm@0 1805
rlm@0 1806 if(data != NULL)
rlm@0 1807 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
rlm@0 1808
rlm@0 1809 if(storesrc)
rlm@0 1810 {
rlm@0 1811 ALBuf->OriginalChannels = SrcChannels;
rlm@0 1812 ALBuf->OriginalType = SrcType;
rlm@0 1813 ALBuf->OriginalSize = frames * 36 * OrigChannels;
rlm@0 1814 ALBuf->OriginalAlign = 36 * OrigChannels;
rlm@0 1815 }
rlm@0 1816 }
rlm@0 1817 else
rlm@0 1818 {
rlm@0 1819 ALuint OrigBytes = BytesFromUserFmt(SrcType);
rlm@0 1820 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
rlm@0 1821
rlm@0 1822 newsize = frames;
rlm@0 1823 newsize *= NewBytes;
rlm@0 1824 newsize *= NewChannels;
rlm@0 1825 if(newsize > INT_MAX)
rlm@0 1826 return AL_OUT_OF_MEMORY;
rlm@0 1827
rlm@0 1828 temp = realloc(ALBuf->data, newsize);
rlm@0 1829 if(!temp && newsize) return AL_OUT_OF_MEMORY;
rlm@0 1830 ALBuf->data = temp;
rlm@0 1831 ALBuf->size = newsize;
rlm@0 1832
rlm@0 1833 if(data != NULL)
rlm@0 1834 ConvertData(ALBuf->data, DstType, data, SrcType, NewChannels, frames);
rlm@0 1835
rlm@0 1836 if(storesrc)
rlm@0 1837 {
rlm@0 1838 ALBuf->OriginalChannels = SrcChannels;
rlm@0 1839 ALBuf->OriginalType = SrcType;
rlm@0 1840 ALBuf->OriginalSize = frames * OrigBytes * OrigChannels;
rlm@0 1841 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
rlm@0 1842 }
rlm@0 1843 }
rlm@0 1844
rlm@0 1845 if(!storesrc)
rlm@0 1846 {
rlm@0 1847 ALBuf->OriginalChannels = DstChannels;
rlm@0 1848 ALBuf->OriginalType = DstType;
rlm@0 1849 ALBuf->OriginalSize = frames * NewBytes * NewChannels;
rlm@0 1850 ALBuf->OriginalAlign = NewBytes * NewChannels;
rlm@0 1851 }
rlm@0 1852 ALBuf->Frequency = freq;
rlm@0 1853 ALBuf->FmtChannels = DstChannels;
rlm@0 1854 ALBuf->FmtType = DstType;
rlm@0 1855
rlm@0 1856 ALBuf->LoopStart = 0;
rlm@0 1857 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
rlm@0 1858
rlm@0 1859 return AL_NO_ERROR;
rlm@0 1860 }
rlm@0 1861
rlm@0 1862
rlm@0 1863 ALuint BytesFromUserFmt(enum UserFmtType type)
rlm@0 1864 {
rlm@0 1865 switch(type)
rlm@0 1866 {
rlm@0 1867 case UserFmtByte: return sizeof(ALbyte);
rlm@0 1868 case UserFmtUByte: return sizeof(ALubyte);
rlm@0 1869 case UserFmtShort: return sizeof(ALshort);
rlm@0 1870 case UserFmtUShort: return sizeof(ALushort);
rlm@0 1871 case UserFmtInt: return sizeof(ALint);
rlm@0 1872 case UserFmtUInt: return sizeof(ALuint);
rlm@0 1873 case UserFmtFloat: return sizeof(ALfloat);
rlm@0 1874 case UserFmtDouble: return sizeof(ALdouble);
rlm@0 1875 case UserFmtByte3: return sizeof(ALbyte3);
rlm@0 1876 case UserFmtUByte3: return sizeof(ALubyte3);
rlm@0 1877 case UserFmtMulaw: return sizeof(ALubyte);
rlm@0 1878 case UserFmtIMA4: break; /* not handled here */
rlm@0 1879 }
rlm@0 1880 return 0;
rlm@0 1881 }
rlm@0 1882 ALuint ChannelsFromUserFmt(enum UserFmtChannels chans)
rlm@0 1883 {
rlm@0 1884 switch(chans)
rlm@0 1885 {
rlm@0 1886 case UserFmtMono: return 1;
rlm@0 1887 case UserFmtStereo: return 2;
rlm@0 1888 case UserFmtRear: return 2;
rlm@0 1889 case UserFmtQuad: return 4;
rlm@0 1890 case UserFmtX51: return 6;
rlm@0 1891 case UserFmtX61: return 7;
rlm@0 1892 case UserFmtX71: return 8;
rlm@0 1893 }
rlm@0 1894 return 0;
rlm@0 1895 }
rlm@0 1896 ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
rlm@0 1897 enum UserFmtType *type)
rlm@0 1898 {
rlm@0 1899 switch(format)
rlm@0 1900 {
rlm@0 1901 case AL_FORMAT_MONO8:
rlm@0 1902 *chans = UserFmtMono;
rlm@0 1903 *type = UserFmtUByte;
rlm@0 1904 return AL_TRUE;
rlm@0 1905 case AL_FORMAT_MONO16:
rlm@0 1906 *chans = UserFmtMono;
rlm@0 1907 *type = UserFmtShort;
rlm@0 1908 return AL_TRUE;
rlm@0 1909 case AL_FORMAT_MONO_FLOAT32:
rlm@0 1910 *chans = UserFmtMono;
rlm@0 1911 *type = UserFmtFloat;
rlm@0 1912 return AL_TRUE;
rlm@0 1913 case AL_FORMAT_MONO_DOUBLE_EXT:
rlm@0 1914 *chans = UserFmtMono;
rlm@0 1915 *type = UserFmtDouble;
rlm@0 1916 return AL_TRUE;
rlm@0 1917 case AL_FORMAT_MONO_IMA4:
rlm@0 1918 *chans = UserFmtMono;
rlm@0 1919 *type = UserFmtIMA4;
rlm@0 1920 return AL_TRUE;
rlm@0 1921 case AL_FORMAT_STEREO8:
rlm@0 1922 *chans = UserFmtStereo;
rlm@0 1923 *type = UserFmtUByte;
rlm@0 1924 return AL_TRUE;
rlm@0 1925 case AL_FORMAT_STEREO16:
rlm@0 1926 *chans = UserFmtStereo;
rlm@0 1927 *type = UserFmtShort;
rlm@0 1928 return AL_TRUE;
rlm@0 1929 case AL_FORMAT_STEREO_FLOAT32:
rlm@0 1930 *chans = UserFmtStereo;
rlm@0 1931 *type = UserFmtFloat;
rlm@0 1932 return AL_TRUE;
rlm@0 1933 case AL_FORMAT_STEREO_DOUBLE_EXT:
rlm@0 1934 *chans = UserFmtStereo;
rlm@0 1935 *type = UserFmtDouble;
rlm@0 1936 return AL_TRUE;
rlm@0 1937 case AL_FORMAT_STEREO_IMA4:
rlm@0 1938 *chans = UserFmtStereo;
rlm@0 1939 *type = UserFmtIMA4;
rlm@0 1940 return AL_TRUE;
rlm@0 1941 case AL_FORMAT_QUAD8_LOKI:
rlm@0 1942 case AL_FORMAT_QUAD8:
rlm@0 1943 *chans = UserFmtQuad;
rlm@0 1944 *type = UserFmtUByte;
rlm@0 1945 return AL_TRUE;
rlm@0 1946 case AL_FORMAT_QUAD16_LOKI:
rlm@0 1947 case AL_FORMAT_QUAD16:
rlm@0 1948 *chans = UserFmtQuad;
rlm@0 1949 *type = UserFmtShort;
rlm@0 1950 return AL_TRUE;
rlm@0 1951 case AL_FORMAT_QUAD32:
rlm@0 1952 *chans = UserFmtQuad;
rlm@0 1953 *type = UserFmtFloat;
rlm@0 1954 return AL_TRUE;
rlm@0 1955 case AL_FORMAT_REAR8:
rlm@0 1956 *chans = UserFmtRear;
rlm@0 1957 *type = UserFmtUByte;
rlm@0 1958 return AL_TRUE;
rlm@0 1959 case AL_FORMAT_REAR16:
rlm@0 1960 *chans = UserFmtRear;
rlm@0 1961 *type = UserFmtShort;
rlm@0 1962 return AL_TRUE;
rlm@0 1963 case AL_FORMAT_REAR32:
rlm@0 1964 *chans = UserFmtRear;
rlm@0 1965 *type = UserFmtFloat;
rlm@0 1966 return AL_TRUE;
rlm@0 1967 case AL_FORMAT_51CHN8:
rlm@0 1968 *chans = UserFmtX51;
rlm@0 1969 *type = UserFmtUByte;
rlm@0 1970 return AL_TRUE;
rlm@0 1971 case AL_FORMAT_51CHN16:
rlm@0 1972 *chans = UserFmtX51;
rlm@0 1973 *type = UserFmtShort;
rlm@0 1974 return AL_TRUE;
rlm@0 1975 case AL_FORMAT_51CHN32:
rlm@0 1976 *chans = UserFmtX51;
rlm@0 1977 *type = UserFmtFloat;
rlm@0 1978 return AL_TRUE;
rlm@0 1979 case AL_FORMAT_61CHN8:
rlm@0 1980 *chans = UserFmtX61;
rlm@0 1981 *type = UserFmtUByte;
rlm@0 1982 return AL_TRUE;
rlm@0 1983 case AL_FORMAT_61CHN16:
rlm@0 1984 *chans = UserFmtX61;
rlm@0 1985 *type = UserFmtShort;
rlm@0 1986 return AL_TRUE;
rlm@0 1987 case AL_FORMAT_61CHN32:
rlm@0 1988 *chans = UserFmtX61;
rlm@0 1989 *type = UserFmtFloat;
rlm@0 1990 return AL_TRUE;
rlm@0 1991 case AL_FORMAT_71CHN8:
rlm@0 1992 *chans = UserFmtX71;
rlm@0 1993 *type = UserFmtUByte;
rlm@0 1994 return AL_TRUE;
rlm@0 1995 case AL_FORMAT_71CHN16:
rlm@0 1996 *chans = UserFmtX71;
rlm@0 1997 *type = UserFmtShort;
rlm@0 1998 return AL_TRUE;
rlm@0 1999 case AL_FORMAT_71CHN32:
rlm@0 2000 *chans = UserFmtX71;
rlm@0 2001 *type = UserFmtFloat;
rlm@0 2002 return AL_TRUE;
rlm@0 2003 case AL_FORMAT_MONO_MULAW:
rlm@0 2004 *chans = UserFmtMono;
rlm@0 2005 *type = UserFmtMulaw;
rlm@0 2006 return AL_TRUE;
rlm@0 2007 case AL_FORMAT_STEREO_MULAW:
rlm@0 2008 *chans = UserFmtStereo;
rlm@0 2009 *type = UserFmtMulaw;
rlm@0 2010 return AL_TRUE;
rlm@0 2011 case AL_FORMAT_QUAD_MULAW:
rlm@0 2012 *chans = UserFmtQuad;
rlm@0 2013 *type = UserFmtMulaw;
rlm@0 2014 return AL_TRUE;
rlm@0 2015 case AL_FORMAT_REAR_MULAW:
rlm@0 2016 *chans = UserFmtRear;
rlm@0 2017 *type = UserFmtMulaw;
rlm@0 2018 return AL_TRUE;
rlm@0 2019 case AL_FORMAT_51CHN_MULAW:
rlm@0 2020 *chans = UserFmtX51;
rlm@0 2021 *type = UserFmtMulaw;
rlm@0 2022 return AL_TRUE;
rlm@0 2023 case AL_FORMAT_61CHN_MULAW:
rlm@0 2024 *chans = UserFmtX61;
rlm@0 2025 *type = UserFmtMulaw;
rlm@0 2026 return AL_TRUE;
rlm@0 2027 case AL_FORMAT_71CHN_MULAW:
rlm@0 2028 *chans = UserFmtX71;
rlm@0 2029 *type = UserFmtMulaw;
rlm@0 2030 return AL_TRUE;
rlm@0 2031 }
rlm@0 2032 return AL_FALSE;
rlm@0 2033 }
rlm@0 2034
rlm@0 2035 ALuint BytesFromFmt(enum FmtType type)
rlm@0 2036 {
rlm@0 2037 switch(type)
rlm@0 2038 {
rlm@0 2039 case FmtByte: return sizeof(ALbyte);
rlm@0 2040 case FmtShort: return sizeof(ALshort);
rlm@0 2041 case FmtFloat: return sizeof(ALfloat);
rlm@0 2042 }
rlm@0 2043 return 0;
rlm@0 2044 }
rlm@0 2045 ALuint ChannelsFromFmt(enum FmtChannels chans)
rlm@0 2046 {
rlm@0 2047 switch(chans)
rlm@0 2048 {
rlm@0 2049 case FmtMono: return 1;
rlm@0 2050 case FmtStereo: return 2;
rlm@0 2051 case FmtRear: return 2;
rlm@0 2052 case FmtQuad: return 4;
rlm@0 2053 case FmtX51: return 6;
rlm@0 2054 case FmtX61: return 7;
rlm@0 2055 case FmtX71: return 8;
rlm@0 2056 }
rlm@0 2057 return 0;
rlm@0 2058 }
rlm@0 2059 ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
rlm@0 2060 {
rlm@0 2061 switch(format)
rlm@0 2062 {
rlm@0 2063 case AL_MONO8:
rlm@0 2064 *chans = FmtMono;
rlm@0 2065 *type = FmtByte;
rlm@0 2066 return AL_TRUE;
rlm@0 2067 case AL_MONO16:
rlm@0 2068 *chans = FmtMono;
rlm@0 2069 *type = FmtShort;
rlm@0 2070 return AL_TRUE;
rlm@0 2071 case AL_MONO32F:
rlm@0 2072 *chans = FmtMono;
rlm@0 2073 *type = FmtFloat;
rlm@0 2074 return AL_TRUE;
rlm@0 2075 case AL_STEREO8:
rlm@0 2076 *chans = FmtStereo;
rlm@0 2077 *type = FmtByte;
rlm@0 2078 return AL_TRUE;
rlm@0 2079 case AL_STEREO16:
rlm@0 2080 *chans = FmtStereo;
rlm@0 2081 *type = FmtShort;
rlm@0 2082 return AL_TRUE;
rlm@0 2083 case AL_STEREO32F:
rlm@0 2084 *chans = FmtStereo;
rlm@0 2085 *type = FmtFloat;
rlm@0 2086 return AL_TRUE;
rlm@0 2087 case AL_FORMAT_QUAD8_LOKI:
rlm@0 2088 case AL_QUAD8:
rlm@0 2089 *chans = FmtQuad;
rlm@0 2090 *type = FmtByte;
rlm@0 2091 return AL_TRUE;
rlm@0 2092 case AL_FORMAT_QUAD16_LOKI:
rlm@0 2093 case AL_QUAD16:
rlm@0 2094 *chans = FmtQuad;
rlm@0 2095 *type = FmtShort;
rlm@0 2096 return AL_TRUE;
rlm@0 2097 case AL_QUAD32F:
rlm@0 2098 *chans = FmtQuad;
rlm@0 2099 *type = FmtFloat;
rlm@0 2100 return AL_TRUE;
rlm@0 2101 case AL_REAR8:
rlm@0 2102 *chans = FmtRear;
rlm@0 2103 *type = FmtByte;
rlm@0 2104 return AL_TRUE;
rlm@0 2105 case AL_REAR16:
rlm@0 2106 *chans = FmtRear;
rlm@0 2107 *type = FmtShort;
rlm@0 2108 return AL_TRUE;
rlm@0 2109 case AL_REAR32F:
rlm@0 2110 *chans = FmtRear;
rlm@0 2111 *type = FmtFloat;
rlm@0 2112 return AL_TRUE;
rlm@0 2113 case AL_5POINT1_8:
rlm@0 2114 *chans = FmtX51;
rlm@0 2115 *type = FmtByte;
rlm@0 2116 return AL_TRUE;
rlm@0 2117 case AL_5POINT1_16:
rlm@0 2118 *chans = FmtX51;
rlm@0 2119 *type = FmtShort;
rlm@0 2120 return AL_TRUE;
rlm@0 2121 case AL_5POINT1_32F:
rlm@0 2122 *chans = FmtX51;
rlm@0 2123 *type = FmtFloat;
rlm@0 2124 return AL_TRUE;
rlm@0 2125 case AL_6POINT1_8:
rlm@0 2126 *chans = FmtX61;
rlm@0 2127 *type = FmtByte;
rlm@0 2128 return AL_TRUE;
rlm@0 2129 case AL_6POINT1_16:
rlm@0 2130 *chans = FmtX61;
rlm@0 2131 *type = FmtShort;
rlm@0 2132 return AL_TRUE;
rlm@0 2133 case AL_6POINT1_32F:
rlm@0 2134 *chans = FmtX61;
rlm@0 2135 *type = FmtFloat;
rlm@0 2136 return AL_TRUE;
rlm@0 2137 case AL_7POINT1_8:
rlm@0 2138 *chans = FmtX71;
rlm@0 2139 *type = FmtByte;
rlm@0 2140 return AL_TRUE;
rlm@0 2141 case AL_7POINT1_16:
rlm@0 2142 *chans = FmtX71;
rlm@0 2143 *type = FmtShort;
rlm@0 2144 return AL_TRUE;
rlm@0 2145 case AL_7POINT1_32F:
rlm@0 2146 *chans = FmtX71;
rlm@0 2147 *type = FmtFloat;
rlm@0 2148 return AL_TRUE;
rlm@0 2149 }
rlm@0 2150 return AL_FALSE;
rlm@0 2151 }
rlm@0 2152
rlm@0 2153
rlm@0 2154 static ALboolean IsValidType(ALenum type)
rlm@0 2155 {
rlm@0 2156 switch(type)
rlm@0 2157 {
rlm@0 2158 case AL_BYTE:
rlm@0 2159 case AL_UNSIGNED_BYTE:
rlm@0 2160 case AL_SHORT:
rlm@0 2161 case AL_UNSIGNED_SHORT:
rlm@0 2162 case AL_INT:
rlm@0 2163 case AL_UNSIGNED_INT:
rlm@0 2164 case AL_FLOAT:
rlm@0 2165 case AL_DOUBLE:
rlm@0 2166 case AL_MULAW:
rlm@0 2167 case AL_IMA4:
rlm@0 2168 case AL_BYTE3:
rlm@0 2169 case AL_UNSIGNED_BYTE3:
rlm@0 2170 return AL_TRUE;
rlm@0 2171 }
rlm@0 2172 return AL_FALSE;
rlm@0 2173 }
rlm@0 2174
rlm@0 2175 static ALboolean IsValidChannels(ALenum channels)
rlm@0 2176 {
rlm@0 2177 switch(channels)
rlm@0 2178 {
rlm@0 2179 case AL_MONO:
rlm@0 2180 case AL_STEREO:
rlm@0 2181 case AL_REAR:
rlm@0 2182 case AL_QUAD:
rlm@0 2183 case AL_5POINT1:
rlm@0 2184 case AL_6POINT1:
rlm@0 2185 case AL_7POINT1:
rlm@0 2186 return AL_TRUE;
rlm@0 2187 }
rlm@0 2188 return AL_FALSE;
rlm@0 2189 }
rlm@0 2190
rlm@0 2191
rlm@0 2192 /*
rlm@0 2193 * ReleaseALBuffers()
rlm@0 2194 *
rlm@0 2195 * INTERNAL: Called to destroy any buffers that still exist on the device
rlm@0 2196 */
rlm@0 2197 ALvoid ReleaseALBuffers(ALCdevice *device)
rlm@0 2198 {
rlm@0 2199 ALsizei i;
rlm@0 2200 for(i = 0;i < device->BufferMap.size;i++)
rlm@0 2201 {
rlm@0 2202 ALbuffer *temp = device->BufferMap.array[i].value;
rlm@0 2203 device->BufferMap.array[i].value = NULL;
rlm@0 2204
rlm@0 2205 free(temp->data);
rlm@0 2206
rlm@0 2207 FreeThunkEntry(temp->buffer);
rlm@0 2208 memset(temp, 0, sizeof(ALbuffer));
rlm@0 2209 free(temp);
rlm@0 2210 }
rlm@0 2211 }