Skip to content

Commit

Permalink
Make Player implement IDisposable
Browse files Browse the repository at this point in the history
  • Loading branch information
mchudy committed Sep 25, 2016
1 parent 116ec0b commit 5301e88
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 26 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
## Getting started

```csharp
Player player = new Player();
player.Play("C D E F G A B");
using(var player = new Player())
{
player.Play("C D E F G A B");
}
```
17 changes: 8 additions & 9 deletions src/NFugue/Playing/ManagedPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace NFugue.Playing
{
public class ManagedPlayer
public class ManagedPlayer : IDisposable
{
private static readonly ILogger log = LogManager.GetCurrentClassLogger();
private readonly Sequencer sequencer = new Sequencer();
private OutputDevice outputDevice;
private readonly OutputDevice outputDevice;

public ManagedPlayer()
{
outputDevice = new OutputDevice(0);
sequencer.PlayingCompleted += (s, e) => Finish();
SubscribeMessageEvents();
}
Expand All @@ -30,10 +31,6 @@ public ManagedPlayer()

public void Start(Sequence sequence)
{
if (outputDevice == null || outputDevice.IsDisposed)
{
outputDevice = new OutputDevice(0);
}
sequencer.Sequence = sequence;
log.Trace($"Playing sequence with division {sequence.Division}");
foreach (var track in sequence)
Expand Down Expand Up @@ -82,9 +79,6 @@ public void Reset()
public void Finish()
{
log.Trace("Playing completed. Closing device...");
outputDevice?.Close();
outputDevice?.Dispose();
outputDevice = null;
Finished?.Invoke(this, EventArgs.Empty);
IsFinished = true;
}
Expand Down Expand Up @@ -113,6 +107,11 @@ private void SubscribeMessageEvents()
outputDevice?.Send(e.Message);
};
}

public void Dispose()
{
outputDevice.Dispose();
}
}

public class SequenceEventArgs : EventArgs
Expand Down
8 changes: 7 additions & 1 deletion src/NFugue/Playing/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using NFugue.Patterns;
using NFugue.Staccato;
using Sanford.Multimedia.Midi;
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace NFugue.Playing
{
public class Player
public class Player : IDisposable
{
private readonly ManagedPlayer managedPlayer = new ManagedPlayer();
private readonly MidiEventManager eventManager = new MidiEventManager();
Expand Down Expand Up @@ -134,5 +135,10 @@ private void SubscribeToParserEvents()
}
};
}

public void Dispose()
{
managedPlayer.Dispose();
}
}
}
8 changes: 7 additions & 1 deletion tests/NFugue.ManualTests/Tests/RhythmTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using NFugue.Patterns;
using NFugue.Playing;
using NFugue.Rhythms;
using System;

namespace NFugue.ManualTests.Tests
{
public class RhythmTests
public class RhythmTests : IDisposable
{
private readonly Rhythm rhythm = new Rhythm();
private readonly Player player = new Player();
Expand All @@ -28,5 +29,10 @@ public void TwoToneRhythmTest()
rhythm.AddLayer("oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo");
player.Play(rhythm);
}

public void Dispose()
{
player.Dispose();
}
}
}
7 changes: 6 additions & 1 deletion tests/NFugue.ManualTests/Tests/StaccatoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace NFugue.ManualTests.Tests
{
public class StaccatoTests
public class StaccatoTests : IDisposable
{
private readonly Player player = new Player();

Expand Down Expand Up @@ -120,5 +120,10 @@ public void MicrotonalTest()
player.Play(microtone);
player.Play(preprocessor.Preprocess(microtone, null));
}

public void Dispose()
{
player.Dispose();
}
}
}
4 changes: 2 additions & 2 deletions tests/NFugue.ManualTests/Utils/ManualTestAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Xunit;
using System;

namespace NFugue.ManualTests.Utils
{
public class ManualTestAttribute : FactAttribute
public class ManualTestAttribute : Attribute
{
public ManualTestAttribute(string title, string description = "")
{
Expand Down
20 changes: 12 additions & 8 deletions tests/NFugue.ManualTests/Utils/ManualTestsRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ public void RunTests()
private void RunTest(MethodInfo method, int testNumber)
{
var type = method.DeclaringType;
if (type != null)
{
var typeInstance = Activator.CreateInstance(type);
var attribute = method.GetCustomAttribute<ManualTestAttribute>();
if (type == null) return;
var typeInstance = Activator.CreateInstance(type);
var attribute = method.GetCustomAttribute<ManualTestAttribute>();

PrintBeforeTest(attribute, testNumber);
PrintBeforeTest(attribute, testNumber);

method.Invoke(typeInstance, null);
method.Invoke(typeInstance, null);

GetTestResult();
testsRun++;
if (typeof(IDisposable).IsAssignableFrom(type))
{
var disposeMethod = type.GetMethod("Dispose");
disposeMethod.Invoke(typeInstance, null);
}

GetTestResult();
testsRun++;
}

private void GetTestResult()
Expand Down
9 changes: 7 additions & 2 deletions tests/NFugue.Tests/Staccato/UnknownTokenTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using FluentAssertions;
using NFugue.Parsing;
using NFugue.Playing;
using System;
using NFugue.Parsing;
using Xunit;

namespace Staccato.Tests
{
public class UnknownTokenTests
public class UnknownTokenTests : IDisposable
{
private readonly Player player = new Player();

Expand All @@ -23,5 +23,10 @@ public void Should_throw_exception_if_flag_set()
Action action = () => player.Play("UNKNOWN");
action.ShouldThrow<ParserException>();
}

public void Dispose()
{
player.Dispose();
}
}
}

0 comments on commit 5301e88

Please sign in to comment.