fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
tone
Posts: 26
Joined: Dec 17, 2019 21:31

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by tone »

Hi,

I've been trying use callbacks, I looked at:
http://shiny3d.de/public/fbsound/englis ... erCallback

which references some example.bas files I cannot find. I downloaded the fbsound-1.0 which has some 'tests' example .bas' in them, but the examples like test15.bas aren't among them.

the fbs_set_mastercallback.bas still uses the:
bs_Init()
fbs_Set_MasterCallback(@MyCallback)
fbs_Load_MP3File(data_path & "rnb_loop.mp3",@hWave)
fbs_Create_Sound(hWave,@hSound)
fbs_Play_Sound(hSound,-1) ' play it endless

technique which I am familiar with, but in my case I am feeding a sine wave into pSamples but the issue is that the tone or frequency of each value of 'aa' will hang there for about 2/3 of a second before shifting onto the next frequency, and I expected it to be pretty much instant, i.e. one sine wave period, then the next sine wave period of a different frequency. Here is a snippit of my code for your interest, I increase the frequency or amplitude based on the arrow keys:

Code: Select all

case "50FF"
    A = A - 1000			'A is amplitude of sine wave, w is the (radial?) frequency
fbs_Destroy_Sound(@hSound):cls
wstep = PI2/44100.0 * j

fbs_Create_Wave(nSamples*3,@hWave,@pSamples)      'nSamples here has to be the same number of samples as 'c' I think
for aa as integer = 1 to 3

 	for i as integer=0 to nSamples-1
		' 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

