其他分享
首页 > 其他分享> > android – 拆分CHANNEL_IN_STEREO的两个AudioRecord通道

android – 拆分CHANNEL_IN_STEREO的两个AudioRecord通道

作者:互联网

我正在开发一个使用Android手机立体声录音的项目(注3).但我需要从不同的渠道(右,左)分割数据.知道怎么做吗?

现在,我使用AudioRecord录制内置麦克风的声音.我可以录制,将声音保存为.raw和.wav文件.

一些代码如下.

private int audioSource = MediaRecorder.AudioSource.MIC;  
private static int sampleRateInHz = 44100;  
private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;  
private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;


bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,  
            channelConfig, audioFormat);  

audioRecord = new AudioRecord(audioSource, sampleRateInHz,  
            channelConfig, audioFormat, bufferSizeInBytes);

// some other codes....

//get the data from audioRecord
readsize = audioRecord.read(audiodata, 0, bufferSizeInBytes); 

解决方法:

最后,我得到了答案.我使用了Android手机的立体声录音. audioFormat是PCM_16BIT.

private int audioSource = MediaRecorder.AudioSource.MIC;  
private static int sampleRateInHz = 48000;  
private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;  
private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;  

这意味着存储在缓冲区中的数据如下.

leftChannel data: [0,1],[4,5]...
rightChannel data: [2,3],[6,7]...

所以分割立体声记录数据的代码.

readSize = audioRecord.read(audioShortData, 0, bufferSizeInBytes);
for(int i = 0; i < readSize/2; i = i + 2)
{
       leftChannelAudioData[i] = audiodata[2*i];
       leftChannelAudioData[i+1] = audiodata[2*i+1]; 
       rightChannelAudioData[i] =  audiodata[2*i+2];
       rightChannelAudioData[i+1] = audiodata[2*i+3];
}

然后,您可以将数据写入文件.

leftChannelFos = new FileOutputStream(rawLeftChannelDataFile);
rightChannelFos = new FileOutputStream(rawRightChannelDataFile);
leftChannelBos = new BufferedOutputStream(leftChannelFos);
rightChannelBos = new BufferedOutputStream(rightChannelFos);
leftChannelDos = new DataOutputStream(leftChannelBos);
rightChannelDos = new  DataOutputStream(rightChannelBos);

leftChannelDos.write(leftChannelAudioData);
rightChannelDos.write(rightChannelAudioData);

快乐的编码!

标签:android,record,audiorecord
来源: https://codeday.me/bug/20190930/1837194.html