AudioTrack的意外声音输出

时间:2014-04-27 21:50:48

标签: android audiotrack

我正在尝试连续播放正弦波。按住按钮时,正弦波的频率发生变化。一旦我松开按钮,它就会恢复正常频率。

当我运行以下代码时,输​​出声音会定期发出“咔嗒”声(尖锐的声压变化) - 每秒约5次点击 - 而不是播放纯正弦音。

一个有趣的行为是,当我按住C5按钮时没有点击。但是当我按住C4按钮时,点击仍然存在。

这个问题的原因是什么?

public class MainActivity extends Activity {
Thread t;
private Button buttonC4, buttonC5;

float buttonC4_val = 0;
float buttonC5_val = 0;

int sr = 44100;
boolean isRunning = true;

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

    // button initialization
    buttonC4 = (Button) this.findViewById(R.id.button_c4);
    buttonC5 = (Button) this.findViewById(R.id.button_c5);

    buttonC4.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                //insert code here
                buttonC4_val = 300;
                buttonC4.setText("You Clicked ME!!!");
                return true;
            case MotionEvent.ACTION_UP:
                //insert code here
                buttonC4_val = 0;
                buttonC4.setText("C4");
                return true;
            }
            return false;
        }
    });

    buttonC5.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                //insert code here
                buttonC5_val = 500;
                buttonC5.setText("You Clicked ME!!!");
                return true;
            case MotionEvent.ACTION_UP:
                //insert code here
                buttonC5_val = 0;
                buttonC5.setText("C5");
                return true;
            }
            return false;
        }
    });

    t = new Thread() {
        public void run() {
            setPriority(Thread.MAX_PRIORITY);

    int bufferSize = AudioTrack.getMinBufferSize(sr,
            AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

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

    audioTrack.play();

    // synthesis loop
     while(isRunning){                  
           double upFreq = (double) buttonC4_val + (double) buttonC5_val;   
       short samples[] = new short[bufferSize];
           int amp = 10000;
           double twopi = 8.*Math.atan(1.);
           double fr = 440.f;
           double ph = 0.0;                 
           fr =  440 + upFreq;

           for(int i=0; i < bufferSize; i++){
         samples[i] = (short) (amp*Math.sin(ph));
         ph += twopi*fr/sr;
           }

      audioTrack.write(samples, 0, bufferSize);
            }
    audioTrack.stop();
    audioTrack.release();
        }
    };
    t.start();
}
}

1 个答案:

答案 0 :(得分:2)

看起来你正在用正弦波填充整个samples缓冲区 - 如果bufferSize不是你正在产生的频率的倍数,那么你将部分通过下一个样本开始播放时的波浪 - 这可能是导致流行音乐的原因。

Amazing diagram

您可以通过更改bufferSize并查看是否更改弹出频率来测试此项。较大的缓冲区应该意味着这些样本不匹配的发生频率较低,因此弹出应该不那么频繁。

解决方案是改变

audioTrack.write(samples, 0, bufferSize);

bufferSize替换为最后0个交叉点的缓冲区中的位置。