Skip to content

Commit

Permalink
Fixed GetMessage and added GetMessageAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel127 committed Sep 5, 2023
1 parent b0b8af9 commit 739f8d7
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 40 deletions.
8 changes: 4 additions & 4 deletions .azure/workflows/azure-ci-development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ steps:
inputs:
version: '3.1.x'
- task: UseDotNet@2
displayName: 'Install Net 6'
displayName: 'Install Net 7'
inputs:
version: '6.0.x'
version: '7.0.x'

# SonarCloud Prepare Analysis
- task: SonarCloudPrepare@1
Expand All @@ -31,8 +31,8 @@ steps:
SonarCloud: 'SonarCloud Daniel127'
organization: 'daniel127'
scannerMode: 'MSBuild'
projectKey: 'rtmidi-dotnet'
projectName: 'rtmidi-dotnet'
projectKey: 'Daniel127_RtMidi.Net'
projectName: 'RtMidi.Net'
extraProperties: |
sonar.exclusions=**/obj/**,**/*.dll
sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/coverage/coverage.opencover.xml
Expand Down
8 changes: 4 additions & 4 deletions .azure/workflows/azure-ci-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ steps:
inputs:
version: '3.1.x'
- task: UseDotNet@2
displayName: 'Install Net 6'
displayName: 'Install Net 7'
inputs:
version: '6.0.x'
version: '7.0.x'

