1
0

utils.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #if !defined(_UTILS_H_INCLUDED_)
  2. #define _UTILS_H_INCLUDED_
  3. #include "../pfc/pfc.h"
  4. class NOVTABLE CStream
  5. {
  6. public:
  7. virtual UINT ReadData(void*,UINT,bool*)=0;
  8. virtual void Flush()=0;
  9. virtual ~CStream() {};
  10. //for sampling
  11. virtual void Pause(int) {};
  12. virtual void Eof() {}
  13. };
  14. class CPipe : public CStream
  15. {
  16. BYTE* buf;
  17. volatile UINT buf_s,buf_n,buf_rp,buf_wp;
  18. critical_section sec;
  19. UINT align;
  20. volatile bool closed;
  21. public:
  22. void WriteData(void*,UINT);
  23. UINT CanWrite() {return buf_s-buf_n;}
  24. UINT ReadData(void*,UINT,bool*);
  25. void Flush()
  26. {
  27. sec.enter();
  28. buf_n=0;
  29. sec.leave();
  30. }
  31. CPipe(UINT _align=4,UINT freq=44100)
  32. {
  33. buf_s=MulDiv(1024*256,freq,22050);
  34. buf=(BYTE*)malloc(buf_s);
  35. buf_wp=buf_rp=0;
  36. buf_n=0;
  37. align=_align;
  38. closed=0;
  39. }
  40. ~CPipe()
  41. {
  42. if (buf) free(buf);
  43. }
  44. void Eof() {closed=1;}
  45. };
  46. class MIDI_file;
  47. #pragma pack(push)
  48. #pragma pack(1)
  49. typedef struct
  50. {
  51. WORD fmt,trax,dtx;
  52. } MIDIHEADER;
  53. #pragma pack(pop)
  54. WORD _inline rev16(WORD x) {return (x>>8)|(x<<8);}
  55. //#define rev16(X) (((X)&0xFF)<<8)|(((X)>>8)&0xFF)
  56. DWORD _fastcall rev32(DWORD);
  57. #define _rv(X) ((((DWORD)(X)&0xFF)<<24)|(((DWORD)(X)&0xFF00)<<8)|(((DWORD)(X)&0xFF0000)>>8)|(((DWORD)(X)&0xFF000000)>>24))
  58. #define FixHeader(H) {(H).fmt=rev16((H).fmt);(H).trax=rev16((H).trax);(H).dtx=rev16((H).dtx);}
  59. struct write_buf;
  60. typedef struct
  61. {
  62. int pos,tm;
  63. } TMAP_ENTRY;
  64. struct CTempoMap
  65. {
  66. public:
  67. TMAP_ENTRY *data;
  68. int pos,size;
  69. void AddEntry(int _p,int tm);
  70. ~CTempoMap() {if (data) free(data);}
  71. int BuildTrack(grow_buf & out);
  72. };
  73. CTempoMap* tmap_merge(CTempoMap*,CTempoMap*);
  74. typedef struct
  75. {
  76. int pos,ofs,len;
  77. } SYSEX_ENTRY;
  78. struct CSysexMap
  79. {
  80. public:
  81. DWORD d_size,e_size;
  82. SYSEX_ENTRY *events;
  83. BYTE* data;
  84. int pos,d_pos;
  85. void CleanUp();
  86. void AddEvent(const BYTE* e,DWORD s,DWORD t);
  87. ~CSysexMap();
  88. CSysexMap* Translate(MIDI_file* _mf);//MIDI_file* mf
  89. int BuildTrack(grow_buf & out);
  90. const char* GetType();
  91. };
  92. typedef struct tagKAR
  93. {
  94. UINT time;
  95. UINT start,end;
  96. BOOL foo;
  97. } KAR_ENTRY;
  98. KAR_ENTRY * kmap_create(MIDI_file* mf,UINT prec,UINT * num,char** text);
  99. CTempoMap* tmap_create();
  100. CSysexMap* smap_create();
  101. int EncodeDelta(BYTE* dst,int d);
  102. unsigned int DecodeDelta(const BYTE* src,unsigned int* _d, unsigned int limit=-1);
  103. int ReadSysex(const BYTE* src,int ml);
  104. char* BuildFilterString(UINT res_id, char* ext, int* len);
  105. BOOL DoOpenFile(HWND w,char* fn,UINT res_id,char* ext,BOOL save);
  106. typedef void (*SYSEXFUNC)(void*,BYTE*,UINT);
  107. void sysex_startup(SYSEXFUNC,void*);
  108. void sysex_startup_midiout(UINT m_id);
  109. bool need_sysex_start();
  110. typedef struct
  111. {
  112. DWORD tm;
  113. DWORD ev;
  114. } MIDI_EVENT;
  115. MIDI_EVENT* do_table(MIDI_file* mf,UINT prec,UINT * size,UINT* _lstart,DWORD cflags);
  116. void gb_write_delta(grow_buf & gb,DWORD d);
  117. void do_messages(HWND w,bool* br);
  118. ATOM do_callback_class(WNDPROC p);
  119. HWND create_callback_wnd(ATOM cl,void* p);
  120. class sysex_table
  121. {
  122. private:
  123. struct entry
  124. {
  125. entry * next;
  126. int size,time;
  127. BYTE * data;
  128. };
  129. entry * entries;
  130. enum {MHP_MAGIC='0PHM'};
  131. public:
  132. sysex_table() {entries=0;}
  133. ~sysex_table() {reset();}
  134. int num_entries() const;
  135. int get_entry(int idx,BYTE ** p_data,int * p_size,int * p_time) const;
  136. void insert_entry(int idx,BYTE * data,int size,int time);
  137. int remove_entry(int idx);
  138. inline void add_entry(BYTE * data,int size,int time) {insert_entry(num_entries(),data,size,time);}
  139. inline void modify_entry(int idx,BYTE * data,int size,int time) {remove_entry(idx);insert_entry(idx,data,size,time);}
  140. inline void reset() {while(entries) remove_entry(0);}
  141. inline int get_time(int idx) const {int time;return get_entry(idx,0,0,&time) ? time : 0;}
  142. int file_read(const char * path);
  143. int file_write(const char * path) const;
  144. void * memblock_write(int * size) const;
  145. int memblock_read(const void * ptr,int size);
  146. int print_preview(int idx,char * out) const;
  147. void print_edit(int idx,HWND wnd) const;
  148. void copy(const sysex_table & src);
  149. sysex_table(const sysex_table & src) {entries=0;copy(src);}
  150. sysex_table& operator=(const sysex_table & src) {copy(src);return *this;}
  151. int is_empty() {return !entries;}
  152. };
  153. #endif