TGC Codebase Backup



Sound Generator by Jack

30th Oct 2016 2:17
Summary

Generate diffrent sounds, and signals. You can give your shot, if it inspired you.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    /*AGK Wave sound creator======================by Ric, and:Tommy S - Original waveform codeGreen Gandalf - Memblock formatLampton Worm - Load/Save formatLampton Worm/Raven - Real time parameter editorXtom - GUI/parameter editorported by Jack from DBP to AGK*/render_x = 1024render_y = 768SetOrientationAllowed( 0, 0, 1, 0 )SetWindowSize( render_x, render_y, 1 )setvirtualresolution(render_x,render_y)SetResolutionMode( 1 )SetScreenResolution( render_x,render_y )SetSyncRate(60,0)print("please wait")sync()/*createsound("explosion",2,6200,200,2000,20,0.2,0,0,0,0,10,1)createsound("explosion2",3,1200,600,3000,10,0.1,0,0,0,0,10,1)createsound("powerup",4,800,700,8000,-0.02,0.2,0,0,0,0,10,1)createsound("warning",5,800,1000,6000,0.5,0.1,0,0,0,0,5,1)createsound("vibrato1",6,500,2000,8000,0,0.1,0.05,0.2,0,0,1,1)createsound("vibrato2",7,400,2000,8000,0,0.1,0.05,0.2,0,0,1,1)createsound("vibrato3",8,600,2000,8000,0,0.1,0.05,0.2,0,0,1,1)createsound("bass hit",9,30,50,16000,0.00,1.5,.0,0,0,0,100,1)createsound("hihat",10,800,40,5000,60.06,2.2,.1,0,0,0,10,1)createsound("door knock",11,200,20,8000,.1,4.0,2.1,10,9.2,9.2,10,1)createsound("vinyl scratch",12,500,80,8000,0.06,0.2,.1,0,0,0,10,1)createsound("cb1",13,250,50,2500,0.00001,0.1,4.0,0.4,0,0,10,1)createsound("cb2",14,500,50,2500,0.00001,0.1,4.0,0.4,0,0,10,1)createsound("cb3",15,437.5,150,1500,0.0001,0.1,0.0,4.0,0.4,0,10,1)createsound("cb4",16,573.75,150,1500,0.0,0.1,4.0,0.4,0,0,10,1)createsound("bubble burst",17,50,80,6000,-.4,2.0,0.1,10,1.2,0.2,10,1)*/createsound("sound effect",1,220,2000,8000,0,0.0,0.0,0.0,0,0,0.5,0)createsound("sound effect",2,220,2000,8000,0,0.0,0.0,0.0,0,0,0.5,1)createsound("sound effect",3,220,2000,8000,0,0.0,0.0,0.0,0,0,0.5,2)createsound("sound effect",4,220,2000,8000,0,0.0,0.0,0.0,0,0,0.5,3)SaveSound(1,"sinus.wav")SaveSound(2,"square.wav")SaveSound(3,"sawtooth.wav")SaveSound(4,"triangle.wav")/*fused2=AddSounds(fused1,4)fused3=AddSounds(fused2,5)fused4=AddSounds(fused3,8)*/SetPrintSize(20)doprint("Press keys to play a sound (sound warning!):")print("1: sound effect")/*print("2: explosion1")print("3: explosion2")print("4: powerup")print("5: warning")print("6: vibrato")print("7: bass hit")print("8: hihat")print("9: door knock")print("0: vinyl scratch")print("q: cowbell")print("w: bubble burst")*/if GetRawKeyPressed(49)=1PlaySound(1)print("1")endifif GetRawKeyPressed(50)=1PlaySound(2)print("2")endifif GetRawKeyPressed(51)=1PlaySound(3)print("3")endifif GetRawKeyPressed(52)=1PlaySound(4)print("4")endifif GetRawKeyPressed(53)=1PlaySound(5)print("5")endifif GetRawKeyPressed(54)=1PlaySound( 6)print("6")endifif GetRawKeyPressed(55)=1PlaySound(7)endifif GetRawKeyPressed(56)=1PlaySound(8)endifif GetRawKeyPressed(57)=1PlaySound(9)endifif GetRawKeyPressed(48)=1PlaySound(10)endifif GetRawKeyPressed(81)=1PlaySound(11)endifif GetRawKeyPressed(87)=1PlaySound(12)endifsync()loopfunction AddSounds(snd1,snd2)position as integersnd1Mem = 2snd2Mem = 3CreateMemblockFromSound(snd1Mem,snd1)snd1Channels=GetMemblockShort(snd1Mem,0)snd1Bits=GetMemblockShort(snd1Mem,2)snd1Hz=GetMemblockInt(snd1Mem,4)snd1Totallength=GetMemblockInt(snd1Mem,8)snd1Samples	=(snd1Totallength/snd1Channels)/(snd1Bits/8)/*CreateMemblockFromSound(snd2Mem,snd2)snd2Channels=GetMemblockShort(snd2Mem,0)snd2Bits=GetMemblockShort(snd2Mem,2)snd2Hz=GetMemblockInt(snd2Mem,4)snd2Totallength=GetMemblockInt(snd2Mem,8)snd2Samples	=(snd2Totallength/snd2Channels)/(snd2Bits/8)if snd1Channels<>snd2Channels and snd1Bits<>snd2Bits and snd1Hz<>snd2Hz then exitfunction -1*/SndMem=CreateMemblock(12+snd1Totallength)SetMemblockShort(SndMem,position,snd1Channels)inc position,2SetMemblockShort(SndMem,position,snd1Bits)inc position,2SetMemblockInt(SndMem,position,snd1Hz)inc position,4SetMemblockInt(SndMem,position,snd1Totallength)inc position,4for x=1 to snd1SamplesSetMemblockbyte(SndMem,position,GetMemblockByte(snd1Mem,position))inc positionnext x/*for x=1 to snd2SamplesSetMemblockbyte(SndMem,position,GetMemblockByte(snd2Mem,position))inc positionnext x*/soundnumber=CreateSoundFromMemblock(SndMem)PlaySound(soundnumber)DeleteMemblock(SndMem)DeleteMemblock(snd1Mem)endfunction	soundnumber/*Some context for the wave file==============================AGK can save sounds of 8 and 16 bit value8 bit sounds are saved as Byte into the memblock16 bit sounds are saved as Short*/function CreateSound(name$,soundnumber,frequency#,length#,loudness#,bend#,decay#,vibratospeed#,vibratodepth#,tremelospeed#,tremelodepth#,attack#,waveform)outWord as integerdword1 as integerdword3 as integerdword4 as integerdword5 as integerfallinloudness# as floatphi# as floattheta# as floatposition as integersamples as integeroutSignal# as floatchannels=1` CHANNELS(2 bytes)bits=8` BITS(2 bytes)hz=44100` HZ(4 bytes)samples=(length#/1000)*hztotallength=(samples*(bits/8)*channels) ` TOTAL LENGTH 	(4 bytes)SndMem=CreateMemblock(12+totallength)SetMemblockShort(SndMem,position,channels)inc position,2SetMemblockShort(SndMem,position,bits)inc position,2SetMemblockInt(SndMem,position,hz)inc position,4SetMemblockInt(SndMem,position,totallength)inc position,4if (bits/8)=1DC#=122.5endifif (bits/8)=2DC#=122.5endifriseinloudness#=loudness#for x=1 to samples/*original:outSignal#=sin((x/DC#)*(frequency#+vibratodepth#*sin(theta#)))*/select waveformcase 0:outSignal#=sin((x/DC#) * (frequency#+vibratodepth#*sin(theta#)))*(255/2.0)endcasecase 1:outSignal#=sign(sin((x/DC#) * (frequency#+vibratodepth#*sin(theta#)))) * (255/2.0)endcasecase 2:outSignal#=mod((x/DC#) * (frequency#+vibratodepth#*sin(theta#)),255) -(255/2.0)endcasecase 3:if outSignal#=>122.5 then tri=1if outSignal#=<-122.5 then tri=0if tri=0outSignal#=outSignal#+(0.688/122.5)* (frequency#+vibratodepth#*sin(theta#))*2elseoutSignal#=outSignal#-(0.688/122.5)* (frequency#+vibratodepth#*sin(theta#))*2endifendcaseendselectif (bits/8)=1outWord=(outSignal#+(255/2.0))endifif (bits/8)=2outWord=outSignal#endifinc theta#,vibratospeed#inc phi#,tremelospeed#dec frequency#,bend#if fallinloudness#<loudness#inc fallinloudness#,decay#endifif riseinloudness#>0dec riseinloudness#,attack#endifif (bits/8)=1SetMemblockByte(SndMem,position,outWord)inc positionendifif (bits/8)=2SetMemblockShort(SndMem,position,outWord)inc position,2endifnext xif GetSoundExists(soundnumber)=1 then DeleteSound(soundnumber)CreateSoundFromMemblock(soundnumber,SndMem)DeleteMemblock(SndMem)endfunctionfunction sign(in#)if in#>0.0out#=1.0elseout#=-1.0endifendfunction out#