Search code examples
javalibgdxjavasound

Ogg audio file clicks upon looping - LIBGDX


I have an ogg file loaded with the Sound class. I set it to loop and it runs ok but I hear a click when it starts to play all over again. The sound in barely 7 seconds long.

Is there a way to fix this because it is quite annoying?


Solution

  • There is undoubtedly a big discontinuity in the signal between the starting moment and the ending moment. With the Java Clip as it is constructed, there is no way to counteract this that I know of.

    I recommend editing the sound in something like Audacity to try and make the two ends more similar. Easiest dodge is to have both ends taper to silence--but then that leaves a gap.

    The other possibility is writing your own Clip and adding a provision that allows a degree of overlap of the two edges. Then, you can add tapers in Audacity and have the tapered parts overlap. This is similar to how audio editing ("splicing") is done in a DAW.

    That is a bit of work though! I have done this and it does work well, but am a month or three away, still, from releasing my audio library. To program it yourself, read the bytes into an array, converting to PCM. Then, you can use the TargetDataLine as an example of an interface that allows reading progressive data (it is overkill, but the read() method is the key). In your read, you make a provision for starting a LERP (linear interpolation) of the PCM data at the overlap point.

    If another means of dealing with this issue comes up, I will be happy to learn about it!