void hmp2mid(char *hmp_name, unsigned char **midbuf, unsigned int *midlen) { int mi, i; short ms, time_div = 0xC0; hmp_file *hmp=NULL; hmp = hmp_open(hmp_name); if (hmp == NULL) return; *midlen = 0; time_div = hmp->tempo*1.6; // write MIDI-header *midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + 4); memcpy(&(*midbuf)[*midlen], "MThd", 4); *midlen += 4; mi = MIDIINT(6); *midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + sizeof(mi)); memcpy(&(*midbuf)[*midlen], &mi, sizeof(mi)); *midlen += sizeof(mi); ms = MIDISHORT(1); *midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + sizeof(ms)); memcpy(&(*midbuf)[*midlen], &ms, sizeof(ms)); *midlen += sizeof(ms); ms = MIDISHORT(hmp->num_trks); *midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + sizeof(ms)); memcpy(&(*midbuf)[*midlen], &ms, sizeof(ms)); *midlen += sizeof(ms); ms = MIDISHORT(time_div); *midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + sizeof(ms)); memcpy(&(*midbuf)[*midlen], &ms, sizeof(ms)); *midlen += sizeof(ms); *midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + sizeof(tempo)); memcpy(&(*midbuf)[*midlen], &tempo, sizeof(tempo)); *midlen += sizeof(tempo); // tracks for (i = 1; i < hmp->num_trks; i++) { int midtrklenpos = 0; *midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + 4); memcpy(&(*midbuf)[*midlen], "MTrk", 4); *midlen += 4; midtrklenpos = *midlen; mi = 0; *midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + sizeof(mi)); *midlen += sizeof(mi); mi = hmptrk2mid(hmp->trks[i].data, hmp->trks[i].len, midbuf, midlen); mi = MIDIINT(mi); memcpy(&(*midbuf)[midtrklenpos], &mi, 4); } hmp_close(hmp); }
void hmp2mid(hmp_file *hmp, unsigned char **midbuf, unsigned int *midlen) { int mi, i, j; short ms, time_div = 0xC0; *midlen = 0; time_div = hmp->tempo*1.6; // write MIDI-header *midbuf = (unsigned char *) malloc(4); memcpy(&(*midbuf)[*midlen], "MThd", 4); *midlen += 4; mi = swapint(6); *midbuf = (unsigned char *) realloc(*midbuf, *midlen + sizeof(mi)); memcpy(&(*midbuf)[*midlen], &mi, sizeof(mi)); *midlen += sizeof(mi); ms = swapshort(1); *midbuf = (unsigned char *) realloc(*midbuf, *midlen + sizeof(ms)); memcpy(&(*midbuf)[*midlen], &ms, sizeof(ms)); *midlen += sizeof(ms); ms = swapshort(hmp->num_trks); *midbuf = (unsigned char *) realloc(*midbuf, *midlen + sizeof(ms)); memcpy(&(*midbuf)[*midlen], &ms, sizeof(ms)); *midlen += sizeof(ms); ms = swapshort(time_div); *midbuf = (unsigned char *) realloc(*midbuf, *midlen + sizeof(ms)); memcpy(&(*midbuf)[*midlen], &ms, sizeof(ms)); *midlen += sizeof(ms); *midbuf = (unsigned char *) realloc(*midbuf, *midlen + sizeof(tempo)); memcpy(&(*midbuf)[*midlen], &tempo, sizeof(tempo)); *midlen += sizeof(tempo); // tracks for (i = 1; i < hmp->num_trks; i++) { int midtrklenpos = 0; *midbuf = (unsigned char *) realloc(*midbuf, *midlen + 4); memcpy(&(*midbuf)[*midlen], "MTrk", 4); *midlen += 4; midtrklenpos = *midlen; mi = 0; *midbuf = (unsigned char *) realloc(*midbuf, *midlen + sizeof(mi)); *midlen += sizeof(mi); mi = hmptrk2mid(hmp->trks[i].data, hmp->trks[i].len, midbuf, midlen, hmp->trks); mi = swapint(mi); memcpy(&(*midbuf)[midtrklenpos], &mi, 4); } }