sounds in code ?

BlitzMax Forums/BlitzMax Programming/sounds in code ?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

took a bit of time over lunch today and came up with this.

I added parameters to create noise and a square wave.. stil a bit iffy but at the bottom I create a sound and play it.. its a little bit like the pacman death noise created with just a handful of commands.
Maybe not impressive to many of you but Im chuffed.. a bit of cleaning up and I can get on with knocking a simple something up using no media. :)

SuperStrict

Type TSoundEffect
	Field blocks:TList
	Field totalSamples:Int
	
	Method New()
		blocks = CreateList()
	End Method
	
	Method AddBlock(duration:Float, startFreq:Float, endFreq:Float, startVol:Float, endVol:Float, noise:Float, waveType:Int)
		Local block:TSoundEffectBlock
		block = TSoundEffectBlock.Create(duration, startFreq, endFreq, startVol, endVol,noise, waveType )
		totalSamples :+ duration*11025
		ListAddLast(blocks, block)
	End Method
	
	Method MakeSound:TSound()
		Local sample:TAudioSample
		Local currentBlock:TSoundEffectBlock
		Local startSample:Int
		Local endSample:Int
		Local samplesInBlock:Int
		
		Local freqAngle:Float	
		Local volume:Float
		Local multiplier:Float
		Local k:Float
		Local angle:Float
		Local currentFreq:Float
		
		Local noiseValue:Float
		Local waveType:Int
		
		startSample = 0
		endSample = 0 

Print "total samples: " + totalsamples

		sample = CreateAudioSample( totalSamples,11025,SF_MONO8 )
		For currentBlock = EachIn blocks
Print "processing block duration : " + currentBlock.duration
		
			noiseValue = currentBlock.noise/100

			waveType = currentBlock.waveType

			startSample = endSample
			samplesInBlock = currentBlock.duration * 11025
			endSample = startSample + samplesInBlock
			
Print "start sample : " + startSample + " end sample : " + endSample
		
			For k = startSample Until endSample
			
				multiplier = (k-startSample) / samplesInBlock
				volume = ( currentBlock.startvol - ( ( currentBlock.startvol-currentBlock.endvol ) * multiplier ) )  * 127.5 / 100
				
				currentFreq = currentBlock.startfreq + ( ( currentBlock.endfreq-currentBlock.startfreq ) * multiplier )
				freqAngle = currentFreq/11025*180

				angle :+ freqAngle
				angle = angle Mod 360
				
				Local writesample:Float
				writesample = Sin(angle)*volume+127.5

				If writesample > 127.5 Then
					writesample :- Rnd()*(writesample-127.5)*noisevalue
				Else
					writesample :+ Rnd()*writesample * noisevalue
				End If
				
				If waveType = 1 Then
					If writeSample > 127.5 Then
						writeSample = 255
					Else
						writeSample = 0
					End If
				End if
				
Print "k = " + k + " angle : " + angle + " volumne : " + volume
				sample.samples[k]=writesample
			Next
		Next
		
		Local sound:TSound=LoadSound( sample,False )
	
		Return sound

	End Method
End Type

Type TSoundEffectBlock
	Field duration:Float
	Field startFreq:Float
	Field endFreq:Float
	Field startVol:Float
	Field endVol:Float
	Field noise:Float
	Field waveType:Int
	
	Function Create:TSoundEffectBlock(duration:Float, startFreq:Float, endFreq:Float, startVol:Float, endVol:Float, noise:Float, waveType:Int)
		Local this:TSoundEffectBlock
		this = New TSoundEffectBlock
		this.duration = duration
		this.startFreq = startFreq
		this.endFreq = endFreq
		this.startVol = startVol
		this.endVol = endVol
		this.noise = noise
		this.waveType = waveType	
		Return this
	End Function
End Type

Local sound:TSound
Local soundEffect:TSoundEffect

soundEffect = New TSoundEffect

soundEffect.AddBlock(.1,600,1000,100,100,0,0)
soundEffect.AddBlock(.25,1000,600,100,100,0,0)

soundEffect.AddBlock(.1,400,800,100,100,0,0)
soundEffect.AddBlock(.25,800,400,100,100,0,0)

soundEffect.AddBlock(.1,200,600,100,100,0,0)
soundEffect.AddBlock(.25,600,200,100,100,0,0)

soundEffect.AddBlock(.05,3000,1000,100,0,0,0)
soundEffect.AddBlock(.15,1000,3000,100,0,0,0)
sound = soundEffect.MakeSound()

Print "made sound"

PlaySound(sound)

Input