Community > Project / Coder's Corner

A simple oscillator demo in Kotlin

(1/1)

Arcturus:
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:


* Oscillators - simple classes with mathematical functions representing oscillators
* Song models - used to describe the song format in which to use the oscillators to actually play a tune. Inspired by how mod music works (except instead of channels and patterns, the instruments are themselves the channels, and just have repeatable phrases within them
* PCM audio generators - Takes the song data and the oscillators and uses them to create the actual PCM data used to play the song
* Player - Sends the PCM data to the output device to actually produce sound
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!

Saga Musix:
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 (spoiler: the formulas will look more complex than just "toggle between -1 and +1" for a square wave).

Arcturus:
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

Navigation

[0] Message Index

Go to full version