soundfont.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. /*
  2. * Soundfont generic routines.
  3. * It is intended that these should be used by any driver that is willing
  4. * to accept soundfont patches.
  5. *
  6. * Copyright (C) 1999 Steve Ratcliffe
  7. * Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /*
  24. * Deal with reading in of a soundfont. Code follows the OSS way
  25. * of doing things so that the old sfxload utility can be used.
  26. * Everything may change when there is an alsa way of doing things.
  27. */
  28. #include <sound/driver.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/slab.h>
  31. #include <sound/core.h>
  32. #include <sound/soundfont.h>
  33. #include <sound/seq_oss_legacy.h>
  34. /* Prototypes for static functions */
  35. static int open_patch(snd_sf_list_t *sflist, const char __user *data, int count, int client);
  36. static snd_soundfont_t *newsf(snd_sf_list_t *sflist, int type, char *name);
  37. static int is_identical_font(snd_soundfont_t *sf, int type, unsigned char *name);
  38. static int close_patch(snd_sf_list_t *sflist);
  39. static int probe_data(snd_sf_list_t *sflist, int sample_id);
  40. static void set_zone_counter(snd_sf_list_t *sflist, snd_soundfont_t *sf, snd_sf_zone_t *zp);
  41. static snd_sf_zone_t *sf_zone_new(snd_sf_list_t *sflist, snd_soundfont_t *sf);
  42. static void set_sample_counter(snd_sf_list_t *sflist, snd_soundfont_t *sf, snd_sf_sample_t *sp);
  43. static snd_sf_sample_t *sf_sample_new(snd_sf_list_t *sflist, snd_soundfont_t *sf);
  44. static void sf_sample_delete(snd_sf_list_t *sflist, snd_soundfont_t *sf, snd_sf_sample_t *sp);
  45. static int load_map(snd_sf_list_t *sflist, const void __user *data, int count);
  46. static int load_info(snd_sf_list_t *sflist, const void __user *data, long count);
  47. static int remove_info(snd_sf_list_t *sflist, snd_soundfont_t *sf, int bank, int instr);
  48. static void init_voice_info(soundfont_voice_info_t *avp);
  49. static void init_voice_parm(soundfont_voice_parm_t *pp);
  50. static snd_sf_sample_t *set_sample(snd_soundfont_t *sf, soundfont_voice_info_t *avp);
  51. static snd_sf_sample_t *find_sample(snd_soundfont_t *sf, int sample_id);
  52. static int load_data(snd_sf_list_t *sflist, const void __user *data, long count);
  53. static void rebuild_presets(snd_sf_list_t *sflist);
  54. static void add_preset(snd_sf_list_t *sflist, snd_sf_zone_t *cur);
  55. static void delete_preset(snd_sf_list_t *sflist, snd_sf_zone_t *zp);
  56. static snd_sf_zone_t *search_first_zone(snd_sf_list_t *sflist, int bank, int preset, int key);
  57. static int search_zones(snd_sf_list_t *sflist, int *notep, int vel, int preset, int bank, snd_sf_zone_t **table, int max_layers, int level);
  58. static int get_index(int bank, int instr, int key);
  59. static void snd_sf_init(snd_sf_list_t *sflist);
  60. static void snd_sf_clear(snd_sf_list_t *sflist);
  61. /*
  62. * lock access to sflist
  63. */
  64. static void
  65. lock_preset(snd_sf_list_t *sflist)
  66. {
  67. unsigned long flags;
  68. down(&sflist->presets_mutex);
  69. spin_lock_irqsave(&sflist->lock, flags);
  70. sflist->presets_locked = 1;
  71. spin_unlock_irqrestore(&sflist->lock, flags);
  72. }
  73. /*
  74. * remove lock
  75. */
  76. static void
  77. unlock_preset(snd_sf_list_t *sflist)
  78. {
  79. unsigned long flags;
  80. spin_lock_irqsave(&sflist->lock, flags);
  81. sflist->presets_locked = 0;
  82. spin_unlock_irqrestore(&sflist->lock, flags);
  83. up(&sflist->presets_mutex);
  84. }
  85. /*
  86. * close the patch if the patch was opened by this client.
  87. */
  88. int
  89. snd_soundfont_close_check(snd_sf_list_t *sflist, int client)
  90. {
  91. unsigned long flags;
  92. spin_lock_irqsave(&sflist->lock, flags);
  93. if (sflist->open_client == client) {
  94. spin_unlock_irqrestore(&sflist->lock, flags);
  95. return close_patch(sflist);
  96. }
  97. spin_unlock_irqrestore(&sflist->lock, flags);
  98. return 0;
  99. }
  100. /*
  101. * Deal with a soundfont patch. Any driver could use these routines
  102. * although it was designed for the AWE64.
  103. *
  104. * The sample_write and callargs pararameters allow a callback into
  105. * the actual driver to write sample data to the board or whatever
  106. * it wants to do with it.
  107. */
  108. int
  109. snd_soundfont_load(snd_sf_list_t *sflist, const void __user *data, long count, int client)
  110. {
  111. soundfont_patch_info_t patch;
  112. unsigned long flags;
  113. int rc;
  114. if (count < (long)sizeof(patch)) {
  115. snd_printk("patch record too small %ld\n", count);
  116. return -EINVAL;
  117. }
  118. if (copy_from_user(&patch, data, sizeof(patch)))
  119. return -EFAULT;
  120. count -= sizeof(patch);
  121. data += sizeof(patch);
  122. if (patch.key != SNDRV_OSS_SOUNDFONT_PATCH) {
  123. snd_printk("'The wrong kind of patch' %x\n", patch.key);
  124. return -EINVAL;
  125. }
  126. if (count < patch.len) {
  127. snd_printk("Patch too short %ld, need %d\n", count, patch.len);
  128. return -EINVAL;
  129. }
  130. if (patch.len < 0) {
  131. snd_printk("poor length %d\n", patch.len);
  132. return -EINVAL;
  133. }
  134. if (patch.type == SNDRV_SFNT_OPEN_PATCH) {
  135. /* grab sflist to open */
  136. lock_preset(sflist);
  137. rc = open_patch(sflist, data, count, client);
  138. unlock_preset(sflist);
  139. return rc;
  140. }
  141. /* check if other client already opened patch */
  142. spin_lock_irqsave(&sflist->lock, flags);
  143. if (sflist->open_client != client) {
  144. spin_unlock_irqrestore(&sflist->lock, flags);
  145. return -EBUSY;
  146. }
  147. spin_unlock_irqrestore(&sflist->lock, flags);
  148. lock_preset(sflist);
  149. rc = -EINVAL;
  150. switch (patch.type) {
  151. case SNDRV_SFNT_LOAD_INFO:
  152. rc = load_info(sflist, data, count);
  153. break;
  154. case SNDRV_SFNT_LOAD_DATA:
  155. rc = load_data(sflist, data, count);
  156. break;
  157. case SNDRV_SFNT_CLOSE_PATCH:
  158. rc = close_patch(sflist);
  159. break;
  160. case SNDRV_SFNT_REPLACE_DATA:
  161. /*rc = replace_data(&patch, data, count);*/
  162. break;
  163. case SNDRV_SFNT_MAP_PRESET:
  164. rc = load_map(sflist, data, count);
  165. break;
  166. case SNDRV_SFNT_PROBE_DATA:
  167. rc = probe_data(sflist, patch.optarg);
  168. break;
  169. case SNDRV_SFNT_REMOVE_INFO:
  170. /* patch must be opened */
  171. if (sflist->currsf) {
  172. snd_printk("soundfont: remove_info: patch not opened\n");
  173. rc = -EINVAL;
  174. } else {
  175. int bank, instr;
  176. bank = ((unsigned short)patch.optarg >> 8) & 0xff;
  177. instr = (unsigned short)patch.optarg & 0xff;
  178. if (! remove_info(sflist, sflist->currsf, bank, instr))
  179. rc = -EINVAL;
  180. else
  181. rc = 0;
  182. }
  183. break;
  184. }
  185. unlock_preset(sflist);
  186. return rc;
  187. }
  188. /* check if specified type is special font (GUS or preset-alias) */
  189. static inline int
  190. is_special_type(int type)
  191. {
  192. type &= 0x0f;
  193. return (type == SNDRV_SFNT_PAT_TYPE_GUS ||
  194. type == SNDRV_SFNT_PAT_TYPE_MAP);
  195. }
  196. /* open patch; create sf list */
  197. static int
  198. open_patch(snd_sf_list_t *sflist, const char __user *data, int count, int client)
  199. {
  200. soundfont_open_parm_t parm;
  201. snd_soundfont_t *sf;
  202. unsigned long flags;
  203. spin_lock_irqsave(&sflist->lock, flags);
  204. if (sflist->open_client >= 0 || sflist->currsf) {
  205. spin_unlock_irqrestore(&sflist->lock, flags);
  206. return -EBUSY;
  207. }
  208. spin_unlock_irqrestore(&sflist->lock, flags);
  209. if (copy_from_user(&parm, data, sizeof(parm)))
  210. return -EFAULT;
  211. if (is_special_type(parm.type)) {
  212. parm.type |= SNDRV_SFNT_PAT_SHARED;
  213. sf = newsf(sflist, parm.type, NULL);
  214. } else
  215. sf = newsf(sflist, parm.type, parm.name);
  216. if (sf == NULL) {
  217. return -ENOMEM;
  218. }
  219. spin_lock_irqsave(&sflist->lock, flags);
  220. sflist->open_client = client;
  221. sflist->currsf = sf;
  222. spin_unlock_irqrestore(&sflist->lock, flags);
  223. return 0;
  224. }
  225. /*
  226. * Allocate a new soundfont structure.
  227. */
  228. static snd_soundfont_t *
  229. newsf(snd_sf_list_t *sflist, int type, char *name)
  230. {
  231. snd_soundfont_t *sf;
  232. /* check the shared fonts */
  233. if (type & SNDRV_SFNT_PAT_SHARED) {
  234. for (sf = sflist->fonts; sf; sf = sf->next) {
  235. if (is_identical_font(sf, type, name)) {
  236. return sf;
  237. }
  238. }
  239. }
  240. /* not found -- create a new one */
  241. sf = kcalloc(1, sizeof(*sf), GFP_KERNEL);
  242. if (sf == NULL)
  243. return NULL;
  244. sf->id = sflist->fonts_size;
  245. sflist->fonts_size++;
  246. /* prepend this record */
  247. sf->next = sflist->fonts;
  248. sflist->fonts = sf;
  249. sf->type = type;
  250. sf->zones = NULL;
  251. sf->samples = NULL;
  252. if (name)
  253. memcpy(sf->name, name, SNDRV_SFNT_PATCH_NAME_LEN);
  254. return sf;
  255. }
  256. /* check if the given name matches to the existing list */
  257. static int
  258. is_identical_font(snd_soundfont_t *sf, int type, unsigned char *name)
  259. {
  260. return ((sf->type & SNDRV_SFNT_PAT_SHARED) &&
  261. (sf->type & 0x0f) == (type & 0x0f) &&
  262. (name == NULL ||
  263. memcmp(sf->name, name, SNDRV_SFNT_PATCH_NAME_LEN) == 0));
  264. }
  265. /*
  266. * Close the current patch.
  267. */
  268. static int
  269. close_patch(snd_sf_list_t *sflist)
  270. {
  271. unsigned long flags;
  272. spin_lock_irqsave(&sflist->lock, flags);
  273. sflist->currsf = NULL;
  274. sflist->open_client = -1;
  275. spin_unlock_irqrestore(&sflist->lock, flags);
  276. rebuild_presets(sflist);
  277. return 0;
  278. }
  279. /* probe sample in the current list -- nothing to be loaded */
  280. static int
  281. probe_data(snd_sf_list_t *sflist, int sample_id)
  282. {
  283. /* patch must be opened */
  284. if (sflist->currsf) {
  285. /* search the specified sample by optarg */
  286. if (find_sample(sflist->currsf, sample_id))
  287. return 0;
  288. }
  289. return -EINVAL;
  290. }
  291. /*
  292. * increment zone counter
  293. */
  294. static void
  295. set_zone_counter(snd_sf_list_t *sflist, snd_soundfont_t *sf, snd_sf_zone_t *zp)
  296. {
  297. zp->counter = sflist->zone_counter++;
  298. if (sf->type & SNDRV_SFNT_PAT_LOCKED)
  299. sflist->zone_locked = sflist->zone_counter;
  300. }
  301. /*
  302. * allocate a new zone record
  303. */
  304. static snd_sf_zone_t *
  305. sf_zone_new(snd_sf_list_t *sflist, snd_soundfont_t *sf)
  306. {
  307. snd_sf_zone_t *zp;
  308. if ((zp = kcalloc(1, sizeof(*zp), GFP_KERNEL)) == NULL)
  309. return NULL;
  310. zp->next = sf->zones;
  311. sf->zones = zp;
  312. init_voice_info(&zp->v);
  313. set_zone_counter(sflist, sf, zp);
  314. return zp;
  315. }
  316. /*
  317. * increment sample couter
  318. */
  319. static void
  320. set_sample_counter(snd_sf_list_t *sflist, snd_soundfont_t *sf, snd_sf_sample_t *sp)
  321. {
  322. sp->counter = sflist->sample_counter++;
  323. if (sf->type & SNDRV_SFNT_PAT_LOCKED)
  324. sflist->sample_locked = sflist->sample_counter;
  325. }
  326. /*
  327. * allocate a new sample list record
  328. */
  329. static snd_sf_sample_t *
  330. sf_sample_new(snd_sf_list_t *sflist, snd_soundfont_t *sf)
  331. {
  332. snd_sf_sample_t *sp;
  333. if ((sp = kcalloc(1, sizeof(*sp), GFP_KERNEL)) == NULL)
  334. return NULL;
  335. sp->next = sf->samples;
  336. sf->samples = sp;
  337. set_sample_counter(sflist, sf, sp);
  338. return sp;
  339. }
  340. /*
  341. * delete sample list -- this is an exceptional job.
  342. * only the last allocated sample can be deleted.
  343. */
  344. static void
  345. sf_sample_delete(snd_sf_list_t *sflist, snd_soundfont_t *sf, snd_sf_sample_t *sp)
  346. {
  347. /* only last sample is accepted */
  348. if (sp == sf->samples) {
  349. sf->samples = sp->next;
  350. kfree(sp);
  351. }
  352. }
  353. /* load voice map */
  354. static int
  355. load_map(snd_sf_list_t *sflist, const void __user *data, int count)
  356. {
  357. snd_sf_zone_t *zp, *prevp;
  358. snd_soundfont_t *sf;
  359. soundfont_voice_map_t map;
  360. /* get the link info */
  361. if (count < (int)sizeof(map))
  362. return -EINVAL;
  363. if (copy_from_user(&map, data, sizeof(map)))
  364. return -EFAULT;
  365. if (map.map_instr < 0 || map.map_instr >= SF_MAX_INSTRUMENTS)
  366. return -EINVAL;
  367. sf = newsf(sflist, SNDRV_SFNT_PAT_TYPE_MAP|SNDRV_SFNT_PAT_SHARED, NULL);
  368. if (sf == NULL)
  369. return -ENOMEM;
  370. prevp = NULL;
  371. for (zp = sf->zones; zp; prevp = zp, zp = zp->next) {
  372. if (zp->mapped &&
  373. zp->instr == map.map_instr &&
  374. zp->bank == map.map_bank &&
  375. zp->v.low == map.map_key &&
  376. zp->v.start == map.src_instr &&
  377. zp->v.end == map.src_bank &&
  378. zp->v.fixkey == map.src_key) {
  379. /* the same mapping is already present */
  380. /* relink this record to the link head */
  381. if (prevp) {
  382. prevp->next = zp->next;
  383. zp->next = sf->zones;
  384. sf->zones = zp;
  385. }
  386. /* update the counter */
  387. set_zone_counter(sflist, sf, zp);
  388. return 0;
  389. }
  390. }
  391. /* create a new zone */
  392. if ((zp = sf_zone_new(sflist, sf)) == NULL)
  393. return -ENOMEM;
  394. zp->bank = map.map_bank;
  395. zp->instr = map.map_instr;
  396. zp->mapped = 1;
  397. if (map.map_key >= 0) {
  398. zp->v.low = map.map_key;
  399. zp->v.high = map.map_key;
  400. }
  401. zp->v.start = map.src_instr;
  402. zp->v.end = map.src_bank;
  403. zp->v.fixkey = map.src_key;
  404. zp->v.sf_id = sf->id;
  405. add_preset(sflist, zp);
  406. return 0;
  407. }
  408. /* remove the present instrument layers */
  409. static int
  410. remove_info(snd_sf_list_t *sflist, snd_soundfont_t *sf, int bank, int instr)
  411. {
  412. snd_sf_zone_t *prev, *next, *p;
  413. int removed = 0;
  414. prev = NULL;
  415. for (p = sf->zones; p; p = next) {
  416. next = p->next;
  417. if (! p->mapped &&
  418. p->bank == bank && p->instr == instr) {
  419. /* remove this layer */
  420. if (prev)
  421. prev->next = next;
  422. else
  423. sf->zones = next;
  424. removed++;
  425. kfree(p);
  426. } else
  427. prev = p;
  428. }
  429. if (removed)
  430. rebuild_presets(sflist);
  431. return removed;
  432. }
  433. /*
  434. * Read an info record from the user buffer and save it on the current
  435. * open soundfont.
  436. */
  437. static int
  438. load_info(snd_sf_list_t *sflist, const void __user *data, long count)
  439. {
  440. snd_soundfont_t *sf;
  441. snd_sf_zone_t *zone;
  442. soundfont_voice_rec_hdr_t hdr;
  443. int i;
  444. /* patch must be opened */
  445. if ((sf = sflist->currsf) == NULL)
  446. return -EINVAL;
  447. if (is_special_type(sf->type))
  448. return -EINVAL;
  449. if (count < (long)sizeof(hdr)) {
  450. printk("Soundfont error: invalid patch zone length\n");
  451. return -EINVAL;
  452. }
  453. if (copy_from_user((char*)&hdr, data, sizeof(hdr)))
  454. return -EFAULT;
  455. data += sizeof(hdr);
  456. count -= sizeof(hdr);
  457. if (hdr.nvoices <= 0 || hdr.nvoices >= 100) {
  458. printk("Soundfont error: Illegal voice number %d\n", hdr.nvoices);
  459. return -EINVAL;
  460. }
  461. if (count < (long)sizeof(soundfont_voice_info_t)*hdr.nvoices) {
  462. printk("Soundfont Error: patch length(%ld) is smaller than nvoices(%d)\n",
  463. count, hdr.nvoices);
  464. return -EINVAL;
  465. }
  466. switch (hdr.write_mode) {
  467. case SNDRV_SFNT_WR_EXCLUSIVE:
  468. /* exclusive mode - if the instrument already exists,
  469. return error */
  470. for (zone = sf->zones; zone; zone = zone->next) {
  471. if (!zone->mapped &&
  472. zone->bank == hdr.bank &&
  473. zone->instr == hdr.instr)
  474. return -EINVAL;
  475. }
  476. break;
  477. case SNDRV_SFNT_WR_REPLACE:
  478. /* replace mode - remove the instrument if it already exists */
  479. remove_info(sflist, sf, hdr.bank, hdr.instr);
  480. break;
  481. }
  482. for (i = 0; i < hdr.nvoices; i++) {
  483. snd_sf_zone_t tmpzone;
  484. /* copy awe_voice_info parameters */
  485. if (copy_from_user(&tmpzone.v, data, sizeof(tmpzone.v))) {
  486. return -EFAULT;
  487. }
  488. data += sizeof(tmpzone.v);
  489. count -= sizeof(tmpzone.v);
  490. tmpzone.bank = hdr.bank;
  491. tmpzone.instr = hdr.instr;
  492. tmpzone.mapped = 0;
  493. tmpzone.v.sf_id = sf->id;
  494. if (tmpzone.v.mode & SNDRV_SFNT_MODE_INIT_PARM)
  495. init_voice_parm(&tmpzone.v.parm);
  496. /* create a new zone */
  497. if ((zone = sf_zone_new(sflist, sf)) == NULL) {
  498. return -ENOMEM;
  499. }
  500. /* copy the temporary data */
  501. zone->bank = tmpzone.bank;
  502. zone->instr = tmpzone.instr;
  503. zone->v = tmpzone.v;
  504. /* look up the sample */
  505. zone->sample = set_sample(sf, &zone->v);
  506. }
  507. return 0;
  508. }
  509. /* initialize voice_info record */
  510. static void
  511. init_voice_info(soundfont_voice_info_t *avp)
  512. {
  513. memset(avp, 0, sizeof(*avp));
  514. avp->root = 60;
  515. avp->high = 127;
  516. avp->velhigh = 127;
  517. avp->fixkey = -1;
  518. avp->fixvel = -1;
  519. avp->fixpan = -1;
  520. avp->pan = -1;
  521. avp->amplitude = 127;
  522. avp->scaleTuning = 100;
  523. init_voice_parm(&avp->parm);
  524. }
  525. /* initialize voice_parm record:
  526. * Env1/2: delay=0, attack=0, hold=0, sustain=0, decay=0, release=0.
  527. * Vibrato and Tremolo effects are zero.
  528. * Cutoff is maximum.
  529. * Chorus and Reverb effects are zero.
  530. */
  531. static void
  532. init_voice_parm(soundfont_voice_parm_t *pp)
  533. {
  534. memset(pp, 0, sizeof(*pp));
  535. pp->moddelay = 0x8000;
  536. pp->modatkhld = 0x7f7f;
  537. pp->moddcysus = 0x7f7f;
  538. pp->modrelease = 0x807f;
  539. pp->voldelay = 0x8000;
  540. pp->volatkhld = 0x7f7f;
  541. pp->voldcysus = 0x7f7f;
  542. pp->volrelease = 0x807f;
  543. pp->lfo1delay = 0x8000;
  544. pp->lfo2delay = 0x8000;
  545. pp->cutoff = 0xff;
  546. }
  547. /* search the specified sample */
  548. static snd_sf_sample_t *
  549. set_sample(snd_soundfont_t *sf, soundfont_voice_info_t *avp)
  550. {
  551. snd_sf_sample_t *sample;
  552. sample = find_sample(sf, avp->sample);
  553. if (sample == NULL)
  554. return NULL;
  555. /* add in the actual sample offsets:
  556. * The voice_info addresses define only the relative offset
  557. * from sample pointers. Here we calculate the actual DRAM
  558. * offset from sample pointers.
  559. */
  560. avp->start += sample->v.start;
  561. avp->end += sample->v.end;
  562. avp->loopstart += sample->v.loopstart;
  563. avp->loopend += sample->v.loopend;
  564. /* copy mode flags */
  565. avp->sample_mode = sample->v.mode_flags;
  566. return sample;
  567. }
  568. /* find the sample pointer with the given id in the soundfont */
  569. static snd_sf_sample_t *
  570. find_sample(snd_soundfont_t *sf, int sample_id)
  571. {
  572. snd_sf_sample_t *p;
  573. if (sf == NULL)
  574. return NULL;
  575. for (p = sf->samples; p; p = p->next) {
  576. if (p->v.sample == sample_id)
  577. return p;
  578. }
  579. return NULL;
  580. }
  581. /*
  582. * Load sample information, this can include data to be loaded onto
  583. * the soundcard. It can also just be a pointer into soundcard ROM.
  584. * If there is data it will be written to the soundcard via the callback
  585. * routine.
  586. */
  587. static int
  588. load_data(snd_sf_list_t *sflist, const void __user *data, long count)
  589. {
  590. snd_soundfont_t *sf;
  591. soundfont_sample_info_t sample_info;
  592. snd_sf_sample_t *sp;
  593. long off;
  594. /* patch must be opened */
  595. if ((sf = sflist->currsf) == NULL)
  596. return -EINVAL;
  597. if (is_special_type(sf->type))
  598. return -EINVAL;
  599. if (copy_from_user(&sample_info, data, sizeof(sample_info)))
  600. return -EFAULT;
  601. off = sizeof(sample_info);
  602. if (sample_info.size != (count-off)/2)
  603. return -EINVAL;
  604. /* Check for dup */
  605. if (find_sample(sf, sample_info.sample)) {
  606. /* if shared sample, skip this data */
  607. if (sf->type & SNDRV_SFNT_PAT_SHARED)
  608. return 0;
  609. return -EINVAL;
  610. }
  611. /* Allocate a new sample structure */
  612. if ((sp = sf_sample_new(sflist, sf)) == NULL)
  613. return -ENOMEM;
  614. sp->v = sample_info;
  615. sp->v.sf_id = sf->id;
  616. sp->v.dummy = 0;
  617. sp->v.truesize = sp->v.size;
  618. /*
  619. * If there is wave data then load it.
  620. */
  621. if (sp->v.size > 0) {
  622. int rc;
  623. rc = sflist->callback.sample_new
  624. (sflist->callback.private_data, sp, sflist->memhdr,
  625. data + off, count - off);
  626. if (rc < 0) {
  627. sf_sample_delete(sflist, sf, sp);
  628. return rc;
  629. }
  630. sflist->mem_used += sp->v.truesize;
  631. }
  632. return count;
  633. }
  634. /* log2_tbl[i] = log2(i+128) * 0x10000 */
  635. static int log_tbl[129] = {
  636. 0x70000, 0x702df, 0x705b9, 0x7088e, 0x70b5d, 0x70e26, 0x710eb, 0x713aa,
  637. 0x71663, 0x71918, 0x71bc8, 0x71e72, 0x72118, 0x723b9, 0x72655, 0x728ed,
  638. 0x72b80, 0x72e0e, 0x73098, 0x7331d, 0x7359e, 0x7381b, 0x73a93, 0x73d08,
  639. 0x73f78, 0x741e4, 0x7444c, 0x746b0, 0x74910, 0x74b6c, 0x74dc4, 0x75019,
  640. 0x75269, 0x754b6, 0x75700, 0x75946, 0x75b88, 0x75dc7, 0x76002, 0x7623a,
  641. 0x7646e, 0x766a0, 0x768cd, 0x76af8, 0x76d1f, 0x76f43, 0x77164, 0x77382,
  642. 0x7759d, 0x777b4, 0x779c9, 0x77bdb, 0x77dea, 0x77ff5, 0x781fe, 0x78404,
  643. 0x78608, 0x78808, 0x78a06, 0x78c01, 0x78df9, 0x78fef, 0x791e2, 0x793d2,
  644. 0x795c0, 0x797ab, 0x79993, 0x79b79, 0x79d5d, 0x79f3e, 0x7a11d, 0x7a2f9,
  645. 0x7a4d3, 0x7a6ab, 0x7a880, 0x7aa53, 0x7ac24, 0x7adf2, 0x7afbe, 0x7b188,
  646. 0x7b350, 0x7b515, 0x7b6d8, 0x7b899, 0x7ba58, 0x7bc15, 0x7bdd0, 0x7bf89,
  647. 0x7c140, 0x7c2f5, 0x7c4a7, 0x7c658, 0x7c807, 0x7c9b3, 0x7cb5e, 0x7cd07,
  648. 0x7ceae, 0x7d053, 0x7d1f7, 0x7d398, 0x7d538, 0x7d6d6, 0x7d872, 0x7da0c,
  649. 0x7dba4, 0x7dd3b, 0x7ded0, 0x7e063, 0x7e1f4, 0x7e384, 0x7e512, 0x7e69f,
  650. 0x7e829, 0x7e9b3, 0x7eb3a, 0x7ecc0, 0x7ee44, 0x7efc7, 0x7f148, 0x7f2c8,
  651. 0x7f446, 0x7f5c2, 0x7f73d, 0x7f8b7, 0x7fa2f, 0x7fba5, 0x7fd1a, 0x7fe8d,
  652. 0x80000,
  653. };
  654. /* convert from linear to log value
  655. *
  656. * conversion: value = log2(amount / base) * ratio
  657. *
  658. * argument:
  659. * amount = linear value (unsigned, 32bit max)
  660. * offset = base offset (:= log2(base) * 0x10000)
  661. * ratio = division ratio
  662. *
  663. */
  664. int
  665. snd_sf_linear_to_log(unsigned int amount, int offset, int ratio)
  666. {
  667. int v;
  668. int s, low, bit;
  669. if (amount < 2)
  670. return 0;
  671. for (bit = 0; ! (amount & 0x80000000L); bit++)
  672. amount <<= 1;
  673. s = (amount >> 24) & 0x7f;
  674. low = (amount >> 16) & 0xff;
  675. /* linear approxmimation by lower 8 bit */
  676. v = (log_tbl[s + 1] * low + log_tbl[s] * (0x100 - low)) >> 8;
  677. v -= offset;
  678. v = (v * ratio) >> 16;
  679. v += (24 - bit) * ratio;
  680. return v;
  681. }
  682. #define OFFSET_MSEC 653117 /* base = 1000 */
  683. #define OFFSET_ABSCENT 851781 /* base = 8176 */
  684. #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */
  685. #define ABSCENT_RATIO 1200
  686. #define TIMECENT_RATIO 1200
  687. #define SAMPLERATE_RATIO 4096
  688. /*
  689. * mHz to abscent
  690. * conversion: abscent = log2(MHz / 8176) * 1200
  691. */
  692. static int
  693. freq_to_note(int mhz)
  694. {
  695. return snd_sf_linear_to_log(mhz, OFFSET_ABSCENT, ABSCENT_RATIO);
  696. }
  697. /* convert Hz to AWE32 rate offset:
  698. * sample pitch offset for the specified sample rate
  699. * rate=44100 is no offset, each 4096 is 1 octave (twice).
  700. * eg, when rate is 22050, this offset becomes -4096.
  701. *
  702. * conversion: offset = log2(Hz / 44100) * 4096
  703. */
  704. static int
  705. calc_rate_offset(int hz)
  706. {
  707. return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO);
  708. }
  709. /* calculate GUS envelope time */
  710. static int
  711. calc_gus_envelope_time(int rate, int start, int end)
  712. {
  713. int r, p, t;
  714. r = (3 - ((rate >> 6) & 3)) * 3;
  715. p = rate & 0x3f;
  716. t = end - start;
  717. if (t < 0) t = -t;
  718. if (13 > r)
  719. t = t << (13 - r);
  720. else
  721. t = t >> (r - 13);
  722. return (t * 10) / (p * 441);
  723. }
  724. /* convert envelope time parameter to soundfont parameters */
  725. /* attack & decay/release time table (msec) */
  726. static short attack_time_tbl[128] = {
  727. 32767, 32767, 5989, 4235, 2994, 2518, 2117, 1780, 1497, 1373, 1259, 1154, 1058, 970, 890, 816,
  728. 707, 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377,
  729. 361, 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188,
  730. 180, 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94,
  731. 90, 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47,
  732. 45, 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23,
  733. 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
  734. 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 6, 0,
  735. };
  736. static short decay_time_tbl[128] = {
  737. 32767, 32767, 22614, 15990, 11307, 9508, 7995, 6723, 5653, 5184, 4754, 4359, 3997, 3665, 3361, 3082,
  738. 2828, 2765, 2648, 2535, 2428, 2325, 2226, 2132, 2042, 1955, 1872, 1793, 1717, 1644, 1574, 1507,
  739. 1443, 1382, 1324, 1267, 1214, 1162, 1113, 1066, 978, 936, 897, 859, 822, 787, 754, 722,
  740. 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377, 361,
  741. 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188, 180,
  742. 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94, 90,
  743. 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45,
  744. 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23, 22,
  745. };
  746. /* delay time = 0x8000 - msec/92 */
  747. int
  748. snd_sf_calc_parm_hold(int msec)
  749. {
  750. int val = (0x7f * 92 - msec) / 92;
  751. if (val < 1) val = 1;
  752. if (val >= 126) val = 126;
  753. return val;
  754. }
  755. /* search an index for specified time from given time table */
  756. static int
  757. calc_parm_search(int msec, short *table)
  758. {
  759. int left = 1, right = 127, mid;
  760. while (left < right) {
  761. mid = (left + right) / 2;
  762. if (msec < (int)table[mid])
  763. left = mid + 1;
  764. else
  765. right = mid;
  766. }
  767. return left;
  768. }
  769. /* attack time: search from time table */
  770. int
  771. snd_sf_calc_parm_attack(int msec)
  772. {
  773. return calc_parm_search(msec, attack_time_tbl);
  774. }
  775. /* decay/release time: search from time table */
  776. int
  777. snd_sf_calc_parm_decay(int msec)
  778. {
  779. return calc_parm_search(msec, decay_time_tbl);
  780. }
  781. int snd_sf_vol_table[128] = {
  782. 255,111,95,86,79,74,70,66,63,61,58,56,54,52,50,49,
  783. 47,46,45,43,42,41,40,39,38,37,36,35,34,34,33,32,
  784. 31,31,30,29,29,28,27,27,26,26,25,24,24,23,23,22,
  785. 22,21,21,21,20,20,19,19,18,18,18,17,17,16,16,16,
  786. 15,15,15,14,14,14,13,13,13,12,12,12,11,11,11,10,
  787. 10,10,10,9,9,9,8,8,8,8,7,7,7,7,6,6,
  788. 6,6,5,5,5,5,5,4,4,4,4,3,3,3,3,3,
  789. 2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,0,
  790. };
  791. #define calc_gus_sustain(val) (0x7f - snd_sf_vol_table[(val)/2])
  792. #define calc_gus_attenuation(val) snd_sf_vol_table[(val)/2]
  793. /* load GUS patch */
  794. static int
  795. load_guspatch(snd_sf_list_t *sflist, const char __user *data, long count, int client)
  796. {
  797. struct patch_info patch;
  798. snd_soundfont_t *sf;
  799. snd_sf_zone_t *zone;
  800. snd_sf_sample_t *smp;
  801. int note, sample_id;
  802. int rc;
  803. if (count < (long)sizeof(patch)) {
  804. snd_printk("patch record too small %ld\n", count);
  805. return -EINVAL;
  806. }
  807. if (copy_from_user(&patch, data, sizeof(patch)))
  808. return -EFAULT;
  809. count -= sizeof(patch);
  810. data += sizeof(patch);
  811. sf = newsf(sflist, SNDRV_SFNT_PAT_TYPE_GUS|SNDRV_SFNT_PAT_SHARED, NULL);
  812. if (sf == NULL)
  813. return -ENOMEM;
  814. if ((smp = sf_sample_new(sflist, sf)) == NULL)
  815. return -ENOMEM;
  816. sample_id = sflist->sample_counter;
  817. smp->v.sample = sample_id;
  818. smp->v.start = 0;
  819. smp->v.end = patch.len;
  820. smp->v.loopstart = patch.loop_start;
  821. smp->v.loopend = patch.loop_end;
  822. smp->v.size = patch.len;
  823. /* set up mode flags */
  824. smp->v.mode_flags = 0;
  825. if (!(patch.mode & WAVE_16_BITS))
  826. smp->v.mode_flags |= SNDRV_SFNT_SAMPLE_8BITS;
  827. if (patch.mode & WAVE_UNSIGNED)
  828. smp->v.mode_flags |= SNDRV_SFNT_SAMPLE_UNSIGNED;
  829. smp->v.mode_flags |= SNDRV_SFNT_SAMPLE_NO_BLANK;
  830. if (!(patch.mode & (WAVE_LOOPING|WAVE_BIDIR_LOOP|WAVE_LOOP_BACK)))
  831. smp->v.mode_flags |= SNDRV_SFNT_SAMPLE_SINGLESHOT;
  832. if (patch.mode & WAVE_BIDIR_LOOP)
  833. smp->v.mode_flags |= SNDRV_SFNT_SAMPLE_BIDIR_LOOP;
  834. if (patch.mode & WAVE_LOOP_BACK)
  835. smp->v.mode_flags |= SNDRV_SFNT_SAMPLE_REVERSE_LOOP;
  836. if (patch.mode & WAVE_16_BITS) {
  837. /* convert to word offsets */
  838. smp->v.size /= 2;
  839. smp->v.end /= 2;
  840. smp->v.loopstart /= 2;
  841. smp->v.loopend /= 2;
  842. }
  843. /*smp->v.loopend++;*/
  844. smp->v.dummy = 0;
  845. smp->v.truesize = 0;
  846. smp->v.sf_id = sf->id;
  847. /* set up voice info */
  848. if ((zone = sf_zone_new(sflist, sf)) == NULL) {
  849. sf_sample_delete(sflist, sf, smp);
  850. return -ENOMEM;
  851. }
  852. /*
  853. * load wave data
  854. */
  855. if (sflist->callback.sample_new) {
  856. rc = sflist->callback.sample_new
  857. (sflist->callback.private_data, smp, sflist->memhdr, data, count);
  858. if (rc < 0) {
  859. sf_sample_delete(sflist, sf, smp);
  860. return rc;
  861. }
  862. /* memory offset is updated after */
  863. }
  864. /* update the memory offset here */
  865. sflist->mem_used += smp->v.truesize;
  866. zone->v.sample = sample_id; /* the last sample */
  867. zone->v.rate_offset = calc_rate_offset(patch.base_freq);
  868. note = freq_to_note(patch.base_note);
  869. zone->v.root = note / 100;
  870. zone->v.tune = -(note % 100);
  871. zone->v.low = (freq_to_note(patch.low_note) + 99) / 100;
  872. zone->v.high = freq_to_note(patch.high_note) / 100;
  873. /* panning position; -128 - 127 => 0-127 */
  874. zone->v.pan = (patch.panning + 128) / 2;
  875. #if 0
  876. snd_printk("gus: basefrq=%d (ofs=%d) root=%d,tune=%d, range:%d-%d\n",
  877. (int)patch.base_freq, zone->v.rate_offset,
  878. zone->v.root, zone->v.tune, zone->v.low, zone->v.high);
  879. #endif
  880. /* detuning is ignored */
  881. /* 6points volume envelope */
  882. if (patch.mode & WAVE_ENVELOPES) {
  883. int attack, hold, decay, release;
  884. attack = calc_gus_envelope_time
  885. (patch.env_rate[0], 0, patch.env_offset[0]);
  886. hold = calc_gus_envelope_time
  887. (patch.env_rate[1], patch.env_offset[0],
  888. patch.env_offset[1]);
  889. decay = calc_gus_envelope_time
  890. (patch.env_rate[2], patch.env_offset[1],
  891. patch.env_offset[2]);
  892. release = calc_gus_envelope_time
  893. (patch.env_rate[3], patch.env_offset[1],
  894. patch.env_offset[4]);
  895. release += calc_gus_envelope_time
  896. (patch.env_rate[4], patch.env_offset[3],
  897. patch.env_offset[4]);
  898. release += calc_gus_envelope_time
  899. (patch.env_rate[5], patch.env_offset[4],
  900. patch.env_offset[5]);
  901. zone->v.parm.volatkhld =
  902. (snd_sf_calc_parm_hold(hold) << 8) |
  903. snd_sf_calc_parm_attack(attack);
  904. zone->v.parm.voldcysus = (calc_gus_sustain(patch.env_offset[2]) << 8) |
  905. snd_sf_calc_parm_decay(decay);
  906. zone->v.parm.volrelease = 0x8000 | snd_sf_calc_parm_decay(release);
  907. zone->v.attenuation = calc_gus_attenuation(patch.env_offset[0]);
  908. #if 0
  909. snd_printk("gus: atkhld=%x, dcysus=%x, volrel=%x, att=%d\n",
  910. zone->v.parm.volatkhld,
  911. zone->v.parm.voldcysus,
  912. zone->v.parm.volrelease,
  913. zone->v.attenuation);
  914. #endif
  915. }
  916. /* fast release */
  917. if (patch.mode & WAVE_FAST_RELEASE) {
  918. zone->v.parm.volrelease = 0x807f;
  919. }
  920. /* tremolo effect */
  921. if (patch.mode & WAVE_TREMOLO) {
  922. int rate = (patch.tremolo_rate * 1000 / 38) / 42;
  923. zone->v.parm.tremfrq = ((patch.tremolo_depth / 2) << 8) | rate;
  924. }
  925. /* vibrato effect */
  926. if (patch.mode & WAVE_VIBRATO) {
  927. int rate = (patch.vibrato_rate * 1000 / 38) / 42;
  928. zone->v.parm.fm2frq2 = ((patch.vibrato_depth / 6) << 8) | rate;
  929. }
  930. /* scale_freq, scale_factor, volume, and fractions not implemented */
  931. if (!(smp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT))
  932. zone->v.mode = SNDRV_SFNT_MODE_LOOPING;
  933. else
  934. zone->v.mode = 0;
  935. /* append to the tail of the list */
  936. /*zone->bank = ctrls[AWE_MD_GUS_BANK];*/
  937. zone->bank = 0;
  938. zone->instr = patch.instr_no;
  939. zone->mapped = 0;
  940. zone->v.sf_id = sf->id;
  941. zone->sample = set_sample(sf, &zone->v);
  942. /* rebuild preset now */
  943. add_preset(sflist, zone);
  944. return 0;
  945. }
  946. /* load GUS patch */
  947. int
  948. snd_soundfont_load_guspatch(snd_sf_list_t *sflist, const char __user *data,
  949. long count, int client)
  950. {
  951. int rc;
  952. lock_preset(sflist);
  953. rc = load_guspatch(sflist, data, count, client);
  954. unlock_preset(sflist);
  955. return rc;
  956. }
  957. /*
  958. * Rebuild the preset table. This is like a hash table in that it allows
  959. * quick access to the zone information. For each preset there are zone
  960. * structures linked by next_instr and by next_zone. Former is the whole
  961. * link for this preset, and latter is the link for zone (i.e. instrument/
  962. * bank/key combination).
  963. */
  964. static void
  965. rebuild_presets(snd_sf_list_t *sflist)
  966. {
  967. snd_soundfont_t *sf;
  968. snd_sf_zone_t *cur;
  969. /* clear preset table */
  970. memset(sflist->presets, 0, sizeof(sflist->presets));
  971. /* search all fonts and insert each font */
  972. for (sf = sflist->fonts; sf; sf = sf->next) {
  973. for (cur = sf->zones; cur; cur = cur->next) {
  974. if (! cur->mapped && cur->sample == NULL) {
  975. /* try again to search the corresponding sample */
  976. cur->sample = set_sample(sf, &cur->v);
  977. if (cur->sample == NULL)
  978. continue;
  979. }
  980. add_preset(sflist, cur);
  981. }
  982. }
  983. }
  984. /*
  985. * add the given zone to preset table
  986. */
  987. static void
  988. add_preset(snd_sf_list_t *sflist, snd_sf_zone_t *cur)
  989. {
  990. snd_sf_zone_t *zone;
  991. int index;
  992. zone = search_first_zone(sflist, cur->bank, cur->instr, cur->v.low);
  993. if (zone && zone->v.sf_id != cur->v.sf_id) {
  994. /* different instrument was already defined */
  995. snd_sf_zone_t *p;
  996. /* compare the allocated time */
  997. for (p = zone; p; p = p->next_zone) {
  998. if (p->counter > cur->counter)
  999. /* the current is older.. skipped */
  1000. return;
  1001. }
  1002. /* remove old zones */
  1003. delete_preset(sflist, zone);
  1004. zone = NULL; /* do not forget to clear this! */
  1005. }
  1006. /* prepend this zone */
  1007. if ((index = get_index(cur->bank, cur->instr, cur->v.low)) < 0)
  1008. return;
  1009. cur->next_zone = zone; /* zone link */
  1010. cur->next_instr = sflist->presets[index]; /* preset table link */
  1011. sflist->presets[index] = cur;
  1012. }
  1013. /*
  1014. * delete the given zones from preset_table
  1015. */
  1016. static void
  1017. delete_preset(snd_sf_list_t *sflist, snd_sf_zone_t *zp)
  1018. {
  1019. int index;
  1020. snd_sf_zone_t *p;
  1021. if ((index = get_index(zp->bank, zp->instr, zp->v.low)) < 0)
  1022. return;
  1023. for (p = sflist->presets[index]; p; p = p->next_instr) {
  1024. while (p->next_instr == zp) {
  1025. p->next_instr = zp->next_instr;
  1026. zp = zp->next_zone;
  1027. if (zp == NULL)
  1028. return;
  1029. }
  1030. }
  1031. }
  1032. /*
  1033. * Search matching zones from preset table.
  1034. * The note can be rewritten by preset mapping (alias).
  1035. * The found zones are stored on 'table' array. max_layers defines
  1036. * the maximum number of elements in this array.
  1037. * This function returns the number of found zones. 0 if not found.
  1038. */
  1039. int
  1040. snd_soundfont_search_zone(snd_sf_list_t *sflist, int *notep, int vel,
  1041. int preset, int bank,
  1042. int def_preset, int def_bank,
  1043. snd_sf_zone_t **table, int max_layers)
  1044. {
  1045. int nvoices;
  1046. unsigned long flags;
  1047. /* this function is supposed to be called atomically,
  1048. * so we check the lock. if it's busy, just returns 0 to
  1049. * tell the caller the busy state
  1050. */
  1051. spin_lock_irqsave(&sflist->lock, flags);
  1052. if (sflist->presets_locked) {
  1053. spin_unlock_irqrestore(&sflist->lock, flags);
  1054. return 0;
  1055. }
  1056. nvoices = search_zones(sflist, notep, vel, preset, bank, table, max_layers, 0);
  1057. if (! nvoices) {
  1058. if (preset != def_preset || bank != def_bank)
  1059. nvoices = search_zones(sflist, notep, vel, def_preset, def_bank, table, max_layers, 0);
  1060. }
  1061. spin_unlock_irqrestore(&sflist->lock, flags);
  1062. return nvoices;
  1063. }
  1064. /*
  1065. * search the first matching zone
  1066. */
  1067. static snd_sf_zone_t *
  1068. search_first_zone(snd_sf_list_t *sflist, int bank, int preset, int key)
  1069. {
  1070. int index;
  1071. snd_sf_zone_t *zp;
  1072. if ((index = get_index(bank, preset, key)) < 0)
  1073. return NULL;
  1074. for (zp = sflist->presets[index]; zp; zp = zp->next_instr) {
  1075. if (zp->instr == preset && zp->bank == bank)
  1076. return zp;
  1077. }
  1078. return NULL;
  1079. }
  1080. /*
  1081. * search matching zones from sflist. can be called recursively.
  1082. */
  1083. static int
  1084. search_zones(snd_sf_list_t *sflist, int *notep, int vel, int preset, int bank, snd_sf_zone_t **table, int max_layers, int level)
  1085. {
  1086. snd_sf_zone_t *zp;
  1087. int nvoices;
  1088. zp = search_first_zone(sflist, bank, preset, *notep);
  1089. nvoices = 0;
  1090. for (; zp; zp = zp->next_zone) {
  1091. if (*notep >= zp->v.low && *notep <= zp->v.high &&
  1092. vel >= zp->v.vellow && vel <= zp->v.velhigh) {
  1093. if (zp->mapped) {
  1094. /* search preset mapping (aliasing) */
  1095. int key = zp->v.fixkey;
  1096. preset = zp->v.start;
  1097. bank = zp->v.end;
  1098. if (level > 5) /* too deep alias level */
  1099. return 0;
  1100. if (key < 0)
  1101. key = *notep;
  1102. nvoices = search_zones(sflist, &key, vel,
  1103. preset, bank, table,
  1104. max_layers, level + 1);
  1105. if (nvoices > 0)
  1106. *notep = key;
  1107. break;
  1108. }
  1109. table[nvoices++] = zp;
  1110. if (nvoices >= max_layers)
  1111. break;
  1112. }
  1113. }
  1114. return nvoices;
  1115. }
  1116. /* calculate the index of preset table:
  1117. * drums are mapped from 128 to 255 according to its note key.
  1118. * other instruments are mapped from 0 to 127.
  1119. * if the index is out of range, return -1.
  1120. */
  1121. static int
  1122. get_index(int bank, int instr, int key)
  1123. {
  1124. int index;
  1125. if (SF_IS_DRUM_BANK(bank))
  1126. index = key + SF_MAX_INSTRUMENTS;
  1127. else
  1128. index = instr;
  1129. index = index % SF_MAX_PRESETS;
  1130. if (index < 0)
  1131. return -1;
  1132. return index;
  1133. }
  1134. /*
  1135. * Initialise the sflist structure.
  1136. */
  1137. static void
  1138. snd_sf_init(snd_sf_list_t *sflist)
  1139. {
  1140. memset(sflist->presets, 0, sizeof(sflist->presets));
  1141. sflist->mem_used = 0;
  1142. sflist->currsf = NULL;
  1143. sflist->open_client = -1;
  1144. sflist->fonts = NULL;
  1145. sflist->fonts_size = 0;
  1146. sflist->zone_counter = 0;
  1147. sflist->sample_counter = 0;
  1148. sflist->zone_locked = 0;
  1149. sflist->sample_locked = 0;
  1150. }
  1151. /*
  1152. * Release all list records
  1153. */
  1154. static void
  1155. snd_sf_clear(snd_sf_list_t *sflist)
  1156. {
  1157. snd_soundfont_t *sf, *nextsf;
  1158. snd_sf_zone_t *zp, *nextzp;
  1159. snd_sf_sample_t *sp, *nextsp;
  1160. for (sf = sflist->fonts; sf; sf = nextsf) {
  1161. nextsf = sf->next;
  1162. for (zp = sf->zones; zp; zp = nextzp) {
  1163. nextzp = zp->next;
  1164. kfree(zp);
  1165. }
  1166. for (sp = sf->samples; sp; sp = nextsp) {
  1167. nextsp = sp->next;
  1168. if (sflist->callback.sample_free)
  1169. sflist->callback.sample_free(sflist->callback.private_data, sp, sflist->memhdr);
  1170. kfree(sp);
  1171. }
  1172. kfree(sf);
  1173. }
  1174. snd_sf_init(sflist);
  1175. }
  1176. /*
  1177. * Create a new sflist structure
  1178. */
  1179. snd_sf_list_t *
  1180. snd_sf_new(snd_sf_callback_t *callback, snd_util_memhdr_t *hdr)
  1181. {
  1182. snd_sf_list_t *sflist;
  1183. if ((sflist = kcalloc(1, sizeof(*sflist), GFP_KERNEL)) == NULL)
  1184. return NULL;
  1185. init_MUTEX(&sflist->presets_mutex);
  1186. spin_lock_init(&sflist->lock);
  1187. sflist->memhdr = hdr;
  1188. if (callback)
  1189. sflist->callback = *callback;
  1190. snd_sf_init(sflist);
  1191. return sflist;
  1192. }
  1193. /*
  1194. * Free everything allocated off the sflist structure.
  1195. */
  1196. void
  1197. snd_sf_free(snd_sf_list_t *sflist)
  1198. {
  1199. if (sflist == NULL)
  1200. return;
  1201. lock_preset(sflist);
  1202. if (sflist->callback.sample_reset)
  1203. sflist->callback.sample_reset(sflist->callback.private_data);
  1204. snd_sf_clear(sflist);
  1205. unlock_preset(sflist);
  1206. kfree(sflist);
  1207. }
  1208. /*
  1209. * Remove all samples
  1210. * The soundcard should be silet before calling this function.
  1211. */
  1212. int
  1213. snd_soundfont_remove_samples(snd_sf_list_t *sflist)
  1214. {
  1215. lock_preset(sflist);
  1216. if (sflist->callback.sample_reset)
  1217. sflist->callback.sample_reset(sflist->callback.private_data);
  1218. snd_sf_clear(sflist);
  1219. unlock_preset(sflist);
  1220. return 0;
  1221. }
  1222. /*
  1223. * Remove unlocked samples.
  1224. * The soundcard should be silent before calling this function.
  1225. */
  1226. int
  1227. snd_soundfont_remove_unlocked(snd_sf_list_t *sflist)
  1228. {
  1229. snd_soundfont_t *sf;
  1230. snd_sf_zone_t *zp, *nextzp;
  1231. snd_sf_sample_t *sp, *nextsp;
  1232. lock_preset(sflist);
  1233. if (sflist->callback.sample_reset)
  1234. sflist->callback.sample_reset(sflist->callback.private_data);
  1235. /* to be sure */
  1236. memset(sflist->presets, 0, sizeof(sflist->presets));
  1237. for (sf = sflist->fonts; sf; sf = sf->next) {
  1238. for (zp = sf->zones; zp; zp = nextzp) {
  1239. if (zp->counter < sflist->zone_locked)
  1240. break;
  1241. nextzp = zp->next;
  1242. sf->zones = nextzp;
  1243. kfree(zp);
  1244. }
  1245. for (sp = sf->samples; sp; sp = nextsp) {
  1246. if (sp->counter < sflist->sample_locked)
  1247. break;
  1248. nextsp = sp->next;
  1249. sf->samples = nextsp;
  1250. sflist->mem_used -= sp->v.truesize;
  1251. if (sflist->callback.sample_free)
  1252. sflist->callback.sample_free(sflist->callback.private_data, sp, sflist->memhdr);
  1253. kfree(sp);
  1254. }
  1255. }
  1256. sflist->zone_counter = sflist->zone_locked;
  1257. sflist->sample_counter = sflist->sample_locked;
  1258. rebuild_presets(sflist);
  1259. unlock_preset(sflist);
  1260. return 0;
  1261. }