2 years ago

#57183

test-img

MaminDev2

Add RIFF header to a buffer

it's the second week which I'm trying to just record audio in xamarin which has an RIFF header. I tried Audio Recorder Plugin and Audio Record and Media Recorder . I asked many questions but got no answer.

the easiest way was Audio Recorder Plugin, but the output hasn't RIFF header. the output of Media Recorder was .3gp which I couldn't convert it to .wav. and the output of Media Recorder was .pcm which also couldn't convert to .wav

here is the last code I tried :

#region Properties
    int SAMPLING_RATE_IN_HZ = 44100;
    ChannelIn CHANNEL_CONFIG = ChannelIn.Mono;
    Android.Media.Encoding AUDIO_FORMAT = Android.Media.Encoding.Pcm16bit;
    int BUFFER_SIZE_FACTOR = 2;
    int BUFFER_SIZE;
    bool RecordingInProgress = false;
    private AudioRecord recorder = null;
    #endregion
    public void Record()
    {
        BUFFER_SIZE = AudioRecord.GetMinBufferSize(SAMPLING_RATE_IN_HZ,
        CHANNEL_CONFIG, AUDIO_FORMAT) * BUFFER_SIZE_FACTOR;
        recorder = new AudioRecord(AudioSource.Mic, SAMPLING_RATE_IN_HZ,
            CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE);

        recorder.StartRecording();
        RecordingInProgress = true;
        RecordingTask();
    }
    public Task RecordingTask()
    {
        return Task.Run(() =>
        {
            string path = "appdir/demo.pcm";
            MemoryStream buffer = new MemoryStream(BUFFER_SIZE);
            FileOutputStream outStream = new FileOutputStream(path);
            var demo2 = RecordingInProgress;
            while (RecordingInProgress)
            {
                int result = recorder.Read(buffer.GetBuffer(), 0, BUFFER_SIZE);
                if (result < 0)
                {
                    throw new Exception("Reading of audio buffer failed: ");
                }
                   outStream.Write(buffer.GetBuffer(), 0, BUFFER_SIZE);
            }
            
        });
    }
    public void Stop()
    {
        if (null == recorder)
        {
            return;
        }

        RecordingInProgress = false;

        recorder.Stop();

        recorder.Release();

        recorder = null;
    }

}

this code makes a .pcm file that can't convert to anything with even cloud converters. I also tried this :

NWaves.Audio.WaveFile waveFile = new NWaves.Audio.WaveFile(buffer.GetBuffer());
                waveFile.SaveTo(new FileStream("appdir/demo.wav", FileMode.Create));

insted of outStream.Write(buffer.GetBuffer(), 0, BUFFER_SIZE); at the bottom of while closing block but it says : No RIFF found

there is about 4 or 5 way to record audio. but a package like Nwaves can't work with any of them.

the last try I want to do is add RIFF header to the recorded audio buffer(bytes) programmatically or convert .3gp or .pcm to .wav programmatically .

summery: someone help me to record an audio in xamarin which Nwaves can work with. thanks

android

xamarin

audio

encoding

wav

0 Answers

Your Answer

Accepted video resources