Browse Source
Merge pull request #312 from azyobuzin/StereoToMonoOffset
StereoToMonoSampleProvider.Read fills incorrect range
pull/335/head
Mark Heath
7 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
35 additions and
1 deletions
-
NAudio/Wave/SampleProviders/StereoToMonoSampleProvider.cs
-
NAudioTests/WaveStreams/StereoToMonoSampleProviderTests.cs
|
|
@ -50,7 +50,7 @@ namespace NAudio.Wave.SampleProviders |
|
|
|
if (sourceBuffer == null || sourceBuffer.Length < sourceSamplesRequired) sourceBuffer = new float[sourceSamplesRequired]; |
|
|
|
|
|
|
|
var sourceSamplesRead = sourceProvider.Read(sourceBuffer, 0, sourceSamplesRequired); |
|
|
|
var destOffset = offset / 2; |
|
|
|
var destOffset = offset; |
|
|
|
for (var sourceSample = 0; sourceSample < sourceSamplesRead; sourceSample += 2) |
|
|
|
{ |
|
|
|
var left = sourceBuffer[sourceSample]; |
|
|
|
|
|
@ -31,5 +31,39 @@ namespace NAudioTests.WaveStreams |
|
|
|
Assert.AreEqual(1, mono.WaveFormat.Channels); |
|
|
|
Assert.AreEqual(44100, mono.WaveFormat.SampleRate); |
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
public void CorrectOffset() |
|
|
|
{ |
|
|
|
var stereoSampleProvider = new TestSampleProvider(44100, 2) |
|
|
|
{ |
|
|
|
UseConstValue = true, |
|
|
|
ConstValue = 1 |
|
|
|
}; |
|
|
|
var mono = stereoSampleProvider.ToMono(); |
|
|
|
|
|
|
|
var bufferLength = 30; |
|
|
|
var offset = 10; |
|
|
|
var samples = 10; |
|
|
|
|
|
|
|
// [10,20) in buffer will be filled with 1
|
|
|
|
var buffer = new float[bufferLength]; |
|
|
|
var read = mono.Read(buffer, offset, samples); |
|
|
|
Assert.AreEqual(samples, read, "samples read"); |
|
|
|
|
|
|
|
for (int i = 0; i < bufferLength; i++) |
|
|
|
{ |
|
|
|
var sample = buffer[i]; |
|
|
|
|
|
|
|
if (i < offset || i >= offset + samples) |
|
|
|
{ |
|
|
|
Assert.AreEqual(0, sample, "not in Read range"); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
Assert.AreNotEqual(0, sample, "in Read range"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |