gpt4 book ai didi

android - AudioRecord 类究竟是如何工作的?

转载 作者:行者123 更新时间:2023-12-03 00:18:26 30 4
gpt4 key购买 nike

请同时查看我的其他问题,因为我认为它们是相关的:
Question 1
Question 2
Question 3
这是我正在使用的代码,当我按下按钮时,它将麦克风获得的音频信号传递到扬声器:

public class MainActivity extends Activity {
AudioManager am = null;
AudioRecord record =null;
AudioTrack track =null;
final int SAMPLE_FREQUENCY = 44100;
final int SIZE_OF_RECORD_ARRAY = 1024; // 1024 ORIGINAL
final int WAV_SAMPLE_MULTIPLICATION_FACTOR = 1;
int i= 0;
boolean isPlaying = false;
private volatile boolean keepThreadRunning;
private RandomAccessFile stateFile, stateFileTemp;
private File delFile, renFile;
String stateFileLoc = Environment.getExternalStorageDirectory().getPath();
class MyThread extends Thread{
private volatile boolean needsToPassThrough;
// /*
MyThread(){
super();
}

MyThread(boolean newPTV){
this.needsToPassThrough = newPTV;
}
// */

// /*
@Override
public void run(){
// short[] lin = new short[SIZE_OF_RECORD_ARRAY];
byte[] lin = new byte[SIZE_OF_RECORD_ARRAY];
int num = 0;
// /*
if(needsToPassThrough){
record.startRecording();
track.play();
}
// */
while (keepThreadRunning) {
// while (!isInterrupted()) {
num = record.read(lin, 0, SIZE_OF_RECORD_ARRAY);
for(i=0;i<lin.length;i++)
lin[i] *= WAV_SAMPLE_MULTIPLICATION_FACTOR;
track.write(lin, 0, num);
}
// /*
record.stop();
track.stop();
record.release();
track.release();
// */
}
// */

// /*
public void stopThread(){
keepThreadRunning = false;
}
// */
}

MyThread newThread;

private void init() {
int min = AudioRecord.getMinBufferSize(SAMPLE_FREQUENCY, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_FREQUENCY, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, min);
int maxJitter = AudioTrack.getMinBufferSize(SAMPLE_FREQUENCY, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, SAMPLE_FREQUENCY, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);
am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_COMMUNICATION);

try {
stateFile = new RandomAccessFile(stateFileLoc+"/appState.txt", "rwd");
stateFileTemp = new RandomAccessFile(stateFileLoc+"/appStateTemp.txt", "rwd");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
delFile = new File(stateFileLoc+"/appState.txt");
renFile = new File(stateFileLoc+"/appStateTemp.txt");

}

@Override
protected void onResume(){
super.onResume();
// newThread.stopThread();
Log.d("MYLOG", "onResume() called");
init();
keepThreadRunning = true;
try {
if(stateFile.readInt() == 1){
isPlaying = true;
Log.d("MYLOG", "readInt == 1");
}
else{
isPlaying = false;
Log.d("MYLOG", "readInt <> 1");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// */


// newThread = new MyThread(true);
newThread = new MyThread(isPlaying);
newThread.start();

}

@Override
protected void onPause(){
super.onPause();
Log.d("MYLOG", "onPause() called");
newThread.stopThread();
// android.os.Process.killProcess(android.os.Process.myPid());
try {
if(isPlaying)
stateFileTemp.writeInt(1);
else
stateFileTemp.writeInt(0);

delFile.delete();

renFile.renameTo(delFile);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.MODE_IN_COMMUNICATION);
Log.d("MYLOG","onCreate() called");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
protected void onDestroy() {
super.onDestroy();
newThread.stopThread();
// android.os.Process.killProcess(android.os.Process.myPid());
// killProcess(android.os.Process.myPid());
// newThread.interrupt();
delFile.delete();
Log.d("MYLOG", "onDestroy() called");
}

public void passStop(View view){
Button playBtn = (Button) findViewById(R.id.button1);
// /*
if(!isPlaying){
record.startRecording();
track.play();
isPlaying = true;
playBtn.setText("Pause");
}
else{
record.stop();
track.pause();
isPlaying=false;
playBtn.setText("Pass through");
}
// */
}

文件 appState.txtappStateTemp.txt被添加以保存应用程序上次失去焦点时是否正在执行传递,但这在这里可能不是很重要。我想知道的是:
  • record.read() 时会发生什么被调用而不调用 record.startrecording() ?
  • SIZE_OF_RECORD_ARRAY有什么意义?我认为它至少应该是 AudioRecord.getMinBufferSize() 返回的值但在这个程序中,即使我将其设置为 1,它也不会影响输出。
  • 如果我使用 16 位 PCM 编码,我至少需要一个短变量来存储音频样本的数字等价物。然而,在这段代码中,即使我将 lin 变量从短数组更改为字节数组,输出也没有明显变化。那么 read 函数是如何将数字样本存储在数组中的呢?它会自动为每个样本分配 2 个字节的元素吗?如果是这样的话,它是小端还是大端?
  • 最佳答案

    问题 1 和 3 应该很容易让您检查您的应用程序,但这里是:

    1: What happens when record.read() is called without calling record.startrecording() ?



    我希望没有来自底层音频输入流的数据流,并且 read()因此返回 0 或可能返回错误代码,表示未读取任何数据。

    2: What is the significance of SIZE_OF_RECORD_ARRAY? I thought it should be at least the value returned by AudioRecord.getMinBufferSize() but in this program it doesn't affect the output at all even if I set it to 1.


    getMinBufferSize 的值在调用 AudioRecord 时指定缓冲区大小很重要。构造函数。您正在使用 SIZE_OF_RECORD_ARRAY 更改的内容只是您每次调用 read() 时读取的数据量- 虽然调用 read() 并不是一个特别好的主意每个字节一次(因为所有这些函数调用的开销),我可以想象它仍然可以工作。

    3: If I use 16 bit PCM encoding I need at least a short variable to store the digital equivalent of the audio samples. However in this code even if I change the lin variable from short array to byte array, there is no apparent change in the output. So how does the read function store the digital samples in the array? Does it automatically allocate 2 byte elements for each sample? If that is the case, does it do it as little endian or big endian?



    底层 native 代码始终使用 byte版本。 short版本只是 byte 的包装。版本。所以是的,在这种情况下,每个样本都将使用一对字节。
    至于字节序;在绝大多数 Android 设备上,它将是小端的。

    关于android - AudioRecord 类究竟是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18393947/

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