问题
I am reading midi files in as3 (flash cs5) with the help of the helpful library that is called midas ( http://code.google.com/p/midas3/) - the midi-as3 library.
I am trying to figure out a simple way to calculate the whole duration of the midi file (for example - total time of 4 minutes or 6 minutes...). I assume I could calculate the last note of each track + check the tempo and figure it out, but I was wondering if:
Is the duration of the midi file is written somewhere in the data that I could just pull out and use?
or
Is there an easy way to calculate it without running through the whole file and compare last-notes/tempos.
回答1:
Nope, you need to read the entire file and determine the time when you read the last note. MIDI files are essentially streaming data, so there is no "length" field in the file's header.
Edit: Upon further thought, "streaming" isn't exactly a great way to describe MIDI files. MIDI files do have a fixed length in bytes, which is is stored in the IFF chunk header. However, there is no property as for the length of the file in seconds, but assuming that you can read all of the bytes into a sequence (and don't forget to take tempo changes into account!), it should not be too difficult to determine the length of the file in seconds.
来源:https://stackoverflow.com/questions/6037534/how-to-calculate-the-time-length-of-a-midi-file