gmf.cpp 694 B

123456789101112131415161718192021222324252627
  1. #include "main.h"
  2. #include "cvt.h"
  3. bool is_gmf(const BYTE* p,size_t s)
  4. {
  5. return s>0x20 && *(DWORD*)p==_rv('GMF\x01');
  6. }
  7. bool load_gmf(MIDI_file * mf,const BYTE* buf,size_t siz)
  8. {
  9. grow_buf wb;
  10. wb.write_dword(_rv('MThd'));
  11. wb.write_dword(_rv(6));
  12. MIDIHEADER h={0x000,0x100,0xC000};
  13. wb.write(&h,6);
  14. wb.write_dword(_rv('MTrk'));
  15. int tempo=100000*rev16(*(WORD*)(buf+4));
  16. wb.write_dword(rev32(siz-9+8+3));//MTrk size
  17. BYTE tempo_event[8]={0,0xFF,0x51,0x03,(BYTE)((tempo>>16)&0xFF),(BYTE)((tempo>>8)&0xFF),(BYTE)(tempo&0xFF),0};
  18. wb.write(tempo_event,8);
  19. wb.write(buf+8,siz-9);
  20. wb.write("\xFF\x2F\x00",3);
  21. mf->size = wb.get_size();
  22. mf->data = (BYTE*)wb.finish();
  23. return !!mf->data;
  24. }