'sw = 640 /2
sh = 480 /2
for x as integer=0 to sw-1
  ' right channel
  pset (x   ,sh+pE[-sw*2+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   ,sh+pE[-sw*2+x*2+1]*0.01),5 ' end of samples
  'pset (sw+x,sh+pS[      x*2+1]*0.01),6 ' start of samples
next
N.B. MathsFunc is just a sine wave at this point. And the code was started from fbs_create_wave.bas, so much is the same like nSamples = 44100. In the above snippit when you press the down arrow key it will loop three ascending tones indefinitely. But you can distinctly hear each of the three (each frequency about 2/3 sec), but I expected it would be too quick to distinctly hear.

Could someone please explain why and how to remove the extra sine waves between the three 'aa' loops, so its just three sine waves of increasing frequency? Am I misunderstanding that there is 44100 samples per sine period?
Or alternatively point me in the direction of some example callback code I can use to learn how to do the same thing.

Thanks in advance,


EDIT: what I said about the callback example still stands, however I just found the problem is that there are multiple sine periods within the nSamples loop, which is the reason the note hangs for a fraction of a second.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by badidea »

tone wrote:I downloaded the fbsound-1.0 which has some 'tests' example .bas' in them, but the examples like test15.bas aren't among them.
The fbound-0.1 had an example test15.bas, this seems to be the same as fbs_set_mastercallback.bas in later versions (1.0 and 1.1).
tone wrote:Here is a snippit of my code for your interest, I increase the frequency or amplitude based on the arrow keys:
It is only a piece of code, which cannot be compiled, difficult to test.
tone wrote:Or alternatively point me in the direction of some example callback code I can use to learn how to do the same thing.
Did this code not work for you? https://freebasic.net/forum/viewtopic.p ... 02#p267336
Comment 'if Seconds<>oldSeconds then and corresponding end if for a faster sweep.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by D.J.Peters »

file: fbs_on_the_fly.bas

Code: Select all

'  ######################
' # fbs_on_the_fly.bas #
'######################

'#include "../inc/fbsound.bi"
#include "../inc/fbsound_dynamic.bi"

sub MyCallback(byval pSamples as FBS_SAMPLE ptr, _
               byval nChannels as integer, _
               byval nSamples  as integer)
  const as double PISTEP = PI2/44100.0
  static as FBS_SAMPLE sinus(44100-1)
  static as integer Hz(127) 
  static as boolean bInit = false
  static as integer frequence=440,wave_index=0
  static as integer old_played_seconds=0,played_samples=0
  
  if bInit=false then
    ' fill the array only once
    for i as integer=0 to ubound(sinus)
      sinus(i)=sin(i*PISTEP)*8000
    next  
    dim as integer a = 440 ' A is 440 hz...
    for x as integer = 0 to 127
      HZ(x) = (a / 32) * (2 ^ ((x - 9) / 12))
    next
    bInit=true
  end if
  
  
  dim as integer iLeft,iRight=1
  for i as integer=0 to nSamples-1
    pSamples[iLeft] = sinus(wave_index)
    pSamples[iRight] = pSamples[iLeft]*-1
    wave_index=(wave_index+frequence) mod 44100
    iLeft+=2
    iRight+=2
  next  
  
  played_samples+=nSamples
  
  dim as integer played_seconds = played_samples\44100
  if played_seconds<>old_played_seconds then
    old_played_seconds=played_seconds
    dim as integer note = 56+rnd*24 ' two octaves
    frequence=Hz(note)
    windowtitle "note: " & note & " frq: " & frequence
  end if  

  dim as integer n,index,x,max_x=511
  nSamples-=1
  if nSamples<max_x then max_x=nSamples

  cls
  pset(0,240+pSamples[index] shr 5):index+=nChannels
  for x=1 to max_x
    line -(x,240 + (pSamples[index] shr 5)),2
    index+=nChannels
  next
  if nChannels=2 then
    index=1
    pset(0,240+pSamples[index] shr 5):index+=2
    for x=1 to max_x
      line -(x,240 + (pSamples[index] shr 5)),4
      index+=2
    next
  end if
  flip
end sub

'
' main
'
dim as integer key

screenres 512,512,8,2
screenset 1,0
windowtitle "[esc]=quit"

fbs_Init()
fbs_Set_MasterCallback(@MyCallback)
fbs_Enable_MasterCallback()
'
' main loop
'
while (key<>27)
  key=asc(inkey)
  if key=27 then fbs_Disable_MasterCallback()
  sleep 100
wend

tone
Posts: 26
Joined: Dec 17, 2019 21:31

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by tone »

Hi,

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.

Here is the code you can compile:

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
Last edited by tone on Mar 22, 2020 9:08, edited 1 time in total.
tone
Posts: 26
Joined: Dec 17, 2019 21:31

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by tone »

D.J.Peters wrote:file: fbs_on_the_fly.bas

Code: Select all

'  ######################
' # fbs_on_the_fly.bas #
'######################

'#include "../inc/fbsound.bi"
#include "../inc/fbsound_dynamic.bi"

sub MyCallback(byval pSamples as FBS_SAMPLE ptr, _
               byval nChannels as integer, _
               byval nSamples  as integer)
  const as double PISTEP = PI2/44100.0
  static as FBS_SAMPLE sinus(44100-1)
  static as integer Hz(127) 
  static as boolean bInit = false
  static as integer frequence=440,wave_index=0
  static as integer old_played_seconds=0,played_samples=0
  
  if bInit=false then
    ' fill the array only once
    for i as integer=0 to ubound(sinus)
      sinus(i)=sin(i*PISTEP)*8000
    next  
    dim as integer a = 440 ' A is 440 hz...
    for x as integer = 0 to 127
      HZ(x) = (a / 32) * (2 ^ ((x - 9) / 12))
    next
    bInit=true
  end if
  
  
  dim as integer iLeft,iRight=1
  for i as integer=0 to nSamples-1
    pSamples[iLeft] = sinus(wave_index)
    pSamples[iRight] = pSamples[iLeft]*-1
    wave_index=(wave_index+frequence) mod 44100
    iLeft+=2
    iRight+=2
  next  
  
  played_samples+=nSamples
  
  dim as integer played_seconds = played_samples\44100
  if played_seconds<>old_played_seconds then
    old_played_seconds=played_seconds
    dim as integer note = 56+rnd*24 ' two octaves
    frequence=Hz(note)
    windowtitle "note: " & note & " frq: " & frequence
  end if  

  dim as integer n,index,x,max_x=511
  nSamples-=1
  if nSamples<max_x then max_x=nSamples

  cls
  pset(0,240+pSamples[index] shr 5):index+=nChannels
  for x=1 to max_x
    line -(x,240 + (pSamples[index] shr 5)),2
    index+=nChannels
  next
  if nChannels=2 then
    index=1
    pset(0,240+pSamples[index] shr 5):index+=2
    for x=1 to max_x
      line -(x,240 + (pSamples[index] shr 5)),4
      index+=2
    next
  end if
  flip
end sub

'
' main
'
dim as integer key

screenres 512,512,8,2
screenset 1,0
windowtitle "[esc]=quit"

fbs_Init()
fbs_Set_MasterCallback(@MyCallback)
fbs_Enable_MasterCallback()
'
' main loop
'
while (key<>27)
  key=asc(inkey)
  if key=27 then fbs_Disable_MasterCallback()
  sleep 100
wend

Hi DJ Peters,

unfortunately when I compile (no errors), there's no sound. Any ideas why?

Cheers,
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by badidea »

tone wrote:unfortunately when I compile (no errors), there's no sound. Any ideas why?
I hear sound, but that graphics update stops after a while.

edit: typo
Last edited by badidea on Mar 22, 2020 14:38, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by D.J.Peters »

@tone as a beginner put the file : "fbs_on_the_fly.bas" in the original/unchanged fbsound-1.1/tests folder
(don't move any file to any other place don't change the #include line ...)

cd fbsound-1.1/tests
fbc fbs_on_the_fly.bas
fbs_on_the_fly.exe (windows)
./fbs_on_the_fly (linux)

this should work !

if not check the return value of the fbs_Init() commands.

Joshy

Code: Select all

'  ######################
' # fbs_on_the_fly.bas #
'######################
#include "../inc/fbsound_dynamic.bi"

sub MyCallback(byval pSamples as FBS_SAMPLE ptr, _
               byval nChannels as integer, _
               byval nSamples  as integer)
  const as double PISTEP = PI2/44100.0
  static as FBS_SAMPLE sinus(44100-1)
  static as integer Hz(127) 
  static as boolean bInit = false
  static as integer frequence=440,wave_index=0
  static as integer old_played_seconds=0,played_samples=0
  
  if bInit=false then
    ' fill the array only once
    for i as integer=0 to ubound(sinus)
      sinus(i)=sin(i*PISTEP)*8000
    next  
    dim as integer a = 440 ' A is 440 hz...
    for x as integer = 0 to 127
      HZ(x) = (a / 32) * (2 ^ ((x - 9) / 12))
    next
    bInit=true
  end if
  
  
  dim as integer iLeft,iRight=1
  for i as integer=0 to nSamples-1
    pSamples[iLeft] = sinus(wave_index)
    pSamples[iRight] = pSamples[iLeft]*-1
    wave_index=(wave_index+frequence) mod 44100
    iLeft+=2
    iRight+=2
  next  
  
  played_samples+=nSamples
  
  dim as integer played_seconds = played_samples\44100
  if played_seconds<>old_played_seconds then
    old_played_seconds=played_seconds
    dim as integer note = 56+rnd*24 ' two octaves
    frequence=Hz(note)
    windowtitle "note: " & note & " frq: " & frequence
  end if  

  dim as integer n,index,x,max_x=511
  nSamples-=1
  if nSamples<max_x then max_x=nSamples

  cls
  pset(0,240+pSamples[index] shr 5):index+=nChannels
  for x=1 to max_x
    line -(x,240 + (pSamples[index] shr 5)),2
    index+=nChannels
  next
  if nChannels=2 then
    index=1
    pset(0,240+pSamples[index] shr 5):index+=2
    for x=1 to max_x
      line -(x,240 + (pSamples[index] shr 5)),4
      index+=2
    next
  end if
  flip
end sub

'
' main
'
dim as integer key

if fbs_Init()=false then
  print "error: fbs_Init() " & fbs_Get_PlugError()
  beep : sleep: end 1
end if

screenres 512,512,8,2
screenset 1,0
windowtitle "[esc]=quit"

fbs_Set_MasterCallback(@MyCallback)
fbs_Enable_MasterCallback()
'
' main loop
'
while (key<>27)
  key=asc(inkey)
  if key=27 then fbs_Disable_MasterCallback()
  sleep 100
wend
tone
Posts: 26
Joined: Dec 17, 2019 21:31

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by tone »

D.J.Peters wrote:@tone as a beginner put the file : "fbs_on_the_fly.bas" in the original/unchanged fbsound-1.1/tests folder
(don't move any file to any other place don't change the #include line ...)

cd fbsound-1.1/tests
fbc fbs_on_the_fly.bas
fbs_on_the_fly.exe (windows)
./fbs_on_the_fly (linux)

this should work !

if not check the return value of the fbs_Init() commands.

Joshy

Code: Select all

'  ######################
' # fbs_on_the_fly.bas #
'######################
#include "../inc/fbsound_dynamic.bi"

sub MyCallback(byval pSamples as FBS_SAMPLE ptr, _
               byval nChannels as integer, _
               byval nSamples  as integer)
  const as double PISTEP = PI2/44100.0
  static as FBS_SAMPLE sinus(44100-1)
  static as integer Hz(127) 
  static as boolean bInit = false
  static as integer frequence=440,wave_index=0
  static as integer old_played_seconds=0,played_samples=0
  
  if bInit=false then
    ' fill the array only once
    for i as integer=0 to ubound(sinus)
      sinus(i)=sin(i*PISTEP)*8000
    next  
    dim as integer a = 440 ' A is 440 hz...
    for x as integer = 0 to 127
      HZ(x) = (a / 32) * (2 ^ ((x - 9) / 12))
    next
    bInit=true
  end if
  
  
  dim as integer iLeft,iRight=1
  for i as integer=0 to nSamples-1
    pSamples[iLeft] = sinus(wave_index)
    pSamples[iRight] = pSamples[iLeft]*-1
    wave_index=(wave_index+frequence) mod 44100
    iLeft+=2
    iRight+=2
  next  
  
  played_samples+=nSamples
  
  dim as integer played_seconds = played_samples\44100
  if played_seconds<>old_played_seconds then
    old_played_seconds=played_seconds
    dim as integer note = 56+rnd*24 ' two octaves
    frequence=Hz(note)
    windowtitle "note: " & note & " frq: " & frequence
  end if  

  dim as integer n,index,x,max_x=511
  nSamples-=1
  if nSamples<max_x then max_x=nSamples

  cls
  pset(0,240+pSamples[index] shr 5):index+=nChannels
  for x=1 to max_x
    line -(x,240 + (pSamples[index] shr 5)),2
    index+=nChannels
  next
  if nChannels=2 then
    index=1
    pset(0,240+pSamples[index] shr 5):index+=2
    for x=1 to max_x
      line -(x,240 + (pSamples[index] shr 5)),4
      index+=2
    next
  end if
  flip
end sub

'
' main
'
dim as integer key

if fbs_Init()=false then
  print "error: fbs_Init() " & fbs_Get_PlugError()
  beep : sleep: end 1
end if

screenres 512,512,8,2
screenset 1,0
windowtitle "[esc]=quit"

fbs_Set_MasterCallback(@MyCallback)
fbs_Enable_MasterCallback()
'
' main loop
'
while (key<>27)
  key=asc(inkey)
  if key=27 then fbs_Disable_MasterCallback()
  sleep 100
wend
Hi,
Okay, I've moved to the test folder.
For some reason it's not loading the lib fbsound-64, the compiler says null pointer access for the fbs_Init() line.
I am on linux. My library path is: #include "../inc/fbsound_dynamic.bi"
I assume the '../' means to go up one folder in the directory, in which case there is fbsound_dynamic.bi in the inc folder, so should not be an issue there.

Side question, It has been a long time since I wrote a piece of code with pointers. I was wondering, in your function where you pass:
byval pSamples as FBS_SAMPLE ptr

I assume byval is like a non-specified data-type, but what is FBS_SAMPLE ?

Thanks
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by badidea »

tone wrote:Side question, It has been a long time since I wrote a piece of code with pointers. I was wondering, in your function where you pass:
byval pSamples as FBS_SAMPLE ptr

I assume byval is like a non-specified data-type, but what is FBS_SAMPLE ?
You can find the definition of FBS_SAMPLE in fbsound_dynamic.bi (type FBS_SAMPLE as short).

byval (by value) means that pSamples contains of copy of the contents of the pointer used in the callBack call. In this case a memory address because it is a pointer. This way the subroutine cannot (accidental) change the contents of the original pointer value, as would be the case for byref.

Code example:

Code: Select all

sub testByVal(byval pValue as ulong ptr)
	static value as long = 456
	pValue = @value
end sub

sub testByRef(byref pValue as ulong ptr)
	static value as long = 789
	pValue = @value
end sub

dim as ulong value = 123
dim as ulong ptr pValue = @value

print "Memory location: " & pValue & " contains: " & *pValue
testByVal(pValue)
print "Memory location: " & pValue & " contains: " & *pValue
testByRef(pValue)
print "Memory location: " & pValue & " contains: " & *pValue
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by D.J.Peters »

@tone may be libfbsound-64.so not loaded while any open reference !
try the ldd command

Joshy
console wrote:joshy@UBUNTUx86-64:~$ cd fbsound-1.1/tests/
joshy@UBUNTUx86-64:~/fbsound-1.1/tests$ fbc on_the_fly.bas
joshy@UBUNTUx86-64:~/fbsound-1.1/tests$ ldd on_the_fly
linux-vdso.so.1 (0x00007ffec15a5000)
libgtk3-nocsd.so.0 => /usr/lib/x86_64-linux-gnu/libgtk3-nocsd.so.0 (0x00007f8d4d070000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f8d4cd38000)
libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f8d4cb26000)
libXpm.so.4 => /usr/lib/x86_64-linux-gnu/libXpm.so.4 (0x00007f8d4c914000)
libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f8d4c709000)
libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f8d4c4ff000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f8d4c2d5000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8d4bf37000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8d4bd33000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8d4bb14000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8d4b723000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f8d4b4fb000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8d4d277000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f8d4b2f7000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f8d4b0f1000)
libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f8d4aedc000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f8d4acd4000)
joshy@UBUNTUx86-64:~/fbsound-1.1/tests$ ldd libfbsound-64.so
linux-vdso.so.1 (0x00007fffb07f1000)
libgtk3-nocsd.so.0 => /usr/lib/x86_64-linux-gnu/libgtk3-nocsd.so.0 (0x00007fe432fd2000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007fe432da8000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe432a0a000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe432806000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe4325e7000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe4321f6000)
/lib64/ld-linux-x86-64.so.2 (0x00007fe4334cb000)
thebigh
Posts: 43
Joined: Dec 14, 2018 11:11

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by thebigh »

