alSoundfont.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "alMain.h"
  6. #include "alMidi.h"
  7. #include "alThunk.h"
  8. #include "alError.h"
  9. #include "midi/base.h"
  10. extern inline struct ALsoundfont *LookupSfont(ALCdevice *device, ALuint id);
  11. extern inline struct ALsoundfont *RemoveSfont(ALCdevice *device, ALuint id);
  12. void ALsoundfont_Construct(ALsoundfont *self);
  13. void ALsoundfont_Destruct(ALsoundfont *self);
  14. void ALsoundfont_deleteSoundfont(ALsoundfont *self, ALCdevice *device);
  15. ALsoundfont *ALsoundfont_getDefSoundfont(ALCcontext *context);
  16. static size_t ALsoundfont_read(ALvoid *buf, size_t bytes, ALvoid *ptr);
  17. AL_API void AL_APIENTRY alGenSoundfontsSOFT(ALsizei n, ALuint *ids)
  18. {
  19. ALCdevice *device;
  20. ALCcontext *context;
  21. ALsizei cur = 0;
  22. ALenum err;
  23. context = GetContextRef();
  24. if(!context) return;
  25. if(!(n >= 0))
  26. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  27. device = context->Device;
  28. for(cur = 0;cur < n;cur++)
  29. {
  30. ALsoundfont *sfont = calloc(1, sizeof(ALsoundfont));
  31. if(!sfont)
  32. {
  33. alDeleteSoundfontsSOFT(cur, ids);
  34. SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done);
  35. }
  36. ALsoundfont_Construct(sfont);
  37. err = NewThunkEntry(&sfont->id);
  38. if(err == AL_NO_ERROR)
  39. err = InsertUIntMapEntry(&device->SfontMap, sfont->id, sfont);
  40. if(err != AL_NO_ERROR)
  41. {
  42. ALsoundfont_Destruct(sfont);
  43. memset(sfont, 0, sizeof(ALsoundfont));
  44. free(sfont);
  45. alDeleteSoundfontsSOFT(cur, ids);
  46. SET_ERROR_AND_GOTO(context, err, done);
  47. }
  48. ids[cur] = sfont->id;
  49. }
  50. done:
  51. ALCcontext_DecRef(context);
  52. }
  53. AL_API ALvoid AL_APIENTRY alDeleteSoundfontsSOFT(ALsizei n, const ALuint *ids)
  54. {
  55. ALCdevice *device;
  56. ALCcontext *context;
  57. ALsoundfont *sfont;
  58. ALsizei i;
  59. context = GetContextRef();
  60. if(!context) return;
  61. if(!(n >= 0))
  62. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  63. device = context->Device;
  64. for(i = 0;i < n;i++)
  65. {
  66. /* Check for valid soundfont ID */
  67. if(ids[i] == 0)
  68. {
  69. if(!(sfont=device->DefaultSfont))
  70. continue;
  71. }
  72. else if((sfont=LookupSfont(device, ids[i])) == NULL)
  73. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  74. if(sfont->Mapped != AL_FALSE || sfont->ref != 0)
  75. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  76. }
  77. for(i = 0;i < n;i++)
  78. {
  79. if(ids[i] == 0)
  80. {
  81. MidiSynth *synth = device->Synth;
  82. WriteLock(&synth->Lock);
  83. if(device->DefaultSfont != NULL)
  84. ALsoundfont_deleteSoundfont(device->DefaultSfont, device);
  85. device->DefaultSfont = NULL;
  86. WriteUnlock(&synth->Lock);
  87. continue;
  88. }
  89. else if((sfont=RemoveSfont(device, ids[i])) == NULL)
  90. continue;
  91. ALsoundfont_Destruct(sfont);
  92. memset(sfont, 0, sizeof(*sfont));
  93. free(sfont);
  94. }
  95. done:
  96. ALCcontext_DecRef(context);
  97. }
  98. AL_API ALboolean AL_APIENTRY alIsSoundfontSOFT(ALuint id)
  99. {
  100. ALCcontext *context;
  101. ALboolean ret;
  102. context = GetContextRef();
  103. if(!context) return AL_FALSE;
  104. ret = ((!id || LookupSfont(context->Device, id)) ?
  105. AL_TRUE : AL_FALSE);
  106. ALCcontext_DecRef(context);
  107. return ret;
  108. }
  109. AL_API ALvoid AL_APIENTRY alSoundfontSamplesSOFT(ALuint id, ALenum type, ALsizei count, const ALvoid *samples)
  110. {
  111. ALCdevice *device;
  112. ALCcontext *context;
  113. ALsoundfont *sfont;
  114. void *ptr;
  115. context = GetContextRef();
  116. if(!context) return;
  117. device = context->Device;
  118. if(id == 0)
  119. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  120. if(!(sfont=LookupSfont(device, id)))
  121. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  122. if(type != AL_SHORT_SOFT)
  123. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  124. if(count <= 0)
  125. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  126. WriteLock(&sfont->Lock);
  127. if(sfont->ref != 0)
  128. alSetError(context, AL_INVALID_OPERATION);
  129. else if(sfont->Mapped)
  130. alSetError(context, AL_INVALID_OPERATION);
  131. else if(!(ptr=realloc(sfont->Samples, count * sizeof(ALshort))))
  132. alSetError(context, AL_OUT_OF_MEMORY);
  133. else
  134. {
  135. sfont->Samples = ptr;
  136. sfont->NumSamples = count;
  137. if(samples != NULL)
  138. memcpy(sfont->Samples, samples, count * sizeof(ALshort));
  139. }
  140. WriteUnlock(&sfont->Lock);
  141. done:
  142. ALCcontext_DecRef(context);
  143. }
  144. AL_API void AL_APIENTRY alGetSoundfontSamplesSOFT(ALuint id, ALsizei offset, ALsizei count, ALenum type, ALvoid *samples)
  145. {
  146. ALCdevice *device;
  147. ALCcontext *context;
  148. ALsoundfont *sfont;
  149. context = GetContextRef();
  150. if(!context) return;
  151. device = context->Device;
  152. if(id == 0)
  153. sfont = ALsoundfont_getDefSoundfont(context);
  154. else if(!(sfont=LookupSfont(device, id)))
  155. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  156. if(type != AL_SHORT_SOFT)
  157. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  158. if(offset < 0 || count <= 0)
  159. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  160. ReadLock(&sfont->Lock);
  161. if(offset >= sfont->NumSamples || count > (sfont->NumSamples-offset))
  162. alSetError(context, AL_INVALID_VALUE);
  163. else if(sfont->Mapped)
  164. alSetError(context, AL_INVALID_OPERATION);
  165. else
  166. {
  167. /* TODO: Allow conversion. */
  168. memcpy(samples, sfont->Samples + offset*sizeof(ALshort), count * sizeof(ALshort));
  169. }
  170. ReadUnlock(&sfont->Lock);
  171. done:
  172. ALCcontext_DecRef(context);
  173. }
  174. AL_API ALvoid* AL_APIENTRY alSoundfontMapSamplesSOFT(ALuint id, ALsizei offset, ALsizei length)
  175. {
  176. ALCdevice *device;
  177. ALCcontext *context;
  178. ALsoundfont *sfont;
  179. ALvoid *ptr = NULL;
  180. context = GetContextRef();
  181. if(!context) return NULL;
  182. device = context->Device;
  183. if(id == 0)
  184. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  185. if(!(sfont=LookupSfont(device, id)))
  186. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  187. if(offset < 0 || (ALuint)offset > sfont->NumSamples*sizeof(ALshort))
  188. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  189. if(length <= 0 || (ALuint)length > (sfont->NumSamples*sizeof(ALshort) - offset))
  190. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  191. ReadLock(&sfont->Lock);
  192. if(sfont->ref != 0)
  193. alSetError(context, AL_INVALID_OPERATION);
  194. else if(ExchangeInt(&sfont->Mapped, AL_TRUE) == AL_TRUE)
  195. alSetError(context, AL_INVALID_OPERATION);
  196. else
  197. ptr = (ALbyte*)sfont->Samples + offset;
  198. ReadUnlock(&sfont->Lock);
  199. done:
  200. ALCcontext_DecRef(context);
  201. return ptr;
  202. }
  203. AL_API ALvoid AL_APIENTRY alSoundfontUnmapSamplesSOFT(ALuint id)
  204. {
  205. ALCdevice *device;
  206. ALCcontext *context;
  207. ALsoundfont *sfont;
  208. context = GetContextRef();
  209. if(!context) return;
  210. device = context->Device;
  211. if(id == 0)
  212. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  213. if(!(sfont=LookupSfont(device, id)))
  214. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  215. if(ExchangeInt(&sfont->Mapped, AL_FALSE) == AL_FALSE)
  216. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  217. done:
  218. ALCcontext_DecRef(context);
  219. }
  220. AL_API void AL_APIENTRY alGetSoundfontivSOFT(ALuint id, ALenum param, ALint *values)
  221. {
  222. ALCdevice *device;
  223. ALCcontext *context;
  224. ALsoundfont *sfont;
  225. ALsizei i;
  226. context = GetContextRef();
  227. if(!context) return;
  228. device = context->Device;
  229. if(id == 0)
  230. sfont = ALsoundfont_getDefSoundfont(context);
  231. else if(!(sfont=LookupSfont(device, id)))
  232. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  233. switch(param)
  234. {
  235. case AL_PRESETS_SIZE_SOFT:
  236. values[0] = sfont->NumPresets;
  237. break;
  238. case AL_PRESETS_SOFT:
  239. for(i = 0;i < sfont->NumPresets;i++)
  240. values[i] = sfont->Presets[i]->id;
  241. break;
  242. case AL_SAMPLE_LENGTH_SOFT:
  243. values[0] = sfont->NumSamples;
  244. break;
  245. case AL_FORMAT_TYPE_SOFT:
  246. values[0] = AL_SHORT_SOFT;
  247. break;
  248. default:
  249. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  250. }
  251. done:
  252. ALCcontext_DecRef(context);
  253. }
  254. AL_API void AL_APIENTRY alSoundfontPresetsSOFT(ALuint id, ALsizei count, const ALuint *pids)
  255. {
  256. ALCdevice *device;
  257. ALCcontext *context;
  258. ALsoundfont *sfont;
  259. ALsfpreset **presets;
  260. ALsizei i;
  261. context = GetContextRef();
  262. if(!context) return;
  263. device = context->Device;
  264. if(id == 0)
  265. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  266. if(!(sfont=LookupSfont(device, id)))
  267. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  268. if(count < 0)
  269. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  270. WriteLock(&sfont->Lock);
  271. if(sfont->ref != 0)
  272. {
  273. WriteUnlock(&sfont->Lock);
  274. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  275. }
  276. if(count == 0)
  277. presets = NULL;
  278. else
  279. {
  280. presets = calloc(count, sizeof(presets[0]));
  281. if(!presets)
  282. {
  283. WriteUnlock(&sfont->Lock);
  284. SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done);
  285. }
  286. for(i = 0;i < count;i++)
  287. {
  288. if(!(presets[i]=LookupPreset(device, pids[i])))
  289. {
  290. WriteUnlock(&sfont->Lock);
  291. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  292. }
  293. }
  294. }
  295. for(i = 0;i < count;i++)
  296. IncrementRef(&presets[i]->ref);
  297. presets = ExchangePtr((XchgPtr*)&sfont->Presets, presets);
  298. count = ExchangeInt(&sfont->NumPresets, count);
  299. WriteUnlock(&sfont->Lock);
  300. for(i = 0;i < count;i++)
  301. DecrementRef(&presets[i]->ref);
  302. free(presets);
  303. done:
  304. ALCcontext_DecRef(context);
  305. }
  306. AL_API void AL_APIENTRY alLoadSoundfontSOFT(ALuint id, size_t(*cb)(ALvoid*,size_t,ALvoid*), ALvoid *user)
  307. {
  308. ALCdevice *device;
  309. ALCcontext *context;
  310. ALsoundfont *sfont;
  311. Reader reader;
  312. context = GetContextRef();
  313. if(!context) return;
  314. device = context->Device;
  315. if(id == 0)
  316. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  317. if(!(sfont=LookupSfont(device, id)))
  318. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  319. WriteLock(&sfont->Lock);
  320. if(sfont->ref != 0)
  321. {
  322. WriteUnlock(&sfont->Lock);
  323. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  324. }
  325. if(sfont->Mapped)
  326. {
  327. WriteUnlock(&sfont->Lock);
  328. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  329. }
  330. if(sfont->NumPresets > 0)
  331. {
  332. WriteUnlock(&sfont->Lock);
  333. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  334. }
  335. reader.cb = cb;
  336. reader.ptr = user;
  337. reader.error = 0;
  338. loadSf2(&reader, sfont, context);
  339. WriteUnlock(&sfont->Lock);
  340. done:
  341. ALCcontext_DecRef(context);
  342. }
  343. void ALsoundfont_Construct(ALsoundfont *self)
  344. {
  345. self->ref = 0;
  346. self->Presets = NULL;
  347. self->NumPresets = 0;
  348. self->Samples = NULL;
  349. self->NumSamples = 0;
  350. RWLockInit(&self->Lock);
  351. self->Mapped = AL_FALSE;
  352. self->id = 0;
  353. }
  354. void ALsoundfont_Destruct(ALsoundfont *self)
  355. {
  356. ALsizei i;
  357. FreeThunkEntry(self->id);
  358. self->id = 0;
  359. for(i = 0;i < self->NumPresets;i++)
  360. {
  361. DecrementRef(&self->Presets[i]->ref);
  362. self->Presets[i] = NULL;
  363. }
  364. free(self->Presets);
  365. self->Presets = NULL;
  366. self->NumPresets = 0;
  367. free(self->Samples);
  368. self->Samples = NULL;
  369. self->NumSamples = 0;
  370. }
  371. ALsoundfont *ALsoundfont_getDefSoundfont(ALCcontext *context)
  372. {
  373. ALCdevice *device = context->Device;
  374. const char *fname;
  375. if(device->DefaultSfont)
  376. return device->DefaultSfont;
  377. device->DefaultSfont = calloc(1, sizeof(device->DefaultSfont[0]));
  378. ALsoundfont_Construct(device->DefaultSfont);
  379. fname = getenv("ALSOFT_SOUNDFONT");
  380. if((fname && fname[0]) || ConfigValueStr("midi", "soundfont", &fname))
  381. {
  382. FILE *f;
  383. f = fopen(fname, "rb");
  384. if(f == NULL)
  385. ERR("Failed to open %s\n", fname);
  386. else
  387. {
  388. Reader reader;
  389. reader.cb = ALsoundfont_read;
  390. reader.ptr = f;
  391. reader.error = 0;
  392. TRACE("Loading %s\n", fname);
  393. loadSf2(&reader, device->DefaultSfont, context);
  394. fclose(f);
  395. }
  396. }
  397. return device->DefaultSfont;
  398. }
  399. void ALsoundfont_deleteSoundfont(ALsoundfont *self, ALCdevice *device)
  400. {
  401. ALsfpreset **presets;
  402. ALsizei num_presets;
  403. ALsizei i;
  404. presets = ExchangePtr((XchgPtr*)&self->Presets, NULL);
  405. num_presets = ExchangeInt(&self->NumPresets, 0);
  406. for(i = 0;i < num_presets;i++)
  407. {
  408. ALsfpreset *preset = presets[i];
  409. ALfontsound **sounds;
  410. ALsizei num_sounds;
  411. ALboolean deleting;
  412. ALsizei j;
  413. sounds = ExchangePtr((XchgPtr*)&preset->Sounds, NULL);
  414. num_sounds = ExchangeInt(&preset->NumSounds, 0);
  415. DeletePreset(preset, device);
  416. preset = NULL;
  417. for(j = 0;j < num_sounds;j++)
  418. DecrementRef(&sounds[j]->ref);
  419. /* Some fontsounds may not be immediately deletable because they're
  420. * linked to another fontsound. When those fontsounds are deleted
  421. * they should become deletable, so use a loop until all fontsounds
  422. * are deleted. */
  423. do {
  424. deleting = AL_FALSE;
  425. for(j = 0;j < num_sounds;j++)
  426. {
  427. if(sounds[j] && sounds[j]->ref == 0)
  428. {
  429. deleting = AL_TRUE;
  430. RemoveFontsound(device, sounds[j]->id);
  431. ALfontsound_Destruct(sounds[j]);
  432. free(sounds[j]);
  433. sounds[j] = NULL;
  434. }
  435. }
  436. } while(deleting);
  437. free(sounds);
  438. }
  439. ALsoundfont_Destruct(self);
  440. free(self);
  441. }
  442. static size_t ALsoundfont_read(ALvoid *buf, size_t bytes, ALvoid *ptr)
  443. {
  444. return fread(buf, 1, bytes, (FILE*)ptr);
  445. }
  446. /* ReleaseALSoundfonts
  447. *
  448. * Called to destroy any soundfonts that still exist on the device
  449. */
  450. void ReleaseALSoundfonts(ALCdevice *device)
  451. {
  452. ALsizei i;
  453. for(i = 0;i < device->SfontMap.size;i++)
  454. {
  455. ALsoundfont *temp = device->SfontMap.array[i].value;
  456. device->SfontMap.array[i].value = NULL;
  457. ALsoundfont_Destruct(temp);
  458. memset(temp, 0, sizeof(*temp));
  459. free(temp);
  460. }
  461. }