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