# SonarCloud Prepare Analysis
- task: SonarCloudPrepare@1
Expand All @@ -35,8 +35,8 @@ steps:
SonarCloud: 'SonarCloud Daniel127'
organization: 'daniel127'
scannerMode: 'MSBuild'
projectKey: 'rtmidi-dotnet'
projectName: 'rtmidi-dotnet'
projectKey: 'Daniel127_RtMidi.Net'
projectName: 'RtMidi.Net'
extraProperties: |
sonar.exclusions=**/obj/**,**/*.dll
sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/coverage/coverage.opencover.xml
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 7.0.x
- name: Build with dotnet
run: dotnet build --configuration Debug
run: dotnet build --configuration Release
10 changes: 8 additions & 2 deletions RtMidi.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RtMidi.Net", "RtMidi.Net\RtMidi.Net.csproj", "{4A4EAB7A-9B1A-4030-B94B-4A5EC19DECBE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RtMidi.Net", "RtMidi.Net\RtMidi.Net.csproj", "{4A4EAB7A-9B1A-4030-B94B-4A5EC19DECBE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerTest", "WorkerTest\WorkerTest.csproj", "{54B7CA1F-3964-492C-8251-44A5A244D7C8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkerTest", "WorkerTest\WorkerTest.csproj", "{54B7CA1F-3964-492C-8251-44A5A244D7C8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{19D1F538-EE06-41DD-ABF5-6488D3903B51}"
ProjectSection(SolutionItems) = preProject
LICENSE.md = LICENSE.md
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
8 changes: 4 additions & 4 deletions RtMidi.Net/Clients/MidiClient.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using RtMidi.Net.Enums;
using RtMidi.Net.InteropServices;

namespace RtMidi.Net;
namespace RtMidi.Net.Clients;

public abstract class MidiClient : IDisposable

Check warning on line 6 in RtMidi.Net/Clients/MidiClient.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'MidiClient'
{
private readonly MidiDeviceInfo _deviceInfo;
internal readonly RtMidiBase RtMidiClient;
private bool disposedValue;
private bool _disposedValue;

internal MidiClient(uint deviceId, RtMidiBase rtMidiClient)
{
Expand Down Expand Up @@ -43,13 +43,13 @@ public void Close()

protected virtual void Dispose(bool disposing)

Check warning on line 44 in RtMidi.Net/Clients/MidiClient.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'MidiClient.Dispose(bool)'
{
if (!disposedValue)
if (!_disposedValue)
{
if (disposing)
{
RtMidiClient.Dispose();
}
disposedValue = true;
_disposedValue = true;
}
}

Expand Down
37 changes: 34 additions & 3 deletions RtMidi.Net/Clients/MidiInputClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Diagnostics;
using RtMidi.Net.Enums;
using RtMidi.Net.Events;
using RtMidi.Net.InteropServices;

namespace RtMidi.Net;
namespace RtMidi.Net.Clients;

public class MidiInputClient : MidiClient

Check warning on line 8 in RtMidi.Net/Clients/MidiInputClient.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'MidiInputClient'
{
Expand Down Expand Up @@ -74,10 +75,40 @@ private void InternalMessageReceived(double timestamp, IntPtr messagePtr, UIntPt

public (MidiMessage, TimeSpan) GetMessage()
{
var (messageData, timestamp) = _rtMidiInClient.GetMessage();
return (ConvertMessage(messageData), TimeSpan.FromSeconds(timestamp));
Stopwatch stopwatch = new();
byte[] messageData;
stopwatch.Start();
do
{
(messageData, _) = _rtMidiInClient.GetMessage();
} while (messageData.Length == 0);
stopwatch.Stop();
var timestamp = TimeSpan.FromSeconds(stopwatch.Elapsed.TotalSeconds);
return (ConvertMessage(messageData), timestamp);
}

public async Task<(MidiMessage, TimeSpan)> GetMessageAsync(CancellationToken cancellationToken)
{
return await Task.Run(() =>
{
Stopwatch stopwatch = new();
byte[] messageData;
stopwatch.Start();
do
{
(messageData, _) = _rtMidiInClient.GetMessage();
} while (!cancellationToken.IsCancellationRequested && messageData.Length == 0);
stopwatch.Stop();
cancellationToken.ThrowIfCancellationRequested();
var timestamp = TimeSpan.FromSeconds(stopwatch.Elapsed.TotalSeconds);
return (ConvertMessage(messageData), timestamp);
}, cancellationToken);
}


private static MidiMessage ConvertMessage(IReadOnlyList<byte> message)
{
MidiMessageType type;
Expand Down
2 changes: 1 addition & 1 deletion RtMidi.Net/Clients/MidiOutputClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using RtMidi.Net.Enums;
using RtMidi.Net.InteropServices;

namespace RtMidi.Net;
namespace RtMidi.Net.Clients;

public class MidiOutputClient : MidiClient
{
Expand Down
2 changes: 1 addition & 1 deletion RtMidi.Net/RtMidi.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
54 changes: 38 additions & 16 deletions WorkerTest/Worker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using RtMidi.Net;
using RtMidi.Net.Clients;
using RtMidi.Net.Enums;
using RtMidi.Net.Events;

namespace WorkerTest
{
Expand All @@ -16,49 +19,68 @@ public Worker(ILogger<Worker> logger)
_logger = logger;
}


public override Task StartAsync(CancellationToken cancellationToken)
public override async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting");
var devices = MidiManager.GetAvailableDevices();
foreach (var d in devices)
{
_logger.LogInformation($"{d.Port}) {d.Name} - {d.Type}");
}
if (devices.Any())
{
//TODO Change device to test
var devicePort = 0u;
var device = MidiManager.GetDeviceInfo(devicePort, RtMidi.Net.Enums.MidiDeviceType.Input);
var devicePort = 1u; //TODO Change device to test
var device = MidiManager.GetDeviceInfo(devicePort, MidiDeviceType.Input);
_midiInputClient = new MidiInputClient(device);
_midiInputClient.OnMessageReceived += MidiClient_OnMessageReceived;
_midiInputClient.ActivateMessageReceivedEvent();
//TODO The event throws an exception after a while, i'm not sure why
//_midiInputClient.OnMessageReceived += MidiClient_OnMessageReceived;
//_midiInputClient.ActivateMessageReceivedEvent();
_midiInputClient.Open();
}
return base.StartAsync(cancellationToken);
else
{
var exception = new Exception("No Midi devices found");
_logger.LogError(exception, "No Midi devices found");
throw exception;
}

await base.StartAsync(cancellationToken);
}

protected override Task ExecuteAsync(CancellationToken stoppingToken)
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
return Task.CompletedTask;
_logger.LogInformation("Executing");
while (!stoppingToken.IsCancellationRequested)
{
var (message, _) = await _midiInputClient!.GetMessageAsync(stoppingToken);
OnMessageReceived(message);
}
}

public override Task StopAsync(CancellationToken cancellationToken)
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping");
_midiInputClient?.Close();
_midiInputClient?.Dispose();
return base.StopAsync(cancellationToken);
await base.StopAsync(cancellationToken);
}

private void MidiClient_OnMessageReceived(object? sender, MidiMessageReceivedEventArgs args)
{
OnMessageReceived(args.Message);
}

private void MidiClient_OnMessageReceived(object? sender, RtMidi.Net.Events.MidiMessageReceivedEventArgs args)
private void OnMessageReceived(MidiMessage midiMessage)
{
if(args.Message is MidiMessageNote message)
if (midiMessage is MidiMessageNote { Type: MidiMessageType.NoteOn or MidiMessageType.NoteOff } message)
{
_logger.LogInformation($"{message.Note.GetName()} - {message.Type}");
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($"{args.Message.Type}");
_logger.LogInformation("{messageType}", midiMessage.Type);
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions WorkerTest/WorkerTest.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-WorkerTest-B41C8CE5-6C97-4BF3-90FB-7FB7452FA332</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RtMidi.Net\RtMidi.Net.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="librtmidi.so"> <!-- Linux ARM64 (Tested on Raspberry Pi)-->
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="rtmidi.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Binary file added WorkerTest/librtmidi.so
Binary file not shown.

0 comments on commit 739f8d7

Please sign in to comment.