Left Right Channel separation from mic recording

徘徊边缘 提交于 2021-02-19 05:37:05

问题


I am trying to record from mic and send the recorded data to only the left channel separately having zeroes on the right channel but my technique does not seem to work.. I am using audio record and audio track with PCM 16 and mono Mode what do I seem to do wrong ?

package com.example.leftrighttest;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int freq=44100;
        final int bufferSize = (AudioRecord.getMinBufferSize(freq,AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT ));

        AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, freq,AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT, bufferSize );


        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                (int) freq,AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT, bufferSize,
                AudioTrack.MODE_STREAM);

        audioTrack.setPlaybackRate(freq);

        final byte[] buffer = new byte[bufferSize];
       audioRecord.startRecording();




                    byte[] byteBufferFinal = new byte[bufferSize*2]; 

                    //LL RR LL RR LL RR 
                    while(true)
                    {
                    audioRecord.read(buffer, 0, bufferSize);
                   for(int k = 0, index = 0; index < byteBufferFinal.length - 4; k=k+2){
                        byteBufferFinal[index] = buffer[k]; // LEFT {0,1/4,5/8,9/12,13;...}
                        //System.out.println(byteBufferFinal[index]);
                        byteBufferFinal[index+1] = buffer[k+1];
                       // System.out.println(byteBufferFinal[index+1]);
                        index = index + 2;
                        byteBufferFinal[index] =0; //byteBuffer2[k]; // RIGHT {2,3/6,7/10,11;...}
                        //System.out.println(byteBufferFinal[index]);
                        byteBufferFinal[index+1] =0;// byteBuffer2[k+1];
                       // System.out.println(byteBufferFinal[index+1]);
                        index = index + 2;
                    }
                       audioTrack.write( byteBufferFinal, 0, bufferSize*2);
                       audioTrack.play();
                    }

    }



}

回答1:


I figured it out so the proper solution was 1)to change the mode from mono to Stereo 2)read the buffer and the format will be as follows (L,L,R,R,L,L,R,R....) so every two bytes are sent to one channel (pcm 16) then I set zeroes to the channel I want to cancel so If I want ONLY the left channel to work my buffer will be (L,L,0,0,L,L,0,0,.....)



来源:https://stackoverflow.com/questions/21113562/left-right-channel-separation-from-mic-recording

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!