• Godot HelpAudio
  • how to get various sound waveforms in GDscript ? ....at least i got the Sin working!

Hi, as i follow the exemple on the Demo projects: https://github.com/godotengine/godot-demo-projects

so, from this demo, the sin waveform is generated and it is working fine, even if it takes some memory.

i am now looking for other types of shapes: Square Triangle Random

i got this code for the sin:

var hz = 22050.0
var phase = 0.0
var pulse_hz = 440.0
var increment = (1.0 / (hz / pulse_hz)) 
...
playback.push_frame( Vector2(1.0,1.0) * sin(phase * (PI * 2.0)) ) # frames are stereo
phase = fmod((phase + increment), 1.0)
...

how can i obtain the square waveform ? i am not so much into mathematic.... :(

I do not personally know the math around making wave forms, but I found some resources that might be worth a look:


The Pudding: Let's Learn About Waveforms Interesting read and has the source, but I couldn't find the code responsbile for actually creating the wave forms.

Medium: Part 2. Basic sound waves with C++ and JUCE This one has source code and at first glance, it looks like it might be possible to convert to GDScript.

Microsoft Developer Game Theory: Into to Audio Programming Part 4: Algorithms for different sound waves in C# The actual algorithms for generating the sound waves looks a tad cleaner, but to be honest I'm not totally sure what to look for.


Hopefully these are of some help!

3 years later