<Updated 20/8/05> This project has really come to life with thanks to the DBP community - thanks to Tommy S, Green Gandalf, Xtom, Raven and Lampton Worm who have all contributed to the creation of this project. Here's the latest version:
Remember computers like the Spectrum, Commodor 64, BBC micro, Acorn Electron .....? They all had commands that would generate beeps and sounds with no media files. Now so does Dark Basic - with this 'createsound' function, which uses real time fm-synthesis. There are lots of parameters to change, so the variety of sounds you can make is huge.
The function call is:
createsound(name$,soundnumber,frequency#,length#,loudness#,bend#,decay#,vibratospeed#,vibratodepth#,tremelospeed#,tremelodepth#,attack#)
The first five parameters, and the last one, must have non-zero values, the rest can be left at zero. Here's the function, with some example code so you can see how to use the function, and the kind of sounds you can make.
`DB Sound Creator by Ric, and:
`Tommy S - Original waveform code
`Green Gandalf - Memblock format
`Lampton Worm - Load/Save format
`Lampton Worm/Raven - Real time parameter editor
`Xtom - GUI/parameter editor
createsound("sound effect",1,2995,2000,8000,0.02,0.1,0.60,3,0.64,5,10)
createsound("explosion",2,6200,200,2000,20,0.2,0,0,0,0,10)
createsound("explosion2",3,1200,600,3000,10,0.1,0,0,0,0,10)
createsound("powerup",4,800,700,8000,-0.02,0.2,0,0,0,0,10)
createsound("warning",5,800,1000,6000,0.5,0.1,0,0,0,0,5)
createsound("vibrato1",6,500,2000,8000,0,0.1,0.05,0.2,0,0,1)
createsound("vibrato2",7,400,2000,8000,0,0.1,0.05,0.2,0,0,1)
createsound("vibrato3",8,600,2000,8000,0,0.1,0.05,0.2,0,0,1)
createsound("bass hit",9,30,50,16000,0.00,1.5,.0,0,0,0,100)
createsound("hihat",10,800,40,5000,60.06,2.2,.1,0,0,0,10)
createsound("door knock",11,200,20,8000,.1,4.0,2.1,10,9.2,9.2,10)
createsound("vinyl scratch",12,500,80,8000,0.06,0.2,.1,0,0,0,10)
createsound("cb1",13,250,50,2500,0.00001,0.1,4.0,0.4,0,0,10)
createsound("cb2",14,500,50,2500,0.00001,0.1,4.0,0.4,0,0,10)
createsound("cb3",15,437.5,150,1500,0.0001,0.1,0.0,4.0,0.4,0,10)
createsound("cb4",16,573.75,150,1500,0.0,0.1,4.0,0.4,0,0,10)
createsound("bubble burst",17,50,80,6000,-.4,2.0,0.1,10,1.2,0.2,10)
set text font "arial"
set text size 16
do
text 0,0,"Press keys 1 upwards:"
text 0,20,"1: sound effect"
text 0,40,"2: explosion1"
text 0,60,"3: explosion2"
text 0,80,"4: powerup"
text 0,100,"5: warning"
text 0,120,"6: vibrato"
text 0,140,"7: bass hit"
text 0,160,"8: hihat"
text 0,180,"9: door knock"
text 0,200,"0: vinyl scratch"
text 0,220,"-: cowbell"
text 0,240,"+: bubble burst"
if keystate(2)=1 then play sound 1:wait 300
if keystate(3)=1 then play sound 2:wait 300
if keystate(4)=1 then play sound 3:wait 300
if keystate(5)=1 then play sound 4:wait 300
if keystate(6)=1 then play sound 5:wait 300
if keystate(7)=1 then play sound 6:play sound 7:play sound 8:wait 300
if keystate(8)=1 then play sound 9:wait 300
if keystate(9)=1 then play sound 10:wait 300
if keystate(10)=1 then play sound 11:wait 300
if keystate(11)=1 then play sound 12:wait 300
if keystate(12)=1 then play sound 13:play sound 14:play sound 15:play sound 16:wait 300
if keystate(13)=1 then play sound 17:wait 300
loop
function createsound(name$,soundnumber,frequency#,length#,loudness#,bend#,decay#,vibratospeed#,vibratodepth#,tremelospeed#,tremelodepth#,attack#)
outWord as word
dword1 as dword: dword2 as dword: dword3 as dword: dword4 as dword
dword5 as dword: dword6 as dword: dword7 as dword
samples=int((length#/1000)*44100)
if memblock exist(1) then delete memblock 1
make memblock 1,samples*2+28
` write 28 memblock header bytes
dword1=1 ` gg query: is this the number of channels?
dword2=2 ` gg query: is this the number of bytes per sample?
dword3=22050 ` gg query: seems to be half the number of samples per second - why?
dword4=88200 ` gg query: is this the number of bytes per second?
dword5=4 ` gg query: what does this represent?
dword6=16 ` gg query: (ditto) ?
dword7=0 ` gg query: (ditto) ?
position=0
write memblock dword 1, position, dword1 : inc position,4
write memblock dword 1, position, dword2 : inc position,4
write memblock dword 1, position, dword3 : inc position,4
write memblock dword 1, position, dword4 : inc position,4
write memblock dword 1, position, dword5 : inc position,4
write memblock dword 1, position, dword6 : inc position,4
write memblock dword 1, position, dword7 : inc position,4
rem generate and write wave
riseinloudness#=loudness#
for x=1 to samples
outInteger=int(sin((x/122.5)*(frequency#+vibratodepth#*sin(theta#)))*(loudness#-fallinloudness#-riseinloudness#+tremelodepth#*sin(phi#)))*3.0
if outInteger <-32767 then outInteger=-32767 ` gg query: is this the valid range?
if outInteger>32767 then outInteger=32767 ` gg query: (ditto) ?
outWord=outInteger
inc theta#,vibratospeed#
inc phi#,tremelospeed#
dec frequency#,bend#
if fallinloudness#<loudness#
inc fallinloudness#,decay#
endif
if riseinloudness#>0
dec riseinloudness#,attack#
endif
write memblock word 1, position, outWord : inc position,2
next x
if sound exist(soundnumber)=1 then delete sound soundnumber
make sound from memblock 999, 1 ` assumes you won't need sound number 999!
clone sound soundnumber, 999
delete sound 999
` memblock no longer required
delete memblock 1
endfunction
To make life even easier, this program (thanks Xtom et al) will allow you to adjust the different parameters in real time, and save your createsound() effect to use in your code - or you can save your sounds in wav format:
http://forum.thegamecreators.com/xt/xt_apollo_download.php?i=509254
Just save your sound effect, then open it in notepad, and the createsound() function call can be copied and pasted into your code.
Also include in the download is a help file with suggested ranges and values for the parameters.
Enjoy.