Click here to Skip to main content
15,936,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to record audio data from microphone in .wav file and playback it. I need also actual data (amplitude) for ploting graph so i am using AudioUnit. I setup inputCallBack and renderCallBack for AudioUnit object But i don't know how to write AudioBuffers to .wav file from render_CallBack mehtod. I attached my code what i have tried till now.

Please help me...

Step - 1

C#
AudioStreamBasicDescription audioStreamBasicDesc;
AudioUnit.AudioUnit audioUnit;
string m_recordingFilePath;
ExtAudioFile extAudioFileObj;


public override void ViewDidLoad()
            {
                base.ViewDidLoad();


                audioStreamBasicDesc.SampleRate = 16000;
                audioStreamBasicDesc.Format = AudioFormatType.LinearPCM;
                audioStreamBasicDesc.FramesPerPacket = 1;
                audioStreamBasicDesc.ChannelsPerFrame = 1;
                audioStreamBasicDesc.BytesPerFrame = audioStreamBasicDesc.ChannelsPerFrame * sizeof(short);
                audioStreamBasicDesc.BytesPerPacket = audioStreamBasicDesc.ChannelsPerFrame * sizeof(short);
                audioStreamBasicDesc.BitsPerChannel = 16;
                audioStreamBasicDesc.Reserved = 0;
                audioStreamBasicDesc.FormatFlags = AudioFormatFlags.IsSignedInteger | AudioFormatFlags.IsPacked;

var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var tmp = Path.Combine(documents, "..", "tmp");
            m_recordingFilePath = Path.Combine(tmp, String.Format("{0}.wav", "MyFile" + DateTime.Now.ToString("MM-dd-yyyy HH-mm-ss", CultureInfo.InvariantCulture)));       
extAudioFileObj = ExtAudioFile.CreateWithUrl(CFUrl.FromFile(m_recordingFilePath), AudioFileType.WAVE, audioStreamBasicDesc, AudioFileFlags.EraseFlags);
                prepareAudioUnit();
       }


Step - 2
C#
public void prepareAudioUnit()
        {

            var _audioComponent =   AudioComponent.FindComponent(AudioTypeOutput.Remote);

            audioUnit = _audioComponent.CreateAudioUnit();

            audioUnit = new AudioUnit.AudioUnit(_audioComponent);

            audioUnit.SetEnableIO(true,
                AudioUnitScopeType.Input,
                1 // Remote Input
            );

            // setting audio format
            audioUnit.SetAudioFormat(audioStreamBasicDesc,
                AudioUnitScopeType.Output,
                1
            );                                    

            audioUnit.SetInputCallback(input_CallBack, AudioUnitScopeType.Input, 1);

            audioUnit.SetRenderCallback(render_CallBack, AudioUnitScopeType.Global, 0);


            audioUnit.Initialize();
            audioUnit.Start();
        }


Step - 3

C#
AudioUnitStatus input_CallBack(AudioUnitRenderActionFlags actionFlags, AudioTimeStamp timeStamp, uint busNumber, uint numberFrames, AudioUnit.AudioUnit audioUnit)
        {
            return AudioUnitStatus.NoError;
        }


Step - 4

C#
AudioUnitStatus render_CallBack(AudioUnitRenderActionFlags actionFlags, AudioTimeStamp timeStamp, uint busNumber, uint numberFrames, AudioBuffers data)
        {
            // getting microphone input signal
            var status = audioUnit.Render(ref actionFlags,
                             timeStamp,
                             1, // Remote input
                             numberFrames,
                             data);
            if (status != AudioUnitStatus.OK)
            {
                return status;
            }

            //get pointer to buffer
            var outP = data[0].Data;
            unsafe
            {
                var outPtr = (int*)outP.ToPointer();
                for (int i = 0; i < numberFrames; i++)
                {
                    var val = *outPtr;
                    outPtr++;
                    //lastestPickVal = val; //This is for ploting graph
                    Console.WriteLine(val);
                }
            }
        extAudioFileObj.ClientDataFormat = audioStreamBasicDesc;
        var err = extAudioFileObj.Write(numberFrames, data); //Here i am trying to write data into .wav file and file is generated also but corrupted file without actual data (create file size is approx 4kb or 100 kb )
            Console.WriteLine("OUTPUT" + busNumber);
            return AudioUnitStatus.NoError;
        }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900