28 #include <asm/uaccess.h>
29 #include <linux/slab.h>
30 #include <linux/export.h>
37 static int open_patch(
struct snd_sf_list *sflist,
const char __user *
data,
42 static int probe_data(
struct snd_sf_list *sflist,
int sample_id);
43 static void set_zone_counter(
struct snd_sf_list *sflist,
47 static void set_sample_counter(
struct snd_sf_list *sflist,
51 static void sf_sample_delete(
struct snd_sf_list *sflist,
63 static void rebuild_presets(
struct snd_sf_list *sflist);
68 static int search_zones(
struct snd_sf_list *sflist,
int *notep,
int vel,
70 int max_layers,
int level);
72 static void snd_sf_init(
struct snd_sf_list *sflist);
73 static void snd_sf_clear(
struct snd_sf_list *sflist);
85 spin_unlock_irqrestore(&sflist->
lock, flags);
98 spin_unlock_irqrestore(&sflist->
lock, flags);
112 spin_unlock_irqrestore(&sflist->
lock, flags);
113 return close_patch(sflist);
115 spin_unlock_irqrestore(&sflist->
lock, flags);
136 if (count < (
long)
sizeof(patch)) {
143 count -=
sizeof(patch);
144 data +=
sizeof(patch);
150 if (count < patch.
len) {
163 rc = open_patch(sflist, data, count, client);
164 unlock_preset(sflist);
171 spin_unlock_irqrestore(&sflist->
lock, flags);
174 spin_unlock_irqrestore(&sflist->
lock, flags);
178 switch (patch.
type) {
183 rc = load_data(sflist, data, count);
186 rc = close_patch(sflist);
192 rc = load_map(sflist, data, count);
195 rc = probe_data(sflist, patch.
optarg);
201 "patch not opened\n");
207 if (! remove_info(sflist, sflist->
currsf, bank, instr))
214 unlock_preset(sflist);
222 is_special_type(
int type)
241 spin_unlock_irqrestore(&sflist->
lock, flags);
244 spin_unlock_irqrestore(&sflist->
lock, flags);
249 if (is_special_type(
parm.type)) {
251 sf = newsf(sflist,
parm.type,
NULL);
253 sf = newsf(sflist,
parm.type,
parm.name);
261 spin_unlock_irqrestore(&sflist->
lock, flags);
276 for (sf = sflist->
fonts; sf; sf = sf->
next) {
277 if (is_identical_font(sf, type, name)) {
305 is_identical_font(
struct snd_soundfont *sf,
int type,
unsigned char *name)
307 return ((sf->
type & SNDRV_SFNT_PAT_SHARED) &&
308 (sf->
type & 0x0f) == (type & 0x0f) &&
324 spin_unlock_irqrestore(&sflist->
lock, flags);
326 rebuild_presets(sflist);
334 probe_data(
struct snd_sf_list *sflist,
int sample_id)
339 if (find_sample(sflist->
currsf, sample_id))
370 init_voice_info(&zp->
v);
372 set_zone_counter(sflist, sf, zp);
403 set_sample_counter(sflist, sf, sp);
425 load_map(
struct snd_sf_list *sflist,
const void __user *data,
int count)
432 if (count < (
int)
sizeof(
map))
445 for (zp = sf->
zones; zp; prevp = zp, zp = zp->
next) {
449 zp->
v.low ==
map.map_key &&
450 zp->
v.start ==
map.src_instr &&
451 zp->
v.end ==
map.src_bank &&
452 zp->
v.fixkey ==
map.src_key) {
461 set_zone_counter(sflist, sf, zp);
467 if ((zp = sf_zone_new(sflist, sf)) ==
NULL)
473 if (
map.map_key >= 0) {
474 zp->
v.low =
map.map_key;
475 zp->
v.high =
map.map_key;
477 zp->
v.start =
map.src_instr;
478 zp->
v.end =
map.src_bank;
479 zp->
v.fixkey =
map.src_key;
480 zp->
v.sf_id = sf->
id;
482 add_preset(sflist, zp);
497 for (p = sf->
zones; p; p = next) {
512 rebuild_presets(sflist);
533 if (is_special_type(sf->
type))
536 if (count < (
long)
sizeof(
hdr)) {
544 count -=
sizeof(
hdr);
546 if (
hdr.nvoices <= 0 ||
hdr.nvoices >= 100) {
554 "patch length(%ld) is smaller than nvoices(%d)\n",
559 switch (
hdr.write_mode) {
563 for (zone = sf->
zones; zone; zone = zone->
next) {
572 remove_info(sflist, sf,
hdr.bank,
hdr.instr);
576 for (i = 0; i <
hdr.nvoices; i++) {
584 data +=
sizeof(tmpzone.v);
585 count -=
sizeof(tmpzone.v);
587 tmpzone.bank =
hdr.bank;
588 tmpzone.instr =
hdr.instr;
590 tmpzone.v.sf_id = sf->
id;
592 init_voice_parm(&tmpzone.v.parm);
595 if ((zone = sf_zone_new(sflist, sf)) ==
NULL) {
600 zone->
bank = tmpzone.bank;
601 zone->
instr = tmpzone.instr;
605 zone->
sample = set_sample(sf, &zone->
v);
616 memset(avp, 0,
sizeof(*avp));
628 init_voice_parm(&avp->
parm);
640 memset(pp, 0,
sizeof(*pp));
664 sample = find_sample(sf, avp->
sample);
673 avp->
start += sample->
v.start;
674 avp->
end += sample->
v.end;
694 if (p->
v.sample == sample_id)
708 load_data(
struct snd_sf_list *sflist,
const void __user *data,
long count)
719 if (is_special_type(sf->
type))
725 off =
sizeof(sample_info);
727 if (sample_info.size != (count-off)/2)
731 if (find_sample(sf, sample_info.sample)) {
733 if (sf->
type & SNDRV_SFNT_PAT_SHARED)
739 if ((sp = sf_sample_new(sflist, sf)) ==
NULL)
743 sp->
v.sf_id = sf->
id;
745 sp->
v.truesize = sp->
v.size;
750 if (sp->
v.size > 0) {
754 data + off, count - off);
756 sf_sample_delete(sflist, sf, sp);
767 static int log_tbl[129] = {
768 0x70000, 0x702df, 0x705b9, 0x7088e, 0x70b5d, 0x70e26, 0x710eb, 0x713aa,
769 0x71663, 0x71918, 0x71bc8, 0x71e72, 0x72118, 0x723b9, 0x72655, 0x728ed,
770 0x72b80, 0x72e0e, 0x73098, 0x7331d, 0x7359e, 0x7381b, 0x73a93, 0x73d08,
771 0x73f78, 0x741e4, 0x7444c, 0x746b0, 0x74910, 0x74b6c, 0x74dc4, 0x75019,
772 0x75269, 0x754b6, 0x75700, 0x75946, 0x75b88, 0x75dc7, 0x76002, 0x7623a,
773 0x7646e, 0x766a0, 0x768cd, 0x76af8, 0x76d1f, 0x76f43, 0x77164, 0x77382,
774 0x7759d, 0x777b4, 0x779c9, 0x77bdb, 0x77dea, 0x77ff5, 0x781fe, 0x78404,
775 0x78608, 0x78808, 0x78a06, 0x78c01, 0x78df9, 0x78fef, 0x791e2, 0x793d2,
776 0x795c0, 0x797ab, 0x79993, 0x79b79, 0x79d5d, 0x79f3e, 0x7a11d, 0x7a2f9,
777 0x7a4d3, 0x7a6ab, 0x7a880, 0x7aa53, 0x7ac24, 0x7adf2, 0x7afbe, 0x7b188,
778 0x7b350, 0x7b515, 0x7b6d8, 0x7b899, 0x7ba58, 0x7bc15, 0x7bdd0, 0x7bf89,
779 0x7c140, 0x7c2f5, 0x7c4a7, 0x7c658, 0x7c807, 0x7c9b3, 0x7cb5e, 0x7cd07,
780 0x7ceae, 0x7d053, 0x7d1f7, 0x7d398, 0x7d538, 0x7d6d6, 0x7d872, 0x7da0c,
781 0x7dba4, 0x7dd3b, 0x7ded0, 0x7e063, 0x7e1f4, 0x7e384, 0x7e512, 0x7e69f,
782 0x7e829, 0x7e9b3, 0x7eb3a, 0x7ecc0, 0x7ee44, 0x7efc7, 0x7f148, 0x7f2c8,
783 0x7f446, 0x7f5c2, 0x7f73d, 0x7f8b7, 0x7fa2f, 0x7fba5, 0x7fd1a, 0x7fe8d,
805 for (bit = 0; ! (amount & 0x80000000
L); bit++)
807 s = (amount >> 24) & 0x7f;
808 low = (amount >> 16) & 0xff;
810 v = (log_tbl[s + 1] * low + log_tbl[
s] * (0x100 -
low)) >> 8;
812 v = (v *
ratio) >> 16;
813 v += (24 -
bit) * ratio;
820 #define OFFSET_MSEC 653117
821 #define OFFSET_ABSCENT 851781
822 #define OFFSET_SAMPLERATE 1011119
824 #define ABSCENT_RATIO 1200
825 #define TIMECENT_RATIO 1200
826 #define SAMPLERATE_RATIO 4096
833 freq_to_note(
int mhz)
846 calc_rate_offset(
int hz)
857 r = (3 - ((rate >> 6) & 3)) * 3;
865 return (t * 10) / (p * 441);
871 static short attack_time_tbl[128] = {
872 32767, 32767, 5989, 4235, 2994, 2518, 2117, 1780, 1497, 1373, 1259, 1154, 1058, 970, 890, 816,
873 707, 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377,
874 361, 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188,
875 180, 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94,
876 90, 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47,
877 45, 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23,
878 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
879 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 6, 0,
882 static short decay_time_tbl[128] = {
883 32767, 32767, 22614, 15990, 11307, 9508, 7995, 6723, 5653, 5184, 4754, 4359, 3997, 3665, 3361, 3082,
884 2828, 2765, 2648, 2535, 2428, 2325, 2226, 2132, 2042, 1955, 1872, 1793, 1717, 1644, 1574, 1507,
885 1443, 1382, 1324, 1267, 1214, 1162, 1113, 1066, 978, 936, 897, 859, 822, 787, 754, 722,
886 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377, 361,
887 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188, 180,
888 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94, 90,
889 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45,
890 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23, 22,
897 int val = (0x7f * 92 -
msec) / 92;
898 if (val < 1) val = 1;
899 if (val >= 126) val = 126;
908 while (left <
right) {
910 if (msec < (
int)table[
mid])
922 return calc_parm_search(msec, attack_time_tbl);
929 return calc_parm_search(msec, decay_time_tbl);
933 255,111,95,86,79,74,70,66,63,61,58,56,54,52,50,49,
934 47,46,45,43,42,41,40,39,38,37,36,35,34,34,33,32,
935 31,31,30,29,29,28,27,27,26,26,25,24,24,23,23,22,
936 22,21,21,21,20,20,19,19,18,18,18,17,17,16,16,16,
937 15,15,15,14,14,14,13,13,13,12,12,12,11,11,11,10,
938 10,10,10,9,9,9,8,8,8,8,7,7,7,7,6,6,
939 6,6,5,5,5,5,5,4,4,4,4,3,3,3,3,3,
940 2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,0,
944 #define calc_gus_sustain(val) (0x7f - snd_sf_vol_table[(val)/2])
945 #define calc_gus_attenuation(val) snd_sf_vol_table[(val)/2]
949 load_guspatch(
struct snd_sf_list *sflist,
const char __user *data,
950 long count,
int client)
959 if (count < (
long)
sizeof(patch)) {
966 count -=
sizeof(patch);
967 data +=
sizeof(patch);
972 if ((smp = sf_sample_new(sflist, sf)) ==
NULL)
975 smp->
v.sample = sample_id;
977 smp->
v.end = patch.
len;
980 smp->
v.size = patch.
len;
983 smp->
v.mode_flags = 0;
1000 smp->
v.loopstart /= 2;
1001 smp->
v.loopend /= 2;
1006 smp->
v.truesize = 0;
1007 smp->
v.sf_id = sf->
id;
1010 if ((zone = sf_zone_new(sflist, sf)) ==
NULL) {
1011 sf_sample_delete(sflist, sf, smp);
1023 sf_sample_delete(sflist, sf, smp);
1032 zone->
v.sample = sample_id;
1033 zone->
v.rate_offset = calc_rate_offset(patch.
base_freq);
1035 zone->
v.root = note / 100;
1036 zone->
v.tune = -(note % 100);
1037 zone->
v.low = (freq_to_note(patch.
low_note) + 99) / 100;
1038 zone->
v.high = freq_to_note(patch.
high_note) / 100;
1040 zone->
v.pan = (patch.
panning + 128) / 2;
1043 "gus: basefrq=%d (ofs=%d) root=%d,tune=%d, range:%d-%d\n",
1045 zone->
v.root, zone->
v.tune, zone->
v.low, zone->
v.high);
1051 int attack, hold, decay,
release;
1052 attack = calc_gus_envelope_time
1054 hold = calc_gus_envelope_time
1057 decay = calc_gus_envelope_time
1060 release = calc_gus_envelope_time
1063 release += calc_gus_envelope_time
1066 release += calc_gus_envelope_time
1069 zone->
v.parm.volatkhld =
1078 "gus: atkhld=%x, dcysus=%x, volrel=%x, att=%d\n",
1079 zone->
v.parm.volatkhld,
1080 zone->
v.parm.voldcysus,
1081 zone->
v.parm.volrelease,
1082 zone->
v.attenuation);
1088 zone->
v.parm.volrelease = 0x807f;
1114 zone->
v.sf_id = sf->
id;
1116 zone->
sample = set_sample(sf, &zone->
v);
1119 add_preset(sflist, zone);
1127 long count,
int client)
1130 lock_preset(sflist);
1131 rc = load_guspatch(sflist, data, count, client);
1132 unlock_preset(sflist);
1154 for (sf = sflist->
fonts; sf; sf = sf->
next) {
1155 for (cur = sf->
zones; cur; cur = cur->
next) {
1158 cur->
sample = set_sample(sf, &cur->
v);
1163 add_preset(sflist, cur);
1178 zone = search_first_zone(sflist, cur->
bank, cur->
instr, cur->
v.low);
1179 if (zone && zone->
v.sf_id != cur->
v.sf_id) {
1189 delete_preset(sflist, zone);
1194 if ((index = get_index(cur->
bank, cur->
instr, cur->
v.low)) < 0)
1210 if ((index = get_index(zp->
bank, zp->
instr, zp->
v.low)) < 0)
1233 int def_preset,
int def_bank,
1237 unsigned long flags;
1245 spin_unlock_irqrestore(&sflist->
lock, flags);
1248 nvoices = search_zones(sflist, notep, vel, preset, bank,
1249 table, max_layers, 0);
1251 if (preset != def_preset || bank != def_bank)
1252 nvoices = search_zones(sflist, notep, vel,
1253 def_preset, def_bank,
1254 table, max_layers, 0);
1256 spin_unlock_irqrestore(&sflist->
lock, flags);
1270 if ((index = get_index(bank, preset, key)) < 0)
1273 if (zp->
instr == preset && zp->
bank == bank)
1284 search_zones(
struct snd_sf_list *sflist,
int *notep,
int vel,
1286 int max_layers,
int level)
1291 zp = search_first_zone(sflist, bank, preset, *notep);
1294 if (*notep >= zp->
v.low && *notep <= zp->
v.high &&
1295 vel >= zp->
v.vellow && vel <= zp->
v.velhigh) {
1298 int key = zp->
v.fixkey;
1299 preset = zp->
v.start;
1306 nvoices = search_zones(sflist, &key, vel,
1307 preset, bank, table,
1308 max_layers, level + 1);
1313 table[nvoices++] = zp;
1314 if (nvoices >= max_layers)
1329 get_index(
int bank,
int instr,
int key)
1371 for (sf = sflist->
fonts; sf; sf = nextsf) {
1373 for (zp = sf->
zones; zp; zp = nextzp) {
1377 for (sp = sf->
samples; sp; sp = nextsp) {
1387 snd_sf_init(sflist);
1409 snd_sf_init(sflist);
1423 lock_preset(sflist);
1426 snd_sf_clear(sflist);
1427 unlock_preset(sflist);
1439 lock_preset(sflist);
1442 snd_sf_clear(sflist);
1443 unlock_preset(sflist);
1459 lock_preset(sflist);
1467 for (sf = sflist->
fonts; sf; sf = sf->
next) {
1468 for (zp = sf->
zones; zp; zp = nextzp) {
1476 for (sp = sf->
samples; sp; sp = nextsp) {
1492 rebuild_presets(sflist);
1494 unlock_preset(sflist);