0

I am trying to record audio from a microphone within a WFP (.Net Core) application.

From what I have found online, it looks like the best candidate for recording audio in .Net Core is OpenAL. You can get OpenAL from the nuget package OpenTK.NetStandard.

I was able to use an example to implement some code that makes the program play the audio as it is capturing it (a bit like an echo effect, which is why I believe the example is called Parrot).

What I am missing and cannot find any help online for, is how to save the audio to a file instead of just playing it back right away.

I think the part of the code that should save the audio to a file would go somewhere in here:

if (available_samples > 0)
{
    audio_capture.ReadSamples(buffer, available_samples);

    int buf = AL.GenBuffer();
    AL.BufferData(buf, 
        ALFormat.Mono16, 
        buffer, 
        available_samples * BlittableValueType.StrideOf(buffer), 
        audio_capture.SampleFrequency);
    AL.SourceQueueBuffer(src, buf);

    StatusBarService.WriteToStatusBar("Samples consumed: " + available_samples);

    if (AL.GetSourceState(src) != ALSourceState.Playing)
        AL.SourcePlay(src);
}

I think the data I need to save is in one of the following variables:

  • buffer
  • buf
  • AL.GetSourceState(src)

I could only find 1 single example code that saves to a file, but it is in C++, 8 years old, and it does a few things I wouldn't know how to port to C#:

  • it creates a WAVEHEADER sWaveHeader object which holds info about the audio file
  • it creates vector<ALbyte> bufferVector object which is saved to file (I don't have the ALbyte type in the OpenTK.NetStandard nuget
  • writes to file using this method fwrite( &bufferVector[0], sizeof( ALbyte ), 1, pFile ); (where pFile is a FILE* pFile)

Has anyone been able to save audio from a microphone to file .Net Core? I really cannot find an example!

Any help would be really appreciated! :)

Kappacake
  • 1,826
  • 19
  • 38
  • 1
    check this out: https://www.c-sharpcorner.com/blogs/audio-recorder-in-c-sharp1 – rufw91 Aug 30 '20 at 06:03
  • @Rufw91 Thanks for the link. I thought you could not import those Windows internal dlls in Net Core! I ended up using SOX. however, i need to add the executable file and all its dependencies to my built code, so it’s not my fav option. Im going to move forward for a bit using SOX and then will look into building a set of interfaces for each OS following https://scientificprogrammer.net/2019/08/18/building-net-core-audio-application-part-1/ – Kappacake Aug 30 '20 at 07:39

0 Answers0