cmf.cpp 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "main.h"
  2. #include "cvt.h"
  3. bool is_cmf(const BYTE* buf,size_t s)
  4. {
  5. return s>0x20 && *(DWORD*)buf == _rv('CTMF');
  6. }
  7. static BYTE tempodat[7]={0,0xFF,0x51,0x03,0,0,0};
  8. bool load_cmf(MIDI_file * mf,const BYTE* ptr,size_t sz)
  9. {
  10. if (sz < 14)
  11. return 0;
  12. WORD music_offset = *(WORD*)(ptr+8);
  13. if ((size_t)music_offset > sz)
  14. return 0;
  15. const BYTE* _t=ptr+music_offset;
  16. size_t total_size=sz - music_offset;
  17. mf->size=14+8+7+total_size;
  18. BYTE* _pt=(BYTE*)malloc(mf->size);
  19. if (!_pt) return 0;
  20. mf->data=_pt;
  21. *(DWORD*)_pt=_rv('MThd');
  22. _pt+=4;
  23. *(DWORD*)_pt=_rv(6);
  24. _pt+=4;
  25. *(WORD*)_pt=0;
  26. _pt+=2;
  27. *(WORD*)_pt=0x0100;
  28. _pt+=2;
  29. *(WORD*)_pt=rev16(*(WORD*)(ptr+10));
  30. _pt+=2;
  31. *(DWORD*)_pt=_rv('MTrk');
  32. _pt+=4;
  33. *(DWORD*)_pt=rev32(total_size+7);
  34. _pt+=4;
  35. DWORD tm=(DWORD)(48000000/(*(WORD*)(ptr+12)));
  36. tempodat[4]=(BYTE)((tm>>16)&0xFF);
  37. tempodat[5]=(BYTE)((tm>>8)&0xFF);
  38. tempodat[6]=(BYTE)(tm&0xFF);
  39. memcpy(_pt,tempodat,7);
  40. _pt+=7;
  41. memcpy(_pt,_t,total_size);
  42. return 1;
  43. }