home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / buzz1.zip / Dev / MachineInterface.h < prev   
C/C++ Source or Header  |  1998-03-29  |  9KB  |  323 lines

  1. // Copyright (C) 1998 Oskari Tammelin (ot@iki.fi)
  2. // This header file may be used to write _freeware_ DLL "machines" for Buzz or a BMX player
  3. // Using it for anything else is not allowed without a permission from the author
  4.    
  5. #ifndef __MACHINE_INTERFACE_H
  6. #define __MACHINE_INTERFACE_H
  7.  
  8. #include <stdio.h>
  9. #include <assert.h>
  10.  
  11. #define MI_VERSION                12
  12.   
  13. typedef unsigned char byte;
  14. typedef unsigned short word;
  15. typedef unsigned long dword;
  16.  
  17. double const PI = 3.14159265358979323846;
  18.  
  19. #define MAX_BUFFER_LENGTH        256            // in number of samples
  20.  
  21. // machine types
  22. #define MT_MASTER                0 
  23. #define MT_GENERATOR            1
  24. #define MT_EFFECT                2
  25.  
  26. // special parameter values
  27. #define NOTE_NO                    0
  28. #define NOTE_OFF                255
  29. #define NOTE_MIN                1                    // C-0
  30. #define NOTE_MAX                ((16 * 9) + 12)        // B-9
  31. #define SWITCH_OFF                0
  32. #define SWITCH_ON                1
  33. #define SWITCH_NO                255
  34. #define WAVE_MIN                1
  35. #define WAVE_MAX                200
  36. #define WAVE_NO                    0
  37.  
  38. // CMachineParameter flags
  39. #define MPF_WAVE                1
  40. #define MPF_STATE                2    
  41. #define MPF_TICK_ON_EDIT        4                
  42.  
  43. // CMachineInfo flags
  44.  
  45. #define MIF_MONO_TO_STEREO        1
  46. #define MIF_PLAYS_WAVES            2
  47.  
  48. // work modes
  49. #define WM_NOIO                    0
  50. #define WM_READ                    1
  51. #define WM_WRITE                2
  52. #define WM_READWRITE            3
  53.  
  54.  
  55. enum CMPType { pt_note, pt_switch, pt_byte, pt_word };
  56.  
  57. class CMachineParameter
  58. {
  59. public:
  60.  
  61.     CMPType Type;            // pt_byte
  62.     char const *Name;        // Short name: "Cutoff"
  63.     char const *Description;// Longer description: "Cutoff Frequency (0-7f)"
  64.     int MinValue;            // 0
  65.     int MaxValue;            // 127
  66.     int NoValue;            // 255
  67.     int Flags;
  68.     int DefValue;            // default value for params that have MPF_STATE flag set
  69. };
  70.  
  71. class CMachineAttribute
  72. {
  73. public:
  74.     char const *Name;
  75.     int MinValue;
  76.     int MaxValue;
  77.     int DefValue;
  78. };
  79.  
  80. class CMasterInfo
  81. {
  82. public:
  83.     int BeatsPerMin;        // [16..500]     
  84.     int TicksPerBeat;        // [1..32]
  85.     int SamplesPerSec;        // usually 44100, but machines should support any rate from 11050 to 96000
  86.     int SamplesPerTick;        // (int)((60 * SPS) / (BPM * TPB))  
  87.     int PosInTick;            // [0..SamplesPerTick-1]
  88.     float TicksPerSec;        // (float)SPS / (float)SPT  
  89.  
  90. };
  91.  
  92. // CWaveInfo flags
  93. #define WF_LOOP            1
  94.  
  95. class CWaveInfo
  96. {
  97. public:
  98.     int Flags;
  99.     float Volume;
  100.  
  101. };
  102.  
  103. class CWaveLevel
  104. {
  105. public:
  106.     int numSamples;
  107.     short *pSamples;
  108.     int RootNote;
  109.     int SamplesPerSec;
  110.     int LoopStart;
  111.     int LoopEnd;
  112. };
  113.  
  114. // oscillator waveforms (used with GetOscillatorTable function)
  115. #define OWF_SINE        0
  116. #define OWF_SAWTOOTH    1
  117. #define OWF_PULSE        2        // square 
  118. #define OWF_TRIANGLE    3
  119. #define OWF_NOISE        4    
  120.  
  121. // each oscillator table contains one full cycle of a bandlimited waveform at 11 levels
  122. // level 0 = 2048 samples  
  123. // level 1 = 1024 samples
  124. // level 2 = 512 samples
  125. // ... 
  126. // level 9 = 8 samples 
  127. // level 10 = 4 samples
  128. // level 11 = 2 samples
  129. //
  130. // the waves are normalized to 16bit signed integers   
  131. //
  132. // GetOscillatorTable retusns pointer to a table 
  133. // GetOscTblOffset returns offset in the table for a specified level 
  134.  
  135. inline int GetOscTblOffset(int const level)
  136. {
  137.     assert(level >= 0 && level <= 10);
  138.     return (2048+1024+512+256+128+64+32+16+8+4) & ~((2048+1024+512+256+128+64+32+16+8+4) >> level);
  139. }
  140.  
  141. class CPattern;
  142. class CSequence;
  143.  
  144. class CMICallbacks
  145. {
  146. public:
  147.     virtual CWaveInfo const *GetWave(int const i);
  148.     virtual CWaveLevel const *GetWaveLevel(int const i, int const level);
  149.     virtual void MessageBox(char const *txt);
  150.     virtual void Lock();
  151.     virtual void Unlock();
  152.     virtual int GetWritePos();            
  153.     virtual int GetPlayPos();    
  154.     virtual float *GetAuxBuffer();
  155.     virtual void ClearAuxBuffer();
  156.     virtual int GetFreeWave();
  157.     virtual bool AllocateWave(int const i, int const size, char const *name);
  158.     virtual void ScheduleEvent(int const time, dword const data);
  159.     virtual void MidiOut(int const dev, dword const data);
  160.     virtual short const *GetOscillatorTable(int const waveform);
  161.  
  162.     // envelopes
  163.     virtual int GetEnvSize(int const wave, int const env);
  164.     virtual bool GetEnvPoint(int const wave, int const env, int const i, word &x, word &y, int &flags);
  165.  
  166.     virtual CWaveLevel const *GetNearestWaveLevel(int const i, int const note);
  167.     
  168.     // pattern editing
  169.     virtual void SetNumberOfTracks(int const n);
  170.     virtual CPattern *CreatePattern(char const *name, int const length);
  171.     virtual CPattern *GetPattern(int const index);
  172.     virtual char const *GetPatternName(CPattern *ppat);
  173.     virtual void RenamePattern(char const *oldname, char const *newname);
  174.     virtual void DeletePattern(CPattern *ppat);
  175.     virtual int GetPatternData(CPattern *ppat, int const row, int const group, int const track, int const field);
  176.     virtual void SetPatternData(CPattern *ppat, int const row, int const group, int const track, int const field, int const value);
  177.          
  178.     // sequence editing
  179.     virtual CSequence *CreateSequence();
  180.     virtual void DeleteSequence(CSequence *pseq);
  181.     
  182.  
  183.     // special ppat values for GetSequenceData and SetSequenceData 
  184.     // empty = NULL
  185.     // <break> = (CPattern *)1
  186.     // <mute> = (CPattern *)2
  187.     // <thru> = (CPattern *)3
  188.     virtual CPattern *GetSequenceData(int const row);
  189.     virtual void SetSequenceData(int const row, CPattern *ppat);
  190.         
  191.      
  192. };
  193.  
  194. class CMachineInfo
  195. {
  196. public:
  197.     int Type;                                // MT_GENERATOR or MT_EFFECT
  198.     int Version;                            // MI_VERSION
  199.     int Flags;                
  200.     int minTracks;                            // [0..256] must be >= 1 if numTrackParameters > 0 
  201.     int maxTracks;                            // [minTracks..256] 
  202.     int numGlobalParameters;                
  203.     int numTrackParameters;                    
  204.     CMachineParameter const **Parameters;
  205.     int numAttributes;
  206.     CMachineAttribute const **Attributes;
  207.     char const *Name;                        // "Jeskola Reverb"
  208.     char const *ShortName;                    // "Reverb"
  209.     char const *Author;                        // "Oskari Tammelin"
  210.     char const *Commands;                    // "Command1\nCommand2\nCommand3..."
  211.  
  212. };
  213.  
  214. class CMachineDataInput
  215. {
  216. public:
  217.     virtual void Read(void *pbuf, int const numbytes);
  218.  
  219.     void Read(int &d) { Read(&d, sizeof(int)); }
  220.     void Read(dword &d) { Read(&d, sizeof(dword)); }
  221.     void Read(short &d) { Read(&d, sizeof(short)); }
  222.     void Read(word &d) { Read(&d, sizeof(word)); }
  223.     void Read(char &d) { Read(&d, sizeof(char)); }
  224.     void Read(byte &d) { Read(&d, sizeof(byte)); }
  225.     void Read(float &d) { Read(&d, sizeof(float)); }
  226.     void Read(double &d) { Read(&d, sizeof(double)); }
  227.     void Read(bool &d) { Read(&d, sizeof(bool)); }
  228.  
  229. };
  230.  
  231. class CMachineDataOutput
  232. {
  233. public:
  234.     virtual void Write(void *pbuf, int const numbytes);
  235.  
  236.     void Write(int d) { Write(&d, sizeof(int)); }
  237.     void Write(dword d) { Write(&d, sizeof(dword)); }
  238.     void Write(short d) { Write(&d, sizeof(short)); }
  239.     void Write(word d) { Write(&d, sizeof(word)); }
  240.     void Write(char d) { Write(&d, sizeof(char)); }
  241.     void Write(byte d) { Write(&d, sizeof(byte)); }
  242.     void Write(float d) { Write(&d, sizeof(float)); }
  243.     void Write(double d) { Write(&d, sizeof(double)); }
  244.     void Write(bool d) { Write(&d, sizeof(bool)); }
  245.  
  246. };
  247.  
  248. // envelope info flags
  249. #define EIF_SUSTAIN            1
  250. #define EIF_LOOP            2
  251.  
  252. class CEnvelopeInfo
  253. {
  254. public:
  255.     char const *Name;
  256.     int Flags;
  257. };
  258.  
  259. class CMachineInterface
  260. {
  261. public:
  262.     virtual ~CMachineInterface() {}
  263.     virtual void Init(CMachineDataInput * const pi) {}
  264.     virtual void Tick() {}
  265.     virtual bool Work(float *psamples, int numsamples, int const mode) { return false; }
  266.     virtual bool WorkMonoToStereo(float *pin, float *pout, int numsamples, int const mode) { return false; }
  267.     virtual void Stop() {}
  268.     virtual void Save(CMachineDataOutput * const po) {}
  269.     virtual void AttributesChanged() {}
  270.     virtual void Command(int const i) {}
  271.  
  272.     virtual void SetNumTracks(int const n) {}
  273.     virtual void MuteTrack(int const i) {}
  274.     virtual bool IsTrackMuted(int const i) const { return false; }
  275.  
  276.     virtual void MidiNote(int const channel, int const value, int const velocity) {}
  277.     virtual void Event(dword const data) {}
  278.  
  279.     virtual char const *DescribeValue(int const param, int const value) { return NULL; }
  280.  
  281.     virtual CEnvelopeInfo const **GetEnvelopeInfos() { return NULL; }
  282.  
  283.     virtual bool PlayWave(int const wave, int const note, float const volume) { return false; }
  284.     virtual void StopWave() {}
  285.     virtual int GetWaveEnvPlayPos(int const env) { return -1; }
  286.  
  287. public:
  288.     // initialize these members in the constructor 
  289.     void *GlobalVals;
  290.     void *TrackVals;
  291.     int *AttrVals;
  292.         
  293.     // these members are initialized by the 
  294.     // engine right after it calls CreateMachine()
  295.     // don't touch them in the constructor
  296.     CMasterInfo *pMasterInfo;
  297.     CMICallbacks *pCB;                    
  298.  
  299. };
  300.  
  301. class CMILock
  302. {
  303. public:
  304.     CMILock(CMICallbacks *p) { pCB = p; pCB->Lock(); }
  305.     ~CMILock() { pCB->Unlock(); }
  306. private:
  307.     CMICallbacks *pCB;
  308. };
  309.  
  310. #define MACHINE_LOCK CMILock __machinelock(pCB);
  311.  
  312. #define DLL_EXPORTS extern "C" { \
  313. __declspec(dllexport) CMachineInfo const * __cdecl GetInfo() \
  314. { \
  315.     return &MacInfo; \
  316. } \
  317. __declspec(dllexport) CMachineInterface * __cdecl CreateMachine() \
  318. { \
  319.     return new mi; \
  320. } \
  321.  
  322. #endif