Android AudioRecord with CHANNEL_IN_STEREO read raw Audio buffer resulted in mixed Left and Right audio
Asked Answered
P

0

0

I am trying to record an audio with CHANNEL_IN_STEREO configuration in which I am telling audio recorded to record audio in two channel. But after storing buffer array from recorder Audio left channel and right channel is resulting as mix. I cannot separate Audio Left channel and Right channel.

Below is code written for same :

import androidx.appcompat.app.AppCompatActivity;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity {
        AudioRecord record;
        Boolean status = true;
        private int sampleRate = 44100;
        private int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
        private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
        int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
        byte buffer[] = new byte[8916];
        ByteArrayOutputStream leftbaos;
        ByteArrayOutputStream rightbaos;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button start = findViewById(R.id.start);
            Button stop = findViewById(R.id.stop);
            leftbaos = new ByteArrayOutputStream();
            rightbaos = new ByteArrayOutputStream();
            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startRecorder();
                }
            });
    
            stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    stopRecorder();
    
                }
            });
        }
    
        private void startRecorder() {
            status = true;
            record = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, minBufSize);
            Thread streamThread = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
    
    
                        record.startRecording();
                        while (status == true) {
    
                            int recorded_int  = record.read(buffer, 0, minBufSize);
                            byte leftChannelAudioData[] = new byte[buffer.length/2];
                            byte rightChannelAudioData[] = new byte[buffer.length/2];
                            for(int i = 0; i < buffer.length/2; i = i + 1)
                            {
                                leftChannelAudioData[i] = buffer[2*i];
                                rightChannelAudioData[i] =  buffer[2*i+1];
    
                            }
                            leftbaos.write(leftChannelAudioData,0,recorded_int/2);
                            rightbaos.write(rightChannelAudioData,0,recorded_int/2);
    
    
                        }
    
                    } catch(Exception e) {
                    }
                }
            });
            streamThread.start();
        }
        private void stopRecorder() {
            status = false;
            if(record!= null) {
                //storing left channel audio
                send(leftbaos,"/mydirectory/left.pcm");
                //storing right channel audio
                send(rightbaos,"/mydirectory/right.pcm");
                record.stop();
                record.release();
            }
        }
    
        private void send(ByteArrayOutputStream baos,String path) {
            File file = new File(path);
    
    
            try {
                FileOutputStream out = new FileOutputStream(file);
                baos.writeTo(out);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
    
    }

I have Used below mentioned permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

In above code I am storing ByteArrayOutputStream in PCM file once recording gets stopped. How can I separate Audio Left channel and Right channel from Audio Record.

Patter answered 14/12, 2019 at 21:30 Comment(17)
1.) Do you realize your question is using CHANNEL_IN_MONO? 2.) Do you get twice the data, if you switch to stereo? 3.) Do you understand how the channels are interleaved into buffer, during a stereo recording?Homoiousian
I have PCM player and listening to audio recording same on both channelPatter
OK, so you have no problems making a stereo recording, your problem is that both channels sound the same. Does your device have a stereo microphone? If not, in what way do you expect the channels to differ?Homoiousian
I have OnePlus 6 phone.i can see basically both channel recording have both the voices. Recording is not in stereo mode otherwise I will be able to seperate the left and right channel. I have tried same scenario on mi phone it's samePatter
I have record a call from my system app and seperated the channel but result is mix audio in both the channelPatter
#21114062 i have used this reference to separate left and right audioPatter
This question is all over the place. Now it sounds to me like you have a problem parsing the two channels, although it's not clear at what point you are parsing the media (from file, or from the mic?), and your question does not include any channel-parsing code. I am downvoting this question until you have edited it to include a clear, concise problem statement, and an MCVE.Homoiousian
i haved updated the code . @Homoiousian Please let me know the logic of seperating left channel and right channel at buffer level.Patter
OK, you are using ENCODING_PCM_8BIT. The answer you referenced for your separation logic uses ENCODING_PCM_16BIT. That's why it doubles up on the L's and the R's; they are each 2 bytes, right next to each other. You don't need to do that. It goes L/R/L/R/L/R... So your for loop should only have two lines in it, instead of 4. Does that make sense?Homoiousian
Also you have left in CHANNEL_IN_MONO. Please explain this. This is directly at odds with the text of your question. And you are saving leftChannelAudioData twice; once as left, once as right.Homoiousian
Actually i was experimenting the code hence posted the exact code. This is supposed to be working in different channel. Of you can try code at your end. Can tell me is it working for you at your device on channel based?Patter
You haven't fixed your for loop. That was the most important part.Homoiousian
posted the entire code please check nowPatter
You haven't changed it hardly at all. Look carefully at my comments. I've already given you the answer. Your for loop is incorrect. It still assumes the input is L/L/R/R.Homoiousian
check the for loop. now files are too small how to increase the buffer size of left and right pcm filePatter
Let us continue this discussion in chat.Patter
I think this link involves every answer related to Audio_Processing whether it is Pre-Processing or Post-Processing: Android_Audio_Processing_Using_WebRTC, You can also visit this reference: https://mcmap.net/q/245096/-how-to-record-microphone-to-more-compressed-format-during-webrtc-call-on-androidFrolicsome

© 2022 - 2024 — McMap. All rights reserved.