Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sound effects experimentation
#1
I love the new sound effects in the latest N7 version. I am working on this program to let me see the effects of changing the various parameters that you need to use when creating the sound effects.

You need to click the mouse when inside the blue buttons to change the paramenters, and click the red button to play the new sound. 

I've only incorporated one of the new functions so far, and spent little time on aesthetics, as you can see.

I'll leave this now until tomorrow, and see whether I can think of any changes that I can make, before moving on to the other 2 functions. In the meantime, I would love to hear any suggestions for improvements that you may have?

All the best - Kevin.


Code:
visible screen_w = 1024,screen_h = 768
'Open a window
set window "Sound FX",screen_w,screen_h,1
'enable double buffering
set redraw off


sampleRate = 12000
duration = 0.25
startFreq = 500
endFreq = 100
fadeOut = 0.75




############  to calculate FPS  ##########################
visible framecount,lasttime = 999,fps,frametime = 0,starttime = 0,endtime = 0
##########################################################
##############################################################################################
#######################################   GAME LOOP  #########################################
##############################################################################################
do
###  for FPS calc #########
framecount = framecount + 1
starttime = clock()
###########################
'clear the screen
set color 255,255,255
cls
set color 0,0,0

set caret 110,30
set justification center
write "CreateSquareSfx"
'-----------------------------------------------------------------
play_sound = false
if mousebutton(0)
    if mousey() > 60 and mousey() < 110
        if mousex() > 20 and mousex() < 220
            play_sound = true
            laserShotSnd = CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
            play sound laserShotSnd
        endif   
    endif
endif
if play_sound = true
    set color 0,0,0
else
    set color 255,0,0
endif
draw rect 20,60,200,50,1
set caret 120,75
set color 255,255,255
write "PLAY SOUND"
set color 0,0,0
set caret 120,130

write "DURATION 0.1 - 2.0"
set caret 90,180
write "DURATION = "
set caret 160,180;write duration
set color 128,128,226
draw rect 20,150,200,25,1
set color 0,0,0
draw rect 20 + duration * 200/2,150,2,25
if mousebutton(0)
    if mousey() > 150 and mousey() < 175
        if mousex() > 20 + duration * 200/2 and mousex() < 220
            duration = duration + 0.05
        elseif mousex() > 20 and mousex() < 20 + duration * 200/2
            duration = duration - 0.05
        endif
    endif
endif

'-------------------------------------------------

set caret 120,230

write "startFreq 0 - 1000"
set caret 90,280
write "startFreq = "
set caret 160,280;write startFreq
set color 128,128,226
draw rect 20,250,200,25,1
set color 0,0,0
draw rect 20 + startFreq * 200/1000,250,2,25
if mousebutton(0)
    if mousey() > 250 and mousey() < 275
        if mousex() > 20 + startFreq * 200/1000 and mousex() < 220
            startFreq = startFreq + 10
        elseif mousex() > 20 and mousex() < 20 + startFreq * 200/1000
            startFreq = startFreq - 10
        endif
    endif
endif

'-----------------------------------------------------------------

'-------------------------------------------------

set caret 120,330

write "endFreq 0 - 1000"
set caret 90,380
write "endFreq = "
set caret 160,380;write endFreq
set color 128,128,226
draw rect 20,350,200,25,1
set color 0,0,0
draw rect 20 + endFreq * 200/1000,350,2,25
if mousebutton(0)
    if mousey() > 350 and mousey() < 375
        if mousex() > 20 + endFreq * 200/1000 and mousex() < 220
            endFreq = endFreq + 10
        elseif mousex() > 20 and mousex() < 20 + endFreq * 200/1000
            endFreq = endFreq - 10
        endif
    endif
endif

'-----------------------------------------------------------------

'-------------------------------------------------

set caret 120,430

write "fadeOut 0 - 1"
set caret 90,480
write "fadeOut = "
set caret 160,480;write fadeOut
set color 128,128,226
draw rect 20,450,200,25,1
set color 0,0,0
draw rect 20 + fadeOut * 200/1,450,2,25
if mousebutton(0)
    if mousey() > 450 and mousey() < 475
        if mousex() > 20 + fadeOut * 200/1 and mousex() < 220
            fadeOut = fadeOut + 0.005
        elseif mousex() > 20 and mousex() < 20 + fadeOut * 200/1
            fadeOut = fadeOut - 0.005
        endif
    endif
