sounds in code ?

BlitzMax Forums/BlitzMax Programming/sounds in code ?

QuietBloke(Posted 2007) [#1]
Ive looked around but I cant see anyone here who has done this so I thought Id ask just on the off-chance.

Im looking for a way to create a sound file on the fly in code. Something like the old games from my youth ( space invaders, galaxians, pacman, defender, asteriods, etc etc ).
What Im after is a way of creating a wav or whatever by supplying start/end freq, start/end volume/ duration maybe also the option of noise so I can do them explosion sounds. Of course most sounds will be made up of a sound getting louder, playing for a bit and fading so I thought if I had an option to append a sound on the end of an existing sound this will give me all I need to create simple sounds on the fly.

Why do I want this ?.. well.. I never seem to write anything cause I get bogged down with trying to create the flashy graphics, the special effects, trying to get hold of the sound files etc. To this end I've decided the next game I write will have zero external media.

Well.. its something to do isnt it !?


QuietBloke(Posted 2007) [#2]
errm.. OK.. just read what I wrote and I figured it needs explaining what Im after...

I have looked at the TAudioSample and seen some examples of it but I just dont get it.
The examples I have seen dont seem to be change the volume of a sample which is one thing Im after.. but also.. I just dont get it.. for example in the help you have a this explanation
'length is the number of samples to allocate for the sample' ??.. eh ?

frequency I suppose is the sampling frequency.. fine.. but then what does the data you put into the sample actually mean ?

can I define the volume of the sound ?
can someone here explain what the data you supply mean in a way even I can understand ?


Boulderdash(Posted 2007) [#3]
''Create_Audio_Samples()

''Function Create_Audio_Samples()
Local fr:Long=14000
Local f:Long
Local k:Long

fr=frt[0]
Global sample_000:TAudioSample=CreateAudioSample( 32,fr,SF_MONO8 )
For k=0 Until 32
f=0
If k>15 Then f=127
sample_000.samples[k]=f
Next
Global Sound_000:TSound=LoadSound( sample_000,True )

fr=frt[1]
Global sample_001:TAudioSample=CreateAudioSample( 32,fr,SF_MONO8 )
For k=0 Until 32
f=0
If k>15 Then f=127
sample_001.samples[k]=f
Next
Global Sound_001:TSound=LoadSound( sample_001,True )

fr=frt[2]
Global sample_002:TAudioSample=CreateAudioSample( 32,fr,SF_MONO8 )
For k=0 Until 32
f=0
If k>15 Then f=127
sample_002.samples[k]=f
Next
Global Sound_002:TSound=LoadSound( sample_002,True )

fr=frt[3]
Global sample_003:TAudioSample=CreateAudioSample( 32,fr,SF_MONO8 )
For k=0 Until 32
f=0
If k>15 Then f=127
sample_003.samples[k]=f
Next
Global Sound_003:TSound=LoadSound( sample_003,True )


Boulderdash(Posted 2007) [#4]
This is some code cut from my emulator, it is not complete
but it may help quide you.

I just made the length of the samples 32 no particular reason,yes you can change the volume!

Do you want a noise channel also?

I can make retro tones (square wave or I can code to make sin wave)


Boulderdash(Posted 2007) [#5]
If Pitch=0 Then Sound_handle2=PlaySound(Sound_000)
If Pitch=1 Then Sound_handle2=PlaySound(Sound_001)
If Pitch=2 Then Sound_handle2=PlaySound(Sound_002)
If Pitch=3 Then Sound_handle2=PlaySound(Sound_003)

SetChannelVolume(Sound_handle,Your_Volume_variable)

OK so you are going to need some elaborate coding to make it happen, I had to repeat tonnes of instructions becuase loops would not work for this when creating the samples maybe someelse could shed some light on this?

Here is a link to my working program complete with spacey's sounds from the 80's

http://software.wizz.googlepages.com/Emulator2001_t.exe

how many tones do you want 128 or 256 maybe, I can help

You wont have files on your drive with samples.

A noise channel:
Global sampleb:TAudioSample=CreateAudioSample( 22232,fr,SF_MONO8 )
For k=0 Until 22232
If T=0 Then f=Rand(0,255)
T:+1
If T=8 Then T=0
sampleb.samples[k]=f
Next
Global soundb:TSound=LoadSound( sampleb,True )
Sound_handleb=CueSound(soundb)
SetChannelVolume(Sound_handleb,0)
noise_cued=1


QuietBloke(Posted 2007) [#6]
Hi Gav,

thanx for the reply.. its a bit late this evening to try your code out.. Ill have a go tommorow... Im also on the Mac tonight so I cant try your emulator out but ill also run that tommorow..
I'm thinking Im not going to get my head round this until I try tweaking the values and seeing what happens..
Can you elaborate on what you needed to do to change the volume ?


Boulderdash(Posted 2007) [#7]
I will have to make a working "retro organ" to demo how to do this.

Give me a day or two to get round to it

Do you intend on making sound effects as well as music?

If you are making sound effects that change frequencies rapidly (like every frame or 2) then you may need my "Software enhancement" of sound sample to dramatically improve the sound quality\\

So I need to know if you are making music or sound effects for your game, I use a value of 0-128 for pitch so you may be limited unless you mod it yourself once i send it to you


So i will post some examples soon, since other bmaxers might want retro sound also.

There are many benifits from sound samples:

1 no media files
2 no gap as a sound loops
3 you can make any wave form


Perturbatio(Posted 2007) [#8]
This was my first ever foray into BMax sound generation:
http://www.blitzbasic.com/Community/posts.php?topic=71061#794654


QuietBloke(Posted 2007) [#9]
I think my requirements have not quite been understood... I do not want to actually generate the sounds on the fly.... what I am after is a way of creating a sound file using code.

So.. instead of loading sounds at the start of my App I build them using simple method calls : say for example :

shootingSound:TSound = CreateSound(1,10000,12000,0,255)

woud create a soundbank that playes a sound rising from 10000hz to 12000hz and volume rising from 0 to max over a timespan of 1/10 second.

AppendSound(shootingSound,5,12000,12000,255,255)

would append the the soundbank a sound of 12000hz and full volume for 5/10 seconds.

AppendSound(shootingSound,10,12000,8000,255,0)

would append to the soundbank a sound with a freq falling from 12000 to 8000, volumne falling from max to zero for 10/10 seconds.

then in my game I can just PlaySound(shootingSound)

Its really just for sound effects.
Is this easily possible using the Audio lib supplied with BMax ?


Damien Sturdy(Posted 2007) [#10]
I keep meaning to update my sound system so it doesnt generate WAVS but I still think this might be of use:

http://www.blitzbasic.com/Community/posts.php?topic=70022#783679


QuietBloke(Posted 2007) [#11]
Hi Cygnus,

well .. I only had a chance to glance over the code and I must say.. its all completely over my head.
Does it actually generatae sounds from scratch ?
I tried running the musicusage.bmx file but it fails with load of wav failed..

Is it cause Im running on a Mac ?.. it has generated files s0.wav to s8.wav but these are unplayable.

Ill try it later on a PC.

thanx for all the help so far.. I still dont understand any of it ! lol


QuietBloke(Posted 2007) [#12]
OK.. I think Im getting to grips with the Audio commands. Thanx again for the replies.. seems my problem is not as complex as I thought.. it was just I didnt understand what I was doing ( it happens a lot ).

Still.. just in case its of any help to anyone else who wants to create sounds in code heres my quick and dirty test code I knocked up tonight.



This is basically the first part of what I was after.
Im not convinced the sound itself is actually the frequency of the sound but its good enough for now.
The createsound function will create a TSound. The first param is duration in seconds, then you supply start and end frequencie ( 0-11025 ) and start/end volume ( 0-100%).

Now I need a way of supplying a list of these params so I can create sounds made of any number of these chanks of sounds.

Maybe when I get that working I can apply effects to the sound... so I can do them there explosion sounds.


QuietBloke(Posted 2007) [#13]
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. :)




Damien Sturdy(Posted 2007) [#14]
QuietBloke: It's untested on Mac

the code generates the wav files in windows format. The code does need converting so that it does not need to generate wav files (ie in memory) At that point it'll be cross platform I think!

When it works you'll have access to the old 6502 era machines music systems (not quite c64, more BBC/NES with sample channels...)

You have Vol envelope, Pitch envelope, LFOs etc. you just have to ensure updatesound() is called often enough (this could be setup with events if needed...)


[edit]

OK It seems to be an Endian problem. I may look over this at the weekend.


QuietBloke(Posted 2007) [#15]
Hi Cygnus,

Ill take another look at your code now I have vague understanding of whats going on in terms of the data.

In the meantime at least I have something workable.

cheers.


Damien Sturdy(Posted 2007) [#16]
its use is pretty simple and I think theres a max demo included? (havent checked since i've been away from home..)