| Niels Meijer (5) | ||||
|
Hi everybody, I have some troubles with the well-known MIDI-protocol. I am trying to make some kind of program that is able to generically generate .midi music. It is not that complicated yet, so do not bother with it, in fact I am just running tests right now. I have this problem when I try to play a self generated MIDI-file with only one note. I was able to copy a existing midi in code (rdbuf()), so it is not my compiler's fault (Dev-C++ 4.9.9.2). I would rather not work with MIDI-libraries so as to only use my own code. After some hard work on the Internet i finally managed to get the information I needed about MIDI on byte level. I made the midi with fstream and afterwards I wrote a few lines C++ to test if I could correctly apply my newly aquired knowledge. This is the program:
in file txt.txt:
I started with the header chunk, then a track chunk, a note on and a note off event, and a end-of-track meta-event. I have no idea where the large number at the end comes from, is it the cause of all the trouble? When i open MIDI.mid with Notepad, the number 0xffffffff is not represented in Unicode-character, so I cannot get it out of the file. I guess it is just a glitch. The problem occurs when I try to play the file. Windows Media Player starts complaining about insufficient memory so I guess I forgot some kind of EOF event. I also tried Synthesia, becuase I know Windows long enough, but it also refuses to play my file... For the interested, my knowledge originates form this lovely tutorial: http://www.sonicspot.com/guide/midifiles.html if you see the error, PLEASE contact me. C++ is my hobby, I learned C++ from a book, so I do not have a teacher to answer my questions. You people are my only hope... Atleast thanks for reading! Kind regards, Niels Meijer | ||||
|
|
||||
| helios (10126) | ||
get() returns -1 when the stream reaches the EOF. fail() doesn't check if the stream has reached the EOF. Check good(), instead.
| ||
|
|
||
| Niels Meijer (5) | ||||
Wow, I didn't expect such a fast answer! I took your advice and to make a long story short here is the complete sourcecode of my program:
MIDI.mid remains unplayable so I guess the fault lies in my MIDI-syntax. After uncommenting the forth line I placed '//' before line 5. This is the content of txt.txt:
Where in the world does 0xffffffff come from? Did I apply good() correctly? Although I still can't play MIDI.mid, nevertheless thanks for your fast answer! | ||||
|
|
||||
| helios (10126) | |||
I guess get() only sets the EOF flag if it's called while the stream is at the EOF, and not if it returns while at the EOF. I swear, the C I/O semantics must have been designed by an inhabitant of the Backwards Dimension.
Personally, unless I have reasons not to (memory constraints, for example), I just open everything as binary and load entire files to memory. Saves me the trouble of dealing with this backwards BS, and it's more efficient. | |||
|
|
|||
| Niels Meijer (5) | |
|
Ah! the number at the end has disappeared! I only had to use an int instead of a char, because char would put ASCII graphs instead of hexadecimal numbers in txt.txt. Thanks again! And I agree about the semantics designers. Ok, so now my C++ code is fine, but does anyone know what is wrong with the MIDI syntax? I still can't play the file... The MIDI protocol is so simple, and yet capable of fustrating newbies like me this much! | |
|
|
|