Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record Audio in .NET Core

As the title suggests, I'm looking to capture audio from a microphone in .NET Core. It's important for me that it's cross platform. I'm doing a lot of video processing in OpenCV on Windows, Linux and OSX already. Audio is a missing piece of the puzzle for me.

like image 681
Wojtek Turowicz Avatar asked Sep 21 '18 13:09

Wojtek Turowicz


1 Answers

OpenTK.NETCore is a really good cross platform port for OpenTK.

Using it's AudioCapture class I have been able to capture microphone data across different OS platforms.

using (var audioCapture = new AudioCapture(_recorders.First(), _sampling_rate, ALFormat.Mono16, buffer_length_samples))
{
    audioCapture.Start();

    int available_samples = audioCapture.AvailableSamples;

    _buffer = _pool.Rent(MathHelper.NextPowerOfTwo((int)((available_samples * _sampleToByte / (double)BlittableValueType.StrideOf(_buffer)) + 0.5)));

    if (available_samples > 0)
    {
        audioCapture.ReadSamples(_buffer, available_samples);
    }
    else
    {
        _pool.Return(_buffer);
    }
}

Edit: Based on Parrot project in OpenTK samples.

like image 164
Wojtek Turowicz Avatar answered Sep 19 '22 19:09

Wojtek Turowicz