soundfont.c 38 KB

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