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

标签 android audio wav

请同时查看我的其他问题,因为我认为它们是相关的:
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/

    相关文章:

    java - 间接扩展 Activity

    android - 在 Android 中滑动解锁动画

    python - 如何使用 Python 只读取目录中的 wav 文件?

    c++ - Wav文件停止功能

    python - 如何从 python 将 wav 转换为 flac?

    java - 为 Android 中的 Button(任何 View 类)添加了方法

    java - 如何修复 ExoPlayer 视频卡住 2.9.6

    java - Max for Live 与 JVAP 工具

    javascript - 如何在使用Kineticjs完成的动画中使用声音效果

    audio - 如何通过对等连接在浏览器之间播放和可视化音频流?