diff --git a/RtMidi.Net/Clients/MidiInputClient.cs b/RtMidi.Net/Clients/MidiInputClient.cs index f3f41b2..f8ee07e 100644 --- a/RtMidi.Net/Clients/MidiInputClient.cs +++ b/RtMidi.Net/Clients/MidiInputClient.cs @@ -1,7 +1,7 @@ -using System.Diagnostics; using RtMidi.Net.Enums; using RtMidi.Net.Events; using RtMidi.Net.InteropServices; +using System.Diagnostics; namespace RtMidi.Net.Clients; @@ -46,7 +46,7 @@ public bool IgnoreSenseMessages public MidiInputClient(MidiDeviceInfo deviceInfo) : base(deviceInfo, new RtMidiIn()) { - _rtMidiInClient = (RtMidiIn) RtMidiClient; + _rtMidiInClient = (RtMidiIn)RtMidiClient; } public void ActivateMessageReceivedEvent() @@ -66,8 +66,8 @@ private void IgnoreMessageTypes(bool sysex, bool time, bool sense) private void InternalMessageReceived(double timestamp, IntPtr messagePtr, UIntPtr messageSize, IntPtr userDataPtr) { - var messageData = new byte [(int) messageSize]; - System.Runtime.InteropServices.Marshal.Copy(messagePtr, messageData, 0, (int) messageSize); + var messageData = new byte[(int)messageSize]; + System.Runtime.InteropServices.Marshal.Copy(messagePtr, messageData, 0, (int)messageSize); var message = ConvertMessage(messageData); var midiEventArgs = new MidiMessageReceivedEventArgs(message, TimeSpan.FromSeconds(timestamp)); OnMessageReceived?.Invoke(this, midiEventArgs); @@ -93,7 +93,7 @@ public async Task<(MidiMessage, TimeSpan)> GetMessageAsync(CancellationToken can { Stopwatch stopwatch = new(); byte[] messageData; - + stopwatch.Start(); do { @@ -113,10 +113,10 @@ private static MidiMessage ConvertMessage(IReadOnlyList message) { MidiMessageType type; - if (message[0] is >= (byte) MidiMessageType.NoteOff and < (byte) MidiMessageType.SystemExclusive) + if (message[0] is >= (byte)MidiMessageType.NoteOff and < (byte)MidiMessageType.SystemExclusive) { - var channel = (MidiChannel) (0b_0000_1111 & message[0]); - type = (MidiMessageType) (0b_1111_0000 & message[0]); + var channel = (MidiChannel)(0b_0000_1111 & message[0]); + type = (MidiMessageType)(0b_1111_0000 & message[0]); switch (type) { @@ -143,7 +143,7 @@ private static MidiMessage ConvertMessage(IReadOnlyList message) return new MidiMessageUnknown(message.ToArray()); } - type = (MidiMessageType) message[0]; + type = (MidiMessageType)message[0]; switch (type) { case MidiMessageType.SystemExclusive: diff --git a/RtMidi.Net/Clients/MidiOutputClient.cs b/RtMidi.Net/Clients/MidiOutputClient.cs index b231b9f..2abbe81 100644 --- a/RtMidi.Net/Clients/MidiOutputClient.cs +++ b/RtMidi.Net/Clients/MidiOutputClient.cs @@ -9,7 +9,7 @@ public class MidiOutputClient : MidiClient public MidiOutputClient(MidiDeviceInfo deviceInfo) : base(deviceInfo, new RtMidiOut()) { - _rtMidiOutClient = (RtMidiOut) RtMidiClient; + _rtMidiOutClient = (RtMidiOut)RtMidiClient; } public void SendMessage(byte[] message) @@ -64,7 +64,7 @@ private static byte[] ConvertMessage(MidiMessage message) }, MidiMessageType.SystemExclusive when message is MidiMessageSystemExclusive(var data, _) => data, //TODO Create Message for TimeCodeQuarterFrame - MidiMessageType.TimeCodeQuarterFrame when message is MidiMessageUnknown (var data, _) => data, + MidiMessageType.TimeCodeQuarterFrame when message is MidiMessageUnknown(var data, _) => data, MidiMessageType.SongPositionPointer when message is MidiMessageSongPositionPointer songPosPointer => new[] { (byte) songPosPointer.Type, @@ -97,12 +97,12 @@ private static byte[] ConvertMessage(MidiMessage message) byte ByteFrom(MidiMessageType messageType, MidiChannel channel) { - return (byte) ((byte) messageType | (byte) channel); + return (byte)((byte)messageType | (byte)channel); } byte[] DefaultMessage(MidiMessage midiMessage) { - return new[] {(byte) midiMessage.Type}; + return new[] { (byte)midiMessage.Type }; } } } \ No newline at end of file diff --git a/RtMidi.Net/Enums/MidiMessageType.cs b/RtMidi.Net/Enums/MidiMessageType.cs index 2b26c9f..1bdde86 100644 --- a/RtMidi.Net/Enums/MidiMessageType.cs +++ b/RtMidi.Net/Enums/MidiMessageType.cs @@ -9,7 +9,7 @@ namespace RtMidi.Net.Enums; public enum MidiMessageType : byte { Unknown = 0, - + NoteOff = 0b_1000_0000, NoteOn = 0b_1001_0000, PolyphonicKeyPressure = 0b_1010_0000, diff --git a/RtMidi.Net/Enums/MusicNote.cs b/RtMidi.Net/Enums/MusicNote.cs index 217be87..e7f73d3 100644 --- a/RtMidi.Net/Enums/MusicNote.cs +++ b/RtMidi.Net/Enums/MusicNote.cs @@ -14,7 +14,7 @@ public enum MusicNote : byte A = 9, ASharp = 10, B = 11, - + Do = C, DoSharp = CSharp, Re = D, diff --git a/RtMidi.Net/InteropServices/RtMidiBase.cs b/RtMidi.Net/InteropServices/RtMidiBase.cs index 75fe21c..7bfa88a 100644 --- a/RtMidi.Net/InteropServices/RtMidiBase.cs +++ b/RtMidi.Net/InteropServices/RtMidiBase.cs @@ -1,5 +1,5 @@ -using System.Runtime.InteropServices; using RtMidi.Net.Enums; +using System.Runtime.InteropServices; namespace RtMidi.Net.InteropServices; @@ -19,7 +19,7 @@ internal abstract class RtMidiBase : IDisposable /// /// Returns true if a port is open and false if not. public bool IsPortOpen { get; private set; } - + protected RtMidiBase(IntPtr rtMidiPtr) { RtMidiPtr = rtMidiPtr; @@ -97,7 +97,7 @@ public string GetPortName(uint portNumber, uint? nameSize = null) bufferNameSize = nameSize.Value; } - messagePtr = Marshal.AllocHGlobal((int) bufferNameSize); + messagePtr = Marshal.AllocHGlobal((int)bufferNameSize); try { resultInterop = RtMidiInterop.rtmidi_get_port_name(RtMidiPtr, portNumber, messagePtr, ref bufferNameSize); @@ -159,7 +159,7 @@ public static IReadOnlyCollection GetCompiledApi() var unmanagedPointer = Marshal.AllocHGlobal(apis.Length); Marshal.Copy(apis, 0, unmanagedPointer, apis.Length); var result = RtMidiInterop.rtmidi_get_compiled_api(unmanagedPointer, sizeof(MidiApi)); - apis = new byte [result]; + apis = new byte[result]; Marshal.Copy(unmanagedPointer, apis, 0, result); var apisResult = apis.Cast().ToList(); Marshal.FreeHGlobal(unmanagedPointer); diff --git a/RtMidi.Net/InteropServices/RtMidiCallback.cs b/RtMidi.Net/InteropServices/RtMidiCallback.cs index 305add1..ef32c7e 100644 --- a/RtMidi.Net/InteropServices/RtMidiCallback.cs +++ b/RtMidi.Net/InteropServices/RtMidiCallback.cs @@ -3,4 +3,4 @@ namespace RtMidi.Net.InteropServices; [UnmanagedFunctionPointer(CallingConvention.Cdecl)] -internal delegate void RtMidiCallback (double timestamp, IntPtr message, UIntPtr size, IntPtr userData); \ No newline at end of file +internal delegate void RtMidiCallback(double timestamp, IntPtr message, UIntPtr size, IntPtr userData); \ No newline at end of file diff --git a/RtMidi.Net/InteropServices/RtMidiIn.cs b/RtMidi.Net/InteropServices/RtMidiIn.cs index da30a16..f482b66 100644 --- a/RtMidi.Net/InteropServices/RtMidiIn.cs +++ b/RtMidi.Net/InteropServices/RtMidiIn.cs @@ -1,5 +1,5 @@ -using System.Runtime.InteropServices; using RtMidi.Net.Enums; +using System.Runtime.InteropServices; namespace RtMidi.Net.InteropServices; @@ -121,12 +121,12 @@ public void IgnoreTypes(bool midiSysex = true, bool midiTime = true, bool midiSe { var size = 1024; var messagePtr = Marshal.AllocHGlobal(size); - + var timestamp = RtMidiInterop.rtmidi_in_get_message(RtMidiPtr, messagePtr, ref size); var message = new byte[size]; Marshal.Copy(messagePtr, message, 0, size); Marshal.FreeHGlobal(messagePtr); - + return (message, timestamp); } diff --git a/RtMidi.Net/InteropServices/RtMidiOut.cs b/RtMidi.Net/InteropServices/RtMidiOut.cs index 7eabd10..9e8297f 100644 --- a/RtMidi.Net/InteropServices/RtMidiOut.cs +++ b/RtMidi.Net/InteropServices/RtMidiOut.cs @@ -19,7 +19,7 @@ internal class RtMidiOut : RtMidiBase public RtMidiOut() : base(RtMidiInterop.rtmidi_out_create_default()) { } - + /// /// Default constructor that allows an optional client name. /// @@ -36,7 +36,7 @@ public RtMidiOut(MidiApi api, string clientName) : base(RtMidiInterop.rtmidi_out_create(api, clientName)) { } - + /// public override MidiApi GetCurrentApi() { diff --git a/RtMidi.Net/MidiNote.cs b/RtMidi.Net/MidiNote.cs index 1cbe6c1..e0b787d 100644 --- a/RtMidi.Net/MidiNote.cs +++ b/RtMidi.Net/MidiNote.cs @@ -21,8 +21,8 @@ namespace RtMidi.Net; public MidiNote(byte note) { if (note > 127) throw new ArgumentOutOfRangeException(nameof(note), "The note must be 0 to 127"); - Note = (MusicNote) (note % 12); - Octave = (MusicOctave) (note / 12); + Note = (MusicNote)(note % 12); + Octave = (MusicOctave)(note / 12); } /// @@ -42,7 +42,7 @@ public MidiNote(MusicNote note, MusicOctave octave) /// The MIDI byte representation public byte GetByteRepresentation() { - return (byte) ((byte) Note + (byte) ((byte) Octave * 12)); + return (byte)((byte)Note + (byte)((byte)Octave * 12)); } /// @@ -67,13 +67,13 @@ public string GetName(bool useLatinName = false) if (useLatinName) { var latinName = notesNames.Except(angloSaxonNames).ToList(); - noteName = latinName[(int) Note]; + noteName = latinName[(int)Note]; } else { - noteName = angloSaxonNames[(int) Note]; + noteName = angloSaxonNames[(int)Note]; } - return $"{noteName.Replace("Sharp", "#")}{(int) Octave}"; + return $"{noteName.Replace("Sharp", "#")}{(int)Octave}"; } } \ No newline at end of file diff --git a/WorkerTest/Worker.cs b/WorkerTest/Worker.cs index ba19229..3fbe77d 100644 --- a/WorkerTest/Worker.cs +++ b/WorkerTest/Worker.cs @@ -3,95 +3,94 @@ using RtMidi.Net.Enums; using RtMidi.Net.Events; -namespace WorkerTest +namespace WorkerTest; + +public class Worker : BackgroundService { - public class Worker : BackgroundService - { - private readonly ILogger _logger; - private MidiInputClient? _midiInputClient; - private const bool UseEventHandler = true; //Change to test + private readonly ILogger _logger; + private MidiInputClient? _midiInputClient; + private const bool UseEventHandler = true; //Change to test - /// - /// Constructor - /// - /// - public Worker(ILogger logger) - { - _logger = logger; - } + /// + /// Constructor + /// + /// + public Worker(ILogger logger) + { + _logger = logger; + } - public override async Task StartAsync(CancellationToken cancellationToken) + public override async Task StartAsync(CancellationToken cancellationToken) + { + _logger.LogInformation("Starting"); + var rtMidiVersion = MidiManager.GetRtMidiVersion(); + _logger.LogInformation("RtMidi version: {rtMidiVersion}", rtMidiVersion); + var devices = MidiManager.GetAvailableDevices(); + if (devices.Any()) { - _logger.LogInformation("Starting"); - var rtMidiVersion = MidiManager.GetRtMidiVersion(); - _logger.LogInformation("RtMidi version: {rtMidiVersion}", rtMidiVersion); - var devices = MidiManager.GetAvailableDevices(); - if (devices.Any()) + _logger.LogInformation("Available devices:"); + foreach (var d in devices.OrderBy(info => info.Port)) { - _logger.LogInformation("Available devices:"); - foreach (var d in devices.OrderBy(info => info.Port)) - { - _logger.LogInformation("{port}) {name} - {type}", d.Port, d.Name, d.Type); - } - - var devicePort = 1u; //Change device to test - var device = MidiManager.GetDeviceInfo(devicePort, MidiDeviceType.Input); - _midiInputClient = new MidiInputClient(device); - if (UseEventHandler) - { - _midiInputClient.OnMessageReceived += MidiClient_OnMessageReceived; - _midiInputClient.ActivateMessageReceivedEvent(); - } - _midiInputClient.Open(); + _logger.LogInformation("{port}) {name} - {type}", d.Port, d.Name, d.Type); } - else + + var devicePort = 1u; //Change device to test + var device = MidiManager.GetDeviceInfo(devicePort, MidiDeviceType.Input); + _midiInputClient = new MidiInputClient(device); + if (UseEventHandler) { - var exception = new Exception("No Midi devices found"); - _logger.LogError(exception, "No Midi devices found"); - throw exception; + _midiInputClient.OnMessageReceived += MidiClient_OnMessageReceived; + _midiInputClient.ActivateMessageReceivedEvent(); } - - await base.StartAsync(cancellationToken); + _midiInputClient.Open(); + } + else + { + var exception = new Exception("No Midi devices found"); + _logger.LogError(exception, "No Midi devices found"); + throw exception; } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + await base.StartAsync(cancellationToken); + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + _logger.LogInformation("Executing"); + if (!UseEventHandler) { - _logger.LogInformation("Executing"); - if (!UseEventHandler) + while (!stoppingToken.IsCancellationRequested) { - while (!stoppingToken.IsCancellationRequested) - { - var (message, _) = await _midiInputClient!.GetMessageAsync(stoppingToken); - OnMessageReceived(message); - } + var (message, _) = await _midiInputClient!.GetMessageAsync(stoppingToken); + OnMessageReceived(message); } } + } - public override async Task StopAsync(CancellationToken cancellationToken) - { - _logger.LogInformation("Stopping"); - _midiInputClient?.Close(); - _midiInputClient?.Dispose(); - await base.StopAsync(cancellationToken); - } + public override async Task StopAsync(CancellationToken cancellationToken) + { + _logger.LogInformation("Stopping"); + _midiInputClient?.Close(); + _midiInputClient?.Dispose(); + await base.StopAsync(cancellationToken); + } + + private void MidiClient_OnMessageReceived(object? sender, MidiMessageReceivedEventArgs args) + { + OnMessageReceived(args.Message); + } - private void MidiClient_OnMessageReceived(object? sender, MidiMessageReceivedEventArgs args) + private void OnMessageReceived(MidiMessage midiMessage) + { + if (midiMessage is MidiMessageNote { Type: MidiMessageType.NoteOn or MidiMessageType.NoteOff } message) { - OnMessageReceived(args.Message); + const int offset = 21; + var note = message.Note.GetByteRepresentation() - offset; + _logger.LogInformation("{noteName} - {messageType}, Note number: {noteNumber}", message.Note.GetName(), message.Type, note); } - - private void OnMessageReceived(MidiMessage midiMessage) + else { - if (midiMessage is MidiMessageNote { Type: MidiMessageType.NoteOn or MidiMessageType.NoteOff } message) - { - const int offset = 21; - var note = message.Note.GetByteRepresentation() - offset; - _logger.LogInformation("{noteName} - {messageType}, Note number: {noteNumber}", message.Note.GetName(), message.Type, note); - } - else - { - _logger.LogInformation("{messageType}", midiMessage.Type); - } + _logger.LogInformation("{messageType}", midiMessage.Type); } } } \ No newline at end of file