blob: b3ea719d33db0b737a6fb64c5d7d311b14014f99 [file] [log] [blame]
/*
* sound/awe_wave.c
*
* The low level driver for the AWE32/SB32/AWE64 wave table synth.
* version 0.4.4; Jan. 4, 2000
*
* Copyright (C) 1996-2000 Takashi Iwai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Changelog:
* Aug 18, 2003, Adam Belay <ambx1@neo.rr.com>
* - detection code rewrite
*/
#include <linux/awe_voice.h>
#include <linux/config.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/pnp.h>
#include "sound_config.h"
#include "awe_wave.h"
#include "awe_hw.h"
#ifdef AWE_HAS_GUS_COMPATIBILITY
#include "tuning.h"
#include <linux/ultrasound.h>
#endif
/*
* debug message
*/
#ifdef AWE_DEBUG_ON
#define DEBUG(LVL,XXX) {if (ctrls[AWE_MD_DEBUG_MODE] > LVL) { XXX; }}
#define ERRMSG(XXX) {if (ctrls[AWE_MD_DEBUG_MODE]) { XXX; }}
#define FATALERR(XXX) XXX
#else
#define DEBUG(LVL,XXX) /**/
#define ERRMSG(XXX) XXX
#define FATALERR(XXX) XXX
#endif
/*
* bank and voice record
*/
typedef struct _sf_list sf_list;
typedef struct _awe_voice_list awe_voice_list;
typedef struct _awe_sample_list awe_sample_list;
/* soundfont record */
struct _sf_list {
unsigned short sf_id; /* id number */
unsigned short type; /* lock & shared flags */
int num_info; /* current info table index */
int num_sample; /* current sample table index */
int mem_ptr; /* current word byte pointer */
awe_voice_list *infos, *last_infos; /* instruments */
awe_sample_list *samples, *last_samples; /* samples */
#ifdef AWE_ALLOW_SAMPLE_SHARING
sf_list *shared; /* shared list */
unsigned char name[AWE_PATCH_NAME_LEN]; /* sharing id */
#endif
sf_list *next, *prev;
};
/* instrument list */
struct _awe_voice_list {
awe_voice_info v; /* instrument information */
sf_list *holder; /* parent sf_list of this record */
unsigned char bank, instr; /* preset number information */
char type, disabled; /* type=normal/mapped, disabled=boolean */
awe_voice_list *next; /* linked list with same sf_id */
awe_voice_list *next_instr; /* instrument list */
awe_voice_list *next_bank; /* hash table list */
};
/* voice list type */
#define V_ST_NORMAL 0
#define V_ST_MAPPED 1
/* sample list */
struct _awe_sample_list {
awe_sample_info v; /* sample information */
sf_list *holder; /* parent sf_list of this record */
awe_sample_list *next; /* linked list with same sf_id */
};
/* sample and information table */
static int current_sf_id; /* current number of fonts */
static int locked_sf_id; /* locked position */
static sf_list *sfhead, *sftail; /* linked-lists */
#define awe_free_mem_ptr() (sftail ? sftail->mem_ptr : 0)
#define awe_free_info() (sftail ? sftail->num_info : 0)
#define awe_free_sample() (sftail ? sftail->num_sample : 0)
#define AWE_MAX_PRESETS 256
#define AWE_DEFAULT_PRESET 0
#define AWE_DEFAULT_BANK 0
#define AWE_DEFAULT_DRUM 0
#define AWE_DRUM_BANK 128
#define MAX_LAYERS AWE_MAX_VOICES
/* preset table index */
static awe_voice_list *preset_table[AWE_MAX_PRESETS];
/*
* voice table
*/
/* effects table */
typedef struct FX_Rec { /* channel effects */
unsigned char flags[AWE_FX_END];
short val[AWE_FX_END];
} FX_Rec;
/* channel parameters */
typedef struct _awe_chan_info {
int channel; /* channel number */
int bank; /* current tone bank */
int instr; /* current program */
int bender; /* midi pitchbend (-8192 - 8192) */
int bender_range; /* midi bender range (x100) */
int panning; /* panning (0-127) */
int main_vol; /* channel volume (0-127) */
int expression_vol; /* midi expression (0-127) */
int chan_press; /* channel pressure */
int sustained; /* sustain status in MIDI */
FX_Rec fx; /* effects */
FX_Rec fx_layer[MAX_LAYERS]; /* layer effects */
} awe_chan_info;
/* voice parameters */
typedef struct _voice_info {
int state;
#define AWE_ST_OFF (1<<0) /* no sound */
#define AWE_ST_ON (1<<1) /* playing */
#define AWE_ST_STANDBY (1<<2) /* stand by for playing */
#define AWE_ST_SUSTAINED (1<<3) /* sustained */
#define AWE_ST_MARK (1<<4) /* marked for allocation */
#define AWE_ST_DRAM (1<<5) /* DRAM read/write */
#define AWE_ST_FM (1<<6) /* reserved for FM */
#define AWE_ST_RELEASED (1<<7) /* released */
int ch; /* midi channel */
int key; /* internal key for search */
int layer; /* layer number (for channel mode only) */
int time; /* allocated time */
awe_chan_info *cinfo; /* channel info */
int note; /* midi key (0-127) */
int velocity; /* midi velocity (0-127) */
int sostenuto; /* sostenuto on/off */
awe_voice_info *sample; /* assigned voice */
/* EMU8000 parameters */
int apitch; /* pitch parameter */
int avol; /* volume parameter */
int apan; /* panning parameter */
int acutoff; /* cutoff parameter */
short aaux; /* aux word */
} voice_info;
/* voice information */
static voice_info voices[AWE_MAX_VOICES];
#define IS_NO_SOUND(v) (voices[v].state & (AWE_ST_OFF|AWE_ST_RELEASED|AWE_ST_STANDBY|AWE_ST_SUSTAINED))
#define IS_NO_EFFECT(v) (voices[v].state != AWE_ST_ON)
#define IS_PLAYING(v) (voices[v].state & (AWE_ST_ON|AWE_ST_SUSTAINED|AWE_ST_RELEASED))
#define IS_EMPTY(v) (voices[v].state & (AWE_ST_OFF|AWE_ST_MARK|AWE_ST_DRAM|AWE_ST_FM))
/* MIDI channel effects information (for hw control) */
static awe_chan_info channels[AWE_MAX_CHANNELS];
/*
* global variables
*/
#ifndef AWE_DEFAULT_BASE_ADDR
#define AWE_DEFAULT_BASE_ADDR 0 /* autodetect */
#endif
#ifndef AWE_DEFAULT_MEM_SIZE
#define AWE_DEFAULT_MEM_SIZE -1 /* autodetect */
#endif
static int io = AWE_DEFAULT_BASE_ADDR; /* Emu8000 base address */
static int memsize = AWE_DEFAULT_MEM_SIZE; /* memory size in Kbytes */
#ifdef CONFIG_PNP
static int isapnp = -1;
#else
static int isapnp;
#endif
MODULE_AUTHOR("Takashi Iwai <iwai@ww.uni-erlangen.de>");
MODULE_DESCRIPTION("SB AWE32/64 WaveTable driver");
MODULE_LICENSE("GPL");
module_param(io, int, 0);
MODULE_PARM_DESC(io, "base i/o port of Emu8000");
module_param(memsize, int, 0);
MODULE_PARM_DESC(memsize, "onboard DRAM size in Kbytes");
module_param(isapnp, bool, 0);
MODULE_PARM_DESC(isapnp, "use ISAPnP detection");
/* DRAM start offset */
static int awe_mem_start = AWE_DRAM_OFFSET;
/* maximum channels for playing */
static int awe_max_voices = AWE_MAX_VOICES;
static int patch_opened; /* sample already loaded? */
static char atten_relative = FALSE;
static short atten_offset;
static int awe_present = FALSE; /* awe device present? */
static int awe_busy = FALSE; /* awe device opened? */
static int my_dev = -1;
#define DEFAULT_DRUM_FLAGS ((1 << 9) | (1 << 25))
#define IS_DRUM_CHANNEL(c) (drum_flags & (1 << (c)))
#define DRUM_CHANNEL_ON(c) (drum_flags |= (1 << (c)))
#define DRUM_CHANNEL_OFF(c) (drum_flags &= ~(1 << (c)))
static unsigned int drum_flags = DEFAULT_DRUM_FLAGS; /* channel flags */
static int playing_mode = AWE_PLAY_INDIRECT;
#define SINGLE_LAYER_MODE() (playing_mode == AWE_PLAY_INDIRECT || playing_mode == AWE_PLAY_DIRECT)
#define MULTI_LAYER_MODE() (playing_mode == AWE_PLAY_MULTI || playing_mode == AWE_PLAY_MULTI2)
static int current_alloc_time; /* voice allocation index for channel mode */
static struct synth_info awe_info = {
"AWE32 Synth", /* name */
0, /* device */
SYNTH_TYPE_SAMPLE, /* synth_type */
SAMPLE_TYPE_AWE32, /* synth_subtype */
0, /* perc_mode (obsolete) */
AWE_MAX_VOICES, /* nr_voices */
0, /* nr_drums (obsolete) */
400 /* instr_bank_size */
};
static struct voice_alloc_info *voice_alloc; /* set at initialization */
/*
* function prototypes
*/
static int awe_request_region(void);
static void awe_release_region(void);
static void awe_reset_samples(void);
/* emu8000 chip i/o access */
static void setup_ports(int p1, int p2, int p3);
static void awe_poke(unsigned short cmd, unsigned short port, unsigned short data);
static void awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data);
static unsigned short awe_peek(unsigned short cmd, unsigned short port);
static unsigned int awe_peek_dw(unsigned short cmd, unsigned short port);
static void awe_wait(unsigned short delay);
/* initialize emu8000 chip */
static void awe_initialize(void);
/* set voice parameters */
static void awe_init_ctrl_parms(int init_all);
static void awe_init_voice_info(awe_voice_info *vp);
static void awe_init_voice_parm(awe_voice_parm *pp);
#ifdef AWE_HAS_GUS_COMPATIBILITY
static int freq_to_note(int freq);
static int calc_rate_offset(int Hz);
/*static int calc_parm_delay(int msec);*/
static int calc_parm_hold(int msec);
static int calc_parm_attack(int msec);
static int calc_parm_decay(int msec);
static int calc_parm_search(int msec, short *table);
#endif /* gus compat */
/* turn on/off note */
static void awe_note_on(int voice);
static void awe_note_off(int voice);
static void awe_terminate(int voice);
static void awe_exclusive_off(int voice);
static void awe_note_off_all(int do_sustain);
/* calculate voice parameters */
typedef void (*fx_affect_func)(int voice, int forced);
static void awe_set_pitch(int voice, int forced);
static void awe_set_voice_pitch(int voice, int forced);
static void awe_set_volume(int voice, int forced);
static void awe_set_voice_vol(int voice, int forced);
static void awe_set_pan(int voice, int forced);
static void awe_fx_fmmod(int voice, int forced);
static void awe_fx_tremfrq(int voice, int forced);
static void awe_fx_fm2frq2(int voice, int forced);
static void awe_fx_filterQ(int voice, int forced);
static void awe_calc_pitch(int voice);
#ifdef AWE_HAS_GUS_COMPATIBILITY
static void awe_calc_pitch_from_freq(int voice, int freq);
#endif
static void awe_calc_volume(int voice);
static void awe_update_volume(void);
static void awe_change_master_volume(short val);
static void awe_voice_init(int voice, int init_all);
static void awe_channel_init(int ch, int init_all);
static void awe_fx_init(int ch);
static void awe_send_effect(int voice, int layer, int type, int val);
static void awe_modwheel_change(int voice, int value);
/* sequencer interface */
static int awe_open(int dev, int mode);
static void awe_close(int dev);
static int awe_ioctl(int dev, unsigned int cmd, void __user * arg);
static int awe_kill_note(int dev, int voice, int note, int velocity);
static int awe_start_note(int dev, int v, int note_num, int volume);
static int awe_set_instr(int dev, int voice, int instr_no);
static int awe_set_instr_2(int dev, int voice, int instr_no);
static void awe_reset(int dev);
static void awe_hw_control(int dev, unsigned char *event);
static int awe_load_patch(int dev, int format, const char __user *addr,
int offs, int count, int pmgr_flag);
static void awe_aftertouch(int dev, int voice, int pressure);
static void awe_controller(int dev, int voice, int ctrl_num, int value);
static void awe_panning(int dev, int voice, int value);
static void awe_volume_method(int dev, int mode);
static void awe_bender(int dev, int voice, int value);
static int awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc);
static void awe_setup_voice(int dev, int voice, int chn);
#define awe_key_pressure(dev,voice,key,press) awe_start_note(dev,voice,(key)+128,press)
/* hardware controls */
#ifdef AWE_HAS_GUS_COMPATIBILITY
static void awe_hw_gus_control(int dev, int cmd, unsigned char *event);
#endif
static void awe_hw_awe_control(int dev, int cmd, unsigned char *event);
static void awe_voice_change(int voice, fx_affect_func func);
static void awe_sostenuto_on(int voice, int forced);
static void awe_sustain_off(int voice, int forced);
static void awe_terminate_and_init(int voice, int forced);
/* voice search */
static int awe_search_key(int bank, int preset, int note);
static awe_voice_list *awe_search_instr(int bank, int preset, int note);
static int awe_search_multi_voices(awe_voice_list *rec, int note, int velocity, awe_voice_info **vlist);
static void awe_alloc_multi_voices(int ch, int note, int velocity, int key);
static void awe_alloc_one_voice(int voice, int note, int velocity);
static int awe_clear_voice(void);
/* load / remove patches */
static int awe_open_patch(awe_patch_info *patch, const char __user *addr, int count);
static int awe_close_patch(awe_patch_info *patch, const char __user *addr, int count);
static int awe_unload_patch(awe_patch_info *patch, const char __user *addr, int count);
static int awe_load_info(awe_patch_info *patch, const char __user *addr, int count);
static int awe_remove_info(awe_patch_info *patch, const char __user *addr, int count);
static int awe_load_data(awe_patch_info *patch, const char __user *addr, int count);
static int awe_replace_data(awe_patch_info *patch, const char __user *addr, int count);
static int awe_load_map(awe_patch_info *patch, const char __user *addr, int count);
#ifdef AWE_HAS_GUS_COMPATIBILITY
static int awe_load_guspatch(const char __user *addr, int offs, int size, int pmgr_flag);
#endif
/*static int awe_probe_info(awe_patch_info *patch, const char __user *addr, int count);*/
static int awe_probe_data(awe_patch_info *patch, const char __user *addr, int count);
static sf_list *check_patch_opened(int type, char *name);
static int awe_write_wave_data(const char __user *addr, int offset, awe_sample_list *sp, int channels);
static int awe_create_sf(int type, char *name);
static void awe_free_sf(sf_list *sf);
static void add_sf_info(sf_list *sf, awe_voice_list *rec);
static void add_sf_sample(sf_list *sf, awe_sample_list *smp);
static void purge_old_list(awe_voice_list *rec, awe_voice_list *next);
static void add_info_list(awe_voice_list *rec);
static void awe_remove_samples(int sf_id);
static void rebuild_preset_list(void);
static short awe_set_sample(awe_voice_list *rec);
static awe_sample_list *search_sample_index(sf_list *sf, int sample);
static int is_identical_holder(sf_list *sf1, sf_list *sf2);
#ifdef AWE_ALLOW_SAMPLE_SHARING
static int is_identical_name(unsigned char *name, sf_list *p);
static int is_shared_sf(unsigned char *name);
static int info_duplicated(sf_list *sf, awe_voice_list *rec);
#endif /* allow sharing */
/* lowlevel functions */
static void awe_init_audio(void);
static void awe_init_dma(void);
static void awe_init_array(void);
static void awe_send_array(unsigned short *data);
static void awe_tweak_voice(int voice);
static void awe_tweak(void);
static void awe_init_fm(void);
static int awe_open_dram_for_write(int offset, int channels);
static void awe_open_dram_for_check(void);
static void awe_close_dram(void);
/*static void awe_write_dram(unsigned short c);*/
static int awe_detect_base(int addr);
static int awe_detect(void);
static void awe_check_dram(void);
static int awe_load_chorus_fx(awe_patch_info *patch, const char __user *addr, int count);
static void awe_set_chorus_mode(int mode);
static void awe_update_chorus_mode(void);
static int awe_load_reverb_fx(awe_patch_info *patch, const char __user *addr, int count);
static void awe_set_reverb_mode(int mode);
static void awe_update_reverb_mode(void);
static void awe_equalizer(int bass, int treble);
static void awe_update_equalizer(void);
#ifdef CONFIG_AWE32_MIXER
static void attach_mixer(void);
static void unload_mixer(void);
#endif
#ifdef CONFIG_AWE32_MIDIEMU
static void attach_midiemu(void);
static void unload_midiemu(void);
#endif
#define limitvalue(x, a, b) if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b)
/*
* control parameters
*/
#ifdef AWE_USE_NEW_VOLUME_CALC
#define DEF_VOLUME_CALC TRUE
#else
#define DEF_VOLUME_CALC FALSE
#endif /* new volume */
#define DEF_ZERO_ATTEN 32 /* 12dB below */
#define DEF_MOD_SENSE 18
#define DEF_CHORUS_MODE 2
#define DEF_REVERB_MODE 4
#define DEF_BASS_LEVEL 5
#define DEF_TREBLE_LEVEL 9
static struct CtrlParmsDef {
int value;
int init_each_time;
void (*update)(void);
} ctrl_parms[AWE_MD_END] = {
{0,0, NULL}, {0,0, NULL}, /* <-- not used */
{AWE_VERSION_NUMBER, FALSE, NULL},
{TRUE, FALSE, NULL}, /* exclusive */
{TRUE, FALSE, NULL}, /* realpan */
{AWE_DEFAULT_BANK, FALSE, NULL}, /* gusbank */
{FALSE, TRUE, NULL}, /* keep effect */
{DEF_ZERO_ATTEN, FALSE, awe_update_volume}, /* zero_atten */
{FALSE, FALSE, NULL}, /* chn_prior */
{DEF_MOD_SENSE, FALSE, NULL}, /* modwheel sense */
{AWE_DEFAULT_PRESET, FALSE, NULL}, /* def_preset */
{AWE_DEFAULT_BANK, FALSE, NULL}, /* def_bank */
{AWE_DEFAULT_DRUM, FALSE, NULL}, /* def_drum */
{FALSE, FALSE, NULL}, /* toggle_drum_bank */
{DEF_VOLUME_CALC, FALSE, awe_update_volume}, /* new_volume_calc */
{DEF_CHORUS_MODE, FALSE, awe_update_chorus_mode}, /* chorus mode */
{DEF_REVERB_MODE, FALSE, awe_update_reverb_mode}, /* reverb mode */
{DEF_BASS_LEVEL, FALSE, awe_update_equalizer}, /* bass level */
{DEF_TREBLE_LEVEL, FALSE, awe_update_equalizer}, /* treble level */
{0, FALSE, NULL}, /* debug mode */
{FALSE, FALSE, NULL}, /* pan exchange */
};
static int ctrls[AWE_MD_END];
/*
* synth operation table
*/
static struct synth_operations awe_operations =
{
.owner = THIS_MODULE,
.id = "EMU8K",
.info = &awe_info,
.midi_dev = 0,
.synth_type = SYNTH_TYPE_SAMPLE,
.synth_subtype = SAMPLE_TYPE_AWE32,
.open = awe_open,
.close = awe_close,
.ioctl = awe_ioctl,
.kill_note = awe_kill_note,
.start_note = awe_start_note,
.set_instr = awe_set_instr_2,
.reset = awe_reset,
.hw_control = awe_hw_control,
.load_patch = awe_load_patch,
.aftertouch = awe_aftertouch,
.controller = awe_controller,
.panning = awe_panning,
.volume_method = awe_volume_method,
.bender = awe_bender,
.alloc_voice = awe_alloc,
.setup_voice = awe_setup_voice
};
static void free_tables(void)
{
if (sftail) {
sf_list *p, *prev;
for (p = sftail; p; p = prev) {
prev = p->prev;
awe_free_sf(p);
}
}
sfhead = sftail = NULL;
}
/*
* clear sample tables
*/
static void
awe_reset_samples(void)
{
/* free all bank tables */
memset(preset_table, 0, sizeof(preset_table));
free_tables();
current_sf_id = 0;
locked_sf_id = 0;
patch_opened = 0;
}
/*
* EMU register access
*/
/* select a given AWE32 pointer */
static int awe_ports[5];
static int port_setuped = FALSE;
static int awe_cur_cmd = -1;
#define awe_set_cmd(cmd) \
if (awe_cur_cmd != cmd) { outw(cmd, awe_ports[Pointer]); awe_cur_cmd = cmd; }
/* write 16bit data */
static void
awe_poke(unsigned short cmd, unsigned short port, unsigned short data)
{
awe_set_cmd(cmd);
outw(data, awe_ports[port]);
}
/* write 32bit data */
static void
awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data)
{
unsigned short addr = awe_ports[port];
awe_set_cmd(cmd);
outw(data, addr); /* write lower 16 bits */
outw(data >> 16, addr + 2); /* write higher 16 bits */
}
/* read 16bit data */
static unsigned short
awe_peek(unsigned short cmd, unsigned short port)
{
unsigned short k;
awe_set_cmd(cmd);
k = inw(awe_ports[port]);
return k;
}
/* read 32bit data */
static unsigned int
awe_peek_dw(unsigned short cmd, unsigned short port)
{
unsigned int k1, k2;
unsigned short addr = awe_ports[port];
awe_set_cmd(cmd);
k1 = inw(addr);
k2 = inw(addr + 2);
k1 |= k2 << 16;
return k1;
}
/* wait delay number of AWE32 44100Hz clocks */
#ifdef WAIT_BY_LOOP /* wait by loop -- that's not good.. */
static void
awe_wait(unsigned short delay)
{
unsigned short clock, target;
unsigned short port = awe_ports[AWE_WC_Port];
int counter;
/* sample counter */
awe_set_cmd(AWE_WC_Cmd);
clock = (unsigned short)inw(port);
target = clock + delay;
counter = 0;
if (target < clock) {
for (; (unsigned short)inw(port) > target; counter++)
if (counter > 65536)
break;
}
for (; (unsigned short)inw(port) < target; counter++)
if (counter > 65536)
break;
}
#else
static void awe_wait(unsigned short delay)
{
current->state = TASK_INTERRUPTIBLE;
schedule_timeout((HZ*(unsigned long)delay + 44099)/44100);
}
/*
static void awe_wait(unsigned short delay)
{
udelay(((unsigned long)delay * 1000000L + 44099) / 44100);
}
*/
#endif /* wait by loop */
/* write a word data */
#define awe_write_dram(c) awe_poke(AWE_SMLD, c)
/*
* AWE32 voice parameters
*/
/* initialize voice_info record */
static void
awe_init_voice_info(awe_voice_info *vp)
{
vp->sample = 0;
vp->rate_offset = 0;
vp->start = 0;
vp->end = 0;
vp->loopstart = 0;
vp->loopend = 0;
vp->mode = 0;
vp->root = 60;
vp->tune = 0;
vp->low = 0;
vp->high = 127;
vp->vellow = 0;
vp->velhigh = 127;
vp->fixkey = -1;
vp->fixvel = -1;
vp->fixpan = -1;
vp->pan = -1;
vp->exclusiveClass = 0;
vp->amplitude = 127;
vp->attenuation = 0;
vp->scaleTuning = 100;
awe_init_voice_parm(&vp->parm);
}
/* initialize voice_parm record:
* Env1/2: delay=0, attack=0, hold=0, sustain=0, decay=0, release=0.
* Vibrato and Tremolo effects are zero.
* Cutoff is maximum.
* Chorus and Reverb effects are zero.
*/
static void
awe_init_voice_parm(awe_voice_parm *pp)
{
pp->moddelay = 0x8000;
pp->modatkhld = 0x7f7f;
pp->moddcysus = 0x7f7f;
pp->modrelease = 0x807f;
pp->modkeyhold = 0;
pp->modkeydecay = 0;
pp->voldelay = 0x8000;
pp->volatkhld = 0x7f7f;
pp->voldcysus = 0x7f7f;
pp->volrelease = 0x807f;
pp->volkeyhold = 0;
pp->volkeydecay = 0;
pp->lfo1delay = 0x8000;
pp->lfo2delay = 0x8000;
pp->pefe = 0;
pp->fmmod = 0;
pp->tremfrq = 0;
pp->fm2frq2 = 0;
pp->cutoff = 0xff;
pp->filterQ = 0;
pp->chorus = 0;
pp->reverb = 0;
}
#ifdef AWE_HAS_GUS_COMPATIBILITY
/* convert frequency mHz to abstract cents (= midi key * 100) */
static int
freq_to_note(int mHz)
{
/* abscents = log(mHz/8176) / log(2) * 1200 */
unsigned int max_val = (unsigned int)0xffffffff / 10000;
int i, times;
unsigned int base;
unsigned int freq;
int note, tune;
if (mHz == 0)
return 0;
if (mHz < 0)
return 12799; /* maximum */
freq = mHz;
note = 0;
for (base = 8176 * 2; freq >= base; base *= 2) {
note += 12;
if (note >= 128) /* over maximum */
return 12799;
}
base /= 2;
/* to avoid overflow... */
times = 10000;
while (freq > max_val) {
max_val *= 10;
times /= 10;
base /= 10;
}
freq = freq * times / base;
for (i = 0; i < 12; i++) {
if (freq < semitone_tuning[i+1])
break;
note++;
}
tune = 0;
freq = freq * 10000 / semitone_tuning[i];
for (i = 0; i < 100; i++) {
if (freq < cent_tuning[i+1])
break;
tune++;
}
return note * 100 + tune;
}
/* convert Hz to AWE32 rate offset:
* sample pitch offset for the specified sample rate
* rate=44100 is no offset, each 4096 is 1 octave (twice).
* eg, when rate is 22050, this offset becomes -4096.
*/
static int
calc_rate_offset(int Hz)
{
/* offset = log(Hz / 44100) / log(2) * 4096 */
int freq, base, i;
/* maybe smaller than max (44100Hz) */
if (Hz <= 0 || Hz >= 44100) return 0;
base = 0;
for (freq = Hz * 2; freq < 44100; freq *= 2)
base++;
base *= 1200;
freq = 44100 * 10000 / (freq/2);
for (i = 0; i < 12; i++) {
if (freq < semitone_tuning[i+1])
break;
base += 100;
}
freq = freq * 10000 / semitone_tuning[i];
for (i = 0; i < 100; i++) {
if (freq < cent_tuning[i+1])
break;
base++;
}
return -base * 4096 / 1200;
}
/*
* convert envelope time parameter to AWE32 raw parameter
*/
/* attack & decay/release time table (msec) */
static short attack_time_tbl[128] = {
32767, 32767, 5989, 4235, 2994, 2518, 2117, 1780, 1497, 1373, 1259, 1154, 1058, 970, 890, 816,
707, 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377,
361, 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188,
180, 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94,
90, 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47,
45, 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23,
22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 6, 0,
};
static short decay_time_tbl[128] = {
32767, 32767, 22614, 15990, 11307, 9508, 7995, 6723, 5653, 5184, 4754, 4359, 3997, 3665, 3361, 3082,
2828, 2765, 2648, 2535, 2428, 2325, 2226, 2132, 2042, 1955, 1872, 1793, 1717, 1644, 1574, 1507,
1443, 1382, 1324, 1267, 1214, 1162, 1113, 1066, 978, 936, 897, 859, 822, 787, 754, 722,
691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377, 361,
345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188, 180,
172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94, 90,
86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45,
43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23, 22,
};
#define calc_parm_delay(msec) (0x8000 - (msec) * 1000 / 725);
/* delay time = 0x8000 - msec/92 */
static int
calc_parm_hold(int msec)
{
int val = (0x7f * 92 - msec) / 92;
if (val < 1) val = 1;
if (val > 127) val = 127;
return val;
}
/* attack time: search from time table */
static int
calc_parm_attack(int msec)
{
return calc_parm_search(msec, attack_time_tbl);
}
/* decay/release time: search from time table */
static int
calc_parm_decay(int msec)
{
return calc_parm_search(msec, decay_time_tbl);
}
/* search an index for specified time from given time table */
static int
calc_parm_search(int msec, short *table)
{
int left = 1, right = 127, mid;
while (left < right) {
mid = (left + right) / 2;
if (msec < (int)table[mid])
left = mid + 1;
else
right = mid;
}
return left;
}
#endif /* AWE_HAS_GUS_COMPATIBILITY */
/*
* effects table
*/
/* set an effect value */
#define FX_FLAG_OFF 0
#define FX_FLAG_SET 1
#define FX_FLAG_ADD 2
#define FX_SET(rec,type,value) \
((rec)->flags[type] = FX_FLAG_SET, (rec)->val[type] = (value))
#define FX_ADD(rec,type,value) \
((rec)->flags[type] = FX_FLAG_ADD, (rec)->val[type] = (value))
#define FX_UNSET(rec,type) \
((rec)->flags[type] = FX_FLAG_OFF, (rec)->val[type] = 0)
/* check the effect value is set */
#define FX_ON(rec,type) ((rec)->flags[type])
#define PARM_BYTE 0
#define PARM_WORD 1
#define PARM_SIGN 2
static struct PARM_DEFS {
int type; /* byte or word */
int low, high; /* value range */
fx_affect_func realtime; /* realtime paramater change */
} parm_defs[] = {
{PARM_WORD, 0, 0x8000, NULL}, /* env1 delay */
{PARM_BYTE, 1, 0x7f, NULL}, /* env1 attack */
{PARM_BYTE, 0, 0x7e, NULL}, /* env1 hold */
{PARM_BYTE, 1, 0x7f, NULL}, /* env1 decay */
{PARM_BYTE, 1, 0x7f, NULL}, /* env1 release */
{PARM_BYTE, 0, 0x7f, NULL}, /* env1 sustain */
{PARM_BYTE, 0, 0xff, NULL}, /* env1 pitch */
{PARM_BYTE, 0, 0xff, NULL}, /* env1 cutoff */
{PARM_WORD, 0, 0x8000, NULL}, /* env2 delay */
{PARM_BYTE, 1, 0x7f, NULL}, /* env2 attack */
{PARM_BYTE, 0, 0x7e, NULL}, /* env2 hold */
{PARM_BYTE, 1, 0x7f, NULL}, /* env2 decay */
{PARM_BYTE, 1, 0x7f, NULL}, /* env2 release */
{PARM_BYTE, 0, 0x7f, NULL}, /* env2 sustain */
{PARM_WORD, 0, 0x8000, NULL}, /* lfo1 delay */
{PARM_BYTE, 0, 0xff, awe_fx_tremfrq}, /* lfo1 freq */
{PARM_SIGN, -128, 127, awe_fx_tremfrq}, /* lfo1 volume */
{PARM_SIGN, -128, 127, awe_fx_fmmod}, /* lfo1 pitch */
{PARM_BYTE, 0, 0xff, awe_fx_fmmod}, /* lfo1 cutoff */
{PARM_WORD, 0, 0x8000, NULL}, /* lfo2 delay */
{PARM_BYTE, 0, 0xff, awe_fx_fm2frq2}, /* lfo2 freq */
{PARM_SIGN, -128, 127, awe_fx_fm2frq2}, /* lfo2 pitch */
{PARM_WORD, 0, 0xffff, awe_set_voice_pitch}, /* initial pitch */
{PARM_BYTE, 0, 0xff, NULL}, /* chorus */
{PARM_BYTE, 0, 0xff, NULL}, /* reverb */
{PARM_BYTE, 0, 0xff, awe_set_volume}, /* initial cutoff */
{PARM_BYTE, 0, 15, awe_fx_filterQ}, /* initial resonance */
{PARM_WORD, 0, 0xffff, NULL}, /* sample start */
{PARM_WORD, 0, 0xffff, NULL}, /* loop start */
{PARM_WORD, 0, 0xffff, NULL}, /* loop end */
{PARM_WORD, 0, 0xffff, NULL}, /* coarse sample start */
{PARM_WORD, 0, 0xffff, NULL}, /* coarse loop start */
{PARM_WORD, 0, 0xffff, NULL}, /* coarse loop end */
{PARM_BYTE, 0, 0xff, awe_set_volume}, /* initial attenuation */
};
static unsigned char
FX_BYTE(FX_Rec *rec, FX_Rec *lay, int type, unsigned char value)
{
int effect = 0;
int on = 0;
if (lay && (on = FX_ON(lay, type)) != 0)
effect = lay->val[type];
if (!on && (on = FX_ON(rec, type)) != 0)
effect = rec->val[type];
if (on == FX_FLAG_ADD) {
if (parm_defs[type].type == PARM_SIGN) {
if (value > 0x7f)
effect += (int)value - 0x100;
else
effect += (int)value;
} else {
effect += (int)value;
}
}
if (on) {
if (effect < parm_defs[type].low)
effect = parm_defs[type].low;
else if (effect > parm_defs[type].high)
effect = parm_defs[type].high;
return (unsigned char)effect;
}
return value;
}
/* get word effect value */
static unsigned short
FX_WORD(FX_Rec *rec, FX_Rec *lay, int type, unsigned short value)
{
int effect = 0;
int on = 0;
if (lay && (on = FX_ON(lay, type)) != 0)
effect = lay->val[type];
if (!on && (on = FX_ON(rec, type)) != 0)
effect = rec->val[type];
if (on == FX_FLAG_ADD)
effect += (int)value;
if (on) {
if (effect < parm_defs[type].low)
effect = parm_defs[type].low;
else if (effect > parm_defs[type].high)
effect = parm_defs[type].high;
return (unsigned short)effect;
}
return value;
}
/* get word (upper=type1/lower=type2) effect value */
static unsigned short
FX_COMB(FX_Rec *rec, FX_Rec *lay, int type1, int type2, unsigned short value)
{
unsigned short tmp;
tmp = FX_BYTE(rec, lay, type1, (unsigned char)(value >> 8));
tmp <<= 8;
tmp |= FX_BYTE(rec, lay, type2, (unsigned char)(value & 0xff));
return tmp;
}
/* address offset */
static int
FX_OFFSET(FX_Rec *rec, FX_Rec *lay, int lo, int hi, int mode)
{
int addr = 0;
if (lay && FX_ON(lay, hi))
addr = (short)lay->val[hi];
else if (FX_ON(rec, hi))
addr = (short)rec->val[hi];
addr = addr << 15;
if (lay && FX_ON(lay, lo))
addr += (short)lay->val[lo];
else if (FX_ON(rec, lo))
addr += (short)rec->val[lo];
if (!(mode & AWE_SAMPLE_8BITS))
addr /= 2;
return addr;
}
/*
* turn on/off sample
*/
/* table for volume target calculation */
static unsigned short voltarget[16] = {
0xEAC0, 0XE0C8, 0XD740, 0XCE20, 0XC560, 0XBD08, 0XB500, 0XAD58,
0XA5F8, 0X9EF0, 0X9830, 0X91C0, 0X8B90, 0X85A8, 0X8000, 0X7A90
};
static void
awe_note_on(int voice)
{
unsigned int temp;
int addr;
int vtarget, ftarget, ptarget, pitch;
awe_voice_info *vp;
awe_voice_parm_block *parm;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
/* A voice sample must assigned before calling */
if ((vp = voices[voice].sample) == NULL || vp->index == 0)
return;
parm = (awe_voice_parm_block*)&vp->parm;
/* channel to be silent and idle */
awe_poke(AWE_DCYSUSV(voice), 0x0080);
awe_poke(AWE_VTFT(voice), 0x0000FFFF);
awe_poke(AWE_CVCF(voice), 0x0000FFFF);
awe_poke(AWE_PTRX(voice), 0);
awe_poke(AWE_CPF(voice), 0);
/* set pitch offset */
awe_set_pitch(voice, TRUE);
/* modulation & volume envelope */
if (parm->modatk >= 0x80 && parm->moddelay >= 0x8000) {
awe_poke(AWE_ENVVAL(voice), 0xBFFF);
pitch = (parm->env1pit<<4) + voices[voice].apitch;
if (pitch > 0xffff) pitch = 0xffff;
/* calculate filter target */
ftarget = parm->cutoff + parm->env1fc;
limitvalue(ftarget, 0, 255);
ftarget <<= 8;
} else {
awe_poke(AWE_ENVVAL(voice),
FX_WORD(fx, fx_lay, AWE_FX_ENV1_DELAY, parm->moddelay));
ftarget = parm->cutoff;
ftarget <<= 8;
pitch = voices[voice].apitch;
}
/* calcualte pitch target */
if (pitch != 0xffff) {
ptarget = 1 << (pitch >> 12);
if (pitch & 0x800) ptarget += (ptarget*0x102e)/0x2710;
if (pitch & 0x400) ptarget += (ptarget*0x764)/0x2710;
if (pitch & 0x200) ptarget += (ptarget*0x389)/0x2710;
ptarget += (ptarget>>1);
if (ptarget > 0xffff) ptarget = 0xffff;
} else ptarget = 0xffff;
if (parm->modatk >= 0x80)
awe_poke(AWE_ATKHLD(voice),
FX_BYTE(fx, fx_lay, AWE_FX_ENV1_HOLD, parm->modhld) << 8 | 0x7f);
else
awe_poke(AWE_ATKHLD(voice),
FX_COMB(fx, fx_lay, AWE_FX_ENV1_HOLD, AWE_FX_ENV1_ATTACK,
vp->parm.modatkhld));
awe_poke(AWE_DCYSUS(voice),
FX_COMB(fx, fx_lay, AWE_FX_ENV1_SUSTAIN, AWE_FX_ENV1_DECAY,
vp->parm.moddcysus));
if (parm->volatk >= 0x80 && parm->voldelay >= 0x8000) {
awe_poke(AWE_ENVVOL(voice), 0xBFFF);
vtarget = voltarget[voices[voice].avol%0x10]>>(voices[voice].avol>>4);
} else {
awe_poke(AWE_ENVVOL(voice),
FX_WORD(fx, fx_lay, AWE_FX_ENV2_DELAY, vp->parm.voldelay));
vtarget = 0;
}
if (parm->volatk >= 0x80)
awe_poke(AWE_ATKHLDV(voice),
FX_BYTE(fx, fx_lay, AWE_FX_ENV2_HOLD, parm->volhld) << 8 | 0x7f);
else
awe_poke(AWE_ATKHLDV(voice),
FX_COMB(fx, fx_lay, AWE_FX_ENV2_HOLD, AWE_FX_ENV2_ATTACK,
vp->parm.volatkhld));
/* decay/sustain parameter for volume envelope must be set at last */
/* cutoff and volume */
awe_set_volume(voice, TRUE);
/* modulation envelope heights */
awe_poke(AWE_PEFE(voice),
FX_COMB(fx, fx_lay, AWE_FX_ENV1_PITCH, AWE_FX_ENV1_CUTOFF,
vp->parm.pefe));
/* lfo1/2 delay */
awe_poke(AWE_LFO1VAL(voice),
FX_WORD(fx, fx_lay, AWE_FX_LFO1_DELAY, vp->parm.lfo1delay));
awe_poke(AWE_LFO2VAL(voice),
FX_WORD(fx, fx_lay, AWE_FX_LFO2_DELAY, vp->parm.lfo2delay));
/* lfo1 pitch & cutoff shift */
awe_fx_fmmod(voice, TRUE);
/* lfo1 volume & freq */
awe_fx_tremfrq(voice, TRUE);
/* lfo2 pitch & freq */
awe_fx_fm2frq2(voice, TRUE);
/* pan & loop start */
awe_set_pan(voice, TRUE);
/* chorus & loop end (chorus 8bit, MSB) */
addr = vp->loopend - 1;
addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_END,
AWE_FX_COARSE_LOOP_END, vp->mode);
temp = FX_BYTE(fx, fx_lay, AWE_FX_CHORUS, vp->parm.chorus);
temp = (temp <<24) | (unsigned int)addr;
awe_poke_dw(AWE_CSL(voice), temp);
DEBUG(4,printk("AWE32: [-- loopend=%x/%x]\n", vp->loopend, addr));
/* Q & current address (Q 4bit value, MSB) */
addr = vp->start - 1;
addr += FX_OFFSET(fx, fx_lay, AWE_FX_SAMPLE_START,
AWE_FX_COARSE_SAMPLE_START, vp->mode);
temp = FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ);
temp = (temp<<28) | (unsigned int)addr;
awe_poke_dw(AWE_CCCA(voice), temp);
DEBUG(4,printk("AWE32: [-- startaddr=%x/%x]\n", vp->start, addr));
/* clear unknown registers */
awe_poke_dw(AWE_00A0(voice), 0);
awe_poke_dw(AWE_0080(voice), 0);
/* reset volume */
awe_poke_dw(AWE_VTFT(voice), (vtarget<<16)|ftarget);
awe_poke_dw(AWE_CVCF(voice), (vtarget<<16)|ftarget);
/* set reverb */
temp = FX_BYTE(fx, fx_lay, AWE_FX_REVERB, vp->parm.reverb);
temp = (temp << 8) | (ptarget << 16) | voices[voice].aaux;
awe_poke_dw(AWE_PTRX(voice), temp);
awe_poke_dw(AWE_CPF(voice), ptarget << 16);
/* turn on envelope */
awe_poke(AWE_DCYSUSV(voice),
FX_COMB(fx, fx_lay, AWE_FX_ENV2_SUSTAIN, AWE_FX_ENV2_DECAY,
vp->parm.voldcysus));
voices[voice].state = AWE_ST_ON;
/* clear voice position for the next note on this channel */
if (SINGLE_LAYER_MODE()) {
FX_UNSET(fx, AWE_FX_SAMPLE_START);
FX_UNSET(fx, AWE_FX_COARSE_SAMPLE_START);
}
}
/* turn off the voice */
static void
awe_note_off(int voice)
{
awe_voice_info *vp;
unsigned short tmp;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
if ((vp = voices[voice].sample) == NULL) {
voices[voice].state = AWE_ST_OFF;
return;
}
tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV1_RELEASE,
(unsigned char)vp->parm.modrelease);
awe_poke(AWE_DCYSUS(voice), tmp);
tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV2_RELEASE,
(unsigned char)vp->parm.volrelease);
awe_poke(AWE_DCYSUSV(voice), tmp);
voices[voice].state = AWE_ST_RELEASED;
}
/* force to terminate the voice (no releasing echo) */
static void
awe_terminate(int voice)
{
awe_poke(AWE_DCYSUSV(voice), 0x807F);
awe_tweak_voice(voice);
voices[voice].state = AWE_ST_OFF;
}
/* turn off other voices with the same exclusive class (for drums) */
static void
awe_exclusive_off(int voice)
{
int i, exclass;
if (voices[voice].sample == NULL)
return;
if ((exclass = voices[voice].sample->exclusiveClass) == 0)
return; /* not exclusive */
/* turn off voices with the same class */
for (i = 0; i < awe_max_voices; i++) {
if (i != voice && IS_PLAYING(i) &&
voices[i].sample && voices[i].ch == voices[voice].ch &&
voices[i].sample->exclusiveClass == exclass) {
DEBUG(4,printk("AWE32: [exoff(%d)]\n", i));
awe_terminate(i);
awe_voice_init(i, TRUE);
}
}
}
/*
* change the parameters of an audible voice
*/
/* change pitch */
static void
awe_set_pitch(int voice, int forced)
{
if (IS_NO_EFFECT(voice) && !forced) return;
awe_poke(AWE_IP(voice), voices[voice].apitch);
DEBUG(3,printk("AWE32: [-- pitch=%x]\n", voices[voice].apitch));
}
/* calculate & change pitch */
static void
awe_set_voice_pitch(int voice, int forced)
{
awe_calc_pitch(voice);
awe_set_pitch(voice, forced);
}
/* change volume & cutoff */
static void
awe_set_volume(int voice, int forced)
{
awe_voice_info *vp;
unsigned short tmp2;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
if (!IS_PLAYING(voice) && !forced) return;
if ((vp = voices[voice].sample) == NULL || vp->index == 0)
return;
tmp2 = FX_BYTE(fx, fx_lay, AWE_FX_CUTOFF,
(unsigned char)voices[voice].acutoff);
tmp2 = (tmp2 << 8);
tmp2 |= FX_BYTE(fx, fx_lay, AWE_FX_ATTEN,
(unsigned char)voices[voice].avol);
awe_poke(AWE_IFATN(voice), tmp2);
}
/* calculate & change volume */
static void
awe_set_voice_vol(int voice, int forced)
{
if (IS_EMPTY(voice))
return;
awe_calc_volume(voice);
awe_set_volume(voice, forced);
}
/* change pan; this could make a click noise.. */
static void
awe_set_pan(int voice, int forced)
{
unsigned int temp;
int addr;
awe_voice_info *vp;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
if (IS_NO_EFFECT(voice) && !forced) return;
if ((vp = voices[voice].sample) == NULL || vp->index == 0)
return;
/* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
if (vp->fixpan > 0) /* 0-127 */
temp = 255 - (int)vp->fixpan * 2;
else {
int pos = 0;
if (vp->pan >= 0) /* 0-127 */
pos = (int)vp->pan * 2 - 128;
pos += voices[voice].cinfo->panning; /* -128 - 127 */
temp = 127 - pos;
}
limitvalue(temp, 0, 255);
if (ctrls[AWE_MD_PAN_EXCHANGE]) {
temp = 255 - temp;
}
if (forced || temp != voices[voice].apan) {
voices[voice].apan = temp;
if (temp == 0)
voices[voice].aaux = 0xff;
else
voices[voice].aaux = (-temp) & 0xff;
addr = vp->loopstart - 1;
addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_START,
AWE_FX_COARSE_LOOP_START, vp->mode);
temp = (temp<<24) | (unsigned int)addr;
awe_poke_dw(AWE_PSST(voice), temp);
DEBUG(4,printk("AWE32: [-- loopstart=%x/%x]\n", vp->loopstart, addr));
}
}
/* effects change during playing */
static void
awe_fx_fmmod(int voice, int forced)
{
awe_voice_info *vp;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
if (IS_NO_EFFECT(voice) && !forced) return;
if ((vp = voices[voice].sample) == NULL || vp->index == 0)
return;
awe_poke(AWE_FMMOD(voice),
FX_COMB(fx, fx_lay, AWE_FX_LFO1_PITCH, AWE_FX_LFO1_CUTOFF,
vp->parm.fmmod));
}
/* set tremolo (lfo1) volume & frequency */
static void
awe_fx_tremfrq(int voice, int forced)
{
awe_voice_info *vp;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
if (IS_NO_EFFECT(voice) && !forced) return;
if ((vp = voices[voice].sample) == NULL || vp->index == 0)
return;
awe_poke(AWE_TREMFRQ(voice),
FX_COMB(fx, fx_lay, AWE_FX_LFO1_VOLUME, AWE_FX_LFO1_FREQ,
vp->parm.tremfrq));
}
/* set lfo2 pitch & frequency */
static void
awe_fx_fm2frq2(int voice, int forced)
{
awe_voice_info *vp;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
if (IS_NO_EFFECT(voice) && !forced) return;
if ((vp = voices[voice].sample) == NULL || vp->index == 0)
return;
awe_poke(AWE_FM2FRQ2(voice),
FX_COMB(fx, fx_lay, AWE_FX_LFO2_PITCH, AWE_FX_LFO2_FREQ,
vp->parm.fm2frq2));
}
/* Q & current address (Q 4bit value, MSB) */
static void
awe_fx_filterQ(int voice, int forced)
{
unsigned int addr;
awe_voice_info *vp;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
if (IS_NO_EFFECT(voice) && !forced) return;
if ((vp = voices[voice].sample) == NULL || vp->index == 0)
return;
addr = awe_peek_dw(AWE_CCCA(voice)) & 0xffffff;
addr |= (FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ) << 28);
awe_poke_dw(AWE_CCCA(voice), addr);
}
/*
* calculate pitch offset
*
* 0xE000 is no pitch offset at 44100Hz sample.
* Every 4096 is one octave.
*/
static void
awe_calc_pitch(int voice)
{
voice_info *vp = &voices[voice];
awe_voice_info *ap;
awe_chan_info *cp = voices[voice].cinfo;
int offset;
/* search voice information */
if ((ap = vp->sample) == NULL)
return;
if (ap->index == 0) {
DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
if (awe_set_sample((awe_voice_list*)ap) == 0)
return;
}
/* calculate offset */
if (ap->fixkey >= 0) {
DEBUG(3,printk("AWE32: p-> fixkey(%d) tune(%d)\n", ap->fixkey, ap->tune));
offset = (ap->fixkey - ap->root) * 4096 / 12;
} else {
DEBUG(3,printk("AWE32: p(%d)-> root(%d) tune(%d)\n", vp->note, ap->root, ap->tune));
offset = (vp->note - ap->root) * 4096 / 12;
DEBUG(4,printk("AWE32: p-> ofs=%d\n", offset));
}
offset = (offset * ap->scaleTuning) / 100;
DEBUG(4,printk("AWE32: p-> scale* ofs=%d\n", offset));
offset += ap->tune * 4096 / 1200;
DEBUG(4,printk("AWE32: p-> tune+ ofs=%d\n", offset));
if (cp->bender != 0) {
DEBUG(3,printk("AWE32: p-> bend(%d) %d\n", voice, cp->bender));
/* (819200: 1 semitone) ==> (4096: 12 semitones) */
offset += cp->bender * cp->bender_range / 2400;
}
/* add initial pitch correction */
if (FX_ON(&cp->fx_layer[vp->layer], AWE_FX_INIT_PITCH))
offset += cp->fx_layer[vp->layer].val[AWE_FX_INIT_PITCH];
else if (FX_ON(&cp->fx, AWE_FX_INIT_PITCH))
offset += cp->fx.val[AWE_FX_INIT_PITCH];
/* 0xe000: root pitch */
vp->apitch = 0xe000 + ap->rate_offset + offset;
DEBUG(4,printk("AWE32: p-> sum aofs=%x, rate_ofs=%d\n", vp->apitch, ap->rate_offset));
if (vp->apitch > 0xffff)
vp->apitch = 0xffff;
if (vp->apitch < 0)
vp->apitch = 0;
}
#ifdef AWE_HAS_GUS_COMPATIBILITY
/* calculate MIDI key and semitone from the specified frequency */
static void
awe_calc_pitch_from_freq(int voice, int freq)
{
voice_info *vp = &voices[voice];
awe_voice_info *ap;
FX_Rec *fx = &voices[voice].cinfo->fx;
FX_Rec *fx_lay = NULL;
int offset;
int note;
if (voices[voice].layer < MAX_LAYERS)
fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
/* search voice information */
if ((ap = vp->sample) == NULL)
return;
if (ap->index == 0) {
DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
if (awe_set_sample((awe_voice_list*)ap) == 0)
return;
}
note = freq_to_note(freq);
offset = (note - ap->root * 100 + ap->tune) * 4096 / 1200;
offset = (offset * ap->scaleTuning) / 100;
if (fx_lay && FX_ON(fx_lay, AWE_FX_INIT_PITCH))
offset += fx_lay->val[AWE_FX_INIT_PITCH];
else if (FX_ON(fx, AWE_FX_INIT_PITCH))
offset += fx->val[AWE_FX_INIT_PITCH];
vp->apitch = 0xe000 + ap->rate_offset + offset;
if (vp->apitch > 0xffff)
vp->apitch = 0xffff;
if (vp->apitch < 0)
vp->apitch = 0;
}
#endif /* AWE_HAS_GUS_COMPATIBILITY */
/*
* calculate volume attenuation
*
* Voice volume is controlled by volume attenuation parameter.
* So volume becomes maximum when avol is 0 (no attenuation), and
* minimum when 255 (-96dB or silence).
*/
static int vol_table[128] = {
255,111,95,86,79,74,70,66,63,61,58,56,54,52,50,49,
47,46,45,43,42,41,40,39,38,37,36,35,34,34,33,32,
31,31,30,29,29,28,27,27,26,26,25,24,24,23,23,22,
22,21,21,21,20,20,19,19,18,18,18,17,17,16,16,16,
15,15,15,14,14,14,13,13,13,12,12,12,11,11,11,10,
10,10,10,9,9,9,8,8,8,8,7,7,7,7,6,6,
6,6,5,5,5,5,5,4,4,4,4,3,3,3,3,3,
2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,0,
};
/* tables for volume->attenuation calculation */
static unsigned char voltab1[128] = {
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static unsigned char voltab2[128] = {
0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
};
static unsigned char expressiontab[128] = {
0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static void
awe_calc_volume(int voice)
{
voice_info *vp = &voices[voice];
awe_voice_info *ap;
awe_chan_info *cp = voices[voice].cinfo;
int vol;
/* search voice information */
if ((ap = vp->sample) == NULL)
return;
ap = vp->sample;
if (ap->index == 0) {
DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
if (awe_set_sample((awe_voice_list*)ap) == 0)
return;
}
if (ctrls[AWE_MD_NEW_VOLUME_CALC]) {
int main_vol = cp->main_vol * ap->amplitude / 127;
limitvalue(vp->velocity, 0, 127);
limitvalue(main_vol, 0, 127);
limitvalue(cp->expression_vol, 0, 127);
vol = voltab1[main_vol] + voltab2[vp->velocity];
vol = (vol * 8) / 3;
vol += ap->attenuation;
if (cp->expression_vol < 127)
vol += ((0x100 - vol) * expressiontab[cp->expression_vol])/128;
vol += atten_offset;
if (atten_relative)
vol += ctrls[AWE_MD_ZERO_ATTEN];
limitvalue(vol, 0, 255);
vp->avol = vol;
} else {
/* 0 - 127 */
vol = (vp->velocity * cp->main_vol * cp->expression_vol) / (127*127);
vol = vol * ap->amplitude / 127;
if (vol < 0) vol = 0;
if (vol > 127) vol = 127;
/* calc to attenuation */
vol = vol_table[vol];
vol += (int)ap->attenuation;
vol += atten_offset;
if (atten_relative)
vol += ctrls[AWE_MD_ZERO_ATTEN];
if (vol > 255) vol = 255;
vp->avol = vol;
}
if (cp->bank != AWE_DRUM_BANK && ((awe_voice_parm_block*)(&ap->parm))->volatk < 0x7d) {
int atten;
if (vp->velocity < 70) atten = 70;
else atten = vp->velocity;
vp->acutoff = (atten * ap->parm.cutoff + 0xa0) >> 7;
} else {
vp->acutoff = ap->parm.cutoff;
}
DEBUG(3,printk("AWE32: [-- voice(%d) vol=%x]\n", voice, vol));
}
/* change master volume */
static void
awe_change_master_volume(short val)
{
limitvalue(val, 0, 127);
atten_offset = vol_table[val];
atten_relative = TRUE;
awe_update_volume();
}
/* update volumes of all available channels */
static void awe_update_volume(void)
{
int i;
for (i = 0; i < awe_max_voices; i++)
awe_set_voice_vol(i, TRUE);
}
/* set sostenuto on */
static void awe_sostenuto_on(int voice, int forced)
{
if (IS_NO_EFFECT(voice) && !forced) return;
voices[voice].sostenuto = 127;
}
/* drop sustain */
static void awe_sustain_off(int voice, int forced)
{
if (voices[voice].state == AWE_ST_SUSTAINED) {
awe_note_off(voice);
awe_fx_init(voices[voice].ch);
awe_voice_init(voice, FALSE);
}
}
/* terminate and initialize voice */
static void awe_terminate_and_init(int voice, int forced)
{
awe_terminate(voice);
awe_fx_init(voices[voice].ch);
awe_voice_init(voice, TRUE);
}
/*
* synth operation routines
*/
#define AWE_VOICE_KEY(v) (0x8000 | (v))
#define AWE_CHAN_KEY(c,n) (((c) << 8) | ((n) + 1))
#define KEY_CHAN_MATCH(key,c) (((key) >> 8) == (c))
/* initialize the voice */
static void
awe_voice_init(int voice, int init_all)
{
voice_info *vp = &voices[voice];
/* reset voice search key */
if (playing_mode == AWE_PLAY_DIRECT)
vp->key = AWE_VOICE_KEY(voice);
else
vp->key = 0;
/* clear voice mapping */
voice_alloc->map[voice] = 0;
/* touch the timing flag */
vp->time = current_alloc_time;
/* initialize other parameters if necessary */
if (init_all) {
vp->note = -1;
vp->velocity = 0;
vp->sostenuto = 0;
vp->sample = NULL;
vp->cinfo = &channels[voice];
vp->ch = voice;
vp->state = AWE_ST_OFF;
/* emu8000 parameters */
vp->apitch = 0;
vp->avol = 255;
vp->apan = -1;
}
}
/* clear effects */
static void awe_fx_init(int ch)
{
if (SINGLE_LAYER_MODE() && !ctrls[AWE_MD_KEEP_EFFECT]) {
memset(&channels[ch].fx, 0, sizeof(channels[ch].fx));
memset(&channels[ch].fx_layer, 0, sizeof(&channels[ch].fx_layer));
}
}
/* initialize channel info */
static void awe_channel_init(int ch, int init_all)
{
awe_chan_info *cp = &channels[ch];
cp->channel = ch;
if (init_all) {
cp->panning = 0; /* zero center */
cp->bender_range = 200; /* sense * 100 */
cp->main_vol = 127;
if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(ch)) {
cp->instr = ctrls[AWE_MD_DEF_DRUM];
cp->bank = AWE_DRUM_BANK;
} else {
cp->instr = ctrls[AWE_MD_DEF_PRESET];
cp->bank = ctrls[AWE_MD_DEF_BANK];
}
}
cp->bender = 0; /* zero tune skew */
cp->expression_vol = 127;
cp->chan_press = 0;
cp->sustained = 0;
if (! ctrls[AWE_MD_KEEP_EFFECT]) {
memset(&cp->fx, 0, sizeof(cp->fx));
memset(&cp->fx_layer, 0, sizeof(cp->fx_layer));
}
}
/* change the voice parameters; voice = channel */
static void awe_voice_change(int voice, fx_affect_func func)
{
int i;
switch (playing_mode) {
case AWE_PLAY_DIRECT:
func(voice, FALSE);
break;
case AWE_PLAY_INDIRECT:
for (i = 0; i < awe_max_voices; i++)
if (voices[i].key == AWE_VOICE_KEY(voice))
func(i, FALSE);
break;
default:
for (i = 0; i < awe_max_voices; i++)
if (KEY_CHAN_MATCH(voices[i].key, voice))
func(i, FALSE);
break;
}
}
/*
* device open / close
*/
/* open device:
* reset status of all voices, and clear sample position flag
*/
static int
awe_open(int dev, int mode)
{
if (awe_busy)
return -EBUSY;
awe_busy = TRUE;
/* set default mode */
awe_init_ctrl_parms(FALSE);
atten_relative = TRUE;
atten_offset = 0;
drum_flags = DEFAULT_DRUM_FLAGS;
playing_mode = AWE_PLAY_INDIRECT;
/* reset voices & channels */
awe_reset(dev);
patch_opened = 0;
return 0;
}
/* close device:
* reset all voices again (terminate sounds)
*/
static void
awe_close(int dev)
{
awe_reset(dev);
awe_busy = FALSE;
}
/* set miscellaneous mode parameters
*/
static void
awe_init_ctrl_parms(int init_all)
{
int i;
for (i = 0; i < AWE_MD_END; i++) {
if (init_all || ctrl_parms[i].init_each_time)
ctrls[i] = ctrl_parms[i].value;
}
}
/* sequencer I/O control:
*/
static int
awe_ioctl(int dev, unsigned int cmd, void __user *arg)
{
switch (cmd) {
case SNDCTL_SYNTH_INFO:
if (playing_mode == AWE_PLAY_DIRECT)
awe_info.nr_voices = awe_max_voices;
else
awe_info.nr_voices = AWE_MAX_CHANNELS;
if (copy_to_user(arg, &awe_info, sizeof(awe_info)))
return -EFAULT;
return 0;
break;
case SNDCTL_SEQ_RESETSAMPLES:
awe_reset(dev);
awe_reset_samples();
return 0;
break;
case SNDCTL_SEQ_PERCMODE:
/* what's this? */
return 0;
break;
case SNDCTL_SYNTH_MEMAVL:
return memsize - awe_free_mem_ptr() * 2;
break;
default:
printk(KERN_WARNING "AWE32: unsupported ioctl %d\n", cmd);
return -EINVAL;
break;
}
}
static int voice_in_range(int voice)
{
if (playing_mode == AWE_PLAY_DIRECT) {
if (voice < 0 || voice >= awe_max_voices)
return FALSE;
} else {
if (voice < 0 || voice >= AWE_MAX_CHANNELS)
return FALSE;
}
return TRUE;
}
static void release_voice(int voice, int do_sustain)
{
if (IS_NO_SOUND(voice))
return;
if (do_sustain && (voices[voice].cinfo->sustained == 127 ||
voices[voice].sostenuto == 127))
voices[voice].state = AWE_ST_SUSTAINED;
else {
awe_note_off(voice);
awe_fx_init(voices[voice].ch);
awe_voice_init(voice, FALSE);
}
}
/* release all notes */
static void awe_note_off_all(int do_sustain)
{
int i;
for (i = 0; i < awe_max_voices; i++)
release_voice(i, do_sustain);
}
/* kill a voice:
* not terminate, just release the voice.
*/
static int
awe_kill_note(int dev, int voice, int note, int velocity)
{
int i, v2, key;
DEBUG(2,printk("AWE32: [off(%d) nt=%d vl=%d]\n", voice, note, velocity));
if (! voice_in_range(voice))
return -EINVAL;
switch (playing_mode) {
case AWE_PLAY_DIRECT:
case AWE_PLAY_INDIRECT:
key = AWE_VOICE_KEY(voice);
break;
case AWE_PLAY_MULTI2:
v2 = voice_alloc->map[voice] >> 8;
voice_alloc->map[voice] = 0;
voice = v2;
if (voice < 0 || voice >= AWE_MAX_CHANNELS)
return -EINVAL;
/* continue to below */
default:
key = AWE_CHAN_KEY(voice, note);
break;
}
for (i = 0; i < awe_max_voices; i++) {
if (voices[i].key == key)
release_voice(i, TRUE);
}
return 0;
}
static void start_or_volume_change(int voice, int velocity)
{
voices[voice].velocity = velocity;
awe_calc_volume(voice);
if (voices[voice].state == AWE_ST_STANDBY)
awe_note_on(voice);
else if (voices[voice].state == AWE_ST_ON)
awe_set_volume(voice, FALSE);
}
static void set_and_start_voice(int voice, int state)
{
/* calculate pitch & volume parameters */
voices[voice].state = state;
awe_calc_pitch(voice);
awe_calc_volume(voice);
if (state == AWE_ST_ON)
awe_note_on(voice);
}
/* start a voice:
* if note is 255, identical with aftertouch function.
* Otherwise, start a voice with specified not and volume.
*/
static int
awe_start_note(int dev, int voice, int note, int velocity)
{
int i, key, state, volonly;
DEBUG(2,printk("AWE32: [on(%d) nt=%d vl=%d]\n", voice, note, velocity));
if (! voice_in_range(voice))
return -EINVAL;
if (velocity == 0)
state = AWE_ST_STANDBY; /* stand by for playing */
else
state = AWE_ST_ON; /* really play */
volonly = FALSE;
switch (playing_mode) {
case AWE_PLAY_DIRECT:
case AWE_PLAY_INDIRECT:
key = AWE_VOICE_KEY(voice);
if (note == 255)
volonly = TRUE;
break;
case AWE_PLAY_MULTI2:
voice = voice_alloc->map[voice] >> 8;
if (voice < 0 || voice >= AWE_MAX_CHANNELS)
return -EINVAL;
/* continue to below */
default:
if (note >= 128) { /* key volume mode */
note -= 128;
volonly = TRUE;
}
key = AWE_CHAN_KEY(voice, note);
break;
}
/* dynamic volume change */
if (volonly) {
for (i = 0; i < awe_max_voices; i++) {
if (voices[i].key == key)
start_or_volume_change(i, velocity);
}
return 0;
}
/* if the same note still playing, stop it */
if (playing_mode != AWE_PLAY_DIRECT || ctrls[AWE_MD_EXCLUSIVE_SOUND]) {
for (i = 0; i < awe_max_voices; i++)
if (voices[i].key == key) {
if (voices[i].state == AWE_ST_ON) {
awe_note_off(i);
awe_voice_init(i, FALSE);
} else if (voices[i].state == AWE_ST_STANDBY)
awe_voice_init(i, TRUE);
}
}
/* allocate voices */
if (playing_mode == AWE_PLAY_DIRECT)
awe_alloc_one_voice(voice, note, velocity);
else
awe_alloc_multi_voices(voice, note, velocity, key);
/* turn off other voices exlusively (for drums) */
for (i = 0; i < awe_max_voices; i++)
if (voices[i].key == key)
awe_exclusive_off(i);
/* set up pitch and volume parameters */
for (i = 0; i < awe_max_voices; i++) {
if (voices[i].key == key && voices[i].state == AWE_ST_OFF)
set_and_start_voice(i, state);
}
return 0;
}
/* calculate hash key */
static int
awe_search_key(int bank, int preset, int note)
{
unsigned int key;
#if 1 /* new hash table */
if (bank == AWE_DRUM_BANK)
key = preset + note + 128;
else
key = bank + preset;
#else
key = preset;
#endif
key %= AWE_MAX_PRESETS;
return (int)key;
}
/* search instrument from hash table */
static awe_voice_list *
awe_search_instr(int bank, int preset, int note)
{
awe_voice_list *p;
int key, key2;
key = awe_search_key(bank, preset, note);
for (p = preset_table[key]; p; p = p->next_bank) {
if (p->instr == preset && p->bank == bank)
return p;
}
key2 = awe_search_key(bank, preset, 0); /* search default */
if (key == key2)
return NULL;
for (p = preset_table[key2]; p; p = p->next_bank) {
if (p->instr == preset && p->bank == bank)
return p;
}
return NULL;
}
/* assign the instrument to a voice */
static int
awe_set_instr_2(int dev, int voice, int instr_no)
{
if (playing_mode == AWE_PLAY_MULTI2) {
voice = voice_alloc->map[voice] >> 8;
if (voice < 0 || voice >= AWE_MAX_CHANNELS)
return -EINVAL;
}
return awe_set_instr(dev, voice, instr_no);
}
/* assign the instrument to a channel; voice is the channel number */
static int
awe_set_instr(int dev, int voice, int instr_no)
{
awe_chan_info *cinfo;
if (! voice_in_range(voice))
return -EINVAL;
if (instr_no < 0 || instr_no >= AWE_MAX_PRESETS)
return -EINVAL;
cinfo = &channels[voice];
cinfo->instr = instr_no;
DEBUG(2,printk("AWE32: [program(%d) %d]\n", voice, instr_no));
return 0;
}
/* reset all voices; terminate sounds and initialize parameters */
static void
awe_reset(int dev)
{
int i;
current_alloc_time = 0;
/* don't turn off voice 31 and 32. they are used also for FM voices */
for (i = 0; i < awe_max_voices; i++) {
awe_terminate(i);
awe_voice_init(i, TRUE);
}
for (i = 0; i < AWE_MAX_CHANNELS; i++)
awe_channel_init(i, TRUE);
for (i = 0; i < 16; i++) {
awe_operations.chn_info[i].controllers[CTL_MAIN_VOLUME] = 127;
awe_operations.chn_info[i].controllers[CTL_EXPRESSION] = 127;
}
awe_init_fm();
awe_tweak();
}
/* hardware specific control:
* GUS specific and AWE32 specific controls are available.
*/
static void
awe_hw_control(int dev, unsigned char *event)
{
int cmd = event[2];
if (cmd & _AWE_MODE_FLAG)
awe_hw_awe_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
#ifdef AWE_HAS_GUS_COMPATIBILITY
else
awe_hw_gus_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
#endif
}
#ifdef AWE_HAS_GUS_COMPATIBILITY
/* GUS compatible controls */
static void
awe_hw_gus_control(int dev, int cmd, unsigned char *event)
{
int voice, i, key;
unsigned short p1;
short p2;
int plong;
if (MULTI_LAYER_MODE())
return;
if (cmd == _GUS_NUMVOICES)
return;
voice = event[3];
if (! voice_in_range(voice))
return;
p1 = *(unsigned short *) &event[4];
p2 = *(short *) &event[6];
plong = *(int*) &event[4];
switch (cmd) {
case _GUS_VOICESAMPLE:
awe_set_instr(dev, voice, p1);
return;
case _GUS_VOICEBALA:
/* 0 to 15 --> -128 to 127 */
awe_panning(dev, voice, ((int)p1 << 4) - 128);
return;
case _GUS_VOICEVOL:
case _GUS_VOICEVOL2:
/* not supported yet */
return;
case _GUS_RAMPRANGE:
case _GUS_RAMPRATE:
case _GUS_RAMPMODE:
case _GUS_RAMPON:
case _GUS_RAMPOFF:
/* volume ramping not supported */
return;
case _GUS_VOLUME_SCALE:
return;
case _GUS_VOICE_POS:
FX_SET(&channels[voice].fx, AWE_FX_SAMPLE_START,
(short)(plong & 0x7fff));
FX_SET(&channels[voice].fx, AWE_FX_COARSE_SAMPLE_START,
(plong >> 15) & 0xffff);
return;
}
key = AWE_VOICE_KEY(voice);
for (i = 0; i < awe_max_voices; i++) {
if (voices[i].key == key) {
switch (cmd) {
case _GUS_VOICEON:
awe_note_on(i);
break;
case _GUS_VOICEOFF:
awe_terminate(i);
awe_fx_init(voices[i].ch);
awe_voice_init(i, TRUE);
break;
case _GUS_VOICEFADE:
awe_note_off(i);
awe_fx_init(voices[i].ch);
awe_voice_init(i, FALSE);
break;
case _GUS_VOICEFREQ:
awe_calc_pitch_from_freq(i, plong);
break;
}
}
}
}
#endif /* gus_compat */
/* AWE32 specific controls */
static void
awe_hw_awe_control(int dev, int cmd, unsigned char *event)
{
int voice;
unsigned short p1;
short p2;
int i;
voice = event[3];
if (! voice_in_range(voice))
return;
if (playing_mode == AWE_PLAY_MULTI2) {
voice = voice_alloc->map[voice] >> 8;
if (voice < 0 || voice >= AWE_MAX_CHANNELS)
return;
}
p1 = *(unsigned short *) &event[4];
p2 = *(short *) &event[6];
switch (cmd) {
case _AWE_DEBUG_MODE:
ctrls[AWE_MD_DEBUG_MODE] = p1;
printk(KERN_DEBUG "AWE32: debug mode = %d\n", ctrls[AWE_MD_DEBUG_MODE]);
break;
case _AWE_REVERB_MODE:
ctrls[AWE_MD_REVERB_MODE] = p1;
awe_update_reverb_mode();
break;
case _AWE_CHORUS_MODE:
ctrls[AWE_MD_CHORUS_MODE] = p1;
awe_update_chorus_mode();
break;
case _AWE_REMOVE_LAST_SAMPLES:
DEBUG(0,printk("AWE32: remove last samples\n"));
awe_reset(0);
if (locked_sf_id > 0)
awe_remove_samples(locked_sf_id);
break;
case _AWE_INITIALIZE_CHIP:
awe_initialize();
break;
case _AWE_SEND_EFFECT:
i = -1;
if (p1 >= 0x100) {
i = (p1 >> 8);
if (i < 0 || i >= MAX_LAYERS)
break;
}
awe_send_effect(voice, i, p1, p2);
break;
case _AWE_RESET_CHANNEL:
awe_channel_init(voice, !p1);
break;
case _AWE_TERMINATE_ALL:
awe_reset(0);
break;
case _AWE_TERMINATE_CHANNEL:
awe_voice_change(voice, awe_terminate_and_init);
break;
case _AWE_RELEASE_ALL:
awe_note_off_all(FALSE);
break;
case _AWE_NOTEOFF_ALL:
awe_note_off_all(TRUE);
break;
case _AWE_INITIAL_VOLUME:
DEBUG(0,printk("AWE32: init attenuation %d\n", p1));
atten_relative = (char)p2;
atten_offset = (short)p1;
awe_update_volume();
break;
case _AWE_CHN_PRESSURE:
channels[voice].chan_press = p1;
awe_modwheel_change(voice, p1);
break;
case _AWE_CHANNEL_MODE:
DEBUG(0,printk("AWE32: channel mode = %d\n", p1));
playing_mode = p1;
awe_reset(0);
break;
case _AWE_DRUM_CHANNELS:
DEBUG(0,printk("AWE32: drum flags = %x\n", p1));
drum_flags = *(unsigned int*)&event[4];
break;
case _AWE_MISC_MODE:
DEBUG(0,printk("AWE32: ctrl parms = %d %d\n", p1, p2));
if (p1 > AWE_MD_VERSION && p1 < AWE_MD_END) {
ctrls[p1] = p2;
if (ctrl_parms[p1].update)
ctrl_parms[p1].update();
}
break;
case _AWE_EQUALIZER:
ctrls[AWE_MD_BASS_LEVEL] = p1;
ctrls[AWE_MD_TREBLE_LEVEL] = p2;
awe_update_equalizer();
break;
default:
DEBUG(0,printk("AWE32: hw control cmd=%d voice=%d\n", cmd, voice));
break;
}
}
/* change effects */
static void
awe_send_effect(int voice, int layer, int type, int val)
{
awe_chan_info *cinfo;
FX_Rec *fx;
int mode;
cinfo = &channels[voice];
if (layer >= 0 && layer < MAX_LAYERS)
fx = &cinfo->fx_layer[layer];
else
fx = &cinfo->fx;
if (type & 0x40)
mode = FX_FLAG_OFF;
else if (type & 0x80)
mode = FX_FLAG_ADD;
else
mode = FX_FLAG_SET;
type &= 0x3f;
if (type >= 0 && type < AWE_FX_END) {
DEBUG(2,printk("AWE32: effects (%d) %d %d\n", voice, type, val));
if (mode == FX_FLAG_SET)
FX_SET(fx, type, val);
else if (mode == FX_FLAG_ADD)
FX_ADD(fx, type, val);
else
FX_UNSET(fx, type);
if (mode != FX_FLAG_OFF && parm_defs[type].realtime) {
DEBUG(2,printk("AWE32: fx_realtime (%d)\n", voice));
awe_voice_change(voice, parm_defs[type].realtime);
}
}
}
/* change modulation wheel; voice is already mapped on multi2 mode */
static void
awe_modwheel_change(int voice, int value)
{
int i;
awe_chan_info *cinfo;
cinfo = &channels[voice];
i = value * ctrls[AWE_MD_MOD_SENSE] / 1200;
FX_ADD(&cinfo->fx, AWE_FX_LFO1_PITCH, i);
awe_voice_change(voice, awe_fx_fmmod);
FX_ADD(&cinfo->fx, AWE_FX_LFO2_PITCH, i);
awe_voice_change(voice, awe_fx_fm2frq2);
}
/* voice pressure change */
static void
awe_aftertouch(int dev, int voice, int pressure)
{
int note;
DEBUG(2,printk("AWE32: [after(%d) %d]\n", voice, pressure));
if (! voice_in_range(voice))
return;
switch (playing_mode) {
case AWE_PLAY_DIRECT:
case AWE_PLAY_INDIRECT:
awe_start_note(dev, voice, 255, pressure);
break;
case AWE_PLAY_MULTI2:
note = (voice_alloc->map[voice] & 0xff) - 1;
awe_key_pressure(dev, voice, note + 0x80, pressure);
break;
}
}
/* voice control change */
static void
awe_controller(int dev, int voice, int ctrl_num, int value)
{
awe_chan_info *cinfo;
if (! voice_in_range(voice))
return;
if (playing_mode == AWE_PLAY_MULTI2) {
voice = voice_alloc->map[voice] >> 8;
if (voice < 0 || voice >= AWE_MAX_CHANNELS)
return;
}
cinfo = &<