Wild Audio Sequencing Experiment <gone bizarre>

BlitzMax Forums/BlitzMax Beginners Area/Wild Audio Sequencing Experiment <gone bizarre>

Alright, so that's my first real attempt to get help on a wacky encounter here...

Today marks the first day I'm playing around with audio and I've gotten quite far, as far as I'm concerned, finding out how it appears to be working. I have this little sequencer running and it spits out a nice little doodle with even two different generated samples and a cute vibrato.

BUT... let it run for a few rounds and it somehow gets out of sync for no appearant reason as far as I can see. I even ended up splitting the melody into individul channels for every sound to check if it's a weird Blitz-internal-channel handling nonsense, but it made no difference. I left it like that for the sake of showing that it couldn't be this channel thing as far as I can tell.

HELP!

Code is crude, but here it goes...
SuperStrict

' ================================== Wild sequencing Experiment =========================

Local sample:TAudioSample
Local channel:TChannel[8]

For Local i:Int = 0 To 7
	channel[i]=AllocChannel()
Next

Local speed# = 0
Local time:Int =0
Local steper:Int = 0
Local sequence#[] = [0.5, 1.325, 1.5, 1.0, 2.0, 2.0, 1.0, 1.25]
Local sequencev#[] = [1.0, 0.8, 0.52, 0.25, 0.80, 0.72, 0.48, 0.25]
Local sequencew:Int[] = [0,1,1,1,0,1,1,1]
Local sequencel#[] = [2.0,1.0,0.8,1.0,3.0,0.5,0.8,1.0]
Local sequencet:Int[] = [32,8,8,16,32,16,8,8]
Local volume#
Local pitch#[8]
Local sound:TSound

CreateTimer(60)

While Not KeyHit (key_escape)
	WaitEvent()
	Select EventID()
		Case EVENT_TIMERTICK
			If time Mod (sequencet [(steper-1) Mod 8 ])= 0
			     steper:Mod 8
			     channel[steper].stop
				volume = sequencev[steper]
				pitch[steper] = sequence[steper]
				Local length:Int = 22800*sequencel[steper]
				sample =CreateAudioSample( length ,44125,SF_MONO16BE )
				'channel[steper].stop
				For Local k#=0 Until length-1 
					Local saw#=(-128+(k Mod 256))'*4
					Local sine#=128*Sin(k*1.415)'*2
					Local fade#=1.0-k/length
					fade:*fade*fade
					Local fadein#=k/1024
					If fadein>1
						fadein=1
					EndIf
					If sequencew[steper] = 1
						sample.samples[k]=fadein*(saw*fade)
					Else
						sample.samples[k]=fadein*(sine*fade)
					EndIf
				Next
				sound=LoadSound( sample, False )
				channel[steper]=CueSound(sound)
			
				channel[steper].SetRate( pitch[steper] )
				channel[steper].SetVolume(volume)
				ResumeChannel channel[steper]
				steper:+1
			EndIf
			For Local i:Int = 0 To 7
				channel[i].SetRate(pitch[i]+Sin(time*50)*0.02)
			Next
			time:+1
	EndSelect
Wend