forked from naudio/NAudio
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConcatenatingSampleProvider and FollowedBy extension method
- Loading branch information
Showing
6 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
NAudio/Wave/SampleProviders/ConcatenatingSampleProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace NAudio.Wave.SampleProviders | ||
{ | ||
/// <summary> | ||
/// Sample Provider to concatenate multiple sample providers together | ||
/// </summary> | ||
public class ConcatenatingSampleProvider : ISampleProvider | ||
{ | ||
private readonly ISampleProvider[] providers; | ||
private int currentProviderIndex; | ||
|
||
/// <summary> | ||
/// Creates a new ConcatenatingSampleProvider | ||
/// </summary> | ||
/// <param name="providers">The source providers to play one after the other. Must all share the same sample rate and channel count</param> | ||
public ConcatenatingSampleProvider(IEnumerable<ISampleProvider> providers) | ||
{ | ||
if (providers == null) throw new ArgumentNullException("providers"); | ||
this.providers = providers.ToArray(); | ||
if (this.providers.Length == 0) throw new ArgumentException("Must provide at least one input", "providers"); | ||
if (this.providers.Any(p => p.WaveFormat.Channels != WaveFormat.Channels)) throw new ArgumentException("All inputs must have the same channel count", "providers"); | ||
if (this.providers.Any(p => p.WaveFormat.SampleRate != WaveFormat.SampleRate)) throw new ArgumentException("All inputs must have the same sample rate", "providers"); | ||
} | ||
|
||
/// <summary> | ||
/// The WaveFormat of this Sample Provider | ||
/// </summary> | ||
public WaveFormat WaveFormat { get { return providers[0].WaveFormat; } } | ||
|
||
/// <summary> | ||
/// Read Samples from this sample provider | ||
/// </summary> | ||
public int Read(float[] buffer, int offset, int count) | ||
{ | ||
var read = 0; | ||
while (read < count && currentProviderIndex < providers.Length) | ||
{ | ||
var needed = count - read; | ||
var readThisTime = providers[currentProviderIndex].Read(buffer, read, needed); | ||
read += readThisTime; | ||
if (readThisTime == 0) currentProviderIndex++; | ||
} | ||
return read; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
NAudioTests/WaveStreams/ConcatenatingSampleProviderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Linq; | ||
using NAudio.Wave.SampleProviders; | ||
using NUnit.Framework; | ||
|
||
namespace NAudioTests.WaveStreams | ||
{ | ||
[TestFixture] | ||
public class ConcatenatingSampleProviderTests | ||
{ | ||
[Test] | ||
public void CanPassASingleProvider() | ||
{ | ||
// arrange | ||
const int expectedLength = 5000; | ||
var input = new TestSampleProvider(44100, 2, expectedLength); | ||
var concatenator = new ConcatenatingSampleProvider(new[] {input}); | ||
var buffer = new float[2000]; | ||
var totalRead = 0; | ||
|
||
// act | ||
while (true) | ||
{ | ||
var read = concatenator.Read(buffer, 0, buffer.Length); | ||
if (read == 0) break; | ||
totalRead += read; | ||
Assert.That(totalRead <= expectedLength); | ||
} | ||
Assert.That(totalRead == expectedLength); | ||
} | ||
|
||
[Test] | ||
public void CanPassTwoProviders() | ||
{ | ||
// arrange | ||
var expectedLength = 100; | ||
var input1 = new TestSampleProvider(44100, 2, 50); | ||
var input2 = new TestSampleProvider(44100, 2, 50); | ||
var concatenator = new ConcatenatingSampleProvider(new[] { input1, input2 }); | ||
var buffer = new float[2000]; | ||
|
||
var read = concatenator.Read(buffer, 0, buffer.Length); | ||
Assert.AreEqual(expectedLength, read, "read == expectedLength"); | ||
Assert.AreEqual(49, buffer[49]); | ||
Assert.AreEqual(0, buffer[50]); | ||
Assert.AreEqual(49, buffer[99]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters