我正在尝试创建信号发生器,其频率由陀螺仪传感器数据控制(通过移动手机)。问题是,我正在调用AudioTrack内部onSensorChanged并且我在输出上有“点击”,因为每次更新传感器时,我都会调用
stopSinus();
setSinus(Frequency);
startSinus();
它是这样定义的
siAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
buffsize, AudioTrack.MODE_STATIC);
public void stopSinus() {
int checkPlay = siAudioTrack.getPlayState();
if (checkPlay == 3) { //
siaudioTrack.stop();
}
}
public void startSinus() {
siAudioTrack.reloadStaticData();
siAudioTrack.setLoopPoints(0, sampleCount, -1);
siAudioTrack.play();
}
public void setSinus(int frequency) {
sampleCount = (int) ((float) SAMPLE_RATE / frequency);
short sample[] = new short[sampleCount];
int amplitude = 32767;
double twoPi = 2 * Math.PI;;
double phase = 0.0;
for (int i = 0; i < sampleCount; i++) {
sample[i] = (short) (amplitude * Math.sin(phase));
phase += twoPi * frequency / SAMPLE_RATE;
}
siAudioTrack.write(sample, 0, sampleCount);
}
onSensorChanged每秒变化几次,它会在波浪中间停止发电机 - 这就是“点击”的来源。
有人能告诉我如何在一整波后停止发电机吗?如何找出phaseos何时通过零?或者一些不同的解决方案?
元芳怎么了
红糖糍粑
随时随地看视频慕课网APP
相关分类