soundfont.c 37 KB

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