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.
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
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
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,37 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace NAudio.Wave | ||
{ | ||
/// <summary> | ||
/// Silence producing wave provider | ||
/// Useful for playing silence when doing a WASAPI Loopback Capture | ||
/// </summary> | ||
public class SilenceProvider : IWaveProvider | ||
{ | ||
/// <summary> | ||
/// Creates a new silence producing wave provider | ||
/// </summary> | ||
/// <param name="wf">Desired WaveFormat (should be PCM / IEE float</param> | ||
public SilenceProvider(WaveFormat wf) { WaveFormat = wf; } | ||
|
||
/// <summary> | ||
/// Read silence from into the buffer | ||
/// </summary> | ||
public int Read(byte[] buffer, int offset, int count) | ||
{ | ||
var end = offset + count; | ||
for (var pos = offset; pos < end; pos++) | ||
{ | ||
buffer[pos] = 0; | ||
} | ||
return count; | ||
} | ||
|
||
/// <summary> | ||
/// WaveFormat of this silence producing wave provider | ||
/// </summary> | ||
public WaveFormat WaveFormat { get; private set; } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Linq; | ||
using NAudio.Wave; | ||
using NUnit.Framework; | ||
|
||
namespace NAudioTests.WaveStreams | ||
{ | ||
[TestFixture] | ||
public class SilenceProviderTests | ||
{ | ||
[Test] | ||
public void CanReadSilence() | ||
{ | ||
var sp = new SilenceProvider(new WaveFormat(44100, 2)); | ||
var length = 1000; | ||
var b = Enumerable.Range(1, length).Select(n => (byte) 1).ToArray(); | ||
var read = sp.Read(b, 0, length); | ||
Assert.AreEqual(length, read); | ||
Assert.AreEqual(new byte[length], b); | ||
} | ||
|
||
[Test] | ||
public void RespectsOffsetAndCount() | ||
{ | ||
var sp = new SilenceProvider(new WaveFormat(44100, 2)); | ||
var length = 10; | ||
var b = Enumerable.Range(1, length).Select(n => (byte)1).ToArray(); | ||
var read = sp.Read(b, 2, 4); | ||
Assert.AreEqual(4, read); | ||
Assert.AreEqual(new byte[] { 1, 1, 0, 0, 0, 0, 1, 1, 1, 1}, b); | ||
} | ||
} | ||
} |