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
#2
Ah, that makes it a lot easier to find good parameters Smile

I added a hack to unflag the mousebutton when the user clicks "PLAY SOUND", because I kept getting multiple instances of the sound effect playing.

Code:
...
if mousebutton(0)
    if mousey() > 60 and mousey() < 110
        if mousex() > 20 and mousex() < 220
            tmp = mousebutton(0, true) ' mousebutton will return false until it has been
                                          ' released and pressed again.
            play_sound = true
            laserShotSnd = CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
            play sound laserShotSnd
        endif 
    endif
endif
...

Maybe I should write some examples on how to use the ngui library that NED and all the included editors are using. It makes it easier to make these type of programs.
Reply
#3
Really impressive. Cool
Reply
#4
(01-20-2024, 09:33 PM)Marcus Wrote: Ah, that makes it a lot easier to find good parameters Smile

I added a hack to unflag the mousebutton when the user clicks "PLAY SOUND", because I kept getting multiple instances of the sound effect playing.

Code:
...
if mousebutton(0)
    if mousey() > 60 and mousey() < 110
        if mousex() > 20 and mousex() < 220
            tmp = mousebutton(0, true) ' mousebutton will return false until it has been
                                          ' released and pressed again.
            play_sound = true
            laserShotSnd = CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
            play sound laserShotSnd
        endif 
    endif
endif
...

Maybe I should write some examples on how to use the ngui library that NED and all the included editors are using. It makes it easier to make these type of programs.

I've had a look at the ngui library in the past for this type of program, but wasn't able to work out how to apply it, so I would certainly appreciate seeing some examples Marcus.
Reply
#5
Now added the other 2 functions, and incorporated Marcus's fix for playing sound:
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

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

'CreateSineSfx
sampleRate2 = 12000
duration2 = 0.25
startFreq2 = 500
endFreq2 = 100
fadeOut2 = 0.75

'CreateNoiseSfx
duration3 = 0.2
pitch3 = 1.2
fadeOut3 = 0
sampleRate3 = 12000



##############################################################################################
#######################################   GAME LOOP  #########################################
##############################################################################################
do

'clear the screen
set color 226,226,226
cls
set color 0,0,0

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

set caret 110,30
set justification center
write "CreateSquareSfx";wln
write "==============="
'-----------------------------------------------------------------
play_sound = false
if mousebutton(0)
    if mousey() > 60 and mousey() < 110
        if mousex() > 20 and mousex() < 220
            tmp = mousebutton(0, true) ' mousebutton will return false until it has been
                                          ' released and pressed again.
            play_sound = true
            laserShotSnd = CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)'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
draw rect 20,60,200,50
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,150,200,25
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,250,200,25
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,350,200,25
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,450,200,25
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,550,200,25
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 ,30
set justification center
write "CreateSineSfx";wln
write "============="
'CreateSineSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
'CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
'CreateNoiseSfx(duration, pitch, fadeOut, sampleRate)
'-----------------------------------------------------------------
play_sound2 = false
if mousebutton(0)
    if mousey() > 60 and mousey() < 110
        if mousex() > screen_w / 2 - 100 and mousex() < screen_w / 2 + 100
            tmp = mousebutton(0, true) ' mousebutton will return false until it has been
                                          ' released and pressed again.
            play_sound2 = true
            laserShotSnd2 = CreateSineSfx(duration2, startFreq2, endFreq2, fadeOut2, sampleRate2)'duration, startFreq, endFreq, fadeOut, sampleRate
            play sound laserShotSnd2
        endif   
    endif
endif
if play_sound2 = true
    set color 0,0,0
else
    set color 255,0,0
endif
draw rect screen_w / 2 - 100,60,200,50,1
set caret screen_w / 2 ,75
set color 255,255,255
write "PLAY SOUND"
set color 0,0,0
draw rect screen_w / 2 - 100,60,200,50
set caret screen_w / 2 ,130

write "DURATION 0.1 - 2.0"
set caret screen_w / 2 - 30,180
write "DURATION = "
set caret screen_w / 2 + 40,180;write duration2
set color 128,128,226
draw rect screen_w / 2 - 100,150,200,25,1
set color 0,0,0
draw rect screen_w / 2 - 100,150,200,25
draw rect screen_w / 2 - 100 + duration2 * 200/2,150,2,25
if mousebutton(0)
    if mousey() > 150 and mousey() < 175
        if mousex() > screen_w / 2 - 100 + duration2 * 200/2 and mousex() < screen_w / 2 + 100
            duration2 = duration2 + 0.05
        elseif mousex() > screen_w / 2 - 100 and mousex() < screen_w / 2 - 100 + duration2 * 200/2
            duration2 = duration2 - 0.05
        endif
    endif
endif

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

set caret screen_w / 2 ,230

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

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

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

