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.
CHANNEL_IN_MONO
? 2.) Do you get twice the data, if you switch to stereo? 3.) Do you understand how the channels are interleaved intobuffer
, during a stereo recording? – HomoiousianENCODING_PCM_8BIT
. The answer you referenced for your separation logic usesENCODING_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 yourfor
loop should only have two lines in it, instead of 4. Does that make sense? – HomoiousianCHANNEL_IN_MONO
. Please explain this. This is directly at odds with the text of your question. And you are savingleftChannelAudioData
twice; once as left, once as right. – Homoiousianfor
loop. That was the most important part. – Homoiousianfor
loop is incorrect. It still assumes the input is L/L/R/R. – HomoiousianAudio_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-android – Frolicsome