/*
  MidiSynth, MIDI synthesiser, a repacement for the Acorn MIDI module

  midi.h - MIDI interface definitions

  created  15/01/10
  1.0      14/06/14
*/

#ifndef midi_h
#define midi_h

typedef struct patch_s
{
  unsigned char hi;  // bank hi, 0..127  )
  unsigned char lo;  // bank lo, 0..127  ) channel patch
  unsigned char prg; // program, 0..127  )
}
  patch_t;

// midi in definition
typedef struct m_in_s
{
  unsigned char
    type,          // type              )
    channel,       // channel           )  current message
    count,         // data counter      )
    controller,    // controller number )

    pitch,         // key pitch         )
    velocity,      // key velocity      ) last note or pressure message data
    pressure,      // key pressure      )

    song;          // song number

  unsigned short
    position;      // song position
} m_in_t;


// system exclusive data
#define SYS_LEN 256          // maximum length of system exclusive messages
typedef struct sysex_s
{
  unsigned char buff[SYS_LEN]; // buffer for system exclusive messages
  int ix;                    // index to above buffer
} sysex_t;


// midi channel definition
typedef struct chan_s
{
  patch_t patch, temp;

  unsigned short int
    pressure,
    pitch_wheel,

    // controllers
    mod_wheel,        // pitch modulation
    breath_ctrl,
    foot_pedal,
    portamento_time,  // 0 fastest, 7F:7F slowest
    volume,
    balance,
    pan_posn,
    expression,
    vibrato_rate,     // pitch modulation
    vibrato_depth,    // pitch modulation
    vibrato_delay,    // pitch modulation
    portamento_ctrl,  // initial note to glide from
    switches,         // bit definitions below
    switches_delta,   // as switches but signals changes, set by incoming change, reset when read

    // parameters
    *param_ptr,       // pointer to current parameter
    param_idx,        // parameter index
    pitch_bend_range, // parameter 0
    mod_depth_range,  // parameter 5, pitch modulation

    // derived controls
    bender,           // scaled pitch wheel
    mod,              // scaled pitch modulation
    glide,            // scaled portamento time constant
    vol_expr,         // (volume * expression) squared
    left_vol,         // left volume (pan & balance)
    right_vol;        // right volume (pan & balance)

} chan_t;

// channel switches
#define HOLD_PEDAL      0
#define PORTAMENTO      1
#define SUSTENUTO_PEDAL 2
#define SOFT_PEDAL      3
#define LEGATO_PEDAL    4
#define HOLD_2_PEDAL    5
#define PORTAM_CTRL     6

extern const chan_t def_chan;

int iexp(int x);
void print_midi_msg(unsigned int data, int n); // for debug/info
void midiSynth_midi_in(int data); // midi input stream interpreter

#endif
