
#define NOTEOFF                         0x80
#define NOTEON                          0x90
#define AFTERTOUCH                      0xa0
#define CONTROLCHANGE                   0xb0
#define PROGRAMCHANGE                   0xc0
#define CHANNELPRESSURE                 0xd0
#define PITCHWHEEL                      0xe0
#define SYSTEM                          0xf0

// controllers
#define CTRL_BANK_SELECT                0
#define CTRL_MODULATION_WHEEL           1
#define CTRL_BREATH_CONTROL             2
#define CTRL_FOOT_PEDAL                 4
#define CTRL_PORTAMENTO_TIME            5
#define CTRL_DATAENTRY                  6
#define CTRL_VOLUME                     7
#define CTRL_BALANCE                    8
#define CTRL_PAN                        10
#define CTRL_EXPRESSION                 11
#define CTRL_EFFECT_CONTROL1            12
#define CTRL_EFFECT_CONTROL2            13
#define CTRL_GENERAL_PURPOSE_SLIDER1    16
#define CTRL_GENERAL_PURPOSE_SLIDER2    17
#define CTRL_GENERAL_PURPOSE_SLIDER3    18
#define CTRL_GENERAL_PURPOSE_SLIDER4    19
#define CTRL_BANK_SELECT_FINE           32
#define CTRL_MODULATION_WHEEL_FINE      33
#define CTRL_BREATH_CONTROL_FINE        34
#define CTRL_FOOT_PEDAL_FINE            36
#define CTRL_PORTAMENTO_TIME_FINE       37
#define CTRL_DATAENTRY_FINE             38
#define CTRL_VOLUME_FINE                39
#define CTRL_BALANCE_FINE               40
#define CTRL_PAN_FINE                   42
#define CTRL_EXPRESSION_FINE            43
#define CTRL_EFFECT_CONTROL1_FINE       44
#define CTRL_EFFECT_CONTROL2_FINE       45
#define CTRL_HOLD_PEDAL                 64
#define CTRL_PORTAMENTO                 65
#define CTRL_SUSTENUTO_PEDAL            66
#define CTRL_SOFT_PEDAL                 67
#define CTRL_LEGATO_PEDAL               68
#define CTRL_HOLD2_PEDAL                69
#define CTRL_SNDVARIATION               70
#define CTRL_SNDTIMBRE                  71
#define CTRL_SNDRELEASETIME             72
#define CTRL_SNDATTACKTIME              73
#define CTRL_SNDBRIGHTNESS              74
#define CTRL_SOUND_CONTROL6             75
#define CTRL_SOUND_CONTROL7             76
#define CTRL_SOUND_CONTROL8             77
#define CTRL_SOUND_CONTROL9             78
#define CTRL_SOUND_CONTROL10            79
#define CTRL_GENERAL_PURPOSE_BUTTON1    80
#define CTRL_GENERAL_PURPOSE_BUTTON2    81
#define CTRL_GENERAL_PURPOSE_BUTTON3    82
#define CTRL_GENERAL_PURPOSE_BUTTON4    83
#define CTRL_EFFECTLEVEL                91
#define CTRL_TREMOLOLEVEL               92
#define CTRL_CHORUSLEVEL                93
#define CTRL_CELESTELEVEL               94
#define CTRL_PHASERLEVEL                95

#define CTRL_DATAENTRY_INCREMENT        96
#define CTRL_DATAENTRY_DECREMENT        97
#define CTRL_NRPN_COARSE                98
#define CTRL_NRPN_FINE                  99
#define CTRL_RPN_COARSE                 100
#define CTRL_RPN_FINE                   101

#define CTRL_ALLSOUNDOFF                120
#define CTRL_ALLCTRLOFF                 121
#define CTRL_LOCALKEYBOARD              122
#define CTRL_ALLNOTESOFF                123
#define CTRL_OMNI_MODE_OFF              124
#define CTRL_OMNI_MODE_ON               125
#define CTRL_MONOPHONIC_MODE            126
#define CTRL_POLYPHONIC_MODE            127

