alBuffer.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <assert.h>
  24. #include <limits.h>
  25. #include "alMain.h"
  26. #include "AL/al.h"
  27. #include "AL/alc.h"
  28. #include "alError.h"
  29. #include "alBuffer.h"
  30. #include "alDatabuffer.h"
  31. #include "alThunk.h"
  32. static ALenum LoadData(ALbuffer *ALBuf, const ALvoid *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat);
  33. static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len);
  34. static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len);
  35. static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len);
  36. static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len);
  37. static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len);
  38. #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
  39. /*
  40. * Global Variables
  41. */
  42. static const long g_IMAStep_size[89]={ // IMA ADPCM Stepsize table
  43. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
  44. 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
  45. 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
  46. 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
  47. 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
  48. 15289,16818,18500,20350,22358,24633,27086,29794,32767
  49. };
  50. static const long g_IMACodeword_4[16]={ // IMA4 ADPCM Codeword decode table
  51. 1, 3, 5, 7, 9, 11, 13, 15,
  52. -1,-3,-5,-7,-9,-11,-13,-15,
  53. };
  54. static const long g_IMAIndex_adjust_4[16]={ // IMA4 ADPCM Step index adjust decode table
  55. -1,-1,-1,-1, 2, 4, 6, 8,
  56. -1,-1,-1,-1, 2, 4, 6, 8
  57. };
  58. static const ALshort muLawDecompressionTable[256] = {
  59. -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
  60. -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
  61. -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
  62. -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
  63. -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
  64. -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
  65. -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
  66. -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
  67. -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
  68. -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
  69. -876, -844, -812, -780, -748, -716, -684, -652,
  70. -620, -588, -556, -524, -492, -460, -428, -396,
  71. -372, -356, -340, -324, -308, -292, -276, -260,
  72. -244, -228, -212, -196, -180, -164, -148, -132,
  73. -120, -112, -104, -96, -88, -80, -72, -64,
  74. -56, -48, -40, -32, -24, -16, -8, 0,
  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. };
  92. /*
  93. * alGenBuffers(ALsizei n, ALuint *puiBuffers)
  94. *
  95. * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by puiBuffers
  96. */
  97. AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
  98. {
  99. ALCcontext *Context;
  100. ALsizei i=0;
  101. Context = GetContextSuspended();
  102. if(!Context) return;
  103. // Check that we are actually generation some Buffers
  104. if(n > 0)
  105. {
  106. ALCdevice *device = Context->Device;
  107. ALenum err;
  108. // Check the pointer is valid (and points to enough memory to store Buffer Names)
  109. if(IsBadWritePtr((void*)buffers, n * sizeof(ALuint)))
  110. alSetError(Context, AL_INVALID_VALUE);
  111. else
  112. {
  113. // Create all the new Buffers
  114. while(i < n)
  115. {
  116. ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
  117. if(!buffer)
  118. {
  119. alSetError(Context, AL_OUT_OF_MEMORY);
  120. alDeleteBuffers(i, buffers);
  121. break;
  122. }
  123. buffer->buffer = (ALuint)ALTHUNK_ADDENTRY(buffer);
  124. err = InsertUIntMapEntry(&device->BufferMap, buffer->buffer,
  125. buffer);
  126. if(err != AL_NO_ERROR)
  127. {
  128. ALTHUNK_REMOVEENTRY(buffer->buffer);
  129. memset(buffer, 0, sizeof(ALbuffer));
  130. free(buffer);
  131. alSetError(Context, err);
  132. alDeleteBuffers(i, buffers);
  133. break;
  134. }
  135. buffers[i++] = buffer->buffer;
  136. }
  137. }
  138. }
  139. ProcessContext(Context);
  140. }
  141. /*
  142. * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
  143. *
  144. * Deletes the n AL Buffers pointed to by puiBuffers
  145. */
  146. AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *puiBuffers)
  147. {
  148. ALCcontext *Context;
  149. ALbuffer *ALBuf;
  150. ALsizei i;
  151. Context = GetContextSuspended();
  152. if(!Context) return;
  153. // Check we are actually Deleting some Buffers
  154. if(n < 0)
  155. alSetError(Context, AL_INVALID_VALUE);
  156. else
  157. {
  158. ALCdevice *device = Context->Device;
  159. ALboolean bFailed = AL_FALSE;
  160. // Check that all the buffers are valid and can actually be deleted
  161. for (i = 0; i < n; i++)
  162. {
  163. if(!puiBuffers[i])
  164. continue;
  165. // Check for valid Buffer ID (can be NULL buffer)
  166. if((ALBuf=LookupBuffer(device->BufferMap, puiBuffers[i])) != NULL)
  167. {
  168. if(ALBuf->refcount != 0)
  169. {
  170. // Buffer still in use, cannot be deleted
  171. alSetError(Context, AL_INVALID_OPERATION);
  172. bFailed = AL_TRUE;
  173. break;
  174. }
  175. }
  176. else
  177. {
  178. // Invalid Buffer
  179. alSetError(Context, AL_INVALID_NAME);
  180. bFailed = AL_TRUE;
  181. break;
  182. }
  183. }
  184. // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
  185. if (!bFailed)
  186. {
  187. for (i = 0; i < n; i++)
  188. {
  189. if((ALBuf=LookupBuffer(device->BufferMap, puiBuffers[i])) != NULL)
  190. {
  191. // Release the memory used to store audio data
  192. free(ALBuf->data);
  193. // Release buffer structure
  194. RemoveUIntMapKey(&device->BufferMap, ALBuf->buffer);
  195. ALTHUNK_REMOVEENTRY(ALBuf->buffer);
  196. memset(ALBuf, 0, sizeof(ALbuffer));
  197. free(ALBuf);
  198. }
  199. }
  200. }
  201. }
  202. ProcessContext(Context);
  203. }
  204. /*
  205. * alIsBuffer(ALuint uiBuffer)
  206. *
  207. * Checks if ulBuffer is a valid Buffer Name
  208. */
  209. AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
  210. {
  211. ALCcontext *Context;
  212. ALboolean result;
  213. Context = GetContextSuspended();
  214. if(!Context) return AL_FALSE;
  215. result = ((!buffer || LookupBuffer(Context->Device->BufferMap, buffer)) ?
  216. AL_TRUE : AL_FALSE);
  217. ProcessContext(Context);
  218. return result;
  219. }
  220. /*
  221. * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
  222. *
  223. * Fill buffer with audio data
  224. */
  225. AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
  226. {
  227. ALCcontext *Context;
  228. ALCdevice *device;
  229. ALbuffer *ALBuf;
  230. ALvoid *temp;
  231. ALenum err;
  232. Context = GetContextSuspended();
  233. if(!Context) return;
  234. device = Context->Device;
  235. if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
  236. alSetError(Context, AL_INVALID_NAME); /* Invalid Buffer Name */
  237. else
  238. {
  239. if(Context->SampleSource)
  240. {
  241. ALintptrEXT offset;
  242. if(Context->SampleSource->state == MAPPED)
  243. {
  244. alSetError(Context, AL_INVALID_OPERATION);
  245. ProcessContext(Context);
  246. return;
  247. }
  248. offset = (const ALubyte*)data - (ALubyte*)NULL;
  249. data = Context->SampleSource->data + offset;
  250. }
  251. if(size < 0)
  252. alSetError(Context, AL_INVALID_VALUE);
  253. else if(ALBuf->refcount != 0)
  254. alSetError(Context, AL_INVALID_VALUE);
  255. else
  256. {
  257. switch(format)
  258. {
  259. case AL_FORMAT_MONO8:
  260. case AL_FORMAT_MONO16:
  261. case AL_FORMAT_MONO_FLOAT32:
  262. case AL_FORMAT_MONO_DOUBLE_EXT:
  263. err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO_FLOAT32);
  264. if(err != AL_NO_ERROR)
  265. alSetError(Context, err);
  266. break;
  267. case AL_FORMAT_STEREO8:
  268. case AL_FORMAT_STEREO16:
  269. case AL_FORMAT_STEREO_FLOAT32:
  270. case AL_FORMAT_STEREO_DOUBLE_EXT:
  271. err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO_FLOAT32);
  272. if(err != AL_NO_ERROR)
  273. alSetError(Context, err);
  274. break;
  275. case AL_FORMAT_REAR8:
  276. case AL_FORMAT_REAR16:
  277. case AL_FORMAT_REAR32: {
  278. ALenum NewFormat = AL_FORMAT_QUAD32;
  279. ALuint NewChannels = aluChannelsFromFormat(NewFormat);
  280. ALuint NewBytes = aluBytesFromFormat(NewFormat);
  281. ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
  282. ((format==AL_FORMAT_REAR16) ? 2 :
  283. 4));
  284. ALuint64 newsize, allocsize;
  285. if((size%(OrigBytes*2)) != 0)
  286. {
  287. alSetError(Context, AL_INVALID_VALUE);
  288. break;
  289. }
  290. newsize = size / OrigBytes;
  291. newsize *= 2;
  292. allocsize = (BUFFER_PADDING*NewChannels + newsize)*NewBytes;
  293. if(allocsize > INT_MAX)
  294. {
  295. alSetError(Context, AL_OUT_OF_MEMORY);
  296. break;
  297. }
  298. temp = realloc(ALBuf->data, allocsize);
  299. if(temp)
  300. {
  301. ALBuf->data = temp;
  302. ConvertDataRear(ALBuf->data, data, OrigBytes, newsize);
  303. ALBuf->format = NewFormat;
  304. ALBuf->eOriginalFormat = format;
  305. ALBuf->size = newsize*NewBytes;
  306. ALBuf->frequency = freq;
  307. ALBuf->LoopStart = 0;
  308. ALBuf->LoopEnd = newsize / NewChannels;
  309. ALBuf->OriginalSize = size;
  310. ALBuf->OriginalAlign = OrigBytes * 2;
  311. }
  312. else
  313. alSetError(Context, AL_OUT_OF_MEMORY);
  314. } break;
  315. case AL_FORMAT_QUAD8_LOKI:
  316. case AL_FORMAT_QUAD16_LOKI:
  317. case AL_FORMAT_QUAD8:
  318. case AL_FORMAT_QUAD16:
  319. case AL_FORMAT_QUAD32:
  320. err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_QUAD32);
  321. if(err != AL_NO_ERROR)
  322. alSetError(Context, err);
  323. break;
  324. case AL_FORMAT_51CHN8:
  325. case AL_FORMAT_51CHN16:
  326. case AL_FORMAT_51CHN32:
  327. err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_51CHN32);
  328. if(err != AL_NO_ERROR)
  329. alSetError(Context, err);
  330. break;
  331. case AL_FORMAT_61CHN8:
  332. case AL_FORMAT_61CHN16:
  333. case AL_FORMAT_61CHN32:
  334. err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_61CHN32);
  335. if(err != AL_NO_ERROR)
  336. alSetError(Context, err);
  337. break;
  338. case AL_FORMAT_71CHN8:
  339. case AL_FORMAT_71CHN16:
  340. case AL_FORMAT_71CHN32:
  341. err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_71CHN32);
  342. if(err != AL_NO_ERROR)
  343. alSetError(Context, err);
  344. break;
  345. case AL_FORMAT_MONO_IMA4:
  346. case AL_FORMAT_STEREO_IMA4: {
  347. int Channels = ((format==AL_FORMAT_MONO_IMA4) ? 1 : 2);
  348. ALenum NewFormat = ((Channels==1) ? AL_FORMAT_MONO_FLOAT32 :
  349. AL_FORMAT_STEREO_FLOAT32);
  350. ALuint NewBytes = aluBytesFromFormat(NewFormat);
  351. ALuint64 newsize, allocsize;
  352. // Here is where things vary:
  353. // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
  354. // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
  355. if((size%(36*Channels)) != 0)
  356. {
  357. alSetError(Context, AL_INVALID_VALUE);
  358. break;
  359. }
  360. newsize = size / 36;
  361. newsize *= 65;
  362. allocsize = (BUFFER_PADDING*Channels + newsize)*NewBytes;
  363. if(allocsize > INT_MAX)
  364. {
  365. alSetError(Context, AL_OUT_OF_MEMORY);
  366. break;
  367. }
  368. temp = realloc(ALBuf->data, allocsize);
  369. if(temp)
  370. {
  371. ALBuf->data = temp;
  372. ConvertDataIMA4(ALBuf->data, data, Channels, newsize/(65*Channels));
  373. ALBuf->format = NewFormat;
  374. ALBuf->eOriginalFormat = format;
  375. ALBuf->size = newsize*NewBytes;
  376. ALBuf->frequency = freq;
  377. ALBuf->LoopStart = 0;
  378. ALBuf->LoopEnd = newsize / Channels;
  379. ALBuf->OriginalSize = size;
  380. ALBuf->OriginalAlign = 36 * Channels;
  381. }
  382. else
  383. alSetError(Context, AL_OUT_OF_MEMORY);
  384. } break;
  385. case AL_FORMAT_MONO_MULAW:
  386. case AL_FORMAT_STEREO_MULAW:
  387. case AL_FORMAT_QUAD_MULAW:
  388. case AL_FORMAT_51CHN_MULAW:
  389. case AL_FORMAT_61CHN_MULAW:
  390. case AL_FORMAT_71CHN_MULAW: {
  391. int Channels = ((format==AL_FORMAT_MONO_MULAW) ? 1 :
  392. ((format==AL_FORMAT_STEREO_MULAW) ? 2 :
  393. ((format==AL_FORMAT_QUAD_MULAW) ? 4 :
  394. ((format==AL_FORMAT_51CHN_MULAW) ? 6 :
  395. ((format==AL_FORMAT_61CHN_MULAW) ? 7 : 8)))));
  396. ALenum NewFormat = ((Channels==1) ? AL_FORMAT_MONO_FLOAT32 :
  397. ((Channels==2) ? AL_FORMAT_STEREO_FLOAT32 :
  398. ((Channels==4) ? AL_FORMAT_QUAD32 :
  399. ((Channels==6) ? AL_FORMAT_51CHN32 :
  400. ((Channels==7) ? AL_FORMAT_61CHN32 :
  401. AL_FORMAT_71CHN32)))));
  402. ALuint NewBytes = aluBytesFromFormat(NewFormat);
  403. ALuint64 allocsize;
  404. if((size%(1*Channels)) != 0)
  405. {
  406. alSetError(Context, AL_INVALID_VALUE);
  407. break;
  408. }
  409. allocsize = (BUFFER_PADDING*Channels + size)*NewBytes;
  410. if(allocsize > INT_MAX)
  411. {
  412. alSetError(Context, AL_OUT_OF_MEMORY);
  413. break;
  414. }
  415. temp = realloc(ALBuf->data, allocsize);
  416. if(temp)
  417. {
  418. ALBuf->data = temp;
  419. ConvertDataMULaw(ALBuf->data, data, size);
  420. ALBuf->format = NewFormat;
  421. ALBuf->eOriginalFormat = format;
  422. ALBuf->size = size*NewBytes;
  423. ALBuf->frequency = freq;
  424. ALBuf->LoopStart = 0;
  425. ALBuf->LoopEnd = size / Channels;
  426. ALBuf->OriginalSize = size;
  427. ALBuf->OriginalAlign = 1 * Channels;
  428. }
  429. else
  430. alSetError(Context, AL_OUT_OF_MEMORY);
  431. } break;
  432. case AL_FORMAT_REAR_MULAW: {
  433. ALenum NewFormat = AL_FORMAT_QUAD32;
  434. ALuint NewChannels = aluChannelsFromFormat(NewFormat);
  435. ALuint NewBytes = aluBytesFromFormat(NewFormat);
  436. ALuint64 newsize, allocsize;
  437. if((size%(1*2)) != 0)
  438. {
  439. alSetError(Context, AL_INVALID_VALUE);
  440. break;
  441. }
  442. newsize = size * 2;
  443. allocsize = (BUFFER_PADDING*NewChannels + newsize)*NewBytes;
  444. if(allocsize > INT_MAX)
  445. {
  446. alSetError(Context, AL_OUT_OF_MEMORY);
  447. break;
  448. }
  449. temp = realloc(ALBuf->data, allocsize);
  450. if(temp)
  451. {
  452. ALBuf->data = temp;
  453. ConvertDataMULawRear(ALBuf->data, data, newsize);
  454. ALBuf->format = NewFormat;
  455. ALBuf->eOriginalFormat = format;
  456. ALBuf->size = newsize*NewBytes;
  457. ALBuf->frequency = freq;
  458. ALBuf->LoopStart = 0;
  459. ALBuf->LoopEnd = newsize / NewChannels;
  460. ALBuf->OriginalSize = size;
  461. ALBuf->OriginalAlign = 1 * 2;
  462. }
  463. else
  464. alSetError(Context, AL_OUT_OF_MEMORY);
  465. } break;
  466. default:
  467. alSetError(Context, AL_INVALID_ENUM);
  468. break;
  469. }
  470. }
  471. }
  472. ProcessContext(Context);
  473. }
  474. /*
  475. * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
  476. *
  477. * Fill buffer with audio data
  478. */
  479. AL_API ALvoid AL_APIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
  480. {
  481. ALCcontext *Context;
  482. ALCdevice *device;
  483. ALbuffer *ALBuf;
  484. Context = GetContextSuspended();
  485. if(!Context) return;
  486. device = Context->Device;
  487. if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
  488. alSetError(Context, AL_INVALID_NAME);
  489. else
  490. {
  491. if(Context->SampleSource)
  492. {
  493. ALintptrEXT offset;
  494. if(Context->SampleSource->state == MAPPED)
  495. {
  496. alSetError(Context, AL_INVALID_OPERATION);
  497. ProcessContext(Context);
  498. return;
  499. }
  500. offset = (const ALubyte*)data - (ALubyte*)NULL;
  501. data = Context->SampleSource->data + offset;
  502. }
  503. if(length < 0 || offset < 0 || (length > 0 && data == NULL))
  504. alSetError(Context, AL_INVALID_VALUE);
  505. else if(ALBuf->eOriginalFormat != format)
  506. alSetError(Context, AL_INVALID_ENUM);
  507. else if(offset+length < offset ||
  508. offset+length > ALBuf->OriginalSize ||
  509. (offset%ALBuf->OriginalAlign) != 0 ||
  510. (length%ALBuf->OriginalAlign) != 0)
  511. alSetError(Context, AL_INVALID_VALUE);
  512. else
  513. {
  514. switch(format)
  515. {
  516. case AL_FORMAT_MONO8:
  517. case AL_FORMAT_MONO16:
  518. case AL_FORMAT_MONO_FLOAT32:
  519. case AL_FORMAT_MONO_DOUBLE_EXT:
  520. case AL_FORMAT_STEREO8:
  521. case AL_FORMAT_STEREO16:
  522. case AL_FORMAT_STEREO_FLOAT32:
  523. case AL_FORMAT_STEREO_DOUBLE_EXT:
  524. case AL_FORMAT_QUAD8_LOKI:
  525. case AL_FORMAT_QUAD16_LOKI:
  526. case AL_FORMAT_QUAD8:
  527. case AL_FORMAT_QUAD16:
  528. case AL_FORMAT_QUAD32:
  529. case AL_FORMAT_51CHN8:
  530. case AL_FORMAT_51CHN16:
  531. case AL_FORMAT_51CHN32:
  532. case AL_FORMAT_61CHN8:
  533. case AL_FORMAT_61CHN16:
  534. case AL_FORMAT_61CHN32:
  535. case AL_FORMAT_71CHN8:
  536. case AL_FORMAT_71CHN16:
  537. case AL_FORMAT_71CHN32: {
  538. ALuint Bytes = aluBytesFromFormat(format);
  539. offset /= Bytes;
  540. length /= Bytes;
  541. ConvertData(&ALBuf->data[offset], data, Bytes, length);
  542. } break;
  543. case AL_FORMAT_REAR8:
  544. case AL_FORMAT_REAR16:
  545. case AL_FORMAT_REAR32: {
  546. ALuint Bytes = ((format==AL_FORMAT_REAR8) ? 1 :
  547. ((format==AL_FORMAT_REAR16) ? 2 :
  548. 4));
  549. offset /= Bytes;
  550. offset *= 2;
  551. length /= Bytes;
  552. length *= 2;
  553. ConvertDataRear(&ALBuf->data[offset], data, Bytes, length);
  554. } break;
  555. case AL_FORMAT_MONO_IMA4:
  556. case AL_FORMAT_STEREO_IMA4: {
  557. int Channels = aluChannelsFromFormat(ALBuf->format);
  558. // offset -> sample*channel offset, length -> block count
  559. offset /= 36;
  560. offset *= 65;
  561. length /= ALBuf->OriginalAlign;
  562. ConvertDataIMA4(&ALBuf->data[offset], data, Channels, length);
  563. } break;
  564. case AL_FORMAT_MONO_MULAW:
  565. case AL_FORMAT_STEREO_MULAW:
  566. case AL_FORMAT_QUAD_MULAW:
  567. case AL_FORMAT_51CHN_MULAW:
  568. case AL_FORMAT_61CHN_MULAW:
  569. case AL_FORMAT_71CHN_MULAW:
  570. ConvertDataMULaw(&ALBuf->data[offset], data, length);
  571. break;
  572. case AL_FORMAT_REAR_MULAW:
  573. offset *= 2;
  574. length *= 2;
  575. ConvertDataMULawRear(&ALBuf->data[offset], data, length);
  576. break;
  577. default:
  578. alSetError(Context, AL_INVALID_ENUM);
  579. break;
  580. }
  581. }
  582. }
  583. ProcessContext(Context);
  584. }
  585. AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
  586. {
  587. ALCcontext *pContext;
  588. ALCdevice *device;
  589. (void)flValue;
  590. pContext = GetContextSuspended();
  591. if(!pContext) return;
  592. device = pContext->Device;
  593. if(LookupBuffer(device->BufferMap, buffer) == NULL)
  594. alSetError(pContext, AL_INVALID_NAME);
  595. {
  596. switch(eParam)
  597. {
  598. default:
  599. alSetError(pContext, AL_INVALID_ENUM);
  600. break;
  601. }
  602. }
  603. ProcessContext(pContext);
  604. }
  605. AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
  606. {
  607. ALCcontext *pContext;
  608. ALCdevice *device;
  609. (void)flValue1;
  610. (void)flValue2;
  611. (void)flValue3;
  612. pContext = GetContextSuspended();
  613. if(!pContext) return;
  614. device = pContext->Device;
  615. if(LookupBuffer(device->BufferMap, buffer) == NULL)
  616. alSetError(pContext, AL_INVALID_NAME);
  617. else
  618. {
  619. switch(eParam)
  620. {
  621. default:
  622. alSetError(pContext, AL_INVALID_ENUM);
  623. break;
  624. }
  625. }
  626. ProcessContext(pContext);
  627. }
  628. AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
  629. {
  630. ALCcontext *pContext;
  631. ALCdevice *device;
  632. pContext = GetContextSuspended();
  633. if(!pContext) return;
  634. device = pContext->Device;
  635. if(!flValues)
  636. alSetError(pContext, AL_INVALID_VALUE);
  637. else if(LookupBuffer(device->BufferMap, buffer) == NULL)
  638. alSetError(pContext, AL_INVALID_NAME);
  639. else
  640. {
  641. switch(eParam)
  642. {
  643. default:
  644. alSetError(pContext, AL_INVALID_ENUM);
  645. break;
  646. }
  647. }
  648. ProcessContext(pContext);
  649. }
  650. AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
  651. {
  652. ALCcontext *pContext;
  653. ALCdevice *device;
  654. (void)lValue;
  655. pContext = GetContextSuspended();
  656. if(!pContext) return;
  657. device = pContext->Device;
  658. if(LookupBuffer(device->BufferMap, buffer) == NULL)
  659. alSetError(pContext, AL_INVALID_NAME);
  660. else
  661. {
  662. switch(eParam)
  663. {
  664. default:
  665. alSetError(pContext, AL_INVALID_ENUM);
  666. break;
  667. }
  668. }
  669. ProcessContext(pContext);
  670. }
  671. AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
  672. {
  673. ALCcontext *pContext;
  674. ALCdevice *device;
  675. (void)lValue1;
  676. (void)lValue2;
  677. (void)lValue3;
  678. pContext = GetContextSuspended();
  679. if(!pContext) return;
  680. device = pContext->Device;
  681. if(LookupBuffer(device->BufferMap, buffer) == NULL)
  682. alSetError(pContext, AL_INVALID_NAME);
  683. else
  684. {
  685. switch(eParam)
  686. {
  687. default:
  688. alSetError(pContext, AL_INVALID_ENUM);
  689. break;
  690. }
  691. }
  692. ProcessContext(pContext);
  693. }
  694. AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
  695. {
  696. ALCcontext *pContext;
  697. ALCdevice *device;
  698. ALbuffer *ALBuf;
  699. pContext = GetContextSuspended();
  700. if(!pContext) return;
  701. device = pContext->Device;
  702. if(!plValues)
  703. alSetError(pContext, AL_INVALID_VALUE);
  704. else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
  705. alSetError(pContext, AL_INVALID_NAME);
  706. else
  707. {
  708. switch(eParam)
  709. {
  710. case AL_LOOP_POINTS:
  711. if(ALBuf->refcount > 0)
  712. alSetError(pContext, AL_INVALID_OPERATION);
  713. else if(plValues[0] < 0 || plValues[1] < 0 ||
  714. plValues[0] >= plValues[1] || ALBuf->size == 0)
  715. alSetError(pContext, AL_INVALID_VALUE);
  716. else
  717. {
  718. ALint maxlen = ALBuf->size / aluFrameSizeFromFormat(ALBuf->format);
  719. if(plValues[0] > maxlen || plValues[1] > maxlen)
  720. alSetError(pContext, AL_INVALID_VALUE);
  721. else
  722. {
  723. ALBuf->LoopStart = plValues[0];
  724. ALBuf->LoopEnd = plValues[1];
  725. }
  726. }
  727. break;
  728. default:
  729. alSetError(pContext, AL_INVALID_ENUM);
  730. break;
  731. }
  732. }
  733. ProcessContext(pContext);
  734. }
  735. AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
  736. {
  737. ALCcontext *pContext;
  738. ALCdevice *device;
  739. pContext = GetContextSuspended();
  740. if(!pContext) return;
  741. device = pContext->Device;
  742. if(!pflValue)
  743. alSetError(pContext, AL_INVALID_VALUE);
  744. else if(LookupBuffer(device->BufferMap, buffer) == NULL)
  745. alSetError(pContext, AL_INVALID_NAME);
  746. else
  747. {
  748. switch(eParam)
  749. {
  750. default:
  751. alSetError(pContext, AL_INVALID_ENUM);
  752. break;
  753. }
  754. }
  755. ProcessContext(pContext);
  756. }
  757. AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
  758. {
  759. ALCcontext *pContext;
  760. ALCdevice *device;
  761. pContext = GetContextSuspended();
  762. if(!pContext) return;
  763. device = pContext->Device;
  764. if(!pflValue1 || !pflValue2 || !pflValue3)
  765. alSetError(pContext, AL_INVALID_VALUE);
  766. else if(LookupBuffer(device->BufferMap, buffer) == NULL)
  767. alSetError(pContext, AL_INVALID_NAME);
  768. else
  769. {
  770. switch(eParam)
  771. {
  772. default:
  773. alSetError(pContext, AL_INVALID_ENUM);
  774. break;
  775. }
  776. }
  777. ProcessContext(pContext);
  778. }
  779. AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
  780. {
  781. ALCcontext *pContext;
  782. ALCdevice *device;
  783. pContext = GetContextSuspended();
  784. if(!pContext) return;
  785. device = pContext->Device;
  786. if(!pflValues)
  787. alSetError(pContext, AL_INVALID_VALUE);
  788. else if(LookupBuffer(device->BufferMap, buffer) == NULL)
  789. alSetError(pContext, AL_INVALID_NAME);
  790. else
  791. {
  792. switch(eParam)
  793. {
  794. default:
  795. alSetError(pContext, AL_INVALID_ENUM);
  796. break;
  797. }
  798. }
  799. ProcessContext(pContext);
  800. }
  801. AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
  802. {
  803. ALCcontext *pContext;
  804. ALbuffer *pBuffer;
  805. ALCdevice *device;
  806. pContext = GetContextSuspended();
  807. if(!pContext) return;
  808. device = pContext->Device;
  809. if(!plValue)
  810. alSetError(pContext, AL_INVALID_VALUE);
  811. else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
  812. alSetError(pContext, AL_INVALID_NAME);
  813. else
  814. {
  815. switch(eParam)
  816. {
  817. case AL_FREQUENCY:
  818. *plValue = pBuffer->frequency;
  819. break;
  820. case AL_BITS:
  821. *plValue = aluBytesFromFormat(pBuffer->format) * 8;
  822. break;
  823. case AL_CHANNELS:
  824. *plValue = aluChannelsFromFormat(pBuffer->format);
  825. break;
  826. case AL_SIZE:
  827. *plValue = pBuffer->size;
  828. break;
  829. default:
  830. alSetError(pContext, AL_INVALID_ENUM);
  831. break;
  832. }
  833. }
  834. ProcessContext(pContext);
  835. }
  836. AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
  837. {
  838. ALCcontext *pContext;
  839. ALCdevice *device;
  840. pContext = GetContextSuspended();
  841. if(!pContext) return;
  842. device = pContext->Device;
  843. if(!plValue1 || !plValue2 || !plValue3)
  844. alSetError(pContext, AL_INVALID_VALUE);
  845. else if(LookupBuffer(device->BufferMap, buffer) == NULL)
  846. alSetError(pContext, AL_INVALID_NAME);
  847. else
  848. {
  849. switch(eParam)
  850. {
  851. default:
  852. alSetError(pContext, AL_INVALID_ENUM);
  853. break;
  854. }
  855. }
  856. ProcessContext(pContext);
  857. }
  858. AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
  859. {
  860. ALCcontext *pContext;
  861. ALCdevice *device;
  862. ALbuffer *ALBuf;
  863. pContext = GetContextSuspended();
  864. if(!pContext) return;
  865. device = pContext->Device;
  866. if(!plValues)
  867. alSetError(pContext, AL_INVALID_VALUE);
  868. else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
  869. alSetError(pContext, AL_INVALID_NAME);
  870. else
  871. {
  872. switch(eParam)
  873. {
  874. case AL_FREQUENCY:
  875. case AL_BITS:
  876. case AL_CHANNELS:
  877. case AL_SIZE:
  878. alGetBufferi(buffer, eParam, plValues);
  879. break;
  880. case AL_LOOP_POINTS:
  881. plValues[0] = ALBuf->LoopStart;
  882. plValues[1] = ALBuf->LoopEnd;
  883. break;
  884. default:
  885. alSetError(pContext, AL_INVALID_ENUM);
  886. break;
  887. }
  888. }
  889. ProcessContext(pContext);
  890. }
  891. /*
  892. * LoadData
  893. *
  894. * Loads the specified data into the buffer, using the specified formats.
  895. * Currently, the new format must be 32-bit float, and must have the same
  896. * channel configuration as the original format. This does NOT handle
  897. * compressed formats (eg. IMA4).
  898. */
  899. static ALenum LoadData(ALbuffer *ALBuf, const ALvoid *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
  900. {
  901. ALuint NewBytes = aluBytesFromFormat(NewFormat);
  902. ALuint NewChannels = aluChannelsFromFormat(NewFormat);
  903. ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
  904. ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
  905. ALuint64 newsize, allocsize;
  906. ALvoid *temp;
  907. assert(NewBytes == 4);
  908. assert(NewChannels == OrigChannels);
  909. if ((size%(OrigBytes*OrigChannels)) != 0)
  910. return AL_INVALID_VALUE;
  911. // Allocate extra padding samples
  912. newsize = size / OrigBytes;
  913. allocsize = (BUFFER_PADDING*NewChannels + newsize)*NewBytes;
  914. if(allocsize > INT_MAX)
  915. return AL_OUT_OF_MEMORY;
  916. temp = realloc(ALBuf->data, allocsize);
  917. if(!temp) return AL_OUT_OF_MEMORY;
  918. ALBuf->data = temp;
  919. // Samples are converted here
  920. ConvertData(ALBuf->data, data, OrigBytes, newsize);
  921. ALBuf->format = NewFormat;
  922. ALBuf->eOriginalFormat = OrigFormat;
  923. ALBuf->size = newsize*NewBytes;
  924. ALBuf->frequency = freq;
  925. ALBuf->LoopStart = 0;
  926. ALBuf->LoopEnd = newsize / NewChannels;
  927. ALBuf->OriginalSize = size;
  928. ALBuf->OriginalAlign = OrigBytes * OrigChannels;
  929. return AL_NO_ERROR;
  930. }
  931. static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
  932. {
  933. ALsizei i;
  934. ALint smp;
  935. if(src == NULL)
  936. return;
  937. switch(origBytes)
  938. {
  939. case 1:
  940. for(i = 0;i < len;i++)
  941. {
  942. smp = ((ALubyte*)src)[i];
  943. dst[i] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
  944. }
  945. break;
  946. case 2:
  947. for(i = 0;i < len;i++)
  948. {
  949. smp = ((ALshort*)src)[i];
  950. dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
  951. }
  952. break;
  953. case 4:
  954. for(i = 0;i < len;i++)
  955. dst[i] = ((ALfloat*)src)[i];
  956. break;
  957. case 8:
  958. for(i = 0;i < len;i++)
  959. dst[i] = ((ALdouble*)src)[i];
  960. break;
  961. default:
  962. assert(0);
  963. }
  964. }
  965. static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
  966. {
  967. ALsizei i;
  968. ALint smp;
  969. if(src == NULL)
  970. return;
  971. switch(origBytes)
  972. {
  973. case 1:
  974. for(i = 0;i < len;i+=4)
  975. {
  976. dst[i+0] = 0;
  977. dst[i+1] = 0;
  978. smp = ((ALubyte*)src)[i/2+0];
  979. dst[i+2] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
  980. smp = ((ALubyte*)src)[i/2+1];
  981. dst[i+3] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
  982. }
  983. break;
  984. case 2:
  985. for(i = 0;i < len;i+=4)
  986. {
  987. dst[i+0] = 0;
  988. dst[i+1] = 0;
  989. smp = ((ALshort*)src)[i/2+0];
  990. dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
  991. smp = ((ALshort*)src)[i/2+1];
  992. dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
  993. }
  994. break;
  995. case 4:
  996. for(i = 0;i < len;i+=4)
  997. {
  998. dst[i+0] = 0;
  999. dst[i+1] = 0;
  1000. dst[i+2] = ((ALfloat*)src)[i/2+0];
  1001. dst[i+3] = ((ALfloat*)src)[i/2+1];
  1002. }
  1003. break;
  1004. default:
  1005. assert(0);
  1006. }
  1007. }
  1008. static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint chans, ALsizei len)
  1009. {
  1010. const ALubyte *IMAData;
  1011. ALint Sample[2],Index[2];
  1012. ALuint IMACode[2];
  1013. ALsizei i,j,k,c;
  1014. if(src == NULL)
  1015. return;
  1016. IMAData = src;
  1017. for(i = 0;i < len;i++)
  1018. {
  1019. for(c = 0;c < chans;c++)
  1020. {
  1021. Sample[c] = *(IMAData++);
  1022. Sample[c] |= *(IMAData++) << 8;
  1023. Sample[c] = (Sample[c]^0x8000) - 32768;
  1024. Index[c] = *(IMAData++);
  1025. Index[c] |= *(IMAData++) << 8;
  1026. Index[c] = (Index[c]^0x8000) - 32768;
  1027. Index[c] = ((Index[c]<0) ? 0 : Index[c]);
  1028. Index[c] = ((Index[c]>88) ? 88 : Index[c]);
  1029. dst[i*65*chans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
  1030. }
  1031. for(j = 1;j < 65;j += 8)
  1032. {
  1033. for(c = 0;c < chans;c++)
  1034. {
  1035. IMACode[c] = *(IMAData++);
  1036. IMACode[c] |= *(IMAData++) << 8;
  1037. IMACode[c] |= *(IMAData++) << 16;
  1038. IMACode[c] |= *(IMAData++) << 24;
  1039. }
  1040. for(k = 0;k < 8;k++)
  1041. {
  1042. for(c = 0;c < chans;c++)
  1043. {
  1044. Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
  1045. Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
  1046. if(Sample[c] < -32768) Sample[c] = -32768;
  1047. else if(Sample[c] > 32767) Sample[c] = 32767;
  1048. if(Index[c]<0) Index[c] = 0;
  1049. else if(Index[c]>88) Index[c] = 88;
  1050. dst[(i*65+j+k)*chans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
  1051. IMACode[c] >>= 4;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len)
  1058. {
  1059. ALsizei i;
  1060. ALint smp;
  1061. if(src == NULL)
  1062. return;
  1063. for(i = 0;i < len;i++)
  1064. {
  1065. smp = muLawDecompressionTable[((ALubyte*)src)[i]];
  1066. dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
  1067. }
  1068. }
  1069. static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len)
  1070. {
  1071. ALsizei i;
  1072. ALint smp;
  1073. if(src == NULL)
  1074. return;
  1075. for(i = 0;i < len;i+=4)
  1076. {
  1077. dst[i+0] = 0;
  1078. dst[i+1] = 0;
  1079. smp = muLawDecompressionTable[((ALubyte*)src)[i/2+0]];
  1080. dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
  1081. smp = muLawDecompressionTable[((ALubyte*)src)[i/2+1]];
  1082. dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
  1083. }
  1084. }
  1085. /*
  1086. * ReleaseALBuffers()
  1087. *
  1088. * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
  1089. */
  1090. ALvoid ReleaseALBuffers(ALCdevice *device)
  1091. {
  1092. ALsizei i;
  1093. for(i = 0;i < device->BufferMap.size;i++)
  1094. {
  1095. ALbuffer *temp = device->BufferMap.array[i].value;
  1096. device->BufferMap.array[i].value = NULL;
  1097. // Release sample data
  1098. free(temp->data);
  1099. // Release Buffer structure
  1100. ALTHUNK_REMOVEENTRY(temp->buffer);
  1101. memset(temp, 0, sizeof(ALbuffer));
  1102. free(temp);
  1103. }
  1104. }