soundfont.c 38 KB

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