Skip to content

Commit

Permalink
Added EventRecorded event to Recording
Browse files Browse the repository at this point in the history
  • Loading branch information
melanchall committed Feb 3, 2024
1 parent d25034d commit 1c47c70
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
10 changes: 7 additions & 3 deletions DryWetMidi.Tests/Multimedia/Recording/RecordingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ public void CheckRecording()

var sentEvents = new List<SentEvent>();
var receivedEvents = new List<ReceivedEvent>();
var recordedEvents = new List<ReceivedEvent>();
var stopwatch = new Stopwatch();

var expectedTimes = new List<TimeSpan>();
var expectedRecordedTimes = new List<TimeSpan>();
var currentTime = TimeSpan.Zero;

foreach (var eventToSend in eventsToSend)
{
currentTime += eventToSend.Delay;
Expand All @@ -119,7 +121,6 @@ public void CheckRecording()

using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
{
//SendReceiveUtilities.WarmUpDevice(outputDevice);
outputDevice.EventSent += (_, e) => sentEvents.Add(new SentEvent(e.Event, stopwatch.Elapsed));

using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
Expand All @@ -129,6 +130,8 @@ public void CheckRecording()

using (var recording = new Recording(tempoMap, inputDevice))
{
recording.EventRecorded += (_, e) => recordedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));

var sendingThread = new Thread(() =>
{
SendReceiveUtilities.SendEvents(eventsToSend, outputDevice);
Expand All @@ -152,9 +155,10 @@ public void CheckRecording()
Assert.IsTrue(areEventsReceived, $"Events are not received for [{timeout}] (received are: {string.Join(", ", receivedEvents)}).");

CompareSentReceivedEvents(sentEvents, receivedEvents, expectedTimes);
CompareSentReceivedEvents(sentEvents, recordedEvents, expectedTimes);

var recordedEvents = recording.GetEvents();
CheckRecordedEvents(recordedEvents, expectedRecordedTimes, tempoMap);
var events = recording.GetEvents();
CheckRecordedEvents(events.ToList(), expectedRecordedTimes, tempoMap);
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions DryWetMidi/Multimedia/Recording/MidiEventRecordedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Melanchall.DryWetMidi.Common;
using Melanchall.DryWetMidi.Core;

namespace Melanchall.DryWetMidi.Multimedia
{
public sealed class MidiEventRecordedEventArgs
{
#region Constructor

internal MidiEventRecordedEventArgs(MidiEvent midiEvent)
{
ThrowIfArgument.IsNull(nameof(midiEvent), midiEvent);

Event = midiEvent;
}

#endregion

#region Properties

public MidiEvent Event { get; }

#endregion
}
}
18 changes: 14 additions & 4 deletions DryWetMidi/Multimedia/Recording/Recording.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Linq;
using Melanchall.DryWetMidi.Common;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Interaction;

namespace Melanchall.DryWetMidi.Multimedia
Expand All @@ -26,6 +27,8 @@ public sealed class Recording : IDisposable
/// </summary>
public event EventHandler Stopped;

public event EventHandler<MidiEventRecordedEventArgs> EventRecorded;

#endregion

#region Fields
Expand Down Expand Up @@ -120,11 +123,11 @@ public TTimeSpan GetDuration<TTimeSpan>()
/// Gets MIDI events recorded by the current <see cref="Recording"/>.
/// </summary>
/// <returns>MIDI events recorded by the current <see cref="Recording"/>.</returns>
public IReadOnlyList<TimedEvent> GetEvents()
public ICollection<TimedEvent> GetEvents()
{
return _events.Select(e => new TimedEvent(e.Event, TimeConverter.ConvertFrom((MetricTimeSpan)e.Time, TempoMap)))
.ToList()
.AsReadOnly();
return _events
.Select(e => new TimedEvent(e.Event, TimeConverter.ConvertFrom((MetricTimeSpan)e.Time, TempoMap)))
.ToArray();
}

/// <summary>
Expand Down Expand Up @@ -172,6 +175,13 @@ private void OnEventReceived(object sender, MidiEventReceivedEventArgs e)
return;

_events.Add(new RecordingEvent(e.Event, _stopwatch.Elapsed));

OnEventRecorded(e.Event);
}

private void OnEventRecorded(MidiEvent midiEvent)
{
EventRecorded?.Invoke(this, new MidiEventRecordedEventArgs(midiEvent));
}

#endregion
Expand Down

0 comments on commit 1c47c70

Please sign in to comment.