| 43 | } |
| 44 | |
| 45 | int main(int argc, char* argv[]) |
| 46 | { |
| 47 | if (argc != 2) |
| 48 | { |
| 49 | std::println("Usage: {} <midi_file>", argv[0]); |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | // Load the midi file |
| 54 | std::ifstream file{argv[1], std::ios::binary}; |
| 55 | if (!file) |
| 56 | { |
| 57 | std::println("Error: Could not open file '{}'", argv[1]); |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | std::vector<uint8_t> bytes; |
| 62 | bytes.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()); |
| 63 | file.close(); |
| 64 | |
| 65 | libremidi::reader reader{true}; // Use absolute tick timing |
| 66 | auto result = reader.parse(bytes); |
| 67 | |
| 68 | if (result == libremidi::reader::invalid) |
| 69 | { |
| 70 | std::println("Error: Invalid MIDI file"); |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | // Process all tracks and collect note events |
| 75 | TrackData processed_track; |
| 76 | |
| 77 | std::flat_set<int64_t> dates; |
| 78 | for (const auto& track : reader.tracks) |
| 79 | { |
| 80 | for (const auto& event : track) |
| 81 | { |
| 82 | switch (event.m.get_message_type()) |
| 83 | { |
| 84 | case libremidi::message_type::NOTE_ON: { |
| 85 | int note = event.m[1]; |
| 86 | bool isOn = event.m[2] > 0; |
| 87 | |
| 88 | processed_track.events[note].push_back({event.tick, isOn}); |
| 89 | processed_track.max_tick = std::max(processed_track.max_tick, (int64_t)event.tick); |
| 90 | dates.insert(event.tick); |
| 91 | break; |
| 92 | } |
| 93 | case libremidi::message_type::NOTE_OFF: { |
| 94 | int note = event.m[1]; |
| 95 | bool isOn = false; |
| 96 | |
| 97 | processed_track.events[note].push_back({event.tick, isOn}); |
| 98 | processed_track.max_tick = std::max(processed_track.max_tick, (int64_t)event.tick); |
| 99 | break; |
| 100 | } |
| 101 | default: |
| 102 | continue; |