//||
//||	`rhybag' - Rhythmic BinAural Generator
//||
//||	(c) 1999 Jim Peters <jim@aguazul.demon.co.uk>.  All Rights Reserved.
//||	For latest version see <http://www.aguazul.demon.co.uk/bagen/>.
//||	Released under the GNU GPL.  Use at your own risk.
//||
//||	" 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, version 2.
//||
//||	  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. "
//||
//||	If you really don't have a copy of the GNU GPL, I'll send you one.
//||	

//
//	Data structures
//

typedef struct Note Note;
typedef struct NoteB NoteB;
typedef struct NoteS NoteS;
typedef struct NoteM NoteM;
typedef struct BaList BaList;
typedef struct EnvList EnvList;
typedef struct MixList MixList;
typedef struct Sequence Sequence;
typedef struct ParList ParList;
typedef struct SerList SerList;
typedef struct Event Event;
typedef struct VolSeg VolSeg;
typedef struct PlayS PlayS;
typedef struct PlayB PlayB;

struct Note {
  Note *nxt;		// Next in chain of notes, or 0
  char *nam;
  int typ;		// 'B' binaural, 'S' sample, 'M' mix
};

struct NoteB {
  Note *nxt;		// Next in chain of notes, or 0
  char *nam;
  int typ;		// 'B'
  BaList *bin;
};

struct NoteS {
  Note *nxt;		// Next in chain of notes, or 0
  char *nam;
  int typ;		// 'S'
  char *fnam;		// Filename of raw stereo sample-file
  int tmp_fil;		// Temporary file ?
  int fd;		// File-descriptor of open file, or 0
  void *mmap;		// mmap'd file contents, or 0
  int styp;		// Sample type: 0 mono 16-bit, 1 stereo 16-bit
  int pre;		// Samples before loopable section starts - these are played in -ve time
  int len;		// Samples of loopable section
  int loop;		// Loop the loopable section (otherwise play just once)
  int bits;		// Number of bits required to contain total length +50%
  uint off0;		// Offset into sample at which to start playing
  EnvList *env;		// Envelope
};

struct NoteM {
  Note *nxt;		// Next in chain of notes, or 0
  char *nam;
  int typ;		// 'M'
  MixList *mix;
};

struct BaList {
  BaList *nxt;
  int tim;		// Start time in samples relative to start of note (may be -ve)
  int len;		// Length in samples (temp: -1 for no slide, -2 for slide)
  double carr[3];	// Carrier frequency (Hz) slide parameters
  double freq[3];	// Binaural frequency (Hz) slide parameters
  double vol[3];	// Volume level slide parameters (0-1, typically)
  double voli;		// Volume increment adjust (multiply by volume to get change per sample)
  int cnt;		// Delta-count to use for slides
};

struct EnvList {
  EnvList *nxt;
  int tim;		// Start time in samples relative to start of note (may be -ve)
  int len;		// Length in samples (temp: -1 for no slide, -2 for slide)
  double adj[3];	// Frequency adjust slide parameters; adjust is multiplicative factor 
  double vol[3];	// Volume level slide parameters (0-1, typically)
  double voli;		// Volume increment adjust (multiply by volume to get change per sample)
  int cnt;		// Delta-count to use for slides
};

struct MixList {
  MixList *nxt;
  Note *note;		// Any type of note
  int tim;		// Start time in samples relative to start of note (may be -ve)
  double vol;		// Volume level, multiple of natural volume (0-1, typically)
};

struct Sequence {
  Sequence *nxt;	// Next in list of sequences, or 0
  char *nam;
  int len;		// Length of sequence (samples)
  ParList *par;
};

struct ParList {	// List of sequences running in parallel
  ParList *nxt;		// Next in parallel
  int len;		// Length of series list (samples)
  SerList *ser;		// Serial list
};

struct SerList {	// Element of a series list	
  SerList *nxt;		// Next element in series, or 0
  int tim;		// Time relative to start of sequence (samples)
  int len;		// Length of this item (for handling repeats), or 0
  int rep;		// Repeat count for this item
  Note *note;		// Note if a note plays here, or 0
  Sequence *seq;	// Sequence if a sequence plays here, or 0
  ParList *par;		// Parallel sequences at this point, or 0
};

struct Event {
  Event *nxt;		// Next in list or 0
  int tim;		// Logical start-time relative to sequence start-time (samples)
  int stim;		// Real start-time, taking into account -ve offsets
  int etim;		// Real end-time
  double vol;		// Volume adjust for this note (1 for normal)
  NoteS *not0;		// Sample note to play, or 0
  NoteB *not1;		// Binaural note to play, or 0
};

