#include #include #include typedef unsigned int ui; char * oc, * oe, * bb, * suspense; void pc(char x){if(oc==oe) {printf("Bye\n"); exit(4);} *(oc++) = x;} void ps(char * s){while(*s) pc(*(s++));} void pn(int c, ui v){if(c) {pn(c-1, v>>8); pc(v&255);}} void pvlr(ui n){if(n > 127) pvlr(n>>7); pc(0x80 | n&127);} void pvl(ui n){if(n > 127) pvlr(n>>7); pc(n&127);} void n(int n, int d){pc(n); pc(64); pvl(d*120); pc(n); pc(0); pvl(0);} void te(char * t){pc(0xFF); pc(1); pvl(strlen(t)); ps(t);} void ntrk(){ps("MTrk"); suspense = oc; pn(4, 0);} void closeTrk(){pc(0xFF); pc(47); pvl(0); {char*t = oc; oc = suspense; pn(4, t-(oc+4)); oc = t;}} void tune(){ n(60,2); n(62,2); n(64,2); n(60,2); n(60,2); n(62,2); n(64,2); n(60,2); n(64,2); n(65,2); n(67,4); n(64,2); n(65,2); n(67,4); n(67,1); n(69,1); n(67,1); n(65,1); n(64,2); n(60,2); n(67,1); n(69,1); n(67,1); n(65,1); n(64,2); n(60,2); n(60,2); n(55,2); n(60,4); n(60,2); n(55,2); n(60,4); } int main(){ {int sp = 1000000; bb=oc = (char*)malloc(sp); if(!oc) {printf("Poo\n"); exit(4);} oe = oc+sp;} ps("MThd"); pn(4, 6); pn(2, 1); pn(2, 3); pn(2, 240); ntrk(); pvl(0); te("Hello world: Frere Jacques"); pvl(0); closeTrk(); ntrk(); pvl(0); pc(0xc0); pc(1); // voice pvl(0); pc(0x90); // note down, channel 1 tune(); closeTrk(); ntrk(); {int ch = 0; // works for 1; fails for 0 pvl(0); pc(0xc0+ch); pc(13); // voice pvl(16*120); pc(0x90+ch); // note down, channel 1 } tune(); closeTrk(); {FILE * out = fopen("song.midi", "w"); {char * c; for(c = bb; c < oc; ++c) putc(*c, out);} fclose(out);} free(bb); return 0;}