|
| 1 | +// |
| 2 | +// Created by Syl Morrison on 29/11/2025. |
| 3 | +// |
| 4 | +#include <mostly_harmless/events/mostlyharmless_MidiEvent.h> |
| 5 | +#include <catch2/catch_test_macros.hpp> |
| 6 | +#include <catch2/matchers/catch_matchers_floating_point.hpp> |
| 7 | +namespace mostly_harmless::testing { |
| 8 | + struct MidiMessage { |
| 9 | + std::uint8_t b0; |
| 10 | + std::uint8_t b1; |
| 11 | + std::uint8_t b2; |
| 12 | + }; |
| 13 | + template <typename Expected> |
| 14 | + static auto check_result(std::optional<events::midi::MidiEvent> to_check) -> void { |
| 15 | + REQUIRE(to_check); |
| 16 | + REQUIRE(std::holds_alternative<Expected>(*to_check)); |
| 17 | + } |
| 18 | + TEST_CASE("Test Midi Status Bytes") { |
| 19 | + SECTION("Test NoteOff") { |
| 20 | + const MidiMessage msg{ 0b10000000, 36, 0 }; |
| 21 | + const auto res_opt = events::midi::parse(msg.b0, msg.b1, msg.b2); |
| 22 | + check_result<events::midi::NoteOff>(res_opt); |
| 23 | + const auto [channel, note, velocity] = std::get<events::midi::NoteOff>(*res_opt); |
| 24 | + REQUIRE(channel == 0); |
| 25 | + REQUIRE(note == 36); |
| 26 | + REQUIRE_THAT(velocity, Catch::Matchers::WithinRel(0.0)); |
| 27 | + } |
| 28 | + SECTION("Test NoteOn") { |
| 29 | + const MidiMessage msg{ 0b10010000, 36, 127 }; |
| 30 | + const auto res_opt = events::midi::parse(msg.b0, msg.b1, msg.b2); |
| 31 | + check_result<events::midi::NoteOn>(res_opt); |
| 32 | + const auto [channel, note, velocity] = std::get<events::midi::NoteOn>(*res_opt); |
| 33 | + REQUIRE(channel == 0); |
| 34 | + REQUIRE(note == 36); |
| 35 | + REQUIRE_THAT(velocity, Catch::Matchers::WithinRel(1.0f)); |
| 36 | + } |
| 37 | + SECTION("Test PolyAftertouch") { |
| 38 | + const MidiMessage msg{ 0b10100001, 60, 127 }; |
| 39 | + const auto res_opt = events::midi::parse(msg.b0, msg.b1, msg.b2); |
| 40 | + check_result<events::midi::PolyAftertouch>(res_opt); |
| 41 | + const auto [channel, note, pressure] = std::get<events::midi::PolyAftertouch>(*res_opt); |
| 42 | + REQUIRE(channel == 1); |
| 43 | + REQUIRE(note == 60); |
| 44 | + REQUIRE(pressure == 127); |
| 45 | + } |
| 46 | + SECTION("Test ControlChange") { |
| 47 | + const MidiMessage msg{ 0b10110001, 0, 127 }; |
| 48 | + const auto res_opt = events::midi::parse(msg.b0, msg.b1, msg.b2); |
| 49 | + check_result<events::midi::ControlChange>(res_opt); |
| 50 | + const auto [channel, controllerNumber, value] = std::get<events::midi::ControlChange>(*res_opt); |
| 51 | + REQUIRE(channel == 1); |
| 52 | + REQUIRE(controllerNumber == 0); |
| 53 | + REQUIRE(value == 127); |
| 54 | + } |
| 55 | + SECTION("Test ProgramChange") { |
| 56 | + const MidiMessage msg{ 0b11000000, 2, 0 }; |
| 57 | + const auto res_opt = events::midi::parse(msg.b0, msg.b1, msg.b2); |
| 58 | + check_result<events::midi::ProgramChange>(res_opt); |
| 59 | + const auto [channel, program] = std::get<events::midi::ProgramChange>(*res_opt); |
| 60 | + REQUIRE(channel == 0); |
| 61 | + REQUIRE(program == 2); |
| 62 | + } |
| 63 | + SECTION("Test ChannelAftertouch") { |
| 64 | + const MidiMessage msg{ 0b11010010, 8, 0 }; |
| 65 | + const auto res_opt = events::midi::parse(msg.b0, msg.b1, msg.b2); |
| 66 | + check_result<events::midi::ChannelAftertouch>(res_opt); |
| 67 | + const auto [channel, pressure] = std::get<events::midi::ChannelAftertouch>(*res_opt); |
| 68 | + REQUIRE(channel == 2); |
| 69 | + REQUIRE(pressure == 8); |
| 70 | + } |
| 71 | + SECTION("Test PitchWheel") { |
| 72 | + const MidiMessage msg{ 0b11100000, 0x00, 0x40 }; |
| 73 | + const auto res_opt = events::midi::parse(msg.b0, msg.b1, msg.b2); |
| 74 | + check_result<events::midi::PitchWheel>(res_opt); |
| 75 | + const auto [channel, value] = std::get<events::midi::PitchWheel>(*res_opt); |
| 76 | + REQUIRE(channel == 0); |
| 77 | + REQUIRE_THAT(value, Catch::Matchers::WithinRel(0.0)); |
| 78 | + } |
| 79 | + |
| 80 | + SECTION("Test 0 Velocity Note-On") { |
| 81 | + const MidiMessage msg{ 0b10010000, 40, 0 }; |
| 82 | + const auto res_opt = events::midi::parse(msg.b0, msg.b1, msg.b2); |
| 83 | + check_result<events::midi::NoteOff>(res_opt); |
| 84 | + const auto [channel, note, velocity] = std::get<events::midi::NoteOff>(*res_opt); |
| 85 | + REQUIRE(channel == 0); |
| 86 | + REQUIRE(note == 40); |
| 87 | + REQUIRE_THAT(velocity, Catch::Matchers::WithinRel(0.0)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} // namespace mostly_harmless::testing |
0 commit comments