After a bit of extra perseverance I have figured it out,
And it's as simple as:
Ollie
And it's as simple as:
sample32 = BitConverter.ToSingle(buffer, index);
WASAPI Loopback's buffer byte array at DataAvailable contains 32 bit single-precision floats (not 16 bit shorts), so to get a sample value between 1f and -1f you need to use the following code before SampleAggregator: void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
byte[] buffer = e.Buffer;
int bytesRecorded = e.BytesRecorded;
WriteToFile(buffer, bytesRecorded);
int bufferIncrement = (int)(this.waveIn.WaveFormat.BlockAlign / this.waveIn.WaveFormat.Channels);
int bitsPerSample = this.waveIn.WaveFormat.BitsPerSample;
for (int index = 0; index < e.BytesRecorded; index += bufferIncrement)
{
float sample32 = 0;
if (bitsPerSample <= 16) // Presume 16-bit PCM WAV
{
short sample16 = (short)((buffer[index + 1] << 8) | buffer[index + 0]);
sample32 = sample16 / 32768f;
}
else if (bitsPerSample <= 32) // Presume 32-bit IEEE Float WAV
{
sample32 = BitConverter.ToSingle(buffer, index);
}
else
{
throw new Exception(bitsPerSample + " Bits Per Sample Is Not Supported!");
}
// Clip Sample - Prevents Issues Elsewhere
if (sample32 > 1.0f)
sample32 = 1.0f;
if (sample32 < -1.0f)
sample32 = -1.0f;
sampleAggregator.Add(sample32);
}
}
Hope this helps anyone else in future :)Ollie