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.