korlibs / korge

KorGE Game Engine. Multiplatform Kotlin Game Engine
https://korge.org/
Other
2.52k stars 124 forks source link

Question: Does Korge have a built in solution for generating audio waveforms for an mp3? #976

Closed Kietyo closed 1 year ago

Kietyo commented 2 years ago

I'm creating a keyboard based rhythm game and I'd like to create an editor which shows the audio wave form of the song to make it easier for users to create their own songs to play on.

Kietyo commented 2 years ago

Note to self: Useful reading:

https://www.izotope.com/en/learn/digital-audio-basics-sample-rate-and-bit-depth.html

soywiz commented 2 years ago

Yes. You can convert a Sound into an AudioData with toAudioData or directly load an AudioData. Then that class has the rate (hz). Typically 44100 and the samples: https://github.com/korlibs/korge/blob/main/korau/src/commonMain/kotlin/com/soywiz/korau/sound/AudioData.kt#L20

The samples is a list of amplitudes of the wave at the rate frequency. 44100 means that in 1 second you have 44100 amplitude samples. The samples are typically Shorts (between -32.768 and +32.767) or Floats between (-1f and +1f).

For plotting, you can reduce the number of samples by averaging them for example, then rendering the amplitudes. And that should be your waveform.