I am finding very difficult to find a way to store the audio captured using OpenTk.NetStandard into a proper .WAV file in NetCore C#.
What I am looking for is a solution which will work when running on a Raspberry pi, so NAudio or any Windows specific method won't solve my problem.
I found a couple of other SO answers which show how to capture audio using opentk , but nothing about how to store it in a wav file.
This is an extract of the code which should read data from the microphone I took from anther SO question, I see the AudioCapture class is the one to :
const byte SampleToByte = 2;
short[] _buffer = new short[512];
int _sampling_rate = 16000;
double _buffer_length_ms = 5000;
var _recorders = AudioCapture.AvailableDevices;
int buffer_length_samples = (int)((double)_buffer_length_ms * _sampling_rate * 0.001 / BlittableValueType.StrideOf(_buffer));
using (var audioCapture = new AudioCapture(_recorders.First(), _sampling_rate, ALFormat.Mono16, buffer_length_samples))
{
audioCapture.Start();
int available_samples = audioCapture.AvailableSamples;
_buffer = new short[MathHelper.NextPowerOfTwo((int)(available_samples * SampleToByte / (double)BlittableValueType.StrideOf(_buffer) + 0.5))];
if (available_samples > 0)
{
audioCapture.ReadSamples(_buffer, available_samples);
int buf = AL.GenBuffer();
AL.BufferData(buf, ALFormat.Mono16, buffer, (int)(available_samples * BlittableValueType.StrideOf(_buffer)), audio_capture.SampleFrequency);
AL.SourceQueueBuffer(src, buf);
// TODO: I assume this is where the save to WAV file logic should be placed...
}
}
Any help would be appreciate!
Here is a .NET Core console program that will write a WAV file using Mono 16-bit data. There are links in the source code you should read through to understand what's going on with the values that are being written.
This will record 10 seconds of data and save it to a file in WAV format: