Thanks for your replies, I only just saw them, I'll read through now. I kept working on my code to bring it to a workable state. Although I've been nursing my newborn so the code is really untidy as I haven't been able to clean it up. You can adjust the frequency and amplitude with the keys. I threw a 3rd harmonic in for example and you can adjust the number of periods with the 's' variable. the frequency of each period is the multiple of 's'. Ignore most of the comments.
Code: Select all
'#include once "fbsound-1.1/inc/fbsound_dynamic.bi"
'fbs_Set_PlugPath("./fbsound-1.1/")
'dim shared as integer gSampleRate
'dim shared as integer gMIDINote = 57
'sub MasterCB(byval pSamples as FBS_SAMPLE ptr, _
'byval nChannels as integer, _
'byval nSamples as integer)
'static as double w=0.0
'' no active MIDI note do nothing
'if gMIDINote<1 then exit sub
'' midi note to frequency
'dim as double frequency = fbs_pow(2.0,(gMIDINote-69)/12.0)*440.0
'dim as double wStep = PI2/gSampleRate*frequency
'' render one or two channels with your own code !
'select case as const nChannels
'case 1 ' MONO
'dim as FBS_SAMPLE ptr p = pSamples
'for i as integer=0 to nSamples-1
'*p=sin(w)*8000.0 : w+=wStep : p+=1
'next
'case 2 ' STEREO
'dim as FBS_SAMPLE ptr pL = pSamples,pR=pL+1
'for i as integer=0 to nSamples-1
'*pL=sin(w)*8000.0 : *pR=*pL * -1 : w+=wStep
'pL+=2 : pR+=2
'next
'end select
'print "MIDI note: " & gMIDINote & " frequency: " & frequency
'end sub
'' init fbSound
'ChDir(ExePath())
'if fbs_Init()=false then
'print "error: FBS_INIT() " & FBS_Get_PlugError()
'beep : sleep : end 1
'end if
'' get playback sample rate
'gSampleRate=FBS_Get_PlugRate()
'' set and enable master callback
'FBS_Set_MasterCallback(@MasterCB)
'FBS_Enable_MasterCallback()
'dim as double tRuntime,tNow,tStart=Timer()
'dim as integer oldSeconds=-1,Seconds,noteStep=2
'while inkey()=""
'tNow=Timer():tRuntime=tNow-tStart:Seconds=int(tRuntime)
'if Seconds<>oldSeconds then
'gMIDINote += noteStep
'if gMIDINote<57 or gMIDINote>82 then noteStep*=-1
'oldSeconds = Seconds
'end if
'sleep 10
'wend
'' disable master callback
'FBS_Disable_MasterCallback()
' #######################
' # fbs_create_wave.bas #
'#######################
'#include "../inc/fbsound.bi"
#include once "fbsound-1.1/inc/fbsound_dynamic.bi"
fbs_Set_PlugPath("./fbsound-1.1/")
' example of:
' fbs_Create_Wave(nSamples,@hWAve,@lpSamples)
function GetKeyNB() as integer
dim as string key
key = inkey
select case len(key)
case 0
return 0
case 1
return key[0]
case 2
return key[0] + (key[1] shl 8)
end select
end function
function mathsFunctRight(w as double,A as integer) as double
return sin(w)*A + sin(w*3)*A/3
end function
function mathsFunctLeft(w as double,A as integer) as double
return sin(w+PI)*A + sin(w*3+PI)*A/3
end function
const data_path = "../data/"
chdir(exepath())
' only if not same as exe path
' fbs_Set_PlugPath("./")
if fbs_Init()=false then
print "error: FBS_INIT() " & FBS_Get_PlugError()
beep : sleep : end 1
end if
dim as integer nSamples = 44100
dim as integer hWave,hSound
dim as double w,wstep
dim as FBS_SAMPLE ptr pSamples
dim as FBS_SAMPLE ptr pS,pP,pE
' !!! create window before init fbsound !!!
dim as integer sw=1300,sh=480
screenres sw,sh
dim as double j = 400 'frequency hz
dim as integer key
dim as integer A = 4000 'amplitude
dim as integer s = 4 'number of periods
'^^^^^^^
dim as integer Fs = 1000 '% samples per
dim as double dt = 1/Fs '% per sample
dim as integer StopTime = 1 '%
dim t1() as integer '% entire wave time samples
dim t2() as integer '% time at end of wave period
dim as integer y = 0
dim as integer y1 = 0
dim as integer y2 = 0
dim as integer Fb = 49 '% base frequency
dim as integer c = 0
dim as integer f
'^^^^^^^
do
key = GetKeyNB
select case(hex(key))
case "48FF"
c = 0
A = A + 1000
fbs_Destroy_Sound(@hSound):cls
wstep = PI2/44100.0 * j
fbs_Create_Wave(int(nSamples/j + 0.5)*s,@hWave,@pSamples) 'nSamples here has to be the same number of samples as 'c' I think
for aa as integer = 1 to s
w = 0
for i as integer = 0 to int((nSamples)/j + 0.5)-1
w+=wstep
' right channel
pSamples[c*2 ]=mathsFunctRight(w*aa,A)
' w+=wstep
' left channel
pSamples[c*2+1]=mathsFunctLeft(w*aa,A)
' w+=wstep
c += 1
next
next
fbs_Create_Sound(hWave,@hSound)
fbs_Get_SoundPointers(hSound,@pS,@pP,@pE)
fbs_Play_Sound(hSound,-1) ' loop endless
? "Frequency: ", j
'sw = 640 /2
sh = 480 /2
for x as integer=0 to int(nSamples/j + 0.5)*s
' right channel
pset (x*sw/int(int(nSamples/j +0.5)*s) ,sh+pSamples[x*2]*0.01),3 ' end of samples
'pset (sw+x,sh+pS[ x*2 ]*0.01),4 ' start of samples
' left channel
pset (x*sw/int(int(nSamples/j +0.5)*s) ,sh+pSamples[x*2+1]*0.01),5 ' end of samples
'pset (sw+x,sh+pS[ x*2+1]*0.01),6 ' start of samples
next
case "50FF"
c = 0
A = A - 1000
fbs_Destroy_Sound(@hSound):cls
wstep = PI2/44100.0 * j
fbs_Create_Wave(int(nSamples/j + 0.5)*s,@hWave,@pSamples) 'nSamples here has to be the same number of samples as 'c' I think
for aa as integer = 1 to s
'y2 = sin(w*aa) '% Frequency accellartion wave
w = 0
'f = Fb + 150^y2 '% Frequency equation
'redim preserve t1(c)
't2(aa) = ubound(t1) ' not sure if time should be t1(ubound(t1)) or just ubound(t1) upper-bound
'for t as double = 0 to (StopTime-dt)/f step dt/f
for i as integer = 0 to int((nSamples)/j + 0.5)-1
w+=wstep
' right channel 'maybe don't need to worry about keeping track of time when using fbsound
pSamples[c*2 ]=mathsFunctRight(w*aa,A)
' w+=wstep
'print w/(PI2)
' left channel
pSamples[c*2+1]=mathsFunctLeft(w*aa,A)
'w+=wstep
't1(w) = t2(aa) + i
c += 1
next
'next
'print int(nSamples/j + 0.5)*s, c
next
fbs_Create_Sound(hWave,@hSound)
fbs_Get_SoundPointers(hSound,@pS,@pP,@pE)
fbs_Play_Sound(hSound,-1) ' loop endless
? "Frequency: ", j
'sw = 640 /2
sh = 480 /2
for x as integer=0 to int(nSamples/j + 0.5)*s
' right channel
pset (x*sw/int(int(nSamples/j +0.5)*s) ,sh+pSamples[x*2]*0.01),3 ' end of samples
'pset (sw+x,sh+pS[ x*2 ]*0.01),4 ' start of samples
' left channel
pset (x*sw/int(int(nSamples/j +0.5)*s) ,sh+pSamples[x*2+1]*0.01),5 ' end of samples
'pset (sw+x,sh+pS[ x*2+1]*0.01),6 ' start of samples
next
case "4DFF"
c = 0
j = j + 10
fbs_Destroy_Sound(@hSound):cls
wstep = PI2/44100.0 * j
fbs_Create_Wave(int(nSamples/j + 0.5)*s,@hWave,@pSamples) 'nSamples here has to be the same number of samples as 'c' I think
for aa as integer = 1 to s
w = 0
for i as integer = 0 to int((nSamples)/j + 0.5)-1
w+=wstep
' right channel
pSamples[c*2 ]=mathsFunctRight(w*aa,A)
' left channel
pSamples[c*2+1]=mathsFunctLeft(w*aa,A)
'w+=wstep
c += 1
next
next
fbs_Create_Sound(hWave,@hSound)
fbs_Get_SoundPointers(hSound,@pS,@pP,@pE)
fbs_Play_Sound(hSound,-1) ' loop endless
? "Frequency: ", j
'sw = 640 /2
sh = 480 /2
for x as integer=0 to int(nSamples/j + 0.5)*s
' right channel
pset (x*sw/int(int(nSamples/j +0.5)*s) ,sh+pSamples[x*2]*0.01),3 ' end of samples
'pset (sw+x,sh+pS[ x*2 ]*0.01),4 ' start of samples
' left channel
pset (x*sw/int(int(nSamples/j +0.5)*s) ,sh+pSamples[x*2+1]*0.01),5 ' end of samples
'pset (sw+x,sh+pS[ x*2+1]*0.01),6 ' start of samples
next
case "4BFF"
j = j - 10
fbs_Destroy_Sound(@hSound):cls
wstep = PI2/44100.0 * j
c = 0
fbs_Create_Wave(int(nSamples/j + 0.5)*s,@hWave,@pSamples) 'nSamples here has to be the same number of samples as 'c' I think
for aa as integer = 1 to s
w = 0
for i as integer = 0 to int((nSamples)/j + 0.5)-1
w+=wstep
' right channel
pSamples[c*2 ]=mathsFunctRight(w*aa,A)
' left channel
pSamples[c*2+1]=mathsFunctLeft(w*aa,A)
'w+=wstep
c += 1
next
next
fbs_Create_Sound(hWave,@hSound)
fbs_Get_SoundPointers(hSound,@pS,@pP,@pE)
fbs_Play_Sound(hSound,-1) ' loop endless
? "Frequency: ", j
'sw = 640 /2
sh = 480 /2
for x as integer=0 to int(nSamples/j + 0.5)*s
' right channel
pset (x*sw/int(int(nSamples/j +0.5)*s) ,sh+pSamples[x*2]*0.01),3 ' end of samples
'pset (sw+x,sh+pS[ x*2 ]*0.01),4 ' start of samples
' left channel
pset (x*sw/int(int(nSamples/j +0.5)*s) ,sh+pSamples[x*2+1]*0.01),5 ' end of samples
'pset (sw+x,sh+pS[ x*2+1]*0.01),6 ' start of samples
next
end select
loop until key = 27
print "playing clean sin wave endless"
sleep
end