Another sound question

BlitzMax Forums/BlitzMax Beginners Area/Another sound question

Here's something for you to be getting on with lotonah...
Strict 

Graphics 640,480,0

Local Note = 64

Repeat
	Cls
	DrawText "Use Spacebar to play sound, Up/Down to change note, Escape to quit",10,5
	DrawText "Note = "+Note,10,25
	If KeyHit(KEY_UP) Then Note:+1
	If KeyHit(KEY_DOWN) Then Note:-1
	If KeyHit(KEY_SPACE) Then PlayNote(Note,1000)
	Flip
Until KeyDown(KEY_ESCAPE)
End

Function PlayNote(note#,duration)	' NOTE: duration is in milliseconds

	' create a channel that will be reused each time this function is called
	Global channel:TChannel = AllocChannel()

	' calculate frequency for required note
	Local period#    = (10.0*12.0*16.0*4.0) - (NOTE*16.0*4.0) 
	Local frequency# = (8363.0*2^((6.0*12.0*16.0*4.0 - Period) / (12.0*16.0*4.0)))
	SetChannelRate(channel,frequency/44100)

	' calculate how many bytes are needed to play for the duration time
	Local length = (frequency/1000) * duration

	' create the waveform
	Local sample1:TAudioSample = CreateAudioSample( length,44100,SF_MONO8 )
	Local k ; For k=0 Until length
		sample1.samples[k]=(Sin(k*2)*127.0)+128
	Next
	Local sound1:TSound=LoadSound( sample1 )

	' play it
	PlaySound sound1,channel

End Function