Hello, I have managed to combine NAudio's MixingSampleProvider and MeteringSampleProvider together into one class, in order to provide a volume peak display for the sample provider in real time.
It actually works as expected but only if I use WaveOut.
If I use AsioOut instead, when the AsioOut is played I get an error to the effect that "This object belongs to a different thread, and the calling thread cannot access the object".
(No such error occurs with WaveOut)
The following is the Read Function of my custom ISampleProvider (translated from VB to C#):
public int Read(float[] buffer, int offset, int count)
{
int outputSamples = 0;
this.sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, count);
lock (sources) {
//cycle through each input source, backwards
for (int sourceindex = sources.Count - 1; sourceindex >= 0; sourceindex += -1) {
if (sources(sourceindex).hasData) {
int samplesRead = sources(sourceindex).Read(this.sourceBuffer, 0, count);
int outIndex = offset;
for (int inIndex = 0; inIndex <= samplesRead - 2; inIndex += 2) {
if (inIndex >= outputSamples) {
buffer(outIndex) = this.sourceBuffer(inIndex) * MasterVolFac;
buffer(outIndex + 1) = this.sourceBuffer(inIndex + 1) * MasterVolFac;
} else {
buffer(outIndex) += this.sourceBuffer(inIndex) * MasterVolFac;
buffer(outIndex + 1) += this.sourceBuffer(inIndex + 1) * MasterVolFac;
}
//******************* for metering display
if (ShowOutputMeters) {
for (int chno = 0; chno <= channels - 1; chno++) {
thissampleValue = Math.Abs(buffer(offset + inIndex + chno));
maxSamples(chno) = Math.Max(maxSamples(chno), thissampleValue);
sampleCount += 1;
if (sampleCount >= SamplesPerNotification) {
if (StreamVolume != null) {
StreamVolume(this, args);
}
sampleCount = 0;
Array.Clear(maxSamples, 0, channels);
}
}
}
//******************
outIndex += 2;
}
outputSamples = Math.Max(samplesRead, outputSamples);
}
}
}
return outputSamples;
}
Cheers,
Michael