Skip to content

Commit

Permalink
Rewrite logging as suggested by cbjeukendrup.
Browse files Browse the repository at this point in the history
  • Loading branch information
SolfaMode committed Sep 29, 2024
1 parent cf0d53d commit 9405a78
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
38 changes: 16 additions & 22 deletions src/framework/midi/internal/platform/osx/coremidiinport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
using namespace muse;
using namespace muse::midi;

//#define DEBUG_COREMIDIINPORT
#ifdef DEBUG_COREMIDIINPORT
#define LOG_MIDI_D LOGD
#define LOG_MIDI_W LOGW
#else
#define LOG_MIDI_D LOGN
#define LOG_MIDI_W LOGN
#endif

struct muse::midi::CoreMidiInPort::Core {
MIDIClientRef client = 0;
MIDIPortRef inputPort = 0;
Expand Down Expand Up @@ -187,7 +196,6 @@ void CoreMidiInPort::initCore()
}

QString portName = "MuseScore MIDI input port";
static bool doLog = false; // kors::logger::Logger::instance()->isLevel(kors::logger::Level::Debug);
if (__builtin_available(macOS 11.0, *)) {
MIDIReceiveBlock receiveBlock = ^ (const MIDIEventList* eventList, void* /*srcConnRefCon*/) {
// For reference have a look at Table 4 on page 22f in
Expand Down Expand Up @@ -221,24 +229,18 @@ void CoreMidiInPort::initCore()

const MIDIEventPacket* packet = eventList->packet;
for (UInt32 index = 0; index < eventList->numPackets; index++) {
if (doLog) {
LOGW() << "Receiving MIDIEventPacket with " << packet->wordCount << " words";
}
LOG_MIDI_D() << "Receiving MIDIEventPacket with " << packet->wordCount << " words";
// Handle packet
uint32_t pos = 0;
while (pos < packet->wordCount) {
uint32_t most_significant_4_bit = packet->words[pos] >> 28;
assert(most_significant_4_bit < 6);

uint32_t message_size = message_type_to_size_in_words[most_significant_4_bit];
if (doLog) {
LOGW() << "Receiving midi message with " << message_size << " words";
}
LOG_MIDI_D() << "Receiving midi message with " << message_size << " words";
Event e = Event::fromRawData(&packet->words[pos], message_size);
if (e) {
if (doLog) {
LOGW() << "Received midi message:" << e.to_string();
}
LOG_MIDI_D() << "Received midi message:" << e.to_string();
m_eventReceived.send((tick_t)packet->timeStamp, e);
}
pos += message_size;
Expand All @@ -260,28 +262,20 @@ void CoreMidiInPort::initCore()
while (pos < len) {
Byte status = pointer[pos] >> 4;
if (status < 8 || status >= 15) {
if (doLog) {
LOGW() << "Unhandled status byte:" << status;
}
LOG_MIDI_W() << "Unhandled status byte:" << status;
return;
}
Event::Opcode opcode = static_cast<Event::Opcode>(status);
int msgLen = Event::midi10ByteCountForOpcode(opcode);
if (msgLen == 0) {
if (doLog) {
LOGW() << "Unhandled opcode:" << status;
}
LOG_MIDI_W() << "Unhandled opcode:" << status;
return;
}
Event e = Event::fromMIDI10BytePackage(pointer + pos, msgLen);
if (doLog) {
LOGW() << "Received midi 1.0 message:" << e.to_string();
}
LOG_MIDI_D() << "Received midi 1.0 message:" << e.to_string();
e = e.toMIDI20();
if (e) {
if (doLog) {
LOGW() << "Converted to midi 2.0 midi message:" << e.to_string();
}
LOG_MIDI_D() << "Converted to midi 2.0 midi message:" << e.to_string();
m_eventReceived.send((tick_t)packet->timeStamp, e);
}
pos += msgLen;
Expand Down
37 changes: 18 additions & 19 deletions src/framework/midi/internal/platform/osx/coremidioutport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
using namespace muse;
using namespace muse::midi;

//#define DEBUG_COREMIDIOUTPORT
#ifdef DEBUG_COREMIDIOUTPORT
#define LOG_MIDI_D LOGD
#define LOG_MIDI_W LOGW
#else
#define LOG_MIDI_D LOGN
#define LOG_MIDI_W LOGN
#endif

struct muse::midi::CoreMidiOutPort::Core {
MIDIClientRef client = 0;
MIDIPortRef outputPort = 0;
Expand Down Expand Up @@ -297,29 +306,23 @@ Ret CoreMidiOutPort::sendEvent(const Event& e)

OSStatus result;
MIDITimeStamp timeStamp = AudioGetCurrentHostTime();
static bool doLog = false; // kors::logger::Logger::instance()->isLevel(kors::logger::Level::Debug);

if (__builtin_available(macOS 11.0, *)) {
MIDIProtocolID protocolId = kMIDIProtocol_2_0; // configuration()->useMIDI20Output() ? m_core->destinationProtocolId : kMIDIProtocol_1_0;

ByteCount wordCount = e.midi20WordCount();
if (wordCount == 0) {
LOGW() << "Failed to send message for event: " << e.to_string();
LOG_MIDI_W() << "Failed to send message for event: " << e.to_string();
return make_ret(Err::MidiSendError, "failed send message. unknown word count");
}
if (doLog) {
LOGW() << "Sending MIDIEventList event: " << e.to_string();
}
LOG_MIDI_D() << "Sending MIDIEventList event: " << e.to_string();

MIDIEventList eventList;
MIDIEventPacket* packet = MIDIEventListInit(&eventList, protocolId);

if (doLog) {
bool isChannelVoiceMessage20 = e.messageType() == Event::MessageType::ChannelVoice20;
bool isNoteOnMessage = isChannelVoiceMessage20 && e.opcode() == muse::midi::Event::Opcode::NoteOn;
if (isNoteOnMessage && e.velocity() < 128) {
LOGW() << "Detected low MIDI 2.0 ChannelVoiceMessage velocity.";
}
if (e.messageType() == Event::MessageType::ChannelVoice20 && e.opcode() == muse::midi::Event::Opcode::NoteOn
&& e.velocity() < 128) {
LOG_MIDI_W() << "Detected low MIDI 2.0 ChannelVoiceMessage velocity.";
}

MIDIEventListAdd(&eventList, sizeof(eventList), packet, timeStamp, wordCount, e.rawData());
Expand All @@ -341,14 +344,10 @@ Ret CoreMidiOutPort::sendEvent(const Event& e)
for (const Event& event : events) {
int length = event.to_MIDI10BytesPackage(bytesPackage);
assert(length <= 3);
if (doLog) {
if (length == 0) {
LOGW() << "Failed Sending MIDIPacketList event: " << event.to_string();
} else {
LOGW() << "Sending MIDIPacketList event: " << event.to_string();
}
}
if (length > 0) {
if (length == 0) {
LOG_MIDI_W() << "Failed Sending MIDIPacketList event: " << event.to_string();
} else {
LOG_MIDI_D() << "Sending MIDIPacketList event: " << event.to_string();
packet = MIDIPacketListAdd(&packetList, packetListSize, packet, timeStamp, length, bytesPackage);
}
}
Expand Down

0 comments on commit 9405a78

Please sign in to comment.