Fun with audio samples.

BlitzMax Forums/BlitzMax Programming/Fun with audio samples.

Mr. Goober(Posted 2014) [#1]
I wanted to experiment with making noise-static, so I wrote this little thing up in about 30 minutes. Perhaps someone might find a use for it.

Rem
	Basic 8-bit mono noise.
	-----------------------
	Noise is basically a bunch of random values plugged into an audio sample.
	The audio sample needs to have a large amount of data samples in order
	for this to work.
	
	Created by Makeii - 2/21/2014
EndRem

'	Always work in superstrict (or strict) mode. This is good programming practice
'	and helps you from slipping up.
SuperStrict

'	Get a random seed. Otherwise the static will sound exactly the same
'	Each time you run this.
SeedRnd(MilliSecs())

'	Create a new audio sample (a sample of data samples). This audio sample
'	holds 50,000 data samples and plays at 30,000 Hz (30,000 samples per second).
'	The format is 8-bit monosound.
Global _sample :TAudioSample = CreateAudioSample(50000,30000,SF_MONO8)
For Local i:Short=0 To 49999
	'	random numbers can go to 255, but the differences in the data will generate
	'	a sound that is too loud. By setting it between 0-127, the static produced
	'	will be half the volume, making it unnecessary to change the channel volume.
	_sample.samples[i] = Rand(0,127)
Next

'	Create a new playable sound based on the audio sample created.
'	The second parameter is True, which means it will loop when played.
Global _sound :TSound = LoadSound(_sample,True)

'	Allocate a new channel for playing the sound and play the sound.
Global _channel :TChannel = PlaySound(_sound)

'	Slow the rate of the noise to create a nifty retro explosion.
Global _rate :Float = 1.0
While (_rate > 0.003)
	_rate :* 0.90
	SetChannelRate(_channel,_rate)
	Delay 50
Wend

'	Stop the channel and end program.
StopChannel(_channel)
End



Xerra(Posted 2014) [#2]
The sound is meant to be random? Every time I ran it the noise was pretty much exactly the same. To my ears anyway. I'm assuming, because it's just white noise, that would be the case, meaning it's not really that practical. Unless I am missing something obvious?


Mr. Goober(Posted 2014) [#3]
Nah. it pretty much sounds the same regardless, but the data put into the sound sample will be potentially different, yielding a unique pattern of white noise. It would be more obvious if less samples were used to make the white noise. Edit the source to only have maybe 1024 samples of data. It will sound more like a note, but the way it sounds will likely be different each time.