我正在开发使用触摸式X和Y的示例应用程序,将X等同于频率,将Y等同于振幅。然后计算简单的正弦波并传递该抛出线程。当我生成声音信号时-在写入新数据之间,再生器发出刺耳的声音。
我以前从未使用过声音,但是可以猜想裂纹是正弦波中应有的值跳跃,但是为什么当保证值将为零时,裂纹在音轨末端开裂?所以这就是为什么我需要帮助。在音轨的开头和结尾都听到裂纹。
audioTrack是在onCreate方法中创建的。 start()方法将启动audioTrack。
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, numSamples,
AudioTrack.MODE_STREAM);
start();
这是我的话题
Runnable m_noiseGenerator = new Runnable() {
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while(!m_stop)
{
//rnd.nextBytes(noiseData);
try {
if(playing) {
//audioTrack.flush();
long startTime = System.currentTimeMillis();
audioTrack.write(generatedSnd, 0, generatedSnd.length);
if (audioTrack.getPlayState() == audioTrack.PLAYSTATE_STOPPED)
audioTrack.play();
long finish = System.currentTimeMillis() - startTime;
System.out.print("\naudioTrackThread: WRITTEN IN " + finish + "ms\n");
genToneUpdate = false;
}
} catch (Exception ex) {
System.out.print("THREAD ERROR " + ex);
}
try {
//Thread.sleep(50);
//Thread.sleep(1000);
} catch (Exception ex) {
System.out.print("THREAD ERROR sleep " + ex);
}
}
}
};
这就是我计算和编辑数据的方式
for (int i = 0; i < numSamples; i++) {
samples[i] = ((double) amp / 100) * Math.sin(2 * Math.PI * i * (freq/sampleRate));
}
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
genToneUpdate = true;
}
此处提供了整个应用程序: soundGenerator application