How to improve my Kotlin audio player? (Protracker .mod format)

Hi everyone,

Over the past several weeks, I’ve been working on a side project to make a player for a ProTracker mod. You can view the project here: GitHub - mabersold/kotlin-protracker-demo: A JVM-based kotlin application that loads an Amiga Protracker mod file, converts into a PCM audio stream, and plays it.

The ProTracker format is different from your typical audio file formats. Unlike a WAV or MP3, which are basically just PCM streams, a ProTracker mod file contains several PCM files representing individual instruments, along with a set of instructions on how those instruments should be played. The player is then responsible for generating a PCM stream from those instruments and instructions. You can read more about mod formats here: Module file - Wikipedia

I wrote my player for one specific song, which is included in the repository and requires no command-line arguments to play. For the most part, it plays the song pretty much perfectly, and I’m very pleased with how it turned out.

There is one issue I’ve noticed, occasionally there is a noticeable click in the audio. These happen more or less randomly. I’m guessing this may be a result of running this on the JVM. Throughout the song, I’m constantly generating new samples (referring to the individual elements of a PCM stream) and feeding them to the SourceDataLine for audio output. I’m not using coroutines or any kind of asynchronous processing.

Does anybody have any suggestions as to how I could get rid of these random clicks, or just an idea of where they might be coming from? For reference, today I hear about 5-10 of them while playing a 5 minute song. EDIT: It’s also possible that the issue is with my headset, but I can’t confirm that.

Also, does anybody have suggestions on how I could use coroutines to incorporate basic audio player functionality, such as the ability to stop/pause/unpause a song? Or the ability to provide basic song state data to whatever is controlling the player (ie, time elapsed, time remaining, current order list position, etc).