Mod Archive Forums Mod Archive Forums
Advanced search  

News:

Please note: Your main modarchive.org account will not work here, you must create a forum account to post on the forums.

Pages: [1]   Go Down

Author Topic: A simple oscillator demo in Kotlin  (Read 6278 times)

0 Members and 1 Guest are viewing this topic.

Arcturus

  • New User
  • Offline Offline
  • Posts: 11
  • I used to be cool
    • View Profile
A simple oscillator demo in Kotlin
« 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:

  • 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!
Logged

Saga Musix

  • TMA Moderator
  • Top Poster
  • ****
  • Offline Offline
  • Posts: 2571
  • I love OpenMPT! And Modules! And TMA! And Pie! :>
    • View Profile
    • Saga Musix - free module music and more!
Re: A simple oscillator demo in Kotlin
« Reply #1 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 (spoiler: the formulas will look more complex than just "toggle between -1 and +1" for a square wave).
Logged
» My TMA artist profile
» Visit my music site: https://sagamusix.de/ [de, en]
» Visit my programming website: https://sagagames.de/ [de]
» Open ModPlug Tracker

Arcturus

  • New User
  • Offline Offline
  • Posts: 11
  • I used to be cool
    • View Profile
Re: A simple oscillator demo in Kotlin
« Reply #2 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
Logged
Pages: [1]   Go Up