gpt4 book ai didi

android - 左右声道与麦克风录音分离

转载 作者:行者123 更新时间:2023-11-29 01:46:37 37 4
gpt4 key购买 nike

我正在尝试从麦克风录制并将录制的数据分别发送到左声道,右声道为零,但我的技术似乎不起作用。 我在 PCM 16 和单声道模式下使用音频记录和音轨,我似乎做错了什么?

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) 将模式从单声道更改为立体声 2) 读取缓冲区,格式如下 (L,L,R,R,L,L,R,R ....) 所以每两个字节被发送到一个 channel (pcm 16) 然后我将零设置到我想要取消的 channel 所以如果我只想要左 channel 工作我的缓冲区将是 (L,L,0, 0,L,L,0,0,.....)

关于android - 左右声道与麦克风录音分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21113562/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com