Skip to content
Advertisement

Why glissando frequency goes up too high using java Audiosystem

I try to create a glissando (smooth pitch rise) from a start note to an end note (java code below). I linearly rise from the start note frequency to the stop note frequency like this

JavaScript

In the resulting audio fragment, the end of the glissando clearly has a higher pitch than the stop note. Is there something wrong with my math or is there an audiological reason why this rising sine seems to overshoot? Any ideas are greatly appreciated!

JavaScript

Console output:

JavaScript

Advertisement

Answer

The problem arises because the adjacent pitches for each frame are too wide. The calculation for instantFrequency is good, but arriving at a value by multiplying it by i is dubious. When you go from i to i+1, the distance progressed is as follows:

JavaScript

This is larger than the desired delta value, which should equal the new instantFrequency value, e.g.:

JavaScript

The following code helped me figure out the problem, which had me puzzled for several hours. It was only after sleeping on it that I was able to get to the above succinct explanation (added in an edit).

Here is a simpler case that illustrates the issue. Since the problem occurs before the sin function calculations, I excluded them and all the operations that follow the trig calculation.

JavaScript

Let’s consider aa as analogous to 220 Hz, and bb as analogous to 440 Hz. In each section, we start at 0 and go to position 10. The amount we go forward is calculated similarly to your calculations. For the “fixed rate”, we simply multiply the value of the step by i (trips aa and bb). In trip ab I use a calculation similar to yours. The problem with it is that the last steps are too large. You can see this if you inspect the output lines:

JavaScript

The distance traveled that “step” was close to 3, not the desired 2!

In the last example, trip cc, the calculation for travelIncrement is the same as for instantFrequency. But in this case the increment is simply added to the previous position.

In fact, for purposes of audio synthesis (when creating wave forms computationally), it makes sense to use addition to minimize cpu cost. Along those lines, I usually do something more like the following, removing as many calculations from the inner loop as possible:

JavaScript
Advertisement