There seemed to be alot of interest in Ric's Sound Creator when he made it, but people seemed to struggle with how the sound format actually worked. So I decided to update it with actual FACTS about the header.
Here's the updated snippet:
`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
`enderleit - Fixed the header code with proper names and descriptions. No more guesswork. ;)
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
FormatTag as dword: Channels as dword: SamplesPerSec as dword: AvgBytesPerSec as dword
BlockAlign as dword: BitsPerSample as dword: cbSize as dword
samples=int((length#/1000)*44100)
if memblock exist(1) then delete memblock 1
make memblock 1,samples*2+28
` Setup the 28 bytes of the header.
` Info: http://msdn.microsoft.com/en-us/library/dd757720(VS.85).aspx
` (incorrect order here due to calculation dependencies.)
FormatTag = 1 ` Waveform audio format. WAVE_FORMAT_PCM = 0x0001 = 1
Channels = 2 ` 1 = Mono, 2 = Stereo
SamplesPerSec = 22050 ` 22050 = 22.05kHz. (44100 = 44.1kHz)
BitsPerSample = 16 ` 8bit sample, or 16bit sample.
BlockAlign = Channels * (BitsPerSample/8) ` Block alignment, in bytes. Calculated as: Channels * (BitsPerSample/8)
AvgBytesPerSec = SamplesPerSec * BlockAlign ` Calculated as: SamplesPerSec * BlockAlign
cbSize = 0 ` Additional header info size. Set it to 0.
` Write the header in the correct order!
position=0
write memblock dword 1, position, FormatTag : inc position,4
write memblock dword 1, position, Channels : inc position,4
write memblock dword 1, position, SamplesPerSec : inc position,4
write memblock dword 1, position, AvgBytesPerSec : inc position,4
write memblock dword 1, position, BlockAlign : inc position,4
write memblock dword 1, position, BitsPerSample : inc position,4
write memblock dword 1, position, cbSize : 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 <-32768 then outInteger=-32768 ` gg query: is this the valid range? [Yes]
if outInteger>32767 then outInteger=32767 ` gg query: (ditto) ? [Yes]
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
Comments are welcome.
EDIT: Corrected a minor error on the WORD range.