awe_wave.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:154k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * sound/awe_wave.c
  3.  *
  4.  * The low level driver for the AWE32/SB32/AWE64 wave table synth.
  5.  *   version 0.4.4; Jan. 4, 2000
  6.  *
  7.  * Copyright (C) 1996-2000 Takashi Iwai
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include <linux/awe_voice.h>
  24. #include <linux/config.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/string.h>
  28. #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
  29. #include <linux/isapnp.h>
  30. #endif
  31. #include "sound_config.h"
  32. #include "awe_wave.h"
  33. #include "awe_hw.h"
  34. #ifdef AWE_HAS_GUS_COMPATIBILITY
  35. #include "tuning.h"
  36. #include <linux/ultrasound.h>
  37. #endif
  38. /*
  39.  * debug message
  40.  */
  41. #ifdef AWE_DEBUG_ON
  42. #define DEBUG(LVL,XXX) {if (ctrls[AWE_MD_DEBUG_MODE] > LVL) { XXX; }}
  43. #define ERRMSG(XXX) {if (ctrls[AWE_MD_DEBUG_MODE]) { XXX; }}
  44. #define FATALERR(XXX) XXX
  45. #else
  46. #define DEBUG(LVL,XXX) /**/
  47. #define ERRMSG(XXX) XXX
  48. #define FATALERR(XXX) XXX
  49. #endif
  50. /*
  51.  * bank and voice record
  52.  */
  53. typedef struct _sf_list sf_list;
  54. typedef struct _awe_voice_list awe_voice_list;
  55. typedef struct _awe_sample_list awe_sample_list;
  56. /* soundfont record */
  57. struct _sf_list {
  58. unsigned short sf_id; /* id number */
  59. unsigned short type; /* lock & shared flags */
  60. int num_info; /* current info table index */
  61. int num_sample; /* current sample table index */
  62. int mem_ptr; /* current word byte pointer */
  63. awe_voice_list *infos, *last_infos; /* instruments */
  64. awe_sample_list *samples, *last_samples; /* samples */
  65. #ifdef AWE_ALLOW_SAMPLE_SHARING
  66. sf_list *shared; /* shared list */
  67. unsigned char name[AWE_PATCH_NAME_LEN]; /* sharing id */
  68. #endif
  69. sf_list *next, *prev;
  70. };
  71. /* instrument list */
  72. struct _awe_voice_list {
  73. awe_voice_info v; /* instrument information */
  74. sf_list *holder; /* parent sf_list of this record */
  75. unsigned char bank, instr; /* preset number information */
  76. char type, disabled; /* type=normal/mapped, disabled=boolean */
  77. awe_voice_list *next; /* linked list with same sf_id */
  78. awe_voice_list *next_instr; /* instrument list */
  79. awe_voice_list *next_bank; /* hash table list */
  80. };
  81. /* voice list type */
  82. #define V_ST_NORMAL 0
  83. #define V_ST_MAPPED 1
  84. /* sample list */
  85. struct _awe_sample_list {
  86. awe_sample_info v; /* sample information */
  87. sf_list *holder; /* parent sf_list of this record */
  88. awe_sample_list *next; /* linked list with same sf_id */
  89. };
  90. /* sample and information table */
  91. static int current_sf_id = 0; /* current number of fonts */
  92. static int locked_sf_id = 0; /* locked position */
  93. static sf_list *sfhead = NULL, *sftail = NULL; /* linked-lists */
  94. #define awe_free_mem_ptr() (sftail ? sftail->mem_ptr : 0)
  95. #define awe_free_info() (sftail ? sftail->num_info : 0)
  96. #define awe_free_sample() (sftail ? sftail->num_sample : 0)
  97. #define AWE_MAX_PRESETS 256
  98. #define AWE_DEFAULT_PRESET 0
  99. #define AWE_DEFAULT_BANK 0
  100. #define AWE_DEFAULT_DRUM 0
  101. #define AWE_DRUM_BANK 128
  102. #define MAX_LAYERS AWE_MAX_VOICES
  103. /* preset table index */
  104. static awe_voice_list *preset_table[AWE_MAX_PRESETS];
  105. /*
  106.  * voice table
  107.  */
  108. /* effects table */
  109. typedef struct FX_Rec { /* channel effects */
  110. unsigned char flags[AWE_FX_END];
  111. short val[AWE_FX_END];
  112. } FX_Rec;
  113. /* channel parameters */
  114. typedef struct _awe_chan_info {
  115. int channel; /* channel number */
  116. int bank; /* current tone bank */
  117. int instr; /* current program */
  118. int bender; /* midi pitchbend (-8192 - 8192) */
  119. int bender_range; /* midi bender range (x100) */
  120. int panning; /* panning (0-127) */
  121. int main_vol; /* channel volume (0-127) */
  122. int expression_vol; /* midi expression (0-127) */
  123. int chan_press; /* channel pressure */
  124. int sustained; /* sustain status in MIDI */
  125. FX_Rec fx; /* effects */
  126. FX_Rec fx_layer[MAX_LAYERS]; /* layer effects */
  127. } awe_chan_info;
  128. /* voice parameters */
  129. typedef struct _voice_info {
  130. int state;
  131. #define AWE_ST_OFF (1<<0) /* no sound */
  132. #define AWE_ST_ON (1<<1) /* playing */
  133. #define AWE_ST_STANDBY (1<<2) /* stand by for playing */
  134. #define AWE_ST_SUSTAINED (1<<3) /* sustained */
  135. #define AWE_ST_MARK (1<<4) /* marked for allocation */
  136. #define AWE_ST_DRAM (1<<5) /* DRAM read/write */
  137. #define AWE_ST_FM (1<<6) /* reserved for FM */
  138. #define AWE_ST_RELEASED (1<<7) /* released */
  139. int ch; /* midi channel */
  140. int key; /* internal key for search */
  141. int layer; /* layer number (for channel mode only) */
  142. int time; /* allocated time */
  143. awe_chan_info *cinfo; /* channel info */
  144. int note; /* midi key (0-127) */
  145. int velocity; /* midi velocity (0-127) */
  146. int sostenuto; /* sostenuto on/off */
  147. awe_voice_info *sample; /* assigned voice */
  148. /* EMU8000 parameters */
  149. int apitch; /* pitch parameter */
  150. int avol; /* volume parameter */
  151. int apan; /* panning parameter */
  152. int acutoff; /* cutoff parameter */
  153. short aaux; /* aux word */
  154. } voice_info;
  155. /* voice information */
  156. static voice_info voices[AWE_MAX_VOICES];
  157. #define IS_NO_SOUND(v) (voices[v].state & (AWE_ST_OFF|AWE_ST_RELEASED|AWE_ST_STANDBY|AWE_ST_SUSTAINED))
  158. #define IS_NO_EFFECT(v) (voices[v].state != AWE_ST_ON)
  159. #define IS_PLAYING(v) (voices[v].state & (AWE_ST_ON|AWE_ST_SUSTAINED|AWE_ST_RELEASED))
  160. #define IS_EMPTY(v) (voices[v].state & (AWE_ST_OFF|AWE_ST_MARK|AWE_ST_DRAM|AWE_ST_FM))
  161. /* MIDI channel effects information (for hw control) */
  162. static awe_chan_info channels[AWE_MAX_CHANNELS];
  163. /*
  164.  * global variables
  165.  */
  166. #ifndef AWE_DEFAULT_BASE_ADDR
  167. #define AWE_DEFAULT_BASE_ADDR 0 /* autodetect */
  168. #endif
  169. #ifndef AWE_DEFAULT_MEM_SIZE
  170. #define AWE_DEFAULT_MEM_SIZE -1 /* autodetect */
  171. #endif
  172. int io = AWE_DEFAULT_BASE_ADDR; /* Emu8000 base address */
  173. int memsize = AWE_DEFAULT_MEM_SIZE; /* memory size in Kbytes */
  174. #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
  175. static int isapnp = -1;
  176. #else
  177. static int isapnp = 0;
  178. #endif
  179. MODULE_AUTHOR("Takashi Iwai <iwai@ww.uni-erlangen.de>");
  180. MODULE_DESCRIPTION("SB AWE32/64 WaveTable driver");
  181. MODULE_LICENSE("GPL");
  182. MODULE_PARM(io, "i");
  183. MODULE_PARM_DESC(io, "base i/o port of Emu8000");
  184. MODULE_PARM(memsize, "i");
  185. MODULE_PARM_DESC(memsize, "onboard DRAM size in Kbytes");
  186. MODULE_PARM(isapnp, "i");
  187. MODULE_PARM_DESC(isapnp, "use ISAPnP detection");
  188. EXPORT_NO_SYMBOLS;
  189. /* DRAM start offset */
  190. static int awe_mem_start = AWE_DRAM_OFFSET;
  191. /* maximum channels for playing */
  192. static int awe_max_voices = AWE_MAX_VOICES;
  193. static int patch_opened = 0; /* sample already loaded? */
  194. static char atten_relative = FALSE;
  195. static short atten_offset = 0;
  196. static int awe_present = FALSE; /* awe device present? */
  197. static int awe_busy = FALSE; /* awe device opened? */
  198. static int my_dev = -1;
  199. #define DEFAULT_DRUM_FLAGS ((1 << 9) | (1 << 25))
  200. #define IS_DRUM_CHANNEL(c) (drum_flags & (1 << (c)))
  201. #define DRUM_CHANNEL_ON(c) (drum_flags |= (1 << (c)))
  202. #define DRUM_CHANNEL_OFF(c) (drum_flags &= ~(1 << (c)))
  203. static unsigned int drum_flags = DEFAULT_DRUM_FLAGS; /* channel flags */
  204. static int playing_mode = AWE_PLAY_INDIRECT;
  205. #define SINGLE_LAYER_MODE() (playing_mode == AWE_PLAY_INDIRECT || playing_mode == AWE_PLAY_DIRECT)
  206. #define MULTI_LAYER_MODE() (playing_mode == AWE_PLAY_MULTI || playing_mode == AWE_PLAY_MULTI2)
  207. static int current_alloc_time = 0; /* voice allocation index for channel mode */
  208. static struct synth_info awe_info = {
  209. "AWE32 Synth", /* name */
  210. 0, /* device */
  211. SYNTH_TYPE_SAMPLE, /* synth_type */
  212. SAMPLE_TYPE_AWE32, /* synth_subtype */
  213. 0, /* perc_mode (obsolete) */
  214. AWE_MAX_VOICES, /* nr_voices */
  215. 0, /* nr_drums (obsolete) */
  216. 400 /* instr_bank_size */
  217. };
  218. static struct voice_alloc_info *voice_alloc; /* set at initialization */
  219. /*
  220.  * function prototypes
  221.  */
  222. static int awe_check_port(void);
  223. static void awe_request_region(void);
  224. static void awe_release_region(void);
  225. static void awe_reset_samples(void);
  226. /* emu8000 chip i/o access */
  227. static void setup_ports(int p1, int p2, int p3);
  228. static void awe_poke(unsigned short cmd, unsigned short port, unsigned short data);
  229. static void awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data);
  230. static unsigned short awe_peek(unsigned short cmd, unsigned short port);
  231. static unsigned int awe_peek_dw(unsigned short cmd, unsigned short port);
  232. static void awe_wait(unsigned short delay);
  233. /* initialize emu8000 chip */
  234. static int _attach_awe(void);
  235. static void _unload_awe(void);
  236. static void awe_initialize(void);
  237. /* set voice parameters */
  238. static void awe_init_ctrl_parms(int init_all);
  239. static void awe_init_voice_info(awe_voice_info *vp);
  240. static void awe_init_voice_parm(awe_voice_parm *pp);
  241. #ifdef AWE_HAS_GUS_COMPATIBILITY
  242. static int freq_to_note(int freq);
  243. static int calc_rate_offset(int Hz);
  244. /*static int calc_parm_delay(int msec);*/
  245. static int calc_parm_hold(int msec);
  246. static int calc_parm_attack(int msec);
  247. static int calc_parm_decay(int msec);
  248. static int calc_parm_search(int msec, short *table);
  249. #endif /* gus compat */
  250. /* turn on/off note */
  251. static void awe_note_on(int voice);
  252. static void awe_note_off(int voice);
  253. static void awe_terminate(int voice);
  254. static void awe_exclusive_off(int voice);
  255. static void awe_note_off_all(int do_sustain);
  256. /* calculate voice parameters */
  257. typedef void (*fx_affect_func)(int voice, int forced);
  258. static void awe_set_pitch(int voice, int forced);
  259. static void awe_set_voice_pitch(int voice, int forced);
  260. static void awe_set_volume(int voice, int forced);
  261. static void awe_set_voice_vol(int voice, int forced);
  262. static void awe_set_pan(int voice, int forced);
  263. static void awe_fx_fmmod(int voice, int forced);
  264. static void awe_fx_tremfrq(int voice, int forced);
  265. static void awe_fx_fm2frq2(int voice, int forced);
  266. static void awe_fx_filterQ(int voice, int forced);
  267. static void awe_calc_pitch(int voice);
  268. #ifdef AWE_HAS_GUS_COMPATIBILITY
  269. static void awe_calc_pitch_from_freq(int voice, int freq);
  270. #endif
  271. static void awe_calc_volume(int voice);
  272. static void awe_update_volume(void);
  273. static void awe_change_master_volume(short val);
  274. static void awe_voice_init(int voice, int init_all);
  275. static void awe_channel_init(int ch, int init_all);
  276. static void awe_fx_init(int ch);
  277. static void awe_send_effect(int voice, int layer, int type, int val);
  278. static void awe_modwheel_change(int voice, int value);
  279. /* sequencer interface */
  280. static int awe_open(int dev, int mode);
  281. static void awe_close(int dev);
  282. static int awe_ioctl(int dev, unsigned int cmd, caddr_t arg);
  283. static int awe_kill_note(int dev, int voice, int note, int velocity);
  284. static int awe_start_note(int dev, int v, int note_num, int volume);
  285. static int awe_set_instr(int dev, int voice, int instr_no);
  286. static int awe_set_instr_2(int dev, int voice, int instr_no);
  287. static void awe_reset(int dev);
  288. static void awe_hw_control(int dev, unsigned char *event);
  289. static int awe_load_patch(int dev, int format, const char *addr,
  290.   int offs, int count, int pmgr_flag);
  291. static void awe_aftertouch(int dev, int voice, int pressure);
  292. static void awe_controller(int dev, int voice, int ctrl_num, int value);
  293. static void awe_panning(int dev, int voice, int value);
  294. static void awe_volume_method(int dev, int mode);
  295. static void awe_bender(int dev, int voice, int value);
  296. static int awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc);
  297. static void awe_setup_voice(int dev, int voice, int chn);
  298. #define awe_key_pressure(dev,voice,key,press) awe_start_note(dev,voice,(key)+128,press)
  299. /* hardware controls */
  300. #ifdef AWE_HAS_GUS_COMPATIBILITY
  301. static void awe_hw_gus_control(int dev, int cmd, unsigned char *event);
  302. #endif
  303. static void awe_hw_awe_control(int dev, int cmd, unsigned char *event);
  304. static void awe_voice_change(int voice, fx_affect_func func);
  305. static void awe_sostenuto_on(int voice, int forced);
  306. static void awe_sustain_off(int voice, int forced);
  307. static void awe_terminate_and_init(int voice, int forced);
  308. /* voice search */
  309. static int awe_search_key(int bank, int preset, int note);
  310. static awe_voice_list *awe_search_instr(int bank, int preset, int note);
  311. static int awe_search_multi_voices(awe_voice_list *rec, int note, int velocity, awe_voice_info **vlist);
  312. static void awe_alloc_multi_voices(int ch, int note, int velocity, int key);
  313. static void awe_alloc_one_voice(int voice, int note, int velocity);
  314. static int awe_clear_voice(void);
  315. /* load / remove patches */
  316. static int awe_open_patch(awe_patch_info *patch, const char *addr, int count);
  317. static int awe_close_patch(awe_patch_info *patch, const char *addr, int count);
  318. static int awe_unload_patch(awe_patch_info *patch, const char *addr, int count);
  319. static int awe_load_info(awe_patch_info *patch, const char *addr, int count);
  320. static int awe_remove_info(awe_patch_info *patch, const char *addr, int count);
  321. static int awe_load_data(awe_patch_info *patch, const char *addr, int count);
  322. static int awe_replace_data(awe_patch_info *patch, const char *addr, int count);
  323. static int awe_load_map(awe_patch_info *patch, const char *addr, int count);
  324. #ifdef AWE_HAS_GUS_COMPATIBILITY
  325. static int awe_load_guspatch(const char *addr, int offs, int size, int pmgr_flag);
  326. #endif
  327. /*static int awe_probe_info(awe_patch_info *patch, const char *addr, int count);*/
  328. static int awe_probe_data(awe_patch_info *patch, const char *addr, int count);
  329. static sf_list *check_patch_opened(int type, char *name);
  330. static int awe_write_wave_data(const char *addr, int offset, awe_sample_list *sp, int channels);
  331. static int awe_create_sf(int type, char *name);
  332. static void awe_free_sf(sf_list *sf);
  333. static void add_sf_info(sf_list *sf, awe_voice_list *rec);
  334. static void add_sf_sample(sf_list *sf, awe_sample_list *smp);
  335. static void purge_old_list(awe_voice_list *rec, awe_voice_list *next);
  336. static void add_info_list(awe_voice_list *rec);
  337. static void awe_remove_samples(int sf_id);
  338. static void rebuild_preset_list(void);
  339. static short awe_set_sample(awe_voice_list *rec);
  340. static awe_sample_list *search_sample_index(sf_list *sf, int sample);
  341. static int is_identical_holder(sf_list *sf1, sf_list *sf2);
  342. #ifdef AWE_ALLOW_SAMPLE_SHARING
  343. static int is_identical_name(unsigned char *name, sf_list *p);
  344. static int is_shared_sf(unsigned char *name);
  345. static int info_duplicated(sf_list *sf, awe_voice_list *rec);
  346. #endif /* allow sharing */
  347. /* lowlevel functions */
  348. static void awe_init_audio(void);
  349. static void awe_init_dma(void);
  350. static void awe_init_array(void);
  351. static void awe_send_array(unsigned short *data);
  352. static void awe_tweak_voice(int voice);
  353. static void awe_tweak(void);
  354. static void awe_init_fm(void);
  355. static int awe_open_dram_for_write(int offset, int channels);
  356. static void awe_open_dram_for_check(void);
  357. static void awe_close_dram(void);
  358. /*static void awe_write_dram(unsigned short c);*/
  359. static int awe_detect_base(int addr);
  360. static int awe_detect(void);
  361. static void awe_check_dram(void);
  362. static int awe_load_chorus_fx(awe_patch_info *patch, const char *addr, int count);
  363. static void awe_set_chorus_mode(int mode);
  364. static void awe_update_chorus_mode(void);
  365. static int awe_load_reverb_fx(awe_patch_info *patch, const char *addr, int count);
  366. static void awe_set_reverb_mode(int mode);
  367. static void awe_update_reverb_mode(void);
  368. static void awe_equalizer(int bass, int treble);
  369. static void awe_update_equalizer(void);
  370. #ifdef CONFIG_AWE32_MIXER
  371. static void attach_mixer(void);
  372. static void unload_mixer(void);
  373. #endif
  374. #ifdef CONFIG_AWE32_MIDIEMU
  375. static void attach_midiemu(void);
  376. static void unload_midiemu(void);
  377. #endif
  378. #define limitvalue(x, a, b) if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b)
  379. /*
  380.  * control parameters
  381.  */
  382. #ifdef AWE_USE_NEW_VOLUME_CALC
  383. #define DEF_VOLUME_CALC TRUE
  384. #else
  385. #define DEF_VOLUME_CALC FALSE
  386. #endif /* new volume */
  387. #define DEF_ZERO_ATTEN 32 /* 12dB below */
  388. #define DEF_MOD_SENSE 18
  389. #define DEF_CHORUS_MODE 2
  390. #define DEF_REVERB_MODE 4
  391. #define DEF_BASS_LEVEL 5
  392. #define DEF_TREBLE_LEVEL 9
  393. static struct CtrlParmsDef {
  394. int value;
  395. int init_each_time;
  396. void (*update)(void);
  397. } ctrl_parms[AWE_MD_END] = {
  398. {0,0, NULL}, {0,0, NULL}, /* <-- not used */
  399. {AWE_VERSION_NUMBER, FALSE, NULL},
  400. {TRUE, FALSE, NULL}, /* exclusive */
  401. {TRUE, FALSE, NULL}, /* realpan */
  402. {AWE_DEFAULT_BANK, FALSE, NULL}, /* gusbank */
  403. {FALSE, TRUE, NULL}, /* keep effect */
  404. {DEF_ZERO_ATTEN, FALSE, awe_update_volume}, /* zero_atten */
  405. {FALSE, FALSE, NULL}, /* chn_prior */
  406. {DEF_MOD_SENSE, FALSE, NULL}, /* modwheel sense */
  407. {AWE_DEFAULT_PRESET, FALSE, NULL}, /* def_preset */
  408. {AWE_DEFAULT_BANK, FALSE, NULL}, /* def_bank */
  409. {AWE_DEFAULT_DRUM, FALSE, NULL}, /* def_drum */
  410. {FALSE, FALSE, NULL}, /* toggle_drum_bank */
  411. {DEF_VOLUME_CALC, FALSE, awe_update_volume}, /* new_volume_calc */
  412. {DEF_CHORUS_MODE, FALSE, awe_update_chorus_mode}, /* chorus mode */
  413. {DEF_REVERB_MODE, FALSE, awe_update_reverb_mode}, /* reverb mode */
  414. {DEF_BASS_LEVEL, FALSE, awe_update_equalizer}, /* bass level */
  415. {DEF_TREBLE_LEVEL, FALSE, awe_update_equalizer}, /* treble level */
  416. {0, FALSE, NULL}, /* debug mode */
  417. {FALSE, FALSE, NULL}, /* pan exchange */
  418. };
  419. static int ctrls[AWE_MD_END];
  420. /*
  421.  * synth operation table
  422.  */
  423. static struct synth_operations awe_operations =
  424. {
  425. owner: THIS_MODULE,
  426. id: "EMU8K",
  427. info: &awe_info,
  428. midi_dev: 0,
  429. synth_type: SYNTH_TYPE_SAMPLE,
  430. synth_subtype: SAMPLE_TYPE_AWE32,
  431. open: awe_open,
  432. close: awe_close,
  433. ioctl: awe_ioctl,
  434. kill_note: awe_kill_note,
  435. start_note: awe_start_note,
  436. set_instr: awe_set_instr_2,
  437. reset: awe_reset,
  438. hw_control: awe_hw_control,
  439. load_patch: awe_load_patch,
  440. aftertouch: awe_aftertouch,
  441. controller: awe_controller,
  442. panning: awe_panning,
  443. volume_method: awe_volume_method,
  444. bender: awe_bender,
  445. alloc_voice: awe_alloc,
  446. setup_voice: awe_setup_voice
  447. };
  448. /*
  449.  * General attach / unload interface
  450.  */
  451. static int __init _attach_awe(void)
  452. {
  453. if (awe_present) return 0; /* for OSS38.. called twice? */
  454. /* check presence of AWE32 card */
  455. if (! awe_detect()) {
  456. printk(KERN_ERR "AWE32: not detectedn");
  457. return 0;
  458. }
  459. /* check AWE32 ports are available */
  460. if (awe_check_port()) {
  461. printk(KERN_ERR "AWE32: I/O area already used.n");
  462. return 0;
  463. }
  464. /* set buffers to NULL */
  465. sfhead = sftail = NULL;
  466. my_dev = sound_alloc_synthdev();
  467. if (my_dev == -1) {
  468. printk(KERN_ERR "AWE32 Error: too many synthesizersn");
  469. return 0;
  470. }
  471. voice_alloc = &awe_operations.alloc;
  472. voice_alloc->max_voice = awe_max_voices;
  473. synth_devs[my_dev] = &awe_operations;
  474. #ifdef CONFIG_AWE32_MIXER
  475. attach_mixer();
  476. #endif
  477. #ifdef CONFIG_AWE32_MIDIEMU
  478. attach_midiemu();
  479. #endif
  480. /* reserve I/O ports for awedrv */
  481. awe_request_region();
  482. /* clear all samples */
  483. awe_reset_samples();
  484. /* intialize AWE32 hardware */
  485. awe_initialize();
  486. sprintf(awe_info.name, "AWE32-%s (RAM%dk)",
  487. AWEDRV_VERSION, memsize/1024);
  488. printk(KERN_INFO "<SoundBlaster EMU8000 (RAM%dk)>n", memsize/1024);
  489. awe_present = TRUE;
  490. return 1;
  491. }
  492. static void free_tables(void)
  493. {
  494. if (sftail) {
  495. sf_list *p, *prev;
  496. for (p = sftail; p; p = prev) {
  497. prev = p->prev;
  498. awe_free_sf(p);
  499. }
  500. }
  501. sfhead = sftail = NULL;
  502. }
  503. static void __exit _unload_awe(void)
  504. {
  505. if (awe_present) {
  506. awe_reset_samples();
  507. awe_release_region();
  508. free_tables();
  509. #ifdef CONFIG_AWE32_MIXER
  510. unload_mixer();
  511. #endif
  512. #ifdef CONFIG_AWE32_MIDIEMU
  513. unload_midiemu();
  514. #endif
  515. sound_unload_synthdev(my_dev);
  516. awe_present = FALSE;
  517. }
  518. }
  519. /*
  520.  * clear sample tables 
  521.  */
  522. static void
  523. awe_reset_samples(void)
  524. {
  525. /* free all bank tables */
  526. memset(preset_table, 0, sizeof(preset_table));
  527. free_tables();
  528. current_sf_id = 0;
  529. locked_sf_id = 0;
  530. patch_opened = 0;
  531. }
  532. /*
  533.  * EMU register access
  534.  */
  535. /* select a given AWE32 pointer */
  536. static int awe_ports[5];
  537. static int port_setuped = FALSE;
  538. static int awe_cur_cmd = -1;
  539. #define awe_set_cmd(cmd) 
  540. if (awe_cur_cmd != cmd) { outw(cmd, awe_ports[Pointer]); awe_cur_cmd = cmd; }
  541. /* store values to i/o port array */
  542. static void setup_ports(int port1, int port2, int port3)
  543. {
  544. awe_ports[0] = port1;
  545. if (port2 == 0)
  546. port2 = port1 + 0x400;
  547. awe_ports[1] = port2;
  548. awe_ports[2] = port2 + 2;
  549. if (port3 == 0)
  550. port3 = port1 + 0x800;
  551. awe_ports[3] = port3;
  552. awe_ports[4] = port3 + 2;
  553. port_setuped = TRUE;
  554. }
  555. /* write 16bit data */
  556. static void
  557. awe_poke(unsigned short cmd, unsigned short port, unsigned short data)
  558. {
  559. awe_set_cmd(cmd);
  560. outw(data, awe_ports[port]);
  561. }
  562. /* write 32bit data */
  563. static void
  564. awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data)
  565. {
  566. unsigned short addr = awe_ports[port];
  567. awe_set_cmd(cmd);
  568. outw(data, addr); /* write lower 16 bits */
  569. outw(data >> 16, addr + 2); /* write higher 16 bits */
  570. }
  571. /* read 16bit data */
  572. static unsigned short
  573. awe_peek(unsigned short cmd, unsigned short port)
  574. {
  575. unsigned short k;
  576. awe_set_cmd(cmd);
  577. k = inw(awe_ports[port]);
  578. return k;
  579. }
  580. /* read 32bit data */
  581. static unsigned int
  582. awe_peek_dw(unsigned short cmd, unsigned short port)
  583. {
  584. unsigned int k1, k2;
  585. unsigned short addr = awe_ports[port];
  586. awe_set_cmd(cmd);
  587. k1 = inw(addr);
  588. k2 = inw(addr + 2);
  589. k1 |= k2 << 16;
  590. return k1;
  591. }
  592. /* wait delay number of AWE32 44100Hz clocks */
  593. #ifdef WAIT_BY_LOOP /* wait by loop -- that's not good.. */
  594. static void
  595. awe_wait(unsigned short delay)
  596. {
  597. unsigned short clock, target;
  598. unsigned short port = awe_ports[AWE_WC_Port];
  599. int counter;
  600.   
  601. /* sample counter */
  602. awe_set_cmd(AWE_WC_Cmd);
  603. clock = (unsigned short)inw(port);
  604. target = clock + delay;
  605. counter = 0;
  606. if (target < clock) {
  607. for (; (unsigned short)inw(port) > target; counter++)
  608. if (counter > 65536)
  609. break;
  610. }
  611. for (; (unsigned short)inw(port) < target; counter++)
  612. if (counter > 65536)
  613. break;
  614. }
  615. #else
  616. static void awe_wait(unsigned short delay)
  617. {
  618. current->state = TASK_INTERRUPTIBLE;
  619. schedule_timeout((HZ*(unsigned long)delay + 44099)/44100);
  620. }
  621. /*
  622. static void awe_wait(unsigned short delay)
  623. {
  624. udelay(((unsigned long)delay * 1000000L + 44099) / 44100);
  625. }
  626. */
  627. #endif /* wait by loop */
  628. /* write a word data */
  629. #define awe_write_dram(c) awe_poke(AWE_SMLD, c)
  630. /*
  631.  * port check / request
  632.  *  0x620-623, 0xA20-A23, 0xE20-E23
  633.  */
  634. static int __init
  635. awe_check_port(void)
  636. {
  637. if (! port_setuped) return 0;
  638. return (check_region(awe_ports[0], 4) ||
  639. check_region(awe_ports[1], 4) ||
  640. check_region(awe_ports[3], 4));
  641. }
  642. static void __init
  643. awe_request_region(void)
  644. {
  645. if (! port_setuped) return;
  646. request_region(awe_ports[0], 4, "sound driver (AWE32)");
  647. request_region(awe_ports[1], 4, "sound driver (AWE32)");
  648. request_region(awe_ports[3], 4, "sound driver (AWE32)");
  649. }
  650. static void __exit
  651. awe_release_region(void)
  652. {
  653. if (! port_setuped) return;
  654. release_region(awe_ports[0], 4);
  655. release_region(awe_ports[1], 4);
  656. release_region(awe_ports[3], 4);
  657. }
  658. /*
  659.  * initialization of AWE driver
  660.  */
  661. static void
  662. awe_initialize(void)
  663. {
  664. DEBUG(0,printk("AWE32: initializing..n"));
  665. /* initialize hardware configuration */
  666. awe_poke(AWE_HWCF1, 0x0059);
  667. awe_poke(AWE_HWCF2, 0x0020);
  668. /* disable audio; this seems to reduce a clicking noise a bit.. */
  669. awe_poke(AWE_HWCF3, 0);
  670. /* initialize audio channels */
  671. awe_init_audio();
  672. /* initialize DMA */
  673. awe_init_dma();
  674. /* initialize init array */
  675. awe_init_array();
  676. /* check DRAM memory size */
  677. awe_check_dram();
  678. /* initialize the FM section of the AWE32 */
  679. awe_init_fm();
  680. /* set up voice envelopes */
  681. awe_tweak();
  682. /* enable audio */
  683. awe_poke(AWE_HWCF3, 0x0004);
  684. /* set default values */
  685. awe_init_ctrl_parms(TRUE);
  686. /* set equalizer */
  687. awe_update_equalizer();
  688. /* set reverb & chorus modes */
  689. awe_update_reverb_mode();
  690. awe_update_chorus_mode();
  691. }
  692. /*
  693.  * AWE32 voice parameters
  694.  */
  695. /* initialize voice_info record */
  696. static void
  697. awe_init_voice_info(awe_voice_info *vp)
  698. {
  699. vp->sample = 0;
  700. vp->rate_offset = 0;
  701. vp->start = 0;
  702. vp->end = 0;
  703. vp->loopstart = 0;
  704. vp->loopend = 0;
  705. vp->mode = 0;
  706. vp->root = 60;
  707. vp->tune = 0;
  708. vp->low = 0;
  709. vp->high = 127;
  710. vp->vellow = 0;
  711. vp->velhigh = 127;
  712. vp->fixkey = -1;
  713. vp->fixvel = -1;
  714. vp->fixpan = -1;
  715. vp->pan = -1;
  716. vp->exclusiveClass = 0;
  717. vp->amplitude = 127;
  718. vp->attenuation = 0;
  719. vp->scaleTuning = 100;
  720. awe_init_voice_parm(&vp->parm);
  721. }
  722. /* initialize voice_parm record:
  723.  * Env1/2: delay=0, attack=0, hold=0, sustain=0, decay=0, release=0.
  724.  * Vibrato and Tremolo effects are zero.
  725.  * Cutoff is maximum.
  726.  * Chorus and Reverb effects are zero.
  727.  */
  728. static void
  729. awe_init_voice_parm(awe_voice_parm *pp)
  730. {
  731. pp->moddelay = 0x8000;
  732. pp->modatkhld = 0x7f7f;
  733. pp->moddcysus = 0x7f7f;
  734. pp->modrelease = 0x807f;
  735. pp->modkeyhold = 0;
  736. pp->modkeydecay = 0;
  737. pp->voldelay = 0x8000;
  738. pp->volatkhld = 0x7f7f;
  739. pp->voldcysus = 0x7f7f;
  740. pp->volrelease = 0x807f;
  741. pp->volkeyhold = 0;
  742. pp->volkeydecay = 0;
  743. pp->lfo1delay = 0x8000;
  744. pp->lfo2delay = 0x8000;
  745. pp->pefe = 0;
  746. pp->fmmod = 0;
  747. pp->tremfrq = 0;
  748. pp->fm2frq2 = 0;
  749. pp->cutoff = 0xff;
  750. pp->filterQ = 0;
  751. pp->chorus = 0;
  752. pp->reverb = 0;
  753. }
  754. #ifdef AWE_HAS_GUS_COMPATIBILITY
  755. /* convert frequency mHz to abstract cents (= midi key * 100) */
  756. static int
  757. freq_to_note(int mHz)
  758. {
  759. /* abscents = log(mHz/8176) / log(2) * 1200 */
  760. unsigned int max_val = (unsigned int)0xffffffff / 10000;
  761. int i, times;
  762. unsigned int base;
  763. unsigned int freq;
  764. int note, tune;
  765. if (mHz == 0)
  766. return 0;
  767. if (mHz < 0)
  768. return 12799; /* maximum */
  769. freq = mHz;
  770. note = 0;
  771. for (base = 8176 * 2; freq >= base; base *= 2) {
  772. note += 12;
  773. if (note >= 128) /* over maximum */
  774. return 12799;
  775. }
  776. base /= 2;
  777. /* to avoid overflow... */
  778. times = 10000;
  779. while (freq > max_val) {
  780. max_val *= 10;
  781. times /= 10;
  782. base /= 10;
  783. }
  784. freq = freq * times / base;
  785. for (i = 0; i < 12; i++) {
  786. if (freq < semitone_tuning[i+1])
  787. break;
  788. note++;
  789. }
  790. tune = 0;
  791. freq = freq * 10000 / semitone_tuning[i];
  792. for (i = 0; i < 100; i++) {
  793. if (freq < cent_tuning[i+1])
  794. break;
  795. tune++;
  796. }
  797. return note * 100 + tune;
  798. }
  799. /* convert Hz to AWE32 rate offset:
  800.  * sample pitch offset for the specified sample rate
  801.  * rate=44100 is no offset, each 4096 is 1 octave (twice).
  802.  * eg, when rate is 22050, this offset becomes -4096.
  803.  */
  804. static int
  805. calc_rate_offset(int Hz)
  806. {
  807. /* offset = log(Hz / 44100) / log(2) * 4096 */
  808. int freq, base, i;
  809. /* maybe smaller than max (44100Hz) */
  810. if (Hz <= 0 || Hz >= 44100) return 0;
  811. base = 0;
  812. for (freq = Hz * 2; freq < 44100; freq *= 2)
  813. base++;
  814. base *= 1200;
  815. freq = 44100 * 10000 / (freq/2);
  816. for (i = 0; i < 12; i++) {
  817. if (freq < semitone_tuning[i+1])
  818. break;
  819. base += 100;
  820. }
  821. freq = freq * 10000 / semitone_tuning[i];
  822. for (i = 0; i < 100; i++) {
  823. if (freq < cent_tuning[i+1])
  824. break;
  825. base++;
  826. }
  827. return -base * 4096 / 1200;
  828. }
  829. /*
  830.  * convert envelope time parameter to AWE32 raw parameter
  831.  */
  832. /* attack & decay/release time table (msec) */
  833. static short attack_time_tbl[128] = {
  834. 32767, 32767, 5989, 4235, 2994, 2518, 2117, 1780, 1497, 1373, 1259, 1154, 1058, 970, 890, 816,
  835. 707, 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377,
  836. 361, 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188,
  837. 180, 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94,
  838. 90, 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47,
  839. 45, 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23,
  840. 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
  841. 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 6, 0,
  842. };
  843. static short decay_time_tbl[128] = {
  844. 32767, 32767, 22614, 15990, 11307, 9508, 7995, 6723, 5653, 5184, 4754, 4359, 3997, 3665, 3361, 3082,
  845. 2828, 2765, 2648, 2535, 2428, 2325, 2226, 2132, 2042, 1955, 1872, 1793, 1717, 1644, 1574, 1507,
  846. 1443, 1382, 1324, 1267, 1214, 1162, 1113, 1066, 978, 936, 897, 859, 822, 787, 754, 722,
  847. 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377, 361,
  848. 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188, 180,
  849. 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94, 90,
  850. 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45,
  851. 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23, 22,
  852. };
  853. #define calc_parm_delay(msec) (0x8000 - (msec) * 1000 / 725);
  854. /* delay time = 0x8000 - msec/92 */
  855. static int
  856. calc_parm_hold(int msec)
  857. {
  858. int val = (0x7f * 92 - msec) / 92;
  859. if (val < 1) val = 1;
  860. if (val > 127) val = 127;
  861. return val;
  862. }
  863. /* attack time: search from time table */
  864. static int
  865. calc_parm_attack(int msec)
  866. {
  867. return calc_parm_search(msec, attack_time_tbl);
  868. }
  869. /* decay/release time: search from time table */
  870. static int
  871. calc_parm_decay(int msec)
  872. {
  873. return calc_parm_search(msec, decay_time_tbl);
  874. }
  875. /* search an index for specified time from given time table */
  876. static int
  877. calc_parm_search(int msec, short *table)
  878. {
  879. int left = 1, right = 127, mid;
  880. while (left < right) {
  881. mid = (left + right) / 2;
  882. if (msec < (int)table[mid])
  883. left = mid + 1;
  884. else
  885. right = mid;
  886. }
  887. return left;
  888. }
  889. #endif /* AWE_HAS_GUS_COMPATIBILITY */
  890. /*
  891.  * effects table
  892.  */
  893. /* set an effect value */
  894. #define FX_FLAG_OFF 0
  895. #define FX_FLAG_SET 1
  896. #define FX_FLAG_ADD 2
  897. #define FX_SET(rec,type,value) 
  898. ((rec)->flags[type] = FX_FLAG_SET, (rec)->val[type] = (value))
  899. #define FX_ADD(rec,type,value) 
  900. ((rec)->flags[type] = FX_FLAG_ADD, (rec)->val[type] = (value))
  901. #define FX_UNSET(rec,type) 
  902. ((rec)->flags[type] = FX_FLAG_OFF, (rec)->val[type] = 0)
  903. /* check the effect value is set */
  904. #define FX_ON(rec,type) ((rec)->flags[type])
  905. #define PARM_BYTE 0
  906. #define PARM_WORD 1
  907. #define PARM_SIGN 2
  908. static struct PARM_DEFS {
  909. int type; /* byte or word */
  910. int low, high; /* value range */
  911. fx_affect_func realtime; /* realtime paramater change */
  912. } parm_defs[] = {
  913. {PARM_WORD, 0, 0x8000, NULL}, /* env1 delay */
  914. {PARM_BYTE, 1, 0x7f, NULL}, /* env1 attack */
  915. {PARM_BYTE, 0, 0x7e, NULL}, /* env1 hold */
  916. {PARM_BYTE, 1, 0x7f, NULL}, /* env1 decay */
  917. {PARM_BYTE, 1, 0x7f, NULL}, /* env1 release */
  918. {PARM_BYTE, 0, 0x7f, NULL}, /* env1 sustain */
  919. {PARM_BYTE, 0, 0xff, NULL}, /* env1 pitch */
  920. {PARM_BYTE, 0, 0xff, NULL}, /* env1 cutoff */
  921. {PARM_WORD, 0, 0x8000, NULL}, /* env2 delay */
  922. {PARM_BYTE, 1, 0x7f, NULL}, /* env2 attack */
  923. {PARM_BYTE, 0, 0x7e, NULL}, /* env2 hold */
  924. {PARM_BYTE, 1, 0x7f, NULL}, /* env2 decay */
  925. {PARM_BYTE, 1, 0x7f, NULL}, /* env2 release */
  926. {PARM_BYTE, 0, 0x7f, NULL}, /* env2 sustain */
  927. {PARM_WORD, 0, 0x8000, NULL}, /* lfo1 delay */
  928. {PARM_BYTE, 0, 0xff, awe_fx_tremfrq}, /* lfo1 freq */
  929. {PARM_SIGN, -128, 127, awe_fx_tremfrq}, /* lfo1 volume */
  930. {PARM_SIGN, -128, 127, awe_fx_fmmod}, /* lfo1 pitch */
  931. {PARM_BYTE, 0, 0xff, awe_fx_fmmod}, /* lfo1 cutoff */
  932. {PARM_WORD, 0, 0x8000, NULL}, /* lfo2 delay */
  933. {PARM_BYTE, 0, 0xff, awe_fx_fm2frq2}, /* lfo2 freq */
  934. {PARM_SIGN, -128, 127, awe_fx_fm2frq2}, /* lfo2 pitch */
  935. {PARM_WORD, 0, 0xffff, awe_set_voice_pitch}, /* initial pitch */
  936. {PARM_BYTE, 0, 0xff, NULL}, /* chorus */
  937. {PARM_BYTE, 0, 0xff, NULL}, /* reverb */
  938. {PARM_BYTE, 0, 0xff, awe_set_volume}, /* initial cutoff */
  939. {PARM_BYTE, 0, 15, awe_fx_filterQ}, /* initial resonance */
  940. {PARM_WORD, 0, 0xffff, NULL}, /* sample start */
  941. {PARM_WORD, 0, 0xffff, NULL}, /* loop start */
  942. {PARM_WORD, 0, 0xffff, NULL}, /* loop end */
  943. {PARM_WORD, 0, 0xffff, NULL}, /* coarse sample start */
  944. {PARM_WORD, 0, 0xffff, NULL}, /* coarse loop start */
  945. {PARM_WORD, 0, 0xffff, NULL}, /* coarse loop end */
  946. {PARM_BYTE, 0, 0xff, awe_set_volume}, /* initial attenuation */
  947. };
  948. static unsigned char
  949. FX_BYTE(FX_Rec *rec, FX_Rec *lay, int type, unsigned char value)
  950. {
  951. int effect = 0;
  952. int on = 0;
  953. if (lay && (on = FX_ON(lay, type)) != 0)
  954. effect = lay->val[type];
  955. if (!on && (on = FX_ON(rec, type)) != 0)
  956. effect = rec->val[type];
  957. if (on == FX_FLAG_ADD) {
  958. if (parm_defs[type].type == PARM_SIGN) {
  959. if (value > 0x7f)
  960. effect += (int)value - 0x100;
  961. else
  962. effect += (int)value;
  963. } else {
  964. effect += (int)value;
  965. }
  966. }
  967. if (on) {
  968. if (effect < parm_defs[type].low)
  969. effect = parm_defs[type].low;
  970. else if (effect > parm_defs[type].high)
  971. effect = parm_defs[type].high;
  972. return (unsigned char)effect;
  973. }
  974. return value;
  975. }
  976. /* get word effect value */
  977. static unsigned short
  978. FX_WORD(FX_Rec *rec, FX_Rec *lay, int type, unsigned short value)
  979. {
  980. int effect = 0;
  981. int on = 0;
  982. if (lay && (on = FX_ON(lay, type)) != 0)
  983. effect = lay->val[type];
  984. if (!on && (on = FX_ON(rec, type)) != 0)
  985. effect = rec->val[type];
  986. if (on == FX_FLAG_ADD)
  987. effect += (int)value;
  988. if (on) {
  989. if (effect < parm_defs[type].low)
  990. effect = parm_defs[type].low;
  991. else if (effect > parm_defs[type].high)
  992. effect = parm_defs[type].high;
  993. return (unsigned short)effect;
  994. }
  995. return value;
  996. }
  997. /* get word (upper=type1/lower=type2) effect value */
  998. static unsigned short
  999. FX_COMB(FX_Rec *rec, FX_Rec *lay, int type1, int type2, unsigned short value)
  1000. {
  1001. unsigned short tmp;
  1002. tmp = FX_BYTE(rec, lay, type1, (unsigned char)(value >> 8));
  1003. tmp <<= 8;
  1004. tmp |= FX_BYTE(rec, lay, type2, (unsigned char)(value & 0xff));
  1005. return tmp;
  1006. }
  1007. /* address offset */
  1008. static int
  1009. FX_OFFSET(FX_Rec *rec, FX_Rec *lay, int lo, int hi, int mode)
  1010. {
  1011. int addr = 0;
  1012. if (lay && FX_ON(lay, hi))
  1013. addr = (short)lay->val[hi];
  1014. else if (FX_ON(rec, hi))
  1015. addr = (short)rec->val[hi];
  1016. addr = addr << 15;
  1017. if (lay && FX_ON(lay, lo))
  1018. addr += (short)lay->val[lo];
  1019. else if (FX_ON(rec, lo))
  1020. addr += (short)rec->val[lo];
  1021. if (!(mode & AWE_SAMPLE_8BITS))
  1022. addr /= 2;
  1023. return addr;
  1024. }
  1025. /*
  1026.  * turn on/off sample
  1027.  */
  1028. /* table for volume target calculation */
  1029. static unsigned short voltarget[16] = { 
  1030.    0xEAC0, 0XE0C8, 0XD740, 0XCE20, 0XC560, 0XBD08, 0XB500, 0XAD58,
  1031.    0XA5F8, 0X9EF0, 0X9830, 0X91C0, 0X8B90, 0X85A8, 0X8000, 0X7A90
  1032. };
  1033. static void
  1034. awe_note_on(int voice)
  1035. {
  1036. unsigned int temp;
  1037. int addr;
  1038. int vtarget, ftarget, ptarget, pitch;
  1039. awe_voice_info *vp;
  1040. awe_voice_parm_block *parm;
  1041. FX_Rec *fx = &voices[voice].cinfo->fx;
  1042. FX_Rec *fx_lay = NULL;
  1043. if (voices[voice].layer < MAX_LAYERS)
  1044. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1045. /* A voice sample must assigned before calling */
  1046. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1047. return;
  1048. parm = (awe_voice_parm_block*)&vp->parm;
  1049. /* channel to be silent and idle */
  1050. awe_poke(AWE_DCYSUSV(voice), 0x0080);
  1051. awe_poke(AWE_VTFT(voice), 0x0000FFFF);
  1052. awe_poke(AWE_CVCF(voice), 0x0000FFFF);
  1053. awe_poke(AWE_PTRX(voice), 0);
  1054. awe_poke(AWE_CPF(voice), 0);
  1055. /* set pitch offset */
  1056. awe_set_pitch(voice, TRUE);
  1057. /* modulation & volume envelope */
  1058. if (parm->modatk >= 0x80 && parm->moddelay >= 0x8000) {
  1059. awe_poke(AWE_ENVVAL(voice), 0xBFFF);
  1060. pitch = (parm->env1pit<<4) + voices[voice].apitch;
  1061. if (pitch > 0xffff) pitch = 0xffff;
  1062. /* calculate filter target */
  1063. ftarget = parm->cutoff + parm->env1fc;
  1064. limitvalue(ftarget, 0, 255);
  1065. ftarget <<= 8;
  1066. } else {
  1067. awe_poke(AWE_ENVVAL(voice),
  1068.  FX_WORD(fx, fx_lay, AWE_FX_ENV1_DELAY, parm->moddelay));
  1069. ftarget = parm->cutoff;
  1070. ftarget <<= 8;
  1071. pitch = voices[voice].apitch;
  1072. }
  1073. /* calcualte pitch target */
  1074. if (pitch != 0xffff) {
  1075. ptarget = 1 << (pitch >> 12);
  1076. if (pitch & 0x800) ptarget += (ptarget*0x102e)/0x2710;
  1077. if (pitch & 0x400) ptarget += (ptarget*0x764)/0x2710;
  1078. if (pitch & 0x200) ptarget += (ptarget*0x389)/0x2710;
  1079. ptarget += (ptarget>>1);
  1080. if (ptarget > 0xffff) ptarget = 0xffff;
  1081. } else ptarget = 0xffff;
  1082. if (parm->modatk >= 0x80)
  1083. awe_poke(AWE_ATKHLD(voice),
  1084.  FX_BYTE(fx, fx_lay, AWE_FX_ENV1_HOLD, parm->modhld) << 8 | 0x7f);
  1085. else
  1086. awe_poke(AWE_ATKHLD(voice),
  1087.  FX_COMB(fx, fx_lay, AWE_FX_ENV1_HOLD, AWE_FX_ENV1_ATTACK,
  1088.  vp->parm.modatkhld));
  1089. awe_poke(AWE_DCYSUS(voice),
  1090.  FX_COMB(fx, fx_lay, AWE_FX_ENV1_SUSTAIN, AWE_FX_ENV1_DECAY,
  1091.   vp->parm.moddcysus));
  1092. if (parm->volatk >= 0x80 && parm->voldelay >= 0x8000) {
  1093. awe_poke(AWE_ENVVOL(voice), 0xBFFF);
  1094. vtarget = voltarget[voices[voice].avol%0x10]>>(voices[voice].avol>>4);
  1095. } else {
  1096. awe_poke(AWE_ENVVOL(voice),
  1097.  FX_WORD(fx, fx_lay, AWE_FX_ENV2_DELAY, vp->parm.voldelay));
  1098. vtarget = 0;
  1099. }
  1100. if (parm->volatk >= 0x80)
  1101. awe_poke(AWE_ATKHLDV(voice),
  1102.  FX_BYTE(fx, fx_lay, AWE_FX_ENV2_HOLD, parm->volhld) << 8 | 0x7f);
  1103. else
  1104. awe_poke(AWE_ATKHLDV(voice),
  1105.  FX_COMB(fx, fx_lay, AWE_FX_ENV2_HOLD, AWE_FX_ENV2_ATTACK,
  1106.  vp->parm.volatkhld));
  1107. /* decay/sustain parameter for volume envelope must be set at last */
  1108. /* cutoff and volume */
  1109. awe_set_volume(voice, TRUE);
  1110. /* modulation envelope heights */
  1111. awe_poke(AWE_PEFE(voice),
  1112.  FX_COMB(fx, fx_lay, AWE_FX_ENV1_PITCH, AWE_FX_ENV1_CUTOFF,
  1113.  vp->parm.pefe));
  1114. /* lfo1/2 delay */
  1115. awe_poke(AWE_LFO1VAL(voice),
  1116.  FX_WORD(fx, fx_lay, AWE_FX_LFO1_DELAY, vp->parm.lfo1delay));
  1117. awe_poke(AWE_LFO2VAL(voice),
  1118.  FX_WORD(fx, fx_lay, AWE_FX_LFO2_DELAY, vp->parm.lfo2delay));
  1119. /* lfo1 pitch & cutoff shift */
  1120. awe_fx_fmmod(voice, TRUE);
  1121. /* lfo1 volume & freq */
  1122. awe_fx_tremfrq(voice, TRUE);
  1123. /* lfo2 pitch & freq */
  1124. awe_fx_fm2frq2(voice, TRUE);
  1125. /* pan & loop start */
  1126. awe_set_pan(voice, TRUE);
  1127. /* chorus & loop end (chorus 8bit, MSB) */
  1128. addr = vp->loopend - 1;
  1129. addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_END,
  1130.   AWE_FX_COARSE_LOOP_END, vp->mode);
  1131. temp = FX_BYTE(fx, fx_lay, AWE_FX_CHORUS, vp->parm.chorus);
  1132. temp = (temp <<24) | (unsigned int)addr;
  1133. awe_poke_dw(AWE_CSL(voice), temp);
  1134. DEBUG(4,printk("AWE32: [-- loopend=%x/%x]n", vp->loopend, addr));
  1135. /* Q & current address (Q 4bit value, MSB) */
  1136. addr = vp->start - 1;
  1137. addr += FX_OFFSET(fx, fx_lay, AWE_FX_SAMPLE_START,
  1138.   AWE_FX_COARSE_SAMPLE_START, vp->mode);
  1139. temp = FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ);
  1140. temp = (temp<<28) | (unsigned int)addr;
  1141. awe_poke_dw(AWE_CCCA(voice), temp);
  1142. DEBUG(4,printk("AWE32: [-- startaddr=%x/%x]n", vp->start, addr));
  1143. /* clear unknown registers */
  1144. awe_poke_dw(AWE_00A0(voice), 0);
  1145. awe_poke_dw(AWE_0080(voice), 0);
  1146. /* reset volume */
  1147. awe_poke_dw(AWE_VTFT(voice), (vtarget<<16)|ftarget);
  1148. awe_poke_dw(AWE_CVCF(voice), (vtarget<<16)|ftarget);
  1149. /* set reverb */
  1150. temp = FX_BYTE(fx, fx_lay, AWE_FX_REVERB, vp->parm.reverb);
  1151. temp = (temp << 8) | (ptarget << 16) | voices[voice].aaux;
  1152. awe_poke_dw(AWE_PTRX(voice), temp);
  1153. awe_poke_dw(AWE_CPF(voice), ptarget << 16);
  1154. /* turn on envelope */
  1155. awe_poke(AWE_DCYSUSV(voice),
  1156.  FX_COMB(fx, fx_lay, AWE_FX_ENV2_SUSTAIN, AWE_FX_ENV2_DECAY,
  1157.   vp->parm.voldcysus));
  1158. voices[voice].state = AWE_ST_ON;
  1159. /* clear voice position for the next note on this channel */
  1160. if (SINGLE_LAYER_MODE()) {
  1161. FX_UNSET(fx, AWE_FX_SAMPLE_START);
  1162. FX_UNSET(fx, AWE_FX_COARSE_SAMPLE_START);
  1163. }
  1164. }
  1165. /* turn off the voice */
  1166. static void
  1167. awe_note_off(int voice)
  1168. {
  1169. awe_voice_info *vp;
  1170. unsigned short tmp;
  1171. FX_Rec *fx = &voices[voice].cinfo->fx;
  1172. FX_Rec *fx_lay = NULL;
  1173. if (voices[voice].layer < MAX_LAYERS)
  1174. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1175. if ((vp = voices[voice].sample) == NULL) {
  1176. voices[voice].state = AWE_ST_OFF;
  1177. return;
  1178. }
  1179. tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV1_RELEASE,
  1180.        (unsigned char)vp->parm.modrelease);
  1181. awe_poke(AWE_DCYSUS(voice), tmp);
  1182. tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV2_RELEASE,
  1183.        (unsigned char)vp->parm.volrelease);
  1184. awe_poke(AWE_DCYSUSV(voice), tmp);
  1185. voices[voice].state = AWE_ST_RELEASED;
  1186. }
  1187. /* force to terminate the voice (no releasing echo) */
  1188. static void
  1189. awe_terminate(int voice)
  1190. {
  1191. awe_poke(AWE_DCYSUSV(voice), 0x807F);
  1192. awe_tweak_voice(voice);
  1193. voices[voice].state = AWE_ST_OFF;
  1194. }
  1195. /* turn off other voices with the same exclusive class (for drums) */
  1196. static void
  1197. awe_exclusive_off(int voice)
  1198. {
  1199. int i, exclass;
  1200. if (voices[voice].sample == NULL)
  1201. return;
  1202. if ((exclass = voices[voice].sample->exclusiveClass) == 0)
  1203. return; /* not exclusive */
  1204. /* turn off voices with the same class */
  1205. for (i = 0; i < awe_max_voices; i++) {
  1206. if (i != voice && IS_PLAYING(i) &&
  1207.     voices[i].sample && voices[i].ch == voices[voice].ch &&
  1208.     voices[i].sample->exclusiveClass == exclass) {
  1209. DEBUG(4,printk("AWE32: [exoff(%d)]n", i));
  1210. awe_terminate(i);
  1211. awe_voice_init(i, TRUE);
  1212. }
  1213. }
  1214. }
  1215. /*
  1216.  * change the parameters of an audible voice
  1217.  */
  1218. /* change pitch */
  1219. static void
  1220. awe_set_pitch(int voice, int forced)
  1221. {
  1222. if (IS_NO_EFFECT(voice) && !forced) return;
  1223. awe_poke(AWE_IP(voice), voices[voice].apitch);
  1224. DEBUG(3,printk("AWE32: [-- pitch=%x]n", voices[voice].apitch));
  1225. }
  1226. /* calculate & change pitch */
  1227. static void
  1228. awe_set_voice_pitch(int voice, int forced)
  1229. {
  1230. awe_calc_pitch(voice);
  1231. awe_set_pitch(voice, forced);
  1232. }
  1233. /* change volume & cutoff */
  1234. static void
  1235. awe_set_volume(int voice, int forced)
  1236. {
  1237. awe_voice_info *vp;
  1238. unsigned short tmp2;
  1239. FX_Rec *fx = &voices[voice].cinfo->fx;
  1240. FX_Rec *fx_lay = NULL;
  1241. if (voices[voice].layer < MAX_LAYERS)
  1242. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1243. if (!IS_PLAYING(voice) && !forced) return;
  1244. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1245. return;
  1246. tmp2 = FX_BYTE(fx, fx_lay, AWE_FX_CUTOFF,
  1247.        (unsigned char)voices[voice].acutoff);
  1248. tmp2 = (tmp2 << 8);
  1249. tmp2 |= FX_BYTE(fx, fx_lay, AWE_FX_ATTEN,
  1250. (unsigned char)voices[voice].avol);
  1251. awe_poke(AWE_IFATN(voice), tmp2);
  1252. }
  1253. /* calculate & change volume */
  1254. static void
  1255. awe_set_voice_vol(int voice, int forced)
  1256. {
  1257. if (IS_EMPTY(voice))
  1258. return;
  1259. awe_calc_volume(voice);
  1260. awe_set_volume(voice, forced);
  1261. }
  1262. /* change pan; this could make a click noise.. */
  1263. static void
  1264. awe_set_pan(int voice, int forced)
  1265. {
  1266. unsigned int temp;
  1267. int addr;
  1268. awe_voice_info *vp;
  1269. FX_Rec *fx = &voices[voice].cinfo->fx;
  1270. FX_Rec *fx_lay = NULL;
  1271. if (voices[voice].layer < MAX_LAYERS)
  1272. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1273. if (IS_NO_EFFECT(voice) && !forced) return;
  1274. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1275. return;
  1276. /* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
  1277. if (vp->fixpan > 0) /* 0-127 */
  1278. temp = 255 - (int)vp->fixpan * 2;
  1279. else {
  1280. int pos = 0;
  1281. if (vp->pan >= 0) /* 0-127 */
  1282. pos = (int)vp->pan * 2 - 128;
  1283. pos += voices[voice].cinfo->panning; /* -128 - 127 */
  1284. temp = 127 - pos;
  1285. }
  1286. limitvalue(temp, 0, 255);
  1287. if (ctrls[AWE_MD_PAN_EXCHANGE]) {
  1288. temp = 255 - temp;
  1289. }
  1290. if (forced || temp != voices[voice].apan) {
  1291. voices[voice].apan = temp;
  1292. if (temp == 0)
  1293. voices[voice].aaux = 0xff;
  1294. else
  1295. voices[voice].aaux = (-temp) & 0xff;
  1296. addr = vp->loopstart - 1;
  1297. addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_START,
  1298.   AWE_FX_COARSE_LOOP_START, vp->mode);
  1299. temp = (temp<<24) | (unsigned int)addr;
  1300. awe_poke_dw(AWE_PSST(voice), temp);
  1301. DEBUG(4,printk("AWE32: [-- loopstart=%x/%x]n", vp->loopstart, addr));
  1302. }
  1303. }
  1304. /* effects change during playing */
  1305. static void
  1306. awe_fx_fmmod(int voice, int forced)
  1307. {
  1308. awe_voice_info *vp;
  1309. FX_Rec *fx = &voices[voice].cinfo->fx;
  1310. FX_Rec *fx_lay = NULL;
  1311. if (voices[voice].layer < MAX_LAYERS)
  1312. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1313. if (IS_NO_EFFECT(voice) && !forced) return;
  1314. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1315. return;
  1316. awe_poke(AWE_FMMOD(voice),
  1317.  FX_COMB(fx, fx_lay, AWE_FX_LFO1_PITCH, AWE_FX_LFO1_CUTOFF,
  1318.  vp->parm.fmmod));
  1319. }
  1320. /* set tremolo (lfo1) volume & frequency */
  1321. static void
  1322. awe_fx_tremfrq(int voice, int forced)
  1323. {
  1324. awe_voice_info *vp;
  1325. FX_Rec *fx = &voices[voice].cinfo->fx;
  1326. FX_Rec *fx_lay = NULL;
  1327. if (voices[voice].layer < MAX_LAYERS)
  1328. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1329. if (IS_NO_EFFECT(voice) && !forced) return;
  1330. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1331. return;
  1332. awe_poke(AWE_TREMFRQ(voice),
  1333.  FX_COMB(fx, fx_lay, AWE_FX_LFO1_VOLUME, AWE_FX_LFO1_FREQ,
  1334.  vp->parm.tremfrq));
  1335. }
  1336. /* set lfo2 pitch & frequency */
  1337. static void
  1338. awe_fx_fm2frq2(int voice, int forced)
  1339. {
  1340. awe_voice_info *vp;
  1341. FX_Rec *fx = &voices[voice].cinfo->fx;
  1342. FX_Rec *fx_lay = NULL;
  1343. if (voices[voice].layer < MAX_LAYERS)
  1344. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1345. if (IS_NO_EFFECT(voice) && !forced) return;
  1346. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1347. return;
  1348. awe_poke(AWE_FM2FRQ2(voice),
  1349.  FX_COMB(fx, fx_lay, AWE_FX_LFO2_PITCH, AWE_FX_LFO2_FREQ,
  1350.  vp->parm.fm2frq2));
  1351. }
  1352. /* Q & current address (Q 4bit value, MSB) */
  1353. static void
  1354. awe_fx_filterQ(int voice, int forced)
  1355. {
  1356. unsigned int addr;
  1357. awe_voice_info *vp;
  1358. FX_Rec *fx = &voices[voice].cinfo->fx;
  1359. FX_Rec *fx_lay = NULL;
  1360. if (voices[voice].layer < MAX_LAYERS)
  1361. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1362. if (IS_NO_EFFECT(voice) && !forced) return;
  1363. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1364. return;
  1365. addr = awe_peek_dw(AWE_CCCA(voice)) & 0xffffff;
  1366. addr |= (FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ) << 28);
  1367. awe_poke_dw(AWE_CCCA(voice), addr);
  1368. }
  1369. /*
  1370.  * calculate pitch offset
  1371.  *
  1372.  * 0xE000 is no pitch offset at 44100Hz sample.
  1373.  * Every 4096 is one octave.
  1374.  */
  1375. static void
  1376. awe_calc_pitch(int voice)
  1377. {
  1378. voice_info *vp = &voices[voice];
  1379. awe_voice_info *ap;
  1380. awe_chan_info *cp = voices[voice].cinfo;
  1381. int offset;
  1382. /* search voice information */
  1383. if ((ap = vp->sample) == NULL)
  1384. return;
  1385. if (ap->index == 0) {
  1386. DEBUG(3,printk("AWE32: set sample (%d)n", ap->sample));
  1387. if (awe_set_sample((awe_voice_list*)ap) == 0)
  1388. return;
  1389. }
  1390. /* calculate offset */
  1391. if (ap->fixkey >= 0) {
  1392. DEBUG(3,printk("AWE32: p-> fixkey(%d) tune(%d)n", ap->fixkey, ap->tune));
  1393. offset = (ap->fixkey - ap->root) * 4096 / 12;
  1394. } else {
  1395. DEBUG(3,printk("AWE32: p(%d)-> root(%d) tune(%d)n", vp->note, ap->root, ap->tune));
  1396. offset = (vp->note - ap->root) * 4096 / 12;
  1397. DEBUG(4,printk("AWE32: p-> ofs=%dn", offset));
  1398. }
  1399. offset = (offset * ap->scaleTuning) / 100;
  1400. DEBUG(4,printk("AWE32: p-> scale* ofs=%dn", offset));
  1401. offset += ap->tune * 4096 / 1200;
  1402. DEBUG(4,printk("AWE32: p-> tune+ ofs=%dn", offset));
  1403. if (cp->bender != 0) {
  1404. DEBUG(3,printk("AWE32: p-> bend(%d) %dn", voice, cp->bender));
  1405. /* (819200: 1 semitone) ==> (4096: 12 semitones) */
  1406. offset += cp->bender * cp->bender_range / 2400;
  1407. }
  1408. /* add initial pitch correction */
  1409. if (FX_ON(&cp->fx_layer[vp->layer], AWE_FX_INIT_PITCH))
  1410. offset += cp->fx_layer[vp->layer].val[AWE_FX_INIT_PITCH];
  1411. else if (FX_ON(&cp->fx, AWE_FX_INIT_PITCH))
  1412. offset += cp->fx.val[AWE_FX_INIT_PITCH];
  1413. /* 0xe000: root pitch */
  1414. vp->apitch = 0xe000 + ap->rate_offset + offset;
  1415. DEBUG(4,printk("AWE32: p-> sum aofs=%x, rate_ofs=%dn", vp->apitch, ap->rate_offset));
  1416. if (vp->apitch > 0xffff)
  1417. vp->apitch = 0xffff;
  1418. if (vp->apitch < 0)
  1419. vp->apitch = 0;
  1420. }
  1421. #ifdef AWE_HAS_GUS_COMPATIBILITY
  1422. /* calculate MIDI key and semitone from the specified frequency */
  1423. static void
  1424. awe_calc_pitch_from_freq(int voice, int freq)
  1425. {
  1426. voice_info *vp = &voices[voice];
  1427. awe_voice_info *ap;
  1428. FX_Rec *fx = &voices[voice].cinfo->fx;
  1429. FX_Rec *fx_lay = NULL;
  1430. int offset;
  1431. int note;
  1432. if (voices[voice].layer < MAX_LAYERS)
  1433. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1434. /* search voice information */
  1435. if ((ap = vp->sample) == NULL)
  1436. return;
  1437. if (ap->index == 0) {
  1438. DEBUG(3,printk("AWE32: set sample (%d)n", ap->sample));
  1439. if (awe_set_sample((awe_voice_list*)ap) == 0)
  1440. return;
  1441. }
  1442. note = freq_to_note(freq);
  1443. offset = (note - ap->root * 100 + ap->tune) * 4096 / 1200;
  1444. offset = (offset * ap->scaleTuning) / 100;
  1445. if (fx_lay && FX_ON(fx_lay, AWE_FX_INIT_PITCH))
  1446. offset += fx_lay->val[AWE_FX_INIT_PITCH];
  1447. else if (FX_ON(fx, AWE_FX_INIT_PITCH))
  1448. offset += fx->val[AWE_FX_INIT_PITCH];
  1449. vp->apitch = 0xe000 + ap->rate_offset + offset;
  1450. if (vp->apitch > 0xffff)
  1451. vp->apitch = 0xffff;
  1452. if (vp->apitch < 0)
  1453. vp->apitch = 0;
  1454. }
  1455. #endif /* AWE_HAS_GUS_COMPATIBILITY */
  1456. /*
  1457.  * calculate volume attenuation
  1458.  *
  1459.  * Voice volume is controlled by volume attenuation parameter.
  1460.  * So volume becomes maximum when avol is 0 (no attenuation), and
  1461.  * minimum when 255 (-96dB or silence).
  1462.  */
  1463. static int vol_table[128] = {
  1464. 255,111,95,86,79,74,70,66,63,61,58,56,54,52,50,49,
  1465. 47,46,45,43,42,41,40,39,38,37,36,35,34,34,33,32,
  1466. 31,31,30,29,29,28,27,27,26,26,25,24,24,23,23,22,
  1467. 22,21,21,21,20,20,19,19,18,18,18,17,17,16,16,16,
  1468. 15,15,15,14,14,14,13,13,13,12,12,12,11,11,11,10,
  1469. 10,10,10,9,9,9,8,8,8,8,7,7,7,7,6,6,
  1470. 6,6,5,5,5,5,5,4,4,4,4,3,3,3,3,3,
  1471. 2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,0,
  1472. };
  1473. /* tables for volume->attenuation calculation */
  1474. static unsigned char voltab1[128] = {
  1475.    0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  1476.    0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
  1477.    0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
  1478.    0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
  1479.    0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
  1480.    0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
  1481.    0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
  1482.    0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
  1483.    0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
  1484.    0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
  1485.    0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
  1486.    0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
  1487.    0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1488. };
  1489. static unsigned char voltab2[128] = {
  1490.    0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
  1491.    0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
  1492.    0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
  1493.    0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
  1494.    0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
  1495.    0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
  1496.    0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
  1497.    0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
  1498.    0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
  1499.    0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  1500.    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
  1501.    0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
  1502.    0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  1503. };
  1504. static unsigned char expressiontab[128] = {
  1505.    0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
  1506.    0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
  1507.    0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
  1508.    0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
  1509.    0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
  1510.    0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
  1511.    0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
  1512.    0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
  1513.    0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
  1514.    0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
  1515.    0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
  1516.    0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
  1517.    0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1518. };
  1519. static void
  1520. awe_calc_volume(int voice)
  1521. {
  1522. voice_info *vp = &voices[voice];
  1523. awe_voice_info *ap;
  1524. awe_chan_info *cp = voices[voice].cinfo;
  1525. int vol;
  1526. /* search voice information */
  1527. if ((ap = vp->sample) == NULL)
  1528. return;
  1529. ap = vp->sample;
  1530. if (ap->index == 0) {
  1531. DEBUG(3,printk("AWE32: set sample (%d)n", ap->sample));
  1532. if (awe_set_sample((awe_voice_list*)ap) == 0)
  1533. return;
  1534. }
  1535. if (ctrls[AWE_MD_NEW_VOLUME_CALC]) {
  1536. int main_vol = cp->main_vol * ap->amplitude / 127;
  1537. limitvalue(vp->velocity, 0, 127);
  1538. limitvalue(main_vol, 0, 127);
  1539. limitvalue(cp->expression_vol, 0, 127);
  1540. vol = voltab1[main_vol] + voltab2[vp->velocity];
  1541. vol = (vol * 8) / 3;
  1542. vol += ap->attenuation;
  1543. if (cp->expression_vol < 127)
  1544. vol += ((0x100 - vol) * expressiontab[cp->expression_vol])/128;
  1545. vol += atten_offset;
  1546. if (atten_relative)
  1547. vol += ctrls[AWE_MD_ZERO_ATTEN];
  1548. limitvalue(vol, 0, 255);
  1549. vp->avol = vol;
  1550. } else {
  1551. /* 0 - 127 */
  1552. vol = (vp->velocity * cp->main_vol * cp->expression_vol) / (127*127);
  1553. vol = vol * ap->amplitude / 127;
  1554. if (vol < 0) vol = 0;
  1555. if (vol > 127) vol = 127;
  1556. /* calc to attenuation */
  1557. vol = vol_table[vol];
  1558. vol += (int)ap->attenuation;
  1559. vol += atten_offset;
  1560. if (atten_relative)
  1561. vol += ctrls[AWE_MD_ZERO_ATTEN];
  1562. if (vol > 255) vol = 255;
  1563. vp->avol = vol;
  1564. }
  1565. if (cp->bank !=  AWE_DRUM_BANK && ((awe_voice_parm_block*)(&ap->parm))->volatk < 0x7d) {
  1566. int atten;
  1567. if (vp->velocity < 70) atten = 70;
  1568. else atten = vp->velocity;
  1569. vp->acutoff = (atten * ap->parm.cutoff + 0xa0) >> 7;
  1570. } else {
  1571. vp->acutoff = ap->parm.cutoff;
  1572. }
  1573. DEBUG(3,printk("AWE32: [-- voice(%d) vol=%x]n", voice, vol));
  1574. }
  1575. /* change master volume */
  1576. static void
  1577. awe_change_master_volume(short val)
  1578. {
  1579. limitvalue(val, 0, 127);
  1580. atten_offset = vol_table[val];
  1581. atten_relative = TRUE;
  1582. awe_update_volume();
  1583. }
  1584. /* update volumes of all available channels */
  1585. static void awe_update_volume(void)
  1586. {
  1587. int i;
  1588. for (i = 0; i < awe_max_voices; i++)
  1589. awe_set_voice_vol(i, TRUE);
  1590. }
  1591. /* set sostenuto on */
  1592. static void awe_sostenuto_on(int voice, int forced)
  1593. {
  1594. if (IS_NO_EFFECT(voice) && !forced) return;
  1595. voices[voice].sostenuto = 127;
  1596. }
  1597. /* drop sustain */
  1598. static void awe_sustain_off(int voice, int forced)
  1599. {
  1600. if (voices[voice].state == AWE_ST_SUSTAINED) {
  1601. awe_note_off(voice);
  1602. awe_fx_init(voices[voice].ch);
  1603. awe_voice_init(voice, FALSE);
  1604. }
  1605. }
  1606. /* terminate and initialize voice */
  1607. static void awe_terminate_and_init(int voice, int forced)
  1608. {
  1609. awe_terminate(voice);
  1610. awe_fx_init(voices[voice].ch);
  1611. awe_voice_init(voice, TRUE);
  1612. }
  1613. /*
  1614.  * synth operation routines
  1615.  */
  1616. #define AWE_VOICE_KEY(v) (0x8000 | (v))
  1617. #define AWE_CHAN_KEY(c,n) (((c) << 8) | ((n) + 1))
  1618. #define KEY_CHAN_MATCH(key,c) (((key) >> 8) == (c))
  1619. /* initialize the voice */
  1620. static void
  1621. awe_voice_init(int voice, int init_all)
  1622. {
  1623. voice_info *vp = &voices[voice];
  1624. /* reset voice search key */
  1625. if (playing_mode == AWE_PLAY_DIRECT)
  1626. vp->key = AWE_VOICE_KEY(voice);
  1627. else
  1628. vp->key = 0;
  1629. /* clear voice mapping */
  1630. voice_alloc->map[voice] = 0;
  1631. /* touch the timing flag */
  1632. vp->time = current_alloc_time;
  1633. /* initialize other parameters if necessary */
  1634. if (init_all) {
  1635. vp->note = -1;
  1636. vp->velocity = 0;
  1637. vp->sostenuto = 0;
  1638. vp->sample = NULL;
  1639. vp->cinfo = &channels[voice];
  1640. vp->ch = voice;
  1641. vp->state = AWE_ST_OFF;
  1642. /* emu8000 parameters */
  1643. vp->apitch = 0;
  1644. vp->avol = 255;
  1645. vp->apan = -1;
  1646. }
  1647. }
  1648. /* clear effects */
  1649. static void awe_fx_init(int ch)
  1650. {
  1651. if (SINGLE_LAYER_MODE() && !ctrls[AWE_MD_KEEP_EFFECT]) {
  1652. memset(&channels[ch].fx, 0, sizeof(channels[ch].fx));
  1653. memset(&channels[ch].fx_layer, 0, sizeof(&channels[ch].fx_layer));
  1654. }
  1655. }
  1656. /* initialize channel info */
  1657. static void awe_channel_init(int ch, int init_all)
  1658. {
  1659. awe_chan_info *cp = &channels[ch];
  1660. cp->channel = ch;
  1661. if (init_all) {
  1662. cp->panning = 0; /* zero center */
  1663. cp->bender_range = 200; /* sense * 100 */
  1664. cp->main_vol = 127;
  1665. if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(ch)) {
  1666. cp->instr = ctrls[AWE_MD_DEF_DRUM];
  1667. cp->bank = AWE_DRUM_BANK;
  1668. } else {
  1669. cp->instr = ctrls[AWE_MD_DEF_PRESET];
  1670. cp->bank = ctrls[AWE_MD_DEF_BANK];
  1671. }
  1672. }
  1673. cp->bender = 0; /* zero tune skew */
  1674. cp->expression_vol = 127;
  1675. cp->chan_press = 0;
  1676. cp->sustained = 0;
  1677. if (! ctrls[AWE_MD_KEEP_EFFECT]) {
  1678. memset(&cp->fx, 0, sizeof(cp->fx));
  1679. memset(&cp->fx_layer, 0, sizeof(cp->fx_layer));
  1680. }
  1681. }
  1682. /* change the voice parameters; voice = channel */
  1683. static void awe_voice_change(int voice, fx_affect_func func)
  1684. {
  1685. int i; 
  1686. switch (playing_mode) {
  1687. case AWE_PLAY_DIRECT:
  1688. func(voice, FALSE);
  1689. break;
  1690. case AWE_PLAY_INDIRECT:
  1691. for (i = 0; i < awe_max_voices; i++)
  1692. if (voices[i].key == AWE_VOICE_KEY(voice))
  1693. func(i, FALSE);
  1694. break;
  1695. default:
  1696. for (i = 0; i < awe_max_voices; i++)
  1697. if (KEY_CHAN_MATCH(voices[i].key, voice))
  1698. func(i, FALSE);
  1699. break;
  1700. }
  1701. }
  1702. /*
  1703.  * device open / close
  1704.  */
  1705. /* open device:
  1706.  *   reset status of all voices, and clear sample position flag
  1707.  */
  1708. static int
  1709. awe_open(int dev, int mode)
  1710. {
  1711. if (awe_busy)
  1712. return -EBUSY;
  1713. awe_busy = TRUE;
  1714. /* set default mode */
  1715. awe_init_ctrl_parms(FALSE);
  1716. atten_relative = TRUE;
  1717. atten_offset = 0;
  1718. drum_flags = DEFAULT_DRUM_FLAGS;
  1719. playing_mode = AWE_PLAY_INDIRECT;
  1720. /* reset voices & channels */
  1721. awe_reset(dev);
  1722. patch_opened = 0;
  1723. return 0;
  1724. }
  1725. /* close device:
  1726.  *   reset all voices again (terminate sounds)
  1727.  */
  1728. static void
  1729. awe_close(int dev)
  1730. {
  1731. awe_reset(dev);
  1732. awe_busy = FALSE;
  1733. }
  1734. /* set miscellaneous mode parameters
  1735.  */
  1736. static void
  1737. awe_init_ctrl_parms(int init_all)
  1738. {
  1739. int i;
  1740. for (i = 0; i < AWE_MD_END; i++) {
  1741. if (init_all || ctrl_parms[i].init_each_time)
  1742. ctrls[i] = ctrl_parms[i].value;
  1743. }
  1744. }
  1745. /* sequencer I/O control:
  1746.  */
  1747. static int
  1748. awe_ioctl(int dev, unsigned int cmd, caddr_t arg)
  1749. {
  1750. switch (cmd) {
  1751. case SNDCTL_SYNTH_INFO:
  1752. if (playing_mode == AWE_PLAY_DIRECT)
  1753. awe_info.nr_voices = awe_max_voices;
  1754. else
  1755. awe_info.nr_voices = AWE_MAX_CHANNELS;
  1756. memcpy((char*)arg, &awe_info, sizeof(awe_info));
  1757. return 0;
  1758. break;
  1759. case SNDCTL_SEQ_RESETSAMPLES:
  1760. awe_reset(dev);
  1761. awe_reset_samples();
  1762. return 0;
  1763. break;
  1764. case SNDCTL_SEQ_PERCMODE:
  1765. /* what's this? */
  1766. return 0;
  1767. break;
  1768. case SNDCTL_SYNTH_MEMAVL:
  1769. return memsize - awe_free_mem_ptr() * 2;
  1770. default:
  1771. printk(KERN_WARNING "AWE32: unsupported ioctl %dn", cmd);
  1772. return -EINVAL;
  1773. }
  1774. }
  1775. static int voice_in_range(int voice)
  1776. {
  1777. if (playing_mode == AWE_PLAY_DIRECT) {
  1778. if (voice < 0 || voice >= awe_max_voices)
  1779. return FALSE;
  1780. } else {
  1781. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1782. return FALSE;
  1783. }
  1784. return TRUE;
  1785. }
  1786. static void release_voice(int voice, int do_sustain)
  1787. {
  1788. if (IS_NO_SOUND(voice))
  1789. return;
  1790. if (do_sustain && (voices[voice].cinfo->sustained == 127 ||
  1791.     voices[voice].sostenuto == 127))
  1792. voices[voice].state = AWE_ST_SUSTAINED;
  1793. else {
  1794. awe_note_off(voice);
  1795. awe_fx_init(voices[voice].ch);
  1796. awe_voice_init(voice, FALSE);
  1797. }
  1798. }
  1799. /* release all notes */
  1800. static void awe_note_off_all(int do_sustain)
  1801. {
  1802. int i;
  1803. for (i = 0; i < awe_max_voices; i++)
  1804. release_voice(i, do_sustain);
  1805. }
  1806. /* kill a voice:
  1807.  *   not terminate, just release the voice.
  1808.  */
  1809. static int
  1810. awe_kill_note(int dev, int voice, int note, int velocity)
  1811. {
  1812. int i, v2, key;
  1813. DEBUG(2,printk("AWE32: [off(%d) nt=%d vl=%d]n", voice, note, velocity));
  1814. if (! voice_in_range(voice))
  1815. return -EINVAL;
  1816. switch (playing_mode) {
  1817. case AWE_PLAY_DIRECT:
  1818. case AWE_PLAY_INDIRECT:
  1819. key = AWE_VOICE_KEY(voice);
  1820. break;
  1821. case AWE_PLAY_MULTI2:
  1822. v2 = voice_alloc->map[voice] >> 8;
  1823. voice_alloc->map[voice] = 0;
  1824. voice = v2;
  1825. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1826. return -EINVAL;
  1827. /* continue to below */
  1828. default:
  1829. key = AWE_CHAN_KEY(voice, note);
  1830. break;
  1831. }
  1832. for (i = 0; i < awe_max_voices; i++) {
  1833. if (voices[i].key == key)
  1834. release_voice(i, TRUE);
  1835. }
  1836. return 0;
  1837. }
  1838. static void start_or_volume_change(int voice, int velocity)
  1839. {
  1840. voices[voice].velocity = velocity;
  1841. awe_calc_volume(voice);
  1842. if (voices[voice].state == AWE_ST_STANDBY)
  1843. awe_note_on(voice);
  1844. else if (voices[voice].state == AWE_ST_ON)
  1845. awe_set_volume(voice, FALSE);
  1846. }
  1847. static void set_and_start_voice(int voice, int state)
  1848. {
  1849. /* calculate pitch & volume parameters */
  1850. voices[voice].state = state;
  1851. awe_calc_pitch(voice);
  1852. awe_calc_volume(voice);
  1853. if (state == AWE_ST_ON)
  1854. awe_note_on(voice);
  1855. }
  1856. /* start a voice:
  1857.  *   if note is 255, identical with aftertouch function.
  1858.  *   Otherwise, start a voice with specified not and volume.
  1859.  */
  1860. static int
  1861. awe_start_note(int dev, int voice, int note, int velocity)
  1862. {
  1863. int i, key, state, volonly;
  1864. DEBUG(2,printk("AWE32: [on(%d) nt=%d vl=%d]n", voice, note, velocity));
  1865. if (! voice_in_range(voice))
  1866. return -EINVAL;
  1867.     
  1868. if (velocity == 0)
  1869. state = AWE_ST_STANDBY; /* stand by for playing */
  1870. else
  1871. state = AWE_ST_ON; /* really play */
  1872. volonly = FALSE;
  1873. switch (playing_mode) {
  1874. case AWE_PLAY_DIRECT:
  1875. case AWE_PLAY_INDIRECT:
  1876. key = AWE_VOICE_KEY(voice);
  1877. if (note == 255)
  1878. volonly = TRUE;
  1879. break;
  1880. case AWE_PLAY_MULTI2:
  1881. voice = voice_alloc->map[voice] >> 8;
  1882. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1883. return -EINVAL;
  1884. /* continue to below */
  1885. default:
  1886. if (note >= 128) { /* key volume mode */
  1887. note -= 128;
  1888. volonly = TRUE;
  1889. }
  1890. key = AWE_CHAN_KEY(voice, note);
  1891. break;
  1892. }
  1893. /* dynamic volume change */
  1894. if (volonly) {
  1895. for (i = 0; i < awe_max_voices; i++) {
  1896. if (voices[i].key == key)
  1897. start_or_volume_change(i, velocity);
  1898. }
  1899. return 0;
  1900. }
  1901. /* if the same note still playing, stop it */
  1902. if (playing_mode != AWE_PLAY_DIRECT || ctrls[AWE_MD_EXCLUSIVE_SOUND]) {
  1903. for (i = 0; i < awe_max_voices; i++)
  1904. if (voices[i].key == key) {
  1905. if (voices[i].state == AWE_ST_ON) {
  1906. awe_note_off(i);
  1907. awe_voice_init(i, FALSE);
  1908. } else if (voices[i].state == AWE_ST_STANDBY)
  1909. awe_voice_init(i, TRUE);
  1910. }
  1911. }
  1912. /* allocate voices */
  1913. if (playing_mode == AWE_PLAY_DIRECT)
  1914. awe_alloc_one_voice(voice, note, velocity);
  1915. else
  1916. awe_alloc_multi_voices(voice, note, velocity, key);
  1917. /* turn off other voices exlusively (for drums) */
  1918. for (i = 0; i < awe_max_voices; i++)
  1919. if (voices[i].key == key)
  1920. awe_exclusive_off(i);
  1921. /* set up pitch and volume parameters */
  1922. for (i = 0; i < awe_max_voices; i++) {
  1923. if (voices[i].key == key && voices[i].state == AWE_ST_OFF)
  1924. set_and_start_voice(i, state);
  1925. }
  1926. return 0;
  1927. }
  1928. /* calculate hash key */
  1929. static int
  1930. awe_search_key(int bank, int preset, int note)
  1931. {
  1932. unsigned int key;
  1933. #if 1 /* new hash table */
  1934. if (bank == AWE_DRUM_BANK)
  1935. key = preset + note + 128;
  1936. else
  1937. key = bank + preset;
  1938. #else
  1939. key = preset;
  1940. #endif
  1941. key %= AWE_MAX_PRESETS;
  1942. return (int)key;
  1943. }
  1944. /* search instrument from hash table */
  1945. static awe_voice_list *
  1946. awe_search_instr(int bank, int preset, int note)
  1947. {
  1948. awe_voice_list *p;
  1949. int key, key2;
  1950. key = awe_search_key(bank, preset, note);
  1951. for (p = preset_table[key]; p; p = p->next_bank) {
  1952. if (p->instr == preset && p->bank == bank)
  1953. return p;
  1954. }
  1955. key2 = awe_search_key(bank, preset, 0); /* search default */
  1956. if (key == key2)
  1957. return NULL;
  1958. for (p = preset_table[key2]; p; p = p->next_bank) {
  1959. if (p->instr == preset && p->bank == bank)
  1960. return p;
  1961. }
  1962. return NULL;
  1963. }
  1964. /* assign the instrument to a voice */
  1965. static int
  1966. awe_set_instr_2(int dev, int voice, int instr_no)
  1967. {
  1968. if (playing_mode == AWE_PLAY_MULTI2) {
  1969. voice = voice_alloc->map[voice] >> 8;
  1970. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1971. return -EINVAL;
  1972. }
  1973. return awe_set_instr(dev, voice, instr_no);
  1974. }
  1975. /* assign the instrument to a channel; voice is the channel number */
  1976. static int
  1977. awe_set_instr(int dev, int voice, int instr_no)
  1978. {
  1979. awe_chan_info *cinfo;
  1980. if (! voice_in_range(voice))
  1981. return -EINVAL;
  1982. if (instr_no < 0 || instr_no >= AWE_MAX_PRESETS)
  1983. return -EINVAL;
  1984. cinfo = &channels[voice];
  1985. cinfo->instr = instr_no;
  1986. DEBUG(2,printk("AWE32: [program(%d) %d]n", voice, instr_no));
  1987. return 0;
  1988. }
  1989. /* reset all voices; terminate sounds and initialize parameters */
  1990. static void
  1991. awe_reset(int dev)
  1992. {
  1993. int i;
  1994. current_alloc_time = 0;
  1995. /* don't turn off voice 31 and 32.  they are used also for FM voices */
  1996. for (i = 0; i < awe_max_voices; i++) {
  1997. awe_terminate(i);
  1998. awe_voice_init(i, TRUE);
  1999. }
  2000. for (i = 0; i < AWE_MAX_CHANNELS; i++)
  2001. awe_channel_init(i, TRUE);
  2002. for (i = 0; i < 16; i++) {
  2003. awe_operations.chn_info[i].controllers[CTL_MAIN_VOLUME] = 127;
  2004. awe_operations.chn_info[i].controllers[CTL_EXPRESSION] = 127;
  2005. }
  2006. awe_init_fm();
  2007. awe_tweak();
  2008. }
  2009. /* hardware specific control:
  2010.  *   GUS specific and AWE32 specific controls are available.
  2011.  */
  2012. static void
  2013. awe_hw_control(int dev, unsigned char *event)
  2014. {
  2015. int cmd = event[2];
  2016. if (cmd & _AWE_MODE_FLAG)
  2017. awe_hw_awe_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
  2018. #ifdef AWE_HAS_GUS_COMPATIBILITY
  2019. else
  2020. awe_hw_gus_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
  2021. #endif
  2022. }
  2023. #ifdef AWE_HAS_GUS_COMPATIBILITY
  2024. /* GUS compatible controls */
  2025. static void
  2026. awe_hw_gus_control(int dev, int cmd, unsigned char *event)
  2027. {
  2028. int voice, i, key;
  2029. unsigned short p1;
  2030. short p2;
  2031. int plong;
  2032. if (MULTI_LAYER_MODE())
  2033. return;
  2034. if (cmd == _GUS_NUMVOICES)
  2035. return;
  2036. voice = event[3];
  2037. if (! voice_in_range(voice))
  2038. return;
  2039. p1 = *(unsigned short *) &event[4];
  2040. p2 = *(short *) &event[6];
  2041. plong = *(int*) &event[4];
  2042. switch (cmd) {
  2043. case _GUS_VOICESAMPLE:
  2044. awe_set_instr(dev, voice, p1);
  2045. return;
  2046. case _GUS_VOICEBALA:
  2047. /* 0 to 15 --> -128 to 127 */
  2048. awe_panning(dev, voice, ((int)p1 << 4) - 128);
  2049. return;
  2050. case _GUS_VOICEVOL:
  2051. case _GUS_VOICEVOL2:
  2052. /* not supported yet */
  2053. return;
  2054. case _GUS_RAMPRANGE:
  2055. case _GUS_RAMPRATE:
  2056. case _GUS_RAMPMODE:
  2057. case _GUS_RAMPON:
  2058. case _GUS_RAMPOFF:
  2059. /* volume ramping not supported */
  2060. return;
  2061. case _GUS_VOLUME_SCALE:
  2062. return;
  2063. case _GUS_VOICE_POS:
  2064. FX_SET(&channels[voice].fx, AWE_FX_SAMPLE_START,
  2065.        (short)(plong & 0x7fff));
  2066. FX_SET(&channels[voice].fx, AWE_FX_COARSE_SAMPLE_START,
  2067.        (plong >> 15) & 0xffff);
  2068. return;
  2069. }
  2070. key = AWE_VOICE_KEY(voice);
  2071. for (i = 0; i < awe_max_voices; i++) {
  2072. if (voices[i].key == key) {
  2073. switch (cmd) {
  2074. case _GUS_VOICEON:
  2075. awe_note_on(i);
  2076. break;
  2077. case _GUS_VOICEOFF:
  2078. awe_terminate(i);
  2079. awe_fx_init(voices[i].ch);
  2080. awe_voice_init(i, TRUE);
  2081. break;
  2082. case _GUS_VOICEFADE:
  2083. awe_note_off(i);
  2084. awe_fx_init(voices[i].ch);
  2085. awe_voice_init(i, FALSE);
  2086. break;
  2087. case _GUS_VOICEFREQ:
  2088. awe_calc_pitch_from_freq(i, plong);
  2089. break;
  2090. }
  2091. }
  2092. }
  2093. }
  2094. #endif /* gus_compat */
  2095. /* AWE32 specific controls */
  2096. static void
  2097. awe_hw_awe_control(int dev, int cmd, unsigned char *event)
  2098. {
  2099. int voice;
  2100. unsigned short p1;
  2101. short p2;
  2102. int i;
  2103. voice = event[3];
  2104. if (! voice_in_range(voice))
  2105. return;
  2106. if (playing_mode == AWE_PLAY_MULTI2) {
  2107. voice = voice_alloc->map[voice] >> 8;
  2108. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  2109. return;
  2110. }
  2111. p1 = *(unsigned short *) &event[4];
  2112. p2 = *(short *) &event[6];
  2113. switch (cmd) {
  2114. case _AWE_DEBUG_MODE:
  2115. ctrls[AWE_MD_DEBUG_MODE] = p1;
  2116. printk(KERN_DEBUG "AWE32: debug mode = %dn", ctrls[AWE_MD_DEBUG_MODE]);
  2117. break;
  2118. case _AWE_REVERB_MODE:
  2119. ctrls[AWE_MD_REVERB_MODE] = p1;
  2120. awe_update_reverb_mode();
  2121. break;
  2122. case _AWE_CHORUS_MODE:
  2123. ctrls[AWE_MD_CHORUS_MODE] = p1;
  2124. awe_update_chorus_mode();
  2125. break;
  2126.       
  2127. case _AWE_REMOVE_LAST_SAMPLES:
  2128. DEBUG(0,printk("AWE32: remove last samplesn"));
  2129. awe_reset(0);
  2130. if (locked_sf_id > 0)
  2131. awe_remove_samples(locked_sf_id);
  2132. break;
  2133. case _AWE_INITIALIZE_CHIP:
  2134. awe_initialize();
  2135. break;
  2136. case _AWE_SEND_EFFECT:
  2137. i = -1;
  2138. if (p1 >= 0x100) {
  2139. i = (p1 >> 8);
  2140. if (i < 0 || i >= MAX_LAYERS)
  2141. break;
  2142. }
  2143. awe_send_effect(voice, i, p1, p2);
  2144. break;
  2145. case _AWE_RESET_CHANNEL:
  2146. awe_channel_init(voice, !p1);
  2147. break;
  2148. case _AWE_TERMINATE_ALL:
  2149. awe_reset(0);
  2150. break;
  2151. case _AWE_TERMINATE_CHANNEL:
  2152. awe_voice_change(voice, awe_terminate_and_init);
  2153. break;
  2154. case _AWE_RELEASE_ALL:
  2155. awe_note_off_all(FALSE);
  2156. break;
  2157. case _AWE_NOTEOFF_ALL:
  2158. awe_note_off_all(TRUE);
  2159. break;
  2160. case _AWE_INITIAL_VOLUME:
  2161. DEBUG(0,printk("AWE32: init attenuation %dn", p1));
  2162. atten_relative = (char)p2;
  2163. atten_offset = (short)p1;
  2164. awe_update_volume();
  2165. break;
  2166. case _AWE_CHN_PRESSURE:
  2167. channels[voice].chan_press = p1;
  2168. awe_modwheel_change(voice, p1);
  2169. break;
  2170. case _AWE_CHANNEL_MODE:
  2171. DEBUG(0,printk("AWE32: channel mode = %dn", p1));
  2172. playing_mode = p1;
  2173. awe_reset(0);
  2174. break;
  2175. case _AWE_DRUM_CHANNELS:
  2176. DEBUG(0,printk("AWE32: drum flags = %xn", p1));
  2177. drum_flags = *(unsigned int*)&event[4];
  2178. break;
  2179. case _AWE_MISC_MODE:
  2180. DEBUG(0,printk("AWE32: ctrl parms = %d %dn", p1, p2));
  2181. if (p1 > AWE_MD_VERSION && p1 < AWE_MD_END) {
  2182. ctrls[p1] = p2;
  2183. if (ctrl_parms[p1].update)
  2184. ctrl_parms[p1].update();
  2185. }
  2186. break;
  2187. case _AWE_EQUALIZER:
  2188. ctrls[AWE_MD_BASS_LEVEL] = p1;
  2189. ctrls[AWE_MD_TREBLE_LEVEL] = p2;
  2190. awe_update_equalizer();
  2191. break;
  2192. default:
  2193. DEBUG(0,printk("AWE32: hw control cmd=%d voice=%dn", cmd, voice));
  2194. break;
  2195. }
  2196. }
  2197. /* change effects */
  2198. static void
  2199. awe_send_effect(int voice, int layer, int type, int val)
  2200. {
  2201. awe_chan_info *cinfo;
  2202. FX_Rec *fx;
  2203. int mode;
  2204. cinfo = &channels[voice];
  2205. if (layer >= 0 && layer < MAX_LAYERS)
  2206. fx = &cinfo->fx_layer[layer];
  2207. else
  2208. fx = &cinfo->fx;
  2209. if (type & 0x40)
  2210. mode = FX_FLAG_OFF;
  2211. else if (type & 0x80)
  2212. mode = FX_FLAG_ADD;
  2213. else
  2214. mode = FX_FLAG_SET;
  2215. type &= 0x3f;
  2216. if (type >= 0 && type < AWE_FX_END) {
  2217. DEBUG(2,printk("AWE32: effects (%d) %d %dn", voice, type, val));
  2218. if (mode == FX_FLAG_SET)
  2219. FX_SET(fx, type, val);
  2220. else if (mode == FX_FLAG_ADD)
  2221. FX_ADD(fx, type, val);
  2222. else
  2223. FX_UNSET(fx, type);
  2224. if (mode != FX_FLAG_OFF && parm_defs[type].realtime) {
  2225. DEBUG(2,printk("AWE32: fx_realtime (%d)n", voice));
  2226. awe_voice_change(voice, parm_defs[type].realtime);
  2227. }
  2228. }
  2229. }
  2230. /* change modulation wheel; voice is already mapped on multi2 mode */
  2231. static void
  2232. awe_modwheel_change(int voice, int value)
  2233. {
  2234. int i;
  2235. awe_chan_info *cinfo;
  2236. cinfo = &channels[voice];
  2237. i = value * ctrls[AWE_MD_MOD_SENSE] / 1200;
  2238. FX_ADD(&cinfo->fx, AWE_FX_LFO1_PITCH, i);
  2239. awe_voice_change(voice, awe_fx_fmmod);
  2240. FX_ADD(&cinfo->fx, AWE_FX_LFO2_PITCH, i);
  2241. awe_voice_change(voice, awe_fx_fm2frq2);
  2242. }
  2243. /* voice pressure change */
  2244. static void
  2245. awe_aftertouch(int dev, int voice, int pressure)
  2246. {
  2247. int note;
  2248. DEBUG(2,printk("AWE32: [after(%d) %d]n", voice, pressure));
  2249. if (! voice_in_range(voice))
  2250. return;
  2251. switch (playing_mode) {
  2252. case AWE_PLAY_DIRECT:
  2253. case AWE_PLAY_INDIRECT:
  2254. awe_start_note(dev, voice, 255, pressure);
  2255. break;
  2256. case AWE_PLAY_MULTI2:
  2257. note = (voice_alloc->map[voice] & 0xff) - 1;
  2258. awe_key_pressure(dev, voice, note + 0x80, pressure);
  2259. break;
  2260. }
  2261. }
  2262. /* voice control change */
  2263. static void
  2264. awe_controller(int dev, int voice, int ctrl_num, int value)
  2265. {
  2266. awe_chan_info *cinfo;
  2267. if (! voice_in_range(voice))
  2268. return;
  2269. if (playing_mode == AWE_PLAY_MULTI2) {
  2270. voice = voice_alloc->map[voice] >> 8;
  2271. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  2272. return;
  2273. }
  2274. cinfo = &channels[voice];
  2275. switch (ctrl_num) {
  2276. case CTL_BANK_SELECT: /* MIDI control #0 */
  2277. DEBUG(2,printk("AWE32: [bank(%d) %d]n", voice, value));
  2278. if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(voice) &&
  2279.     !ctrls[AWE_MD_TOGGLE_DRUM_BANK])
  2280. break;
  2281. if (value < 0 || value > 255)
  2282. break;
  2283. cinfo->bank = value;
  2284. if (cinfo->bank == AWE_DRUM_BANK)
  2285. DRUM_CHANNEL_ON(cinfo->channel);
  2286. else
  2287. DRUM_CHANNEL_OFF(cinfo->channel);
  2288. awe_set_instr(dev, voice, cinfo->instr);
  2289. break;
  2290. case CTL_MODWHEEL: /* MIDI control #1 */
  2291. DEBUG(2,printk("AWE32: [modwheel(%d) %d]n", voice, value));
  2292. awe_modwheel_change(voice, value);
  2293. break;
  2294. case CTRL_PITCH_BENDER: /* SEQ1 V2 contorl */
  2295. DEBUG(2,printk("AWE32: [bend(%d) %d]n", voice, value));
  2296. /* zero centered */
  2297. cinfo->bender = value;
  2298. awe_voice_change(voice, awe_set_voice_pitch);
  2299. break;
  2300. case CTRL_PITCH_BENDER_RANGE: /* SEQ1 V2 control */
  2301. DEBUG(2,printk("AWE32: [range(%d) %d]n", voice, value));
  2302. /* value = sense x 100 */
  2303. cinfo->bender_range = value;
  2304. /* no audible pitch change yet.. */
  2305. break;
  2306. case CTL_EXPRESSION: /* MIDI control #11 */
  2307. if (SINGLE_LAYER_MODE())
  2308. value /= 128;
  2309. case CTRL_EXPRESSION: /* SEQ1 V2 control */
  2310. DEBUG(2,printk("AWE32: [expr(%d) %d]n", voice, value));
  2311. /* 0 - 127 */
  2312. cinfo->expression_vol = value;
  2313. awe_voice_change(voice, awe_set_voice_vol);
  2314. break;
  2315. case CTL_PAN: /* MIDI control #10 */
  2316. DEBUG(2,printk("AWE32: [pan(%d) %d]n", voice, value));
  2317. /* (0-127) -> signed 8bit */
  2318. cinfo->panning = value * 2 - 128;
  2319. if (ctrls[AWE_MD_REALTIME_PAN])
  2320. awe_voice_change(voice, awe_set_pan);
  2321. break;
  2322. case CTL_MAIN_VOLUME: /* MIDI control #7 */
  2323. if (SINGLE_LAYER_MODE())
  2324. value = (value * 100) / 16383;
  2325. case CTRL_MAIN_VOLUME: /* SEQ1 V2 control */
  2326. DEBUG(2,printk("AWE32: [mainvol(%d) %d]n", voice, value));
  2327. /* 0 - 127 */
  2328. cinfo->main_vol = value;
  2329. awe_voice_change(voice, awe_set_voice_vol);
  2330. break;
  2331. case CTL_EXT_EFF_DEPTH: /* reverb effects: 0-127 */
  2332. DEBUG(2,printk("AWE32: [reverb(%d) %d]n", voice, value));
  2333. FX_SET(&cinfo->fx, AWE_FX_REVERB, value * 2);
  2334. break;
  2335. case CTL_CHORUS_DEPTH: /* chorus effects: 0-127 */
  2336. DEBUG(2,printk("AWE32: [chorus(%d) %d]n", voice, value));
  2337. FX_SET(&cinfo->fx, AWE_FX_CHORUS, value * 2);
  2338. break;
  2339. case 120:  /* all sounds off */
  2340. awe_note_off_all(FALSE);
  2341. break;
  2342. case 123:  /* all notes off */
  2343. awe_note_off_all(TRUE);
  2344. break;
  2345. case CTL_SUSTAIN: /* MIDI control #64 */
  2346. cinfo->sustained = value;
  2347. if (value != 127)
  2348. awe_voice_change(voice, awe_sustain_off);
  2349. break;
  2350. case CTL_SOSTENUTO: /* MIDI control #66 */
  2351. if (value == 127)
  2352. awe_voice_change(voice, awe_sostenuto_on);
  2353. else
  2354. awe_voice_change(voice, awe_sustain_off);
  2355. break;
  2356. default:
  2357. DEBUG(0,printk("AWE32: [control(%d) ctrl=%d val=%d]n",
  2358.    voice, ctrl_num, value));
  2359. break;
  2360. }
  2361. }
  2362. /* voice pan change (value = -128 - 127) */
  2363. static void
  2364. awe_panning(int dev, int voice, int value)
  2365. {
  2366. awe_chan_info *cinfo;
  2367. if (! voice_in_range(voice))
  2368. return;
  2369. if (playing_mode == AWE_PLAY_MULTI2) {
  2370. voice = voice_alloc->map[voice] >> 8;
  2371. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  2372. return;
  2373. }
  2374. cinfo = &channels[voice];
  2375. cinfo->panning = value;
  2376. DEBUG(2,printk("AWE32: [pan(%d) %d]n", voice, cinfo->panning));
  2377. if (ctrls[AWE_MD_REALTIME_PAN])
  2378. awe_voice_change(voice, awe_set_pan);
  2379. }
  2380. /* volume mode change */
  2381. static void
  2382. awe_volume_method(int dev, int mode)
  2383. {
  2384. /* not impremented */
  2385. DEBUG(0,printk("AWE32: [volmethod mode=%d]n", mode));
  2386. }
  2387. /* pitch wheel change: 0-16384 */
  2388. static void
  2389. awe_bender(int dev, int voice, int value)
  2390. {
  2391. awe_chan_info *cinfo;
  2392. if (! voice_in_range(voice))
  2393. return;
  2394. if (playing_mode == AWE_PLAY_MULTI2) {
  2395. voice = voice_alloc->map[voice] >> 8;
  2396. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  2397. return;
  2398. }
  2399. /* convert to zero centered value */
  2400. cinfo = &channels[voice];
  2401. cinfo->bender = value - 8192;
  2402. DEBUG(2,printk("AWE32: [bend(%d) %d]n", voice, cinfo->bender));
  2403. awe_voice_change(voice, awe_set_voice_pitch);
  2404. }
  2405. /*
  2406.  * load a sound patch:
  2407.  *   three types of patches are accepted: AWE, GUS, and SYSEX.
  2408.  */
  2409. static int
  2410. awe_load_patch(int dev, int format, const char *addr,
  2411.        int offs, int count, int pmgr_flag)
  2412. {
  2413. awe_patch_info patch;
  2414. int rc = 0;
  2415. #ifdef AWE_HAS_GUS_COMPATIBILITY
  2416. if (format == GUS_PATCH) {
  2417. return awe_load_guspatch(addr, offs, count, pmgr_flag);
  2418. } else
  2419. #endif
  2420. if (format == SYSEX_PATCH) {
  2421. /* no system exclusive message supported yet */
  2422. return 0;
  2423. } else if (format != AWE_PATCH) {
  2424. printk(KERN_WARNING "AWE32 Error: Invalid patch format (key) 0x%xn", format);
  2425. return -EINVAL;
  2426. }
  2427. if (count < AWE_PATCH_INFO_SIZE) {
  2428. printk(KERN_WARNING "AWE32 Error: Patch header too shortn");
  2429. return -EINVAL;
  2430. }
  2431. if (copy_from_user(((char*)&patch) + offs, addr + offs, 
  2432.    AWE_PATCH_INFO_SIZE - offs))
  2433. return -EFAULT;
  2434. count -= AWE_PATCH_INFO_SIZE;
  2435. if (count < patch.len) {
  2436. printk(KERN_WARNING "AWE32: sample: Patch record too short (%d<%d)n",
  2437.        count, patch.len);
  2438. return -EINVAL;
  2439. }
  2440. switch (patch.type) {
  2441. case AWE_LOAD_INFO:
  2442. rc = awe_load_info(&patch, addr, count);
  2443. break;
  2444. case AWE_LOAD_DATA:
  2445. rc = awe_load_data(&patch, addr, count);
  2446. break;
  2447. case AWE_OPEN_PATCH:
  2448. rc = awe_open_patch(&patch, addr, count);
  2449. break;
  2450. case AWE_CLOSE_PATCH:
  2451. rc = awe_close_patch(&patch, addr, count);
  2452. break;
  2453. case AWE_UNLOAD_PATCH:
  2454. rc = awe_unload_patch(&patch, addr, count);
  2455. break;
  2456. case AWE_REPLACE_DATA:
  2457. rc = awe_replace_data(&patch, addr, count);
  2458. break;
  2459. case AWE_MAP_PRESET:
  2460. rc = awe_load_map(&patch, addr, count);
  2461. break;
  2462. /* case AWE_PROBE_INFO:
  2463. rc = awe_probe_info(&patch, addr, count);
  2464. break;*/
  2465. case AWE_PROBE_DATA:
  2466. rc = awe_probe_data(&patch, addr, count);
  2467. break;
  2468. case AWE_REMOVE_INFO:
  2469. rc = awe_remove_info(&patch, addr, count);
  2470. break;
  2471. case AWE_LOAD_CHORUS_FX:
  2472. rc = awe_load_chorus_fx(&patch, addr, count);
  2473. break;
  2474. case AWE_LOAD_REVERB_FX:
  2475. rc = awe_load_reverb_fx(&patch, addr, count);
  2476. break;
  2477. default:
  2478. printk(KERN_WARNING "AWE32 Error: unknown patch format type %dn",
  2479.        patch.type);
  2480. rc = -EINVAL;
  2481. }
  2482. return rc;
  2483. }
  2484. /* create an sf list record */
  2485. static int
  2486. awe_create_sf(int type, char *name)
  2487. {
  2488. sf_list *rec;
  2489. /* terminate sounds */
  2490. awe_reset(0);
  2491. rec = (sf_list *)kmalloc(sizeof(*rec), GFP_KERNEL);
  2492. if (rec == NULL)
  2493. return 1; /* no memory */
  2494. rec->sf_id = current_sf_id + 1;
  2495. rec->type = type;
  2496. if (/*current_sf_id == 0 ||*/ (type & AWE_PAT_LOCKED) != 0)
  2497. locked_sf_id = current_sf_id + 1;
  2498. rec->num_info = awe_free_info();
  2499. rec->num_sample = awe_free_sample();
  2500. rec->mem_ptr = awe_free_mem_ptr();
  2501. rec->infos = rec->last_infos = NULL;
  2502. rec->samples = rec->last_samples = NULL;
  2503. /* add to linked-list */
  2504. rec->next = NULL;
  2505. rec->prev = sftail;
  2506. if (sftail)
  2507. sftail->next = rec;
  2508. else
  2509. sfhead = rec;
  2510. sftail = rec;
  2511. current_sf_id++;
  2512. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2513. rec->shared = NULL;
  2514. if (name)
  2515. memcpy(rec->name, name, AWE_PATCH_NAME_LEN);
  2516. else
  2517. strcpy(rec->name, "*TEMPORARY*");
  2518. if (current_sf_id > 1 && name && (type & AWE_PAT_SHARED) != 0) {
  2519. /* is the current font really a shared font? */
  2520. if (is_shared_sf(rec->name)) {
  2521. /* check if the shared font is already installed */
  2522. sf_list *p;
  2523. for (p = rec->prev; p; p = p->prev) {
  2524. if (is_identical_name(rec->name, p)) {
  2525. rec->shared = p;
  2526. break;
  2527. }
  2528. }
  2529. }
  2530. }
  2531. #endif /* allow sharing */
  2532. return 0;
  2533. }
  2534. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2535. /* check if the given name is a valid shared name */
  2536. #define ASC_TO_KEY(c) ((c) - 'A' + 1)
  2537. static int is_shared_sf(unsigned char *name)
  2538. {
  2539. static unsigned char id_head[4] = {
  2540. ASC_TO_KEY('A'), ASC_TO_KEY('W'), ASC_TO_KEY('E'),
  2541. AWE_MAJOR_VERSION,
  2542. };
  2543. if (memcmp(name, id_head, 4) == 0)
  2544. return TRUE;
  2545. return FALSE;
  2546. }
  2547. /* check if the given name matches to the existing list */
  2548. static int is_identical_name(unsigned char *name, sf_list *p) 
  2549. {
  2550. char *id = p->name;
  2551. if (is_shared_sf(id) && memcmp(id, name, AWE_PATCH_NAME_LEN) == 0)
  2552. return TRUE;
  2553. return FALSE;
  2554. }
  2555. /* check if the given voice info exists */
  2556. static int info_duplicated(sf_list *sf, awe_voice_list *rec)
  2557. {
  2558. /* search for all sharing lists */
  2559. for (; sf; sf = sf->shared) {
  2560. awe_voice_list *p;
  2561. for (p = sf->infos; p; p = p->next) {
  2562. if (p->type == V_ST_NORMAL &&
  2563.     p->bank == rec->bank &&
  2564.     p->instr == rec->instr &&
  2565.     p->v.low == rec->v.low &&
  2566.     p->v.high == rec->v.high &&
  2567.     p->v.sample == rec->v.sample)
  2568. return TRUE;
  2569. }
  2570. }
  2571. return FALSE;
  2572. }
  2573. #endif /* AWE_ALLOW_SAMPLE_SHARING */
  2574. /* free sf_list record */
  2575. /* linked-list in this function is not cared */
  2576. static void
  2577. awe_free_sf(sf_list *sf)
  2578. {
  2579. if (sf->infos) {
  2580. awe_voice_list *p, *next;
  2581. for (p = sf->infos; p; p = next) {
  2582. next = p->next;
  2583. kfree(p);
  2584. }
  2585. }
  2586. if (sf->samples) {
  2587. awe_sample_list *p, *next;
  2588. for (p = sf->samples; p; p = next) {
  2589. next = p->next;
  2590. kfree(p);
  2591. }
  2592. }
  2593. kfree(sf);
  2594. }
  2595. /* open patch; create sf list and set opened flag */
  2596. static int
  2597. awe_open_patch(awe_patch_info *patch, const char *addr, int count)
  2598. {
  2599. awe_open_parm parm;
  2600. int shared;
  2601. if (copy_from_user(&parm, addr + AWE_PATCH_INFO_SIZE, sizeof(parm)))
  2602. return -EFAULT;
  2603. shared = FALSE;
  2604. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2605. if (sftail && (parm.type & AWE_PAT_SHARED) != 0) {
  2606. /* is the previous font the same font? */
  2607. if (is_identical_name(parm.name, sftail)) {
  2608. /* then append to the previous */
  2609. shared = TRUE;
  2610. awe_reset(0);
  2611. if (parm.type & AWE_PAT_LOCKED)
  2612. locked_sf_id = current_sf_id;
  2613. }
  2614. }
  2615. #endif /* allow sharing */
  2616. if (! shared) {
  2617. if (awe_create_sf(parm.type, parm.name)) {
  2618. printk(KERN_ERR "AWE32: can't open: failed to alloc new listn");
  2619. return -ENOMEM;
  2620. }
  2621. }
  2622. patch_opened = TRUE;
  2623. return current_sf_id;
  2624. }
  2625. /* check if the patch is already opened */
  2626. static sf_list *
  2627. check_patch_opened(int type, char *name)
  2628. {
  2629. if (! patch_opened) {
  2630. if (awe_create_sf(type, name)) {