endif

'-----------------------------------------------------------------

'-------------------------------------------------

set caret 120,530

write "sampleRate 8000 - 22050"
set caret 90,580
write "sampleRate = "
set caret 160,580;write sampleRate
set color 128,128,226
draw rect 20,550,200,25,1
set color 0,0,0
draw rect 20 + (sampleRate - 8000) * 200/14050,550,2,25
if mousebutton(0)
    if mousey() > 550 and mousey() < 575
        if mousex() > 20 + (sampleRate - 8000) * 200/14050 and mousex() < 220
            sampleRate = min(22050,sampleRate + 200 )
        elseif mousex() > 20 and mousex() < 20 + (sampleRate - 8000) * 200/14050
            sampleRate = max(8000,sampleRate - 200 )
        endif
    endif
endif

'-----------------------------------------------------------------

set color 226,226,226
draw rect 20,630,200,25,1
set color 0,0,0
draw rect 20,630,200,25
set caret 120,635
write "RESTORE DEFAULTS"

if mousebutton(0)
    if mousey() > 630 and mousey() < 655
        if mousex() > 20  and mousex() < 220
            sampleRate = 12000
            duration = 0.25
            startFreq = 500
            endFreq = 100
            fadeOut = 0.75
           
        endif
    endif
endif





set caret 120,680
write "CreateSquareSfx(" + duration + ", " + startFreq + ", ";wln
write  endFreq+", " +fadeOut+", " +sampleRate + ")"

set caret screen_w/2,screen_h - 110
write "mouse = " + mousex() + " / " + mousey();wln
write "FPS = " + str(fps);wln

'copy back buffer to the screen
redraw
'cap FPS to 60
fwait 60

#######  FPS calc  ############################
endtime = clock()
frametime = frametime + endtime - starttime
if frametime > 1000 # 1 second
    fps = framecount
    framecount = 0
    frametime = 0
endif
################################################


until keydown(KEY_ESCAPE)

#########################################################################################
#################################  FUNCTIONS  ###########################################
#########################################################################################
' CreateSquareSfx
' ---------------
' Same as CreateSineSfx but using a square wave.
function CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
    data = []
    a = 0
    da = 2*PI*startFreq/sampleRate
    dda = (2*PI*endFreq/sampleRate - 2*PI*startFreq/sampleRate)/(duration*sampleRate)
    vol = 1
    fadeOut = fadeOut*duration*sampleRate
    fadeOutDelta = 1/(duration*sampleRate - fadeOut)
    for i = 0 to duration*sampleRate - 1
        ' No, using sin here is stupid.
        sa = sin(a)
        if sa < 0  sa = -1
        elseif sa > 0 sa = 1
        data[i] = sa*vol
        a = a + da
        da = da + dda
        if i > fadeOut  vol = vol - fadeOutDelta
    next
    ' 'createsound(leftData, rightData, sampleRate)' creates a new sound and returns a sound id.
    ' 'leftData' is an array with data for the left channel and 'rightData' is for the right
    ' channel. The values in the arays should be in the range [-1..1]. 'sampleRate' is the number
    ' of samples per second, So if you want to create a sound that lasts for three seconds with a
    ' sample rate of 11025, the data arrays should contain 33075 (11025*3) elements each.
    '   You can also use 'create sound sound_id, leftData, rightData, sampleRate' if you want to
    ' use your own sound id.
    return createsound(data, data, sampleRate)
endfunc
Reply


Messages In This Thread
Sound effects experimentation - by kevin - 01-20-2024, 05:12 PM
RE: Sound effects experimentation - by Marcus - 01-20-2024, 09:33 PM
RE: Sound effects experimentation - by kevin - 01-21-2024, 04:50 AM
RE: Sound effects experimentation - by kevin - 01-21-2024, 06:55 AM
RE: Sound effects experimentation - by johnno56 - 01-21-2024, 06:26 PM
RE: Sound effects experimentation - by kevin - 01-21-2024, 07:02 PM
RE: Sound effects experimentation - by johnno56 - 01-21-2024, 07:33 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)