Android: Sine Wave Generation

前端 未结 3 1270
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 10:39

I\'m trying to use AudioTrack to generate sine, square, and sawtooth waves. However, the audio this is creating doesn\'t sound like a pure sine wave, but like it has some kind o

3条回答
  •  一向
    一向 (楼主)
    2021-02-08 11:35

    The only material difference that I can see in your two code samples is that the equation in your first example contains an integer (I), and therefore you're probably doing integer (not floating-point) arithmetic. This would cause a staircasing effect, adding unwanted harmonics to your waveform.

    I suspect that if you simply cast I to a float in your equation, it will produce a pure sine wave.

    samples[i] 
        = (float) Math.sin( (float)i * ((float)(2*Math.PI) * frequency / 44100));
    

提交回复
热议问题