我正在尝试编写一个教育应用程序,可以结合简单的波形并进行播放。 AudioTrack中有一个方法可以接受浮动数组(来自docs):
* audioData the array that holds the data to write.
* The implementation does not clip for sample values within the nominal range
* [-1.0f, 1.0f], provided that all gains in the audio pipeline are
* less than or equal to unity (1.0f), and in the absence of post-processing effects
* that could add energy, such as reverb. For the convenience of applications
* that compute samples using filters with non-unity gain,
* sample values +3 dB beyond the nominal range are permitted.
* However such values may eventually be limited or clipped, depending on various gains
* and later processing in the audio path. Therefore applications are encouraged
* to provide samples values within the nominal range.
public int write(@NonNull float[] audioData, ...)
我认为我应该在[-1.0f, 1.0f]
之间使用浮点数
生成复杂的正弦波时,我将它们中的两个加在一起,从而将范围扩大到[-2.0f, 2.0f]
我想我应该以某种方式 normalize 这个...
private fun playSound(frequency1: Float, frequency2: Float) {
val sineWave = FloatArray(BUFFER_SIZE) //data array
val period1 = SAMPLING_RATE / frequency1
val period2 = SAMPLING_RATE / frequency2
for (i in sineWave.indices) {
sineWave[i] = Math.sin(Math.PI * i / period1).toFloat() +
Math.sin(Math.PI * i / period2).toFloat()
}
player.setVolume(0.5f)
player.write(sineWave, 0, sineWave.size, AudioTrack.WRITE_BLOCKING)
player.play()
}
我不确定这是否正确,因此在创建复杂的正弦波时需要帮助