// MIDI-byte stream
#define SYSTEM_EXCLUSIVE                0xf0
#define SYSTEM_MTC_QUARTER              0xf1
#define SYSTEM_SONG_POS_PTR             0xf2
#define SYSTEM_SONG_SELECT              0xf3
#define SYSTEM_TUNE_REQUEST             0xf6
#define SYSTEM_EXCLUSIVE_END            0xf7
#define SYSTEM_MIDI_CLOCK               0xf8
#define SYSTEM_MIDI_START               0xfa
#define SYSTEM_MIDI_CONTINUE            0xfb
#define SYSTEM_MIDI_STOP                0xfc
#define SYSTEM_ACTIVESENSE              0xfe
#define SYSTEM_RESET                    0xff


#define MIDICTRL_READONLY               0x80000000

#define MIDICTRL_FREQ                   0x00
#define MIDICTRL_POLYPHONY              0x01
#define MIDICTRL_VOLUME                 0x02
#define MIDICTRL_TUNING                 0x03
#define MIDICTRL_INTERPOLATE            0x04
#define MIDICTRL_BASECHANNEL            0x05

#define MIDICTRL_CHN_IS_DRUM            0x00
#define MIDICTRL_CHN_PAN                0x01
#define MIDICTRL_CHN_VOLUME             0x02
#define MIDICTRL_CHN_BANK               0x03
#define MIDICTRL_CHN_PROGRAM            0x04
#define MIDICTRL_READALL_CHN_IS_DRUM    0x05
#define MIDICTRL_READALL_CHN_PAN        0x06
#define MIDICTRL_READALL_CHN_VOLUME     0x07
#define MIDICTRL_READALL_CHN_BANK       0x08
#define MIDICTRL_READALL_CHN_PROGRAM    0x09

#define MIDICTRL_ATTACKTIME             0x00
#define MIDICTRL_HOLDTIME               0x01
#define MIDICTRL_DECAYTIME              0x02
#define MIDICTRL_DECAYAMOUNT            0x03
#define MIDICTRL_SUSTAINTIME            0x04
#define MIDICTRL_MAXDURATION            0x05
#define MIDICTRL_LOOP                   0x06
#define MIDICTRL_PATCH_FREQ             0x07

#define MIDI_UPDATE_TICKER              (1<<0)
#define MIDI_INCR_TICKER_TYPE           (1<<1)
#define MIDI_STEREO                     (1<<2)
#define MIDI_CLEAR_BUFFER               (1<<3)


#define RESAMPLE_BITS           12      /* hardcoded in mcode.s */
#define LOG2SAMPLESPERFRAME     6
#define LOG2TIMERESOLUTION      10      /* sustain/decay/hold/attack time is in 1/1024th of a sec */

#define SOURCE_16BIT            0
#define SOURCE_8BIT             1
#define SOURCE_VOICE_GENERATOR  2

#define NOTESTATE_FINISHED      0
#define NOTESTATE_RELEASE       1
#define NOTESTATE_SUSTAIN       2
#define NOTESTATE_DECAY         3
#define NOTESTATE_HOLD          4
#define NOTESTATE_ATTACK        5

#define NOTEFLAGS_SILENCE       1
#define NOTEFLAGS_FORCE_RELEASE 2
#define NOTEFLAGS_ABORT         4




extern const signed short sinetable[];
extern const unsigned short log2lin_volume[];


int midi_initialise(void);
void midi_kill(void);
void reset_port_structure(MIDIPORT *port);

void midi_rx_bytes(MIDIPORT *port, unsigned char *mb, int n, int *available);
int midi_read_samples(signed short *buffer, int samples, int flags, MIDIPORT *port);
int midi_read_frequency(void);
MIDIPORT *midi_get_default_port(void);

_kernel_oserror *midi_control(unsigned int reason, int arg1, int arg2, int *rval);
_kernel_oserror *midi_info(unsigned int reason, int arg1, int *rval);
_kernel_oserror *midi_control_channel(unsigned int reason, int arg1, int arg2, int *rval);
_kernel_oserror *midi_control_patches(unsigned int reason, int arg1, int arg2, int *rval);
_kernel_oserror *midi_monitor(unsigned int reason, int arg1, int arg2, int *rval);

void kill_notes_using_sample(MIDIPORT *port, SAMPLEDATA *ptr);