set caret screen_w / 2 ,330

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

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

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

set caret screen_w / 2 ,430

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

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

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

set caret screen_w / 2 ,530

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

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

set color 226,226,226
draw rect screen_w / 2 - 100,630,200,25,1
set color 0,0,0
draw rect screen_w / 2 - 100,630,200,25
set caret screen_w / 2,635
write "RESTORE DEFAULTS"

if mousebutton(0)
    if mousey() > 630 and mousey() < 655
        if mousex() > screen_w / 2 - 100  and mousex() < screen_w / 2 + 100
            sampleRate2 = 12000
            duration2 = 0.25
            startFreq2 = 500
            endFreq2 = 100
            fadeOut2 = 0.75
           
        endif
    endif
endif





set caret screen_w / 2 ,680
write "CreateSineSfx(" + duration2 + ", " + startFreq2 + ", ";wln
write  endFreq2 +", " +fadeOut2 +", " +sampleRate2 + ")"

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

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

set caret screen_w - 140 ,30
set justification center
write "CreateNoiseSfx";wln
write "============="
'CreateSineSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
'CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
'CreateNoiseSfx(duration, pitch, fadeOut, sampleRate)
'-----------------------------------------------------------------
play_sound3 = false
if mousebutton(0)
    if mousey() > 60 and mousey() < 110
        if mousex() > screen_w  - 220 and mousex() < screen_w  -20
            tmp = mousebutton(0, true) ' mousebutton will return false until it has been
                                          ' released and pressed again.
            play_sound3 = true
            laserShotSnd3 = CreateNoiseSfx(duration3, pitch3, fadeOut3, sampleRate3)'duration, pitch, fadeOut, sampleRate
            play sound laserShotSnd3
        endif   
    endif
endif
if play_sound3 = true
    set color 0,0,0
else
    set color 255,0,0
endif
draw rect screen_w  - 220,60,200,50,1
set caret screen_w - 120 ,75
set color 255,255,255
write "PLAY SOUND"
set color 0,0,0
draw rect screen_w - 220,60,200,50
set caret screen_w - 120 ,130

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

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

set caret screen_w  - 120 ,230

write "pitch 0 to 5"
set caret screen_w  - 130 ,280
write "pitch = "
set caret screen_w  - 90,280;write pitch3
set color 128,128,226
draw rect screen_w  - 220,250,200,25,1
set color 0,0,0
draw rect screen_w  - 220,250,200,25
draw rect screen_w  - 220 + pitch3  * 200/5,250,2,25
if mousebutton(0)
    if mousey() > 250 and mousey() < 275
        if mousex() > screen_w  - 220 + pitch3  * 200/5 and mousex() < screen_w  - 20
            pitch3 =pitch3 + 0.05
        elseif mousex() > screen_w  - 220 and mousex() < screen_w  - 220 + pitch3  * 200/5
            pitch3 = max(0.01,pitch3 - 0.05)
        endif
    endif
endif

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


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

set caret screen_w - 120 ,330

write "fadeOut 0 - 1"
set caret screen_w - 130,380
write "fadeOut = "
set caret screen_w - 70,380;write fadeOut3
set color 128,128,226
draw rect screen_w - 220,350,200,25,1
set color 0,0,0
draw rect screen_w - 220,350,200,25
draw rect screen_w - 220 + fadeOut3 * 200/1,350,2,25
if mousebutton(0)
    if mousey() > 350 and mousey() < 375
        if mousex() > screen_w - 220 + fadeOut3 * 200/1 and mousex() < screen_w - 20
            fadeOut3 = fadeOut3 + 0.005
        elseif mousex() > screen_w - 220 and mousex() < screen_w - 220 + fadeOut3 * 200/1
            fadeOut3 = fadeOut3 - 0.005
        endif
    endif
endif

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

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

set caret screen_w - 120 ,430

write "sampleRate 8000 - 22050"
set caret screen_w -  140,480
write "sampleRate = "
set caret screen_w - 100 + 40,480;write sampleRate3
set color 128,128,226
draw rect screen_w - 220,450,200,25,1
set color 0,0,0
draw rect screen_w - 220,450,200,25
draw rect screen_w - 220 + (sampleRate3 - 8000) * 200/14050,450,2,25
if mousebutton(0)
    if mousey() > 450 and mousey() < 475
        if mousex() > screen_w - 220 + (sampleRate3 - 8000) * 200/14050 and mousex() < screen_w - 20
            sampleRate3 = min(22050,sampleRate3 + 200 )
        elseif mousex() > screen_w - 220 and mousex() < screen_w - 220 + (sampleRate3 - 8000) * 200/14050
            sampleRate3 = max(8000,sampleRate3 - 200 )
        endif
    endif
endif

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

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

