Skip to content

Commit c2cf61e

Browse files
committed
midi 2 conversion: fix midi2 to midi1, add an example
fixes #160
1 parent 69aeec9 commit c2cf61e

4 files changed

Lines changed: 46 additions & 6 deletions

File tree

cmake/libremidi.examples.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_example(midiobserve)
1717
add_example(echo)
1818
add_example(cmidiin)
1919
add_example(cmidiin2)
20+
add_example(midi1_to_midi2)
2021
add_example(midiclock_in)
2122
add_example(midiclock_out)
2223
add_example(midiout)

examples/midi1_to_midi2.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "utils.hpp"
2+
3+
#include <libremidi/backends.hpp>
4+
#include <libremidi/libremidi.hpp>
5+
6+
#include <iostream>
7+
8+
/**
9+
* This example shows upscale of a MIDI 1 API into MIDI 2 messages
10+
*/
11+
12+
int main()
13+
try
14+
{
15+
// The observer object enumerates available inputs and outputs
16+
// jack is a MIDI 1 API (there's also jack_ump which leverages the newer
17+
// JACK / PipeWire MIDI 2 support).
18+
libremidi::observer obs{{}, libremidi::jack_observer_configuration{}};
19+
auto pi = obs.get_input_ports();
20+
auto po = obs.get_output_ports();
21+
if (pi.empty() || po.empty())
22+
throw std::runtime_error("No MIDI in / out pair available");
23+
24+
// Create a midi in
25+
auto on_ump = [&](const libremidi::ump& message) { std::cerr << message << "\n"; };
26+
libremidi::midi_in midiin{{.on_message = on_ump}, libremidi::jack_input_configuration{}};
27+
28+
if (auto err = midiin.open_port(pi[0]); err != stdx::error{})
29+
err.throw_exception();
30+
31+
// Wait until we exit
32+
char input;
33+
std::cin.get(input);
34+
}
35+
catch (const std::exception& error)
36+
{
37+
std::cerr << error.what() << std::endl;
38+
return EXIT_FAILURE;
39+
}

include/libremidi/detail/conversion.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ struct midi1_to_midi2
210210
switch (cmidi2_convert_midi1_to_ump(&context))
211211
{
212212
case CMIDI2_CONVERSION_RESULT_OK: {
213+
// FIXME handle sysex here
213214
if (auto n = context.ump_proceeded_bytes; n > 0)
214215
return on_ump(context.ump, context.ump_proceeded_bytes / 4, timestamp);
215216
else

include/libremidi/midi_in.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ convert_midi2_to_midi1_input_configuration(const ump_input_configuration& base_c
4141
converter.convert(
4242
msg.bytes.data(), msg.bytes.size(), msg.timestamp,
4343
[cb](const uint32_t* ump, std::size_t n, int64_t ts) {
44-
if(n >= 4)
45-
{
46-
libremidi::ump u{ump[0],ump[1],ump[2],ump[3]};
47-
u.timestamp = ts;
48-
cb(std::move(u));
49-
}
44+
libremidi::ump u{ump[0]};
45+
for (std::size_t i = 1; i < n && i < 4; i++)
46+
u.data[i] = ump[i];
47+
u.timestamp = ts;
48+
cb(std::move(u));
5049
return stdx::error{};
5150
});
5251
};

0 commit comments

Comments
 (0)