Skip to content

Commit

Permalink
Using PatchCollection #1
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriiKh committed Jun 27, 2020
1 parent 6515e06 commit 9b08d7a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions CarefulAudioRepair/Data/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task ScanAsync(
this.scannerTools =
await scanner.ScanAsync(status, progress).ConfigureAwait(false);

foreach (var patch in this.scannerTools.PatchCollection)
foreach (var patch in this.scannerTools.PatchCollection.ToList())
{
this.RegisterPatch(patch);
}
Expand Down Expand Up @@ -122,9 +122,7 @@ public double[] GetInputRange(int start, int length) =>

private void RemoveAllPatches()
{
while (this.scannerTools.PatchCollection.TryTake(out _))
{
}
this.scannerTools.PatchCollection.RemoveAllPatches();
}

private void RegisterPatch(AbstractPatch patch)
Expand Down
20 changes: 18 additions & 2 deletions CarefulAudioRepair/Data/PatchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ internal class PatchCollection : IDisposable

private int[] startPositions;

public bool Finalized { get; private set; } = false;

public PatchCollection()
{
}

public bool Finalized { get; private set; } = false;

public int Count => this.patchCollection.Count;

public void Add(AbstractPatch patch)
{
this.patchCollection.Add(patch);
Expand Down Expand Up @@ -113,6 +115,20 @@ public AbstractPatch[] GetPatchesForRange(AbstractFragment range)
}
}

public List<AbstractPatch> ToList()
{
return this.patchCollection.ToList();
}

public void RemoveAllPatches()
{
while (this.patchCollection.TryTake(out _))
{
}

this.Finalized = false;
}

public void Dispose()
{
this.patchCollection.Dispose();
Expand Down
30 changes: 15 additions & 15 deletions CarefulAudioRepair/Processing/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace CarefulAudioRepair.Processing
internal class Patcher : IPatcher
{
private readonly ImmutableArray<double> immutableArray;
private readonly BlockingCollection<AbstractPatch> patchCollection;
private readonly PatchCollection patchCollection;
private readonly Func<AbstractPatch, int, double> updateFunc;

/// <summary>
Expand All @@ -29,7 +29,7 @@ internal class Patcher : IPatcher
/// Example: (patch, position) => patch.GetOutputSample(position).</param>
public Patcher(
ImmutableArray<double> immutableArray,
BlockingCollection<AbstractPatch> patchCollection,
PatchCollection patchCollection,
Func<AbstractPatch, int, double> updateFunc)
{
this.immutableArray = immutableArray;
Expand All @@ -53,7 +53,7 @@ public double[] GetRange(int start, int length, AbstractPatch anotherPatch = nul
start,
length);

var patches = this.GetPatchesForRange(range);
var patches = this.patchCollection.GetPatchesForRange(range);

foreach (var patch in patches)
{
Expand All @@ -76,7 +76,7 @@ public double[] GetRange(int start, int length, AbstractPatch anotherPatch = nul
/// <returns>Value of sample.</returns>
public double GetValue(int position)
{
var patchForPosition = this.PatchForPosition(position);
var patchForPosition = this.patchCollection.GetPatchForPosition(position);

return patchForPosition is null
? this.immutableArray[position]
Expand All @@ -94,18 +94,18 @@ private void UpdateRange(AbstractFragment range, AbstractPatch patch)
}
}

private AbstractPatch[] GetPatchesForRange(AbstractFragment range)
{
var patchesForRange = this.patchCollection.Where(
p => p?.StartPosition <= range.EndPosition &&
p?.EndPosition >= range.StartPosition);
//private AbstractPatch[] GetPatchesForRange(AbstractFragment range)
//{
// var patchesForRange = this.patchCollection.Where(
// p => p?.StartPosition <= range.EndPosition &&
// p?.EndPosition >= range.StartPosition);

return patchesForRange.ToArray();
}
// return patchesForRange.ToArray();
//}

private AbstractPatch PatchForPosition(int position) =>
this.patchCollection.FirstOrDefault(
p => p?.StartPosition <= position &&
p?.EndPosition >= position);
//private AbstractPatch PatchForPosition(int position) =>
// this.patchCollection.FirstOrDefault(
// p => p?.StartPosition <= position &&
// p?.EndPosition >= position);
}
}
2 changes: 2 additions & 0 deletions CarefulAudioRepair/Processing/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ private void GenerateNewPatches(
}
});

this.tools.PatchCollection.Finalize();

progress.Report(100);
}

Expand Down
4 changes: 2 additions & 2 deletions CarefulAudioRepair/Processing/ScannerTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class ScannerTools : IDisposable
/// <param name="settings">Settings for processing audio.</param>
public ScannerTools(ImmutableArray<double> inputSamples, IAudioProcessingSettings settings)
{
this.PatchCollection = new BlockingCollection<AbstractPatch>();
this.PatchCollection = new PatchCollection();

this.Input = inputSamples;

Expand Down Expand Up @@ -65,7 +65,7 @@ public ScannerTools(ImmutableArray<double> inputSamples, IAudioProcessingSetting
/// <summary>
/// Gets collection of patches.
/// </summary>
public BlockingCollection<AbstractPatch> PatchCollection { get; }
public PatchCollection PatchCollection { get; }

/// <summary>
/// Gets input samples.
Expand Down
4 changes: 2 additions & 2 deletions NUnitTests/PatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PatcherTests
{
private const int _testArrayLength = 1000;
private ImmutableArray<double> _arrayValueEqualsIndex;
private BlockingCollection<AbstractPatch> _patchCollection;
private PatchCollection _patchCollection;
private IPatcher _patcher;

[SetUp]
Expand All @@ -21,7 +21,7 @@ public void Setup()
.Select(i => (double)i)
.ToImmutableArray();

_patchCollection = new BlockingCollection<AbstractPatch>();
_patchCollection = new PatchCollection();

_patcher = new Patcher(
_arrayValueEqualsIndex,
Expand Down

0 comments on commit 9b08d7a

Please sign in to comment.