split two channels of AudioRecord of CHANNEL_IN_STEREO

前端 未结 1 1557
生来不讨喜
生来不讨喜 2020-12-16 08:44

I\'m working on a project using stereo record of the Android phones (note 3). But I need to split the data from different channels (right, left). Any idea of how to perform

相关标签:
1条回答
  • 2020-12-16 09:05

    Finally, I got the answers. I used stereo record of android phone. And the audioFormat is 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;  
    

    which means the data stored in buffer as follows.

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

    So the code of splitting data of stereo record.

    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];
    }
    

    Then you can write the data to file.

    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);
    

    Happy coding!

    0 讨论(0)
提交回复
热议问题