soundfont.c 37 KB

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