/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#ifndef __OPENSPC_H__
#define __OPENSPC_H__

/*
  MAIN
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>

/*
  EMU
*/

extern unsigned char SPC_DSP_Buffer[128];
extern unsigned char SPCRAM[65536];
extern unsigned long SPCcyc,TotalCycles,CycleLatch0,CycleLatch1,CycleLatch2,
 SPC_TIMER0,SPC_TIMER1,SPC_TIMER2;
extern unsigned char SPC_CTRL,SPC_TIMT0,SPC_TIMT1,SPC_TIMT2;


extern int SPCtime;
extern char SPCname[32],SPCtname[32],SPCdname[16],SPCcomment[32],SPCdate[10];

int SPCInit(char *);


/*
  IT
*/

#define NUM_PATT_BUFS 3

typedef struct {
	int mask,pitch,lvol,rvol;
        unsigned char note;
} itdata;

extern itdata ITdata[8];
//Temp memory for patterns before going to file
extern unsigned char *ITpattbuf[NUM_PATT_BUFS];
extern int ITpattlen[NUM_PATT_BUFS];	//lengths of each pattern
extern int ITlastmasks[16];		//Used for pattern compression
extern int ITcurbuf,ITbufpos,ITcurrow;	//Pointers into temp pattern buffers
extern int ITrows;			//Number of rows per pattern
extern int ITdump;

int ITStart(void);	//Opens temp file, inits writing
int ITUpdate(void);	//Dumps pattern buffers to file
int ITWrite(char *fn);	//Stops recording and writes IT file from temp data


/*
  SOUND
*/

#define ATTACK 0	//A of ADSR
#define DECAY 1		//D of ADSR
#define SUSTAIN 2	//S of ADSR
#define RELEASE 3	//R of ADSR
#define DECREASE 4	//GAIN linear decrease mode
#define EXP 5		//GAIN exponential decrease mode
#define INCREASE 6	//GAIN linear increase mode
#define BENT 7		//GAIN bent line increase mode
#define DIRECT 8	//Directly specify ENVX

typedef struct {
int length;
int loopto;
short *buf;
int freq;
} sndsamp;

typedef struct {
	sndsamp *cursamp;
        long sampptr;
        int ave;
        unsigned int envx,envcyc;
        int envstate;
	int ar,dr,sl,sr,gn;
} sndvoice;

extern sndsamp *SNDsamples[100];
extern sndvoice SNDvoices[8];
extern void *SNDoptbuf;
extern int SNDfreq,SNDurate,SNDkeys,SNDvmask,SNDmixlen,SNDrvs;
extern int SNDlevl,SNDlevr;
extern unsigned char SNDbits;
extern int SNDratecnt;
extern int SNDmix;
extern int SNDcrc;

#define sd8bit   1
#define sdStereo 2

void SNDKillSamp(sndsamp *);
int SNDPitchToNote(int,int);
int SNDDoEnv(int);
void SNDMix(void);
void SNDNoteOn(unsigned char);
void SNDNoteOff(unsigned char);
int SNDInit(int,int,int,int,int);

#endif