if mousebutton(0)
    if mousey() > 630 and mousey() < 655
        if mousex() > screen_w - 220  and mousex() < screen_w - 20
            duration3 = 0.2
            pitch3 = 1.2
            fadeOut3 = 0
            sampleRate3 = 12000
           
        endif
    endif
endif





set caret screen_w - 120 ,680
write "CreateNoiseSfx(" + duration3 + ", " + pitch3 + ", ";wln
write  fadeOut3 +", " +sampleRate3 + ")"

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

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

until keydown(KEY_ESCAPE)

#########################################################################################
#################################  FUNCTIONS  ###########################################
#########################################################################################
' CreateSineSfx
' -------------
' Create a sine wave sound effect. 'duration' is the duration of the sound in seconds. 'startFreq'
' is the frequency at the start of the effect and 'endFreq' is the frequency at the end. You can
' use different values of 'startFreq' and 'endFreq' to create slide effects. 'fadeOut' determines
' when/if the sound should start fading out. If 'fadeOut' is 0, the fade out starts immediately, and
' if it's 1 there is no fade out. 'sampleRate' (samples per second) should be in the range [8000 ..
' 22050]. N7 outputs audio at a sample rate of 22050, so higher values than that makes no sense.
function CreateSineSfx(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
        data[i] = sin(a)*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

' 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

' CreateNoiseSfx
' --------------
' Create a noise sound effect. 'duration' is the duration of the sound in seconds. A 'pitch' value
' < 0 increases the lower noise frequencies, while a value > 1 increases the higher frequencies.
' 'fadeOut' determines when/if the sound should start fading out. If 'fadeOut' is 0, the fade out
' starts immediately, and if it's 1 there is no fade out. 'sampleRate' (samples per second) should
' be in the range [8000 .. 22050]. N7 outputs audio at a sample rate of 22050, so higher values than
' that makes no sense.
function CreateNoiseSfx(duration, pitch, fadeOut, sampleRate)
    assert sampleRate >= 8000, "CreateBoomSfx: invalid sample rate"
    assert pitch > 0, "CreateBoomSfx: invalid pitch"
   
    ' Mix four different noise frequencies weighted, in a weird way, by the pitch value.
    freqs = [
            [v: 0, p: sampleRate/500, d: 0, t: 0, w: pitch],
            [v: 0, p: sampleRate/1000, d: 0, t: 0, w: pitch^2],
            [v: 0, p: sampleRate/2000, d: 0, t: 0, w: pitch^3],
            [v: 0, p: sampleRate/8000, d: 0, t: 0, w: pitch^4]]
   
    s = sizeof(freqs)
    data = []
    vol = 1
    fadeOut = fadeOut*duration*sampleRate
    fadeOutDelta = 1/(duration*sampleRate - fadeOut)
    for i = 0 to duration*sampleRate - 1
        v = 0
        w = 0
        for j = 0 to s - 1; f = freqs[j]
            f.t = f.t - 1
            if f.t <= 0
                f.t = f.p
                f.d = ((rnd()*2 - 1) - f.v)/f.p
            endif
            f.v = f.v + f.d
            v = v + f.v*f.w
            w = w + f.w
        next
        data[i] = vol*v/w
        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
#6
Kevin,

Firstly... Cool additions... Immediately drawn to the "noise" generator... "Pitch" may need a "tweak". I wanted to see how "low" it would go... you know for explosions etc... The slider, using the mouse, can get it down to "-0". Yep. That's a minus... "invalid pitch" error... getting the pitch down to "0.05" works fine...

J
Logic is the beginning of wisdom.
Reply
#7
(01-21-2024, 06:26 PM)johnno56 Wrote: Kevin,

Firstly... Cool additions... Immediately drawn to the "noise" generator... "Pitch" may need a "tweak". I wanted to see how "low" it would go... you know for explosions etc... The slider, using the mouse, can get it down to "-0". Yep. That's a minus... "invalid pitch" error... getting the pitch down to "0.05" works fine...

J

Johnno,

Thank you so much for finding that. I've fixed the code above. If you just want to change the code in your saved program, you will need to change lines 480 & 481 to this:
Code:
elseif mousex() > screen_w  - 220 and mousex() < screen_w  - 220 + pitch3  * 200/5
            pitch3 = max(0.01,pitch3 - 0.05)

Line 480 has nothing to do with the -0 issue, but caused a different issue where the slider would move to the left even though the mouse was to the right, but outside of the blue box.

I note that if line 481 is set to "pitch3 = max(0.00,pitch3 - 0.05)" it still causes a crash, so I assume that pitch has to be > 0.

I have absolutely no idea why the value was showing as -0.0 (negative), but the above seems to fix it. I can only assume that there is an issue with rounding.
Reply
#8
Kevin,

That fix worked fine. The lowest is now "0.01". Thank you for the fix. Much appreciated.

J
Logic is the beginning of wisdom.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)