question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Receiving Midi Time Code Events

See original GitHub issue

Hi, I’m new to C# and trying to figure out how the Midi Time Code works. I use _inputDevice.EventReceived and _inputDevice.MidiTimeCodeReceived EventHandler to receive Full/Quarter Frame MTC events.

When I try to use them together, only _inputDevice.EventReceived works. Sorry for the stupid question, but why is _inputDevice.MidiTimeCodeReceived not working in this case?

_inputDevice.MidiTimeCodeReceived += OnMidiTimeCodeReceived; // MTC Quarter Frame Event
_inputDevice.EventReceived += OnEventReceived; // MTC SysEx Full Frame Event

Input device is listening for events. Press any key to exit… 01/03/2021 16:04:12 MTC SysEx Full Frame: [1:53:11:13] 01/03/2021 16:04:12 MTC SysEx Full Frame: [1:2:44:24]

Separately each EventHandler works great

_inputDevice.MidiTimeCodeReceived += OnMidiTimeCodeReceived; // MTC Quarter Frame Event
//     _inputDevice.EventReceived += OnEventReceived; // MTC SysEx Full Frame Event

Input device is listening for events. Press any key to exit… 01/03/2021 16:11:49 MTC Quarter Frame: [0:29:13:27] 01/03/2021 16:11:49 MTC Quarter Frame: [0:29:13:29]

//     _inputDevice.MidiTimeCodeReceived += OnMidiTimeCodeReceived; // MTC Quarter Frame Event
_inputDevice.EventReceived += OnEventReceived; // MTC SysEx Full Frame Event

Input device is listening for events. Press any key to exit… 01/03/2021 16:31:14 MTC SysEx Full Frame: [0:17:13:14] 01/03/2021 16:31:14 MTC SysEx Full Frame: [1:18:44:21]

using System;
using Melanchall.DryWetMidi.Devices;
using Melanchall.DryWetMidi.Core;
namespace TimeCodeGen
{
    class Program
    {
        private static InputDevice _inputDevice;
        static void Main(string[] args)
        {
            _inputDevice = InputDevice.GetByName("testport");
            _inputDevice.RaiseMidiTimeCodeReceived = true;
            _inputDevice.MidiTimeCodeReceived += OnMidiTimeCodeReceived; // MTC Quarter Frame Event
            _inputDevice.EventReceived += OnEventReceived; // MTC SysEx Full Frame Event
            _inputDevice.StartEventsListening();
            Console.WriteLine("Input device is listening for events. Press any key to exit...");
            Console.ReadKey();
            if (_inputDevice != null) { }
            _inputDevice.Dispose();
        }
        // MTC SysEx Full Frame Event
        private static void OnEventReceived(object sender, MidiEventReceivedEventArgs e)
        {
            int ffHours, ffMinutes, ffSeconds, ffFrames;
            var sysEvent = e.Event as NormalSysExEvent;
            byte[] sysData = sysEvent.Data;
            ffHours = sysData[4] - 96;
            ffMinutes = sysData[5];
            ffSeconds = sysData[6];
            ffFrames = sysData[7];
            Console.WriteLine(DateTime.Now + " MTC SysEx Full Frame: [" + ffHours + ":" + ffMinutes + ":" + ffSeconds + ":" + ffFrames + "]");
        }
        // MTC Quarter Frame Event
        private static void OnMidiTimeCodeReceived(object sender, MidiTimeCodeReceivedEventArgs tc)
        {
            Console.WriteLine(DateTime.Now + " MTC Quarter Frame: [" + tc.Hours + ":" + tc.Minutes + ":" + tc.Seconds + ":" + tc.Frames + "]");
        }
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
melanchallcommented, Mar 1, 2021

I’m sure that shown events are not the same as you got when you opened the issue. In your code you have the line:

var sysEvent = e.Event as NormalSysExEvent;

But in your log there are a lot of events that are not NormalSysExEvent so your program will crash on sysEvent.Data. Use this code:

// MTC SysEx Full Frame Event
private static void OnEventReceived(object sender, MidiEventReceivedEventArgs e)
{
    int ffHours, ffMinutes, ffSeconds, ffFrames;
    var sysEvent = e.Event as NormalSysExEvent;
    if (sysEvent == null)
        return;

    byte[] sysData = sysEvent.Data;
    ffHours = sysData[4] - 96;
    ffMinutes = sysData[5];
    ffSeconds = sysData[6];
    ffFrames = sysData[7];
    Console.WriteLine(DateTime.Now + " MTC SysEx Full Frame: [" + ffHours + ":" + ffMinutes + ":" + ffSeconds + ":" + ffFrames + "]");
}
// MTC Quarter Frame Event
private static void OnMidiTimeCodeReceived(object sender, MidiTimeCodeReceivedEventArgs tc)
{
    Console.WriteLine(DateTime.Now + " MTC Quarter Frame: [" + tc.Hours + ":" + tc.Minutes + ":" + tc.Seconds + ":" + tc.Frames + "]");
}

So please uncomment

_inputDevice.MidiTimeCodeReceived += OnMidiTimeCodeReceived; // MTC Quarter Frame Event
_inputDevice.EventReceived += OnEventReceived; // MTC SysEx Full Frame Event

and check again.

0reactions
seejkeecommented, Mar 1, 2021
    if (sysEvent == null)
        return;

@melanchall Thank you very much! It’s worked 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial on getting MIDI MTC Timecode working with EOS ...
Tutorial on getting MIDI MTC Timecode working with EOS Family on PC ... Type in 'Event 1 Enter' and you should get a...
Read more >
MIDI Time Code
MIDI Time Code (MTC) is a sub-protocol within MIDI, and is used to keep 2 devices that control some sort of timed performance...
Read more >
Trigger from MIDI Timecode - Vegas Pro
To view the incoming timecode, right-click the Time Display and choose MIDI Timecode In from the shortcut menu. The text will now display...
Read more >
TimeCode and BEYOND [Complete Help Docs] - Pangolin Wiki
Ensure that your TimeCode device is connected and configured (depends on your timecode data source - SMPTE Settings, MIDI setting dialogs).
Read more >
Transmitting and Receiving MIDI Messages (The Java™ ...
The timing values in a MIDI file are often based on musical concepts such as beats and tempo, and each event's timing measures...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found