Ian said:
Could be the offset is a multiple of a buffer size, more detail required
for anyone to help, post some code.
Ian
Okay, here is the main bulk of the code:
FILE * pInFile;
FILE * pOutFile;
pInFile = fopen(s_inFile, "rb");
pOutFile = fopen(s_outFile, "wb");
// bool flags
bool bFlag = 0;
do {
// read a byte
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
// if we have a note-on status message
if (szBuffer[0] == szNoteOn[0]) {
// write the byte
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
// prepare for while loop
bFlag = 0;
// deal with notes marked after note_on message
do {
// convert note
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
gmToDKFH(&szBuffer[0]);
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
// convert velocity
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
// if velocity is >= 95
if (strcmp(&szBuffer[0], &szVelTarget[0]) >= 0) {
// if we're not going to change the hihats later, or this isn't a hihat
if ((!b_useHihat) || (strcmp(&szBuffer[0], &szOpenHat[0])) &&
(strcmp(&szBuffer[0], &szClosedHat[0]))) {
// set new velocity
szBuffer[0] = (char) 0x7f;
}
}
// write the velocity
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
// if the next byte is a null, write and go back through the loop
// if the next byte is a all notes off status message, write and
exit the loop
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
if (szBuffer[0] == szAllNotesOff[0]) {
bFlag = 1;
}
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
}
while (!bFlag);
// the next byte should be a note_off status message (0x89)
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
// prepare for while loop
bFlag = 0;
// deal with note messages after status message
do {
// convert note and write
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
gmToDKFH(&szBuffer[0]);
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
// convert velocity
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
// if velocity is >= 80
if (strcmp(&szBuffer[0], &szVelTargetOff[0]) >= 0) {
// if we're not going to change the hihats later, or this isn't a hihat
if ((!b_useHihat) || (strcmp(&szBuffer[0], &szOpenHat[0])) &&
(strcmp(&szBuffer[0], &szClosedHat[0]))) {
// set new velocity
szBuffer[0] = (char) 0x7f;
}
}
// write velocity
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
// this byte should be a null, so write it
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
// if we're back at a note_on message, exit loop and push the byte
back into the stream
// if we're at a 0xFF message, we're almost at the end of the file,
so write the byte and exit the loop
// otherwise, push the byte back into the stream and go through the
loop again
dwRead = fread(&szBuffer[0], 1, 1, pInFile);
if (szBuffer[0] == szNoteOn[0]) {
bFlag = 1;
ungetc(szBuffer[0], pInFile);
}
else if (szBuffer[0] == szEOFMarker[0]) {
bFlag = 1;
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
ungetc(szBuffer[0], pInFile);
}
else {
ungetc(szBuffer[0], pInFile);
}
}
while (!bFlag);
}
// if we don't have a note-on, go back through the loop
else {
fwrite(&szBuffer[0], 1, dwRead, pOutFile);
}
}
// exit when we get to the end of the file
while (dwRead > 0);
fclose(pInFile);
fclose(pOutFile);
--------------------
the "gmToDKFH()" function just takes a byte from the buffer and changes
it according to the midi standard I'm converting to.
Here are some of the relevent strings/chars I'm comparing in the program:
// bytes read
size_t dwRead;
// single char file buffer
char szBuffer[2];
szBuffer[1] = 0x00;
// constants
char szNoteOn[2];
sprintf(&szNoteOn[0], "%c", 0x99);
szNoteOn[1] = 0x00;
char szNoteOnMin[2];
sprintf(&szNoteOnMin[0], "%c", MIDI_CMD_NOTE_ON);
szNoteOnMin[1] = 0x00;
char szNoteOnMax[2];
sprintf(&szNoteOnMax[0], "%c", 0x9F);
szNoteOnMax[1] = 0x00;
char szNoteOffMin[2];
sprintf(&szNoteOffMin[0], "%c", MIDI_CMD_NOTE_OFF);
szNoteOffMin[1] = 0x00;
char szNoteOffMax[2];
sprintf(&szNoteOffMax[0], "%c", 0x8F);
szNoteOffMax[1] = 0x00;
char szAllNotesOff[2];
sprintf(&szAllNotesOff[0], "%c", MIDI_CTL_ALL_SOUNDS_OFF);
szAllNotesOff[1] = 0x00;
// velocity 95
char szVelTarget[2];
sprintf(&szVelTarget[0], "%c", 0x5f);
szVelTarget[1] = 0x00;
char szVelTargetOff[2];
sprintf(&szVelTargetOff[0], "%c", 0x50);
szVelTargetOff[1] = 0x00;
// hihat constants
char szOpenHat[2];
sprintf(&szOpenHat[0], "%c", 0x43);
szOpenHat[1] = 0x00;
char szClosedHat[2];
sprintf(&szClosedHat[0], "%c", 0x3e);
szClosedHat[1] = 0x00;
// 0xFF eof marker for midi
char szEOFMarker[2];
sprintf(&szEOFMarker[0], "%c", 0xff);
szEOFMarker[1] = 0x00;
// null target
char szNull[2];
sprintf(&szNull[0], "%c", 0x00);
szNull[1] = 0x00;
-------------------
If any explanation or further code is needed, I'd be happy to provide.
Thanks