Skip to content

Commit

Permalink
Improved implementation of Splitter.SplitByObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
melanchall committed Aug 21, 2023
1 parent c61edb2 commit 62dea39
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public long ConvertFrom(ITimeSpan timeSpan, long time, TempoMap tempoMap)
lastBarLength = BarBeatUtilities.GetBarLength(lastTimeSignature, ticksPerQuarterNote);
lastBeatLength = BarBeatUtilities.GetBeatLength(lastTimeSignature, ticksPerQuarterNote);

// TODO: lastBarLength can be 0
var currentBars = Math.Min(deltaTime / lastBarLength, bars);
bars -= currentBars;
lastTime += currentBars * lastBarLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public long ConvertFrom(ITimeSpan timeSpan, long time, TempoMap tempoMap)
lastBarLength = BarBeatUtilities.GetBarLength(lastTimeSignature, ticksPerQuarterNote);
lastBeatLength = BarBeatUtilities.GetBeatLength(lastTimeSignature, ticksPerQuarterNote);

// TODO: lastBarLength can be 0
var currentBars = Math.Min(deltaTime / lastBarLength, bars);
bars -= currentBars;
lastTime += currentBars * lastBarLength;
Expand Down
18 changes: 3 additions & 15 deletions DryWetMidi/Tools/Splitter/Splitter.SplitByObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ public static IEnumerable<MidiFile> SplitByObjects(
var objectsByKeys = new Dictionary<object, List<ITimedObject>>();
var allFilesObjects = new List<ITimedObject>();

List<ITimedObject> nullKeyObjects = null;

foreach (var obj in objects)
{
if (settings.Filter?.Invoke(obj) == false)
continue;

var key = keySelector(obj) ?? obj.GetObjectId();
if (writeToAllFilesPredicate(obj))
{
if (settings.AllFilesObjectsFilter?.Invoke(obj) == false)
Expand All @@ -60,21 +57,13 @@ public static IEnumerable<MidiFile> SplitByObjects(
{
objectsByKey.Add(obj);
}

nullKeyObjects?.Add(obj);
}
else
{
List<ITimedObject> objectsByKey;
var key = keySelector(obj) ?? obj.GetObjectId();

if (key == null)
{
if (nullKeyObjects == null)
nullKeyObjects = new List<ITimedObject>(allFilesObjects);

objectsByKey = nullKeyObjects;
}
else if (!objectsByKeys.TryGetValue(key, out objectsByKey))
List<ITimedObject> objectsByKey;
if (!objectsByKeys.TryGetValue(key, out objectsByKey))
{
objectsByKeys.Add(key, objectsByKey = new List<ITimedObject>(allFilesObjects));
}
Expand All @@ -85,7 +74,6 @@ public static IEnumerable<MidiFile> SplitByObjects(

return objectsByKeys
.Values
.Concat(new[] { nullKeyObjects ?? Enumerable.Empty<ITimedObject>() })
.Where(objectsByKey => objectsByKey.Any())
.Select(objectsByKey =>
{
Expand Down
12 changes: 7 additions & 5 deletions Utilities/SendMidiData/DataSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ private static SendResult SendData(string[] array, IOutputDevice outputDevice)

try
{
var converter = new BytesToMidiEventConverter();
var midiEvents = converter.ConvertMultiple(bytes.ToArray());
var playback = midiEvents.GetPlayback(TempoMap.Default, outputDevice);
playback.Play();
return SendResult.Sent;
using (var converter = new BytesToMidiEventConverter())
{
var midiEvents = converter.ConvertMultiple(bytes.ToArray());
var playback = midiEvents.GetPlayback(TempoMap.Default, outputDevice);
playback.Play();
return SendResult.Sent;
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit 62dea39

Please sign in to comment.