У меня андроид ТВ + аудио карта с 2 микрофонами.
Я пытаюсь записать в разных файлах два стереоканала (по одному на файл) с помощью AudioRecorder.
Но я получаю одинаковые данные для обоих каналов.
Исходный пример данных pcm (2 байта на канал):
[79, -21, 79, -21, -1, -22, -1, -22, 58, -21, 58, -21, 13, -21, 13, -21, -114, -22, -114, -22, -27, -22, -27, -22,...
Когда я делю его по каналам, я получаю два одинаковых массива.
Мой код:
public class MainActivity extends AppCompatActivity {
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private AudioRecord recorder = null;
private Thread recordingThread = null;
private boolean isRecording = false;
int BufferElements2Rec;
int BytesPerElement = 2; // 2 bytes in 16bit format
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BufferElements2Rec = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
}
private void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING,
BufferElements2Rec * BytesPerElement);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
private void writeAudioDataToFile() {
// Write the output audio in byte
String firstFilePath = "/sdcard/origin.pcm";
FileOutputStream os = null;
try {
os = new FileOutputStream(firstFilePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
File rawLeftChannelDataFile = new File("/sdcard/first.pcm");
File rawRightChannelDataFile = new File("/sdcard/second.pcm");
FileOutputStream leftChannelFos = null;
FileOutputStream rightChannelFos = null;
try {
leftChannelFos = new FileOutputStream(rawLeftChannelDataFile);
rightChannelFos = new FileOutputStream(rawRightChannelDataFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream leftChannelBos = new BufferedOutputStream(leftChannelFos);
BufferedOutputStream rightChannelBos = new BufferedOutputStream(rightChannelFos);
DataOutputStream leftChannelDos = new DataOutputStream(leftChannelBos);
DataOutputStream rightChannelDos = new DataOutputStream(rightChannelBos);
while (isRecording) {
// gets the voice output from microphone to byte format
//int readSize = recorder.read(sData, 0, BufferElements2Rec);
byte[] bData = new byte[BufferElements2Rec];
recorder.read(bData, 0, BufferElements2Rec);
try {
// // writes the data to file from buffer
// // stores the voice buffer
byte[] leftChannelAudioData = new byte[bData.length / 2];
byte[] rightChannelAudioData = new byte[bData.length / 2];
for (int i = 0; i < bData.length / 2; i += 2) {
leftChannelAudioData[i] = bData[2 * i];
leftChannelAudioData[i + 1] = bData[2 * i + 1];
rightChannelAudioData[i] = bData[2 * i + 2];
rightChannelAudioData[i + 1] = bData[2 * i + 3];
}
os.write(bData, 0, bData.length);
leftChannelDos.write(leftChannelAudioData, 0, leftChannelAudioData.length);
rightChannelDos.write(rightChannelAudioData, 0, leftChannelAudioData.length);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
leftChannelDos.close();
rightChannelDos.close();
} catch (IOException e) {
e.printStackTrace();
}
}