NateRickard / Plugin.AudioRecorder

Audio Recorder plugin for Xamarin and Windows
MIT License
164 stars 68 forks source link

Concurrent streaming while recording not working as expected #32

Closed laxnarasi closed 5 years ago

laxnarasi commented 5 years ago

We are trying to implement an SDK in Xamarin to support IoS / Android / UWP consumers , where in we wanted to stream the audio while recording from microphone to CRIS to get the speech to text transalation while talking and return the response to the consumer so that he can display it on the screen progresively. We read the documentation of the Audio Recorder plugin for Xamarin and found that it supports concurrent streaming as per the documentation (It's also possible to get a stream to the recording audio data as it's being recorded). We also referred to the Bing Speech implementation provided in the documentation.

However, we are not able to achieve this functionality. Our observation was that when StartRecording is invoked, a wav file is being written to the GetAudioFilePath () - audiofilepath (ARS_recording.wav) and it always 0 bytes while being on a recording mode (via microphone). Only when StopRecording is invoked the stream writer internally flushes the bytes to the Wav file. Hence the below code recorder.GetAudioFileStream() does not streams any data during recording due to the fact that file ARS_recording.wav is always 0 bytes while recording.

// start recording audio var audioRecordTask = await recorder.StartRecording ();

using (var stream = recorder.GetAudioFileStream ()) { // this will begin sending the recording audio data as it continues to record var simpleResult = await bingSpeechClient.SpeechToTextSimple (stream, recorder.AudioStreamDetails.SampleRate, audioRecordTask); }

Please let us know if we are in the right path and the plugin will help us in solving the above mentioned usecase.

Thanks, Lakshmi Narasimhan V

laxnarasi commented 5 years ago

We figured out the issue and made a fix on writing the WaveHeader for the first stream of data and also the SampleRate needs to be provided as 44100 instead of 16000 from the calling client.

NateRickard commented 5 years ago

A bit late here, but...

The filestream is being written to during recording here. StopRecording only writes the WAV header after all recording is finished and it knows the length to insert in the header. It's not controlling the buffering or flushing of the file (that's left to the OS, and does likely not flush to the file until it's closed), but while the stream is being written to, you can also grab the stream of audio (NOT the file stream) and do other things with it, as shown. Glad you figured out the SampleRate you needed - that should be configurable via the lib but if you see issues there feel free to log another issue.

tongvietdung commented 1 year ago

Hi @NateRickard, the GetAudioFileStream() method only gives read only stream. I want to write WAV header with AudioFunctions.WriteWaveHeader(Stream stream, int channelCount, int sampleRate, int bitsPerSample) method. Is the stream parameter the same as stream from GetAudioFileStream()? Because I had error Stream is not writable. P/s: sorry if the question is trivial, but I just start using your package and had really few knowledge of audio recording.