Hello there D. J. Peters,
thanks for this terrific library. It's just what I need for the little game I'm making, and the documentation is nice and clear too.
Can confirm it works (or at least many of the test programs work) on Ubuntu MATE 18.04 64 bit.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by D.J.Peters »

fbsound V 1.2 are on the way SID WAV MP3 OGG MOD IT XM S3M

Commodore SID files are the fun part (for me)

You have not only to emulate all registers of the the good old SID chip you must implement a full working 65XX CPU also !
But I have done it before see Old school C64 the SID streaming thread shows 0% CPU usage in the Windows XP, 7,10 task manager :-)

Windows 32/64 and Linux 64-bit are done currently I have trouble cross compile on th Linux 64-bit the 32-bit version of fbsound 1.2.
(looks like I must reinstall the ALSA dev package for 32-bit or something like this)

Joshy
changelog.txt wrote:fbsound 1.2:
added:
Commodore C64 SID file decoder
FBS_Create_SIDStream()
FBS_Play_SIDStream()
FBS_End_SIDStream()
FBS_Set_SIDStreamVolume()
FBS_Get_SIDStreamVolume()
FBS_Set_SIDStreamPan()
FBS_Get_SIDStreamPan()
FBS_Get_SIDStreamBuffer()
FBS_Get_SIDAuthor()
FBS_Get_SIDInfo()
FBS_Get_SIDTitle()
FBS_Set_SIDStreamCallback()
FBS_Enable_SIDStreamCallback()
FBS_Disable_SIDStreamCallback()

fbsound 1.1:
is a dynmaic lib now

fbsound 1.0:
first full 32/64-bit version

fbSound 0.20
added: win32/libogg.a win32/libvorbis.a win32/libvorbisfile.a
new: fbs_Load_OGGFile()

fbSound 0.19
added: DirectSound driver plug-ds.dll
changed: one more optional param for fbs_Init([nRate],[nChannels],[nBuffers],[nFrames],[PlugIndex],[DeviceIndex])

fbSound 0.18:
added: win32/libdumb.a lin32/libdumb.a
new: fbs_Load_MODFile() (*.mod, *.it, *.xm, *.s3m)

fbSound 0.17:
added:
fbs_Set_LoadCallback()
fbs_Enable_LoadCallback()
fbs_Disable_LoadCallback()
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: fbsound 1.1 (dynamic) Windows/Linux 32 and 64-bit (wav mp3 ogg mod it xm s3m)

Post by srvaldez »

hello D.J.Peters :-)
I look forward the the release and play with SID
Post Reply