How to calculate the time-length of a midi-file

百般思念 提交于 2020-01-24 11:19:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!