Mod Archive Forums

Community => Project / Coder's Corner => Topic started by: Arcturus on August 20, 2021, 19:11:11

Title: A simple oscillator demo in Kotlin
Post by: Arcturus on August 20, 2021, 19:11:11
Hi everyone,

I just thought I would share a project I recently completed.

https://github.com/mabersold/kotlin-oscillator-demo

This is a very simple demo project of sound synthesis using oscillators, written in Kotlin and running on the JVM. If you run it, it will play a song that might be familiar.

There's a few parts to this project:


I'm not planning on going any further with this project as I'm satisfied with where it is, I was mostly interested in learning how audio works on the JVM and doing it in Kotlin, the language I primarily use at my job. I'll be moving on to another coding project soon.

Enjoy!
Title: Re: A simple oscillator demo in Kotlin
Post by: Saga Musix on August 21, 2021, 14:19:39
Nice stuff :) If you ever want to spend more time on this, here are some hints: These simple implementations of sawtooth, square wave, etc. have problems with oscillator frequencies that do not result in integer sample lengths and are not aliasing-free. If you are interested in fixing that, look into bandlimited wavetable synthesis (http://hackmeopen.com/2010/11/bandlimited-wavetable-synthesis/) (spoiler: the formulas will look more complex than just "toggle between -1 and +1" for a square wave).
Title: Re: A simple oscillator demo in Kotlin
Post by: Arcturus on August 23, 2021, 17:37:57
Yes, I did notice that the oscillators didn't perform exactly right, particularly at higher frequencies. I attempted to make an adjusted x value function in the parent oscillator class, but it didn't work particularly well. The idea was to adjust the x values so that it would be evenly distributed between periods. It wasn't based on any existing algorithm, I just came up with it on my own, which is probably why it didn't work out as I had hoped (while I have quite a bit of experience in software development at this point, I have no background in audio programming).

But this is just a demo project, so I don't feel too bad about that. I may make some adjustments to the oscillators in the future, but I'm also moving on to another project to make a Kotlin-based audio player that can play Protracker mods (my goal is to get it to play Space Debris correctly). So I'm more likely to spend time on that. Now I just need to learn how to resample. Repository is here, incidentally: https://github.com/mabersold/kotlin-protracker-demo