#ifndef _Note_H
#define _Note_H

#include "TimTypes.h"
#include "TimLib:MixStream.h"

struct Note
{
	MixStream    stream;
	Note*        pprevious;
	Note*        pnext;
	Channel*     pchannel;
	const Instrument*  pinstrument;// pointer to current instrument or NULL
	const Sample*      psample;    // pointer to current sample or NULL
	int32_t      finetune;   // sample finetune in 1/256 semitone
	int32_t      ch_pitch;   // pitch in 1/256 semitone, before envelope/vibrato
	int32_t      ch_volume;  // before envelope/fading
	int32_t      ch_panning; // before envelope
	int32_t      inst_volume;// sample scale volume amended by volume swing
	int32_t      pitch;      // pitch, finally converted freq multiplier in 1/65536
	int32_t      volume;     // [0-65536]
	int32_t      panning;    // [0-256], 384 for surround
	int32_t      frequency;  // in Hz
	uint8_t      smp_vibr_rdepth[3]; // running depth of sample vibrato
	uint8_t      smp_vibr_pos;       // current pos within vibrato envelope
	uint8_t      inst_id;    // instrument id
	uint8_t      inst_note;  // instrument note
	uint8_t      smp_id;     // sample id
	uint8_t      inst_flags; // instrument flags
	uint32_t     vol_envelope_pos; // pos in volume envelope
	uint32_t     pan_envelope_pos; // pos in panning envelope
	uint32_t     pitch_envelope_pos; // pos in pitch envelope
	uint32_t     filter_envelope_pos; // pos in resonant filter envelope
	int32_t      fadeout_volume; // 65536 in sustain mode, then decrease by instrument fadeout
	int32_t      envelope_volume; // [0, 256]
	int32_t      new_volume; // new volume to obtain after ramping [0 ... 65536 ...]
	int32_t      old_volume; // volume before ramping [0 ... 65536 ...]
	int32_t      new_panning; // new panning to obtain after ramping
	int32_t      old_panning; // panning before ramping
	int32_t      new_frequency; // new frequency to obtain after ramping
	int32_t      old_frequency; // frequency before ramping
	uint8_t      newnoteaction;
	uint8_t      resonance;
	uint8_t      cutoff;
	uint8_t      envelope_flags; // envelopes flags
	uint32_t     realcutoff;
	uint32_t     frame_count;    // used but virtual pool to cut oldest notes
	uint32_t     hold;           //
};

#define note_inst_flag_sustain      0x01 // note sustain mode is active
#define note_inst_flag_invert       0x02 // use sample backwards
#define note_inst_flag_forceloop    0x04 // set sample loop from flags below
#define note_inst_flag_loop         0x08 // = sample type loop, forced
#define note_inst_flag_loop_bidi    0x10 // = sample type loop bidi, forced
#define note_inst_flag_fadeout      0x40 // note is in fadeout phase
#define note_inst_flag_paused       0x80 // paused by gapper effect
#define note_inst_default_flags     0x01

#define note_envelope_flag_vol      0x01 // use volume envelope if present
#define note_envelope_flag_pan      0x02 // use panning envelope if present
#define note_envelope_flag_pitch    0x04 // use pitch envelope if present
#define note_envelope_flag_filter   0x08 // use filter envelope if present

#endif