struct VolSeg {
  VolSeg *nxt;
  int tim;		// Logical start time
  double vol;		// Volume adjust
  BaList *ba;		// Binaural segment, or 0
  EnvList *en;		// Envelope segment, or 0
};

struct PlayS {
  PlayS *nxt;
  int (*fun)(PlayS*);	// Play-handler
  double vol;		// Volume adjust, including mvol
  int tim;		// Start time from which en->tim times are measured
  uint off;		// 32-bit combined sample buffer offset and fraction
  uint max, dec;	// Maximum value for `off', after which it is reduced by `dec'
  int frb;		// Number of fractional bits in `off'
  int loop;		// Loop when `off' exceeds `max' ?  (else stop)
  EnvList *env;		// List of envelope segments to play
  int len;		// Length remaining to output current data, or 0
  int cnt, d_cnt;	// Current delta count, and period in samples
  double adj[3];	// Parameters for exponential slide of pitch adjust
  double amp[3];	// Parameters for exponential slide of amplitude (0-16384)
  double ampi;		// Increment adjust for amplitude
  void *dat;		// Sample data
};
  
struct PlayB {
  PlayB *nxt;
  double vol;		// Volume adjust, including mvol
  int tim;		// Start time for note (from which ba->tim times are measured)
  BaList *ba;		// List of binaural segments to play
  int len;		// Length remaining to output following data, or 0
  uint off1, off2;	// 32-bit phase offsets (sin_table[] offset << ST_FR)
  int cnt, d_cnt;	// Current delta count, and period in samples
  double carr[3];	// Parameters for exponential slide of carrier increment per sample
  double freq[3];	// Parameters for exponential slide of binaural increment per sample
  double amp[3];	// Parameters for exponential slide of amp (0-16384)
  double ampi;		// Increment adjust for amplitude
};


//
//	Sine table holds a list of calculated sine values for quick
//	lookup.  The offset is a 32-bit quantity: 14 bits of integer
//	part, 18 bits of fraction.  The values in the table are also
//	stored with 14 integer bits and 18 bits of fraction.  14
//	integer bits allow a range of 0-16384 for the amplitude.
//
int *sin_table= 0;
#define ST_FR 18	// Sin-table offset number of bits of fraction
#define ST_AMP 0x1FFFF	// Amplitude of wave in sin table (18 bits, leaving 14 bits for amplitude)
#define AMP_MAX	16384	// Maximum amplitude (to go with ST_AMP)

//
//	exp_table holds 16 octaves of exponential slide, with 300
//	points per octave - this means one point every 4/100ths of a
//	semitone, so slides to notes not an exact multiple of 4/100ths
//	of a semitone will miss at the end.  exp_table[0] always
//	==1.0.  The number of points per octave control the smoothness
//	of slides - too few, and the steps may be noticeable.  Note
//	that binaural frequencies are slid independently from the
//	carrier, so the large error of 0.2% in 1000Hz (2Hz error)
//	won't have any effect on the binaural frequencies generated,
//	which will be accurate to 0.2% of their own frequency.
//
double *exp_table= 0;
#define ET_PPO 300		// Points per octave
#define ET_OCT 16		// Octaves; 16 octaves more than covers the 
				//  range needed for a volume fade to 0

//
//	Globals
//

Note *notelist;		// List of defined notes
Sequence *seqlist;	// List of defined sequences
Event *event;		// List of decoded events for currently-rendering sequence

// Play-related
PlayS *playS;		// List of sample-based playing events
PlayB *playB;		// List of binaural-based playing events
Event *pev;		// Next event to play
int ptim;		// Next time to play (samples)
int petim;		// End time
int *pbuf= 0;		// Play buffer
int plen;		// Buffer size in samples (a sample is int[2])

// Overall
int rate;		// Base sample rate (for all time measurements, output, temp files, etc)
int delta_period;	// Minimum period for delta changes in samples (44 for 44.1kHz, 32 for 8000Hz)
int fade_period;	// Length in samples of time over which to fade sudden volume changes
int fade_ms;		// As above in ms
double bfreq;		// Base frequency for note scale
double mvol;		// Master volume adjust (0-1, typically)
double o_mvol;		// Master volume option; 0-1 typically, or -ve if a proportion
			//  of the automatic value
int tmpfil_cnt= 0;	// Count for generating temporary file names
int o_dump;		// Dump option selected ?




// END //

