我正在尝试连续播放正弦波。按住按钮时,正弦波的频率会发生变化。释放按钮后,它将恢复到正常频率。

当我运行以下代码时,输​​出声音会定期发生“滴答声”(尖锐的声压变化),即定期发出“滴答声”(每秒大约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();
}
}

最佳答案

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



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

解决的办法是改变

audioTrack.write(samples, 0, bufferSize);


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

10-08 00:12