- Joined
- Jan 2, 2020
- Messages
- 1,414
Here's a simple example how to generate waves using Sin.
Touch the screen sideways or use a mouse to visually generate different frequencies.
Touch the screen sideways or use a mouse to visually generate different frequencies.
Cerberus:
#HTML5_CANVAS_WIDTH = 1280 ' ----------------------------------------
#HTML5_CANVAS_HEIGHT = 720 '
#GLFW_WINDOW_WIDTH = 1280 '
#GLFW_WINDOW_HEIGHT = 720 ' Excercise in using trigonometry
Strict '
Import mojo2 ' Create a waveform using sinewave
'
Function Main : Int() '
New Game() ' ----------------------------------------
Return 0
End
Class Game Extends App
Field canvas : Canvas
Field w : Int = 1280
Field h : Int = 720
Field hz : Float = 440.0 ' Frequency that the waveform is stored in the array
Field amplitude : Int = 32000 ' Volume that the waveform is stored in the array
Field samplerate : Int = 44100
Field seconds : Float = 10
Field samples : Int
Field waveform : Float[]
Field t : Float
Field i : Int
Method OnCreate : Int()
canvas=New Canvas
samples = samplerate * seconds
waveform = New Float[samples]
Return 0
End
Method OnRender : Int()
canvas.Clear
hz = MouseX()
canvas.SetColor 0,1,0
canvas.DrawText "Hz = " + hz , 0 , 0
For i = 0 Until samples ' Create waveform
t = Float(i) / Float(samplerate)
waveform[i] = amplitude * Sin(hz * t * 360.0)
Next
canvas.DrawLine 0,h/2,w-1,h/2 ' Draw waveform
For i = 0 Until 44100
canvas.DrawPoint i/(44100/w) , h/2+(waveform[i]/(32000.0/(h/2))*0.9)
Next
canvas.Flush
Return 0
End
Method OnUpdate:Int()
If KeyHit(KEY_ESCAPE) Then EndApp
Return 0
End
End
Last edited by a moderator: