| 12 | #include <vector> |
| 13 | |
| 14 | int main(int argc, char** argv) |
| 15 | { |
| 16 | #if defined(_WIN32) && __has_include(<winrt/base.h>) |
| 17 | // Necessary for using WinUWP and WinMIDI, must be done as early as possible in your main() |
| 18 | winrt::init_apartment(); |
| 19 | #endif |
| 20 | |
| 21 | if (argc < 2) |
| 22 | { |
| 23 | perror("Usage: ./midifile_dump <midifile.mid>"); |
| 24 | return 1; |
| 25 | } |
| 26 | |
| 27 | // Read raw from a MIDI file |
| 28 | std::ifstream file{argv[1], std::ios::binary}; |
| 29 | if (!file.is_open()) |
| 30 | { |
| 31 | std::cerr << "Could not open " << argv[1] << std::endl; |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | std::vector<uint8_t> bytes; |
| 36 | bytes.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()); |
| 37 | |
| 38 | // Initialize our reader object |
| 39 | libremidi::reader r{true}; |
| 40 | |
| 41 | // Parse |
| 42 | libremidi::reader::parse_result result = r.parse(bytes); |
| 43 | |
| 44 | switch (result) |
| 45 | { |
| 46 | case libremidi::reader::validated: |
| 47 | std::cout << "\nParsing validated\n\n"; |
| 48 | // Parsing has succeeded, all the input data is correct MIDI. |
| 49 | break; |
| 50 | |
| 51 | case libremidi::reader::complete: |
| 52 | std::cout << "\nParsing complete\n\n"; |
| 53 | // All the input data is parsed but the MIDI file was not necessarily strict SMF |
| 54 | // (e.g. there are empty tracks or tracks without END OF TRACK) |
| 55 | break; |
| 56 | |
| 57 | case libremidi::reader::incomplete: |
| 58 | std::cout << "\nParsing incomplete\n\n"; |
| 59 | // Not all the input could be parsed. For instance a track could not be read. |
| 60 | break; |
| 61 | |
| 62 | case libremidi::reader::invalid: |
| 63 | std::cout << "\nParsing invalid\n\n"; |
| 64 | // Nothing could be parsed, this is not MIDI data or we do not support it yet. |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | if (result != libremidi::reader::invalid) |
| 69 | { |
| 70 | long beatDuration = (long)(60. * 1'000'000. / r.startingTempo); // [usecs] |
| 71 | long tickDuration = beatDuration / r.ticksPerBeat; // [usecs] |
nothing calls this directly
no test coverage detected