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 theALbyte
type in the OpenTK.NetStandard nuget - writes to file using this method
fwrite( &bufferVector[0], sizeof( ALbyte ), 1, pFile );
(where pFile is aFILE* 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! :)