FBSound API

This document describes the FBSound API version 1.0.
All commands with parameters are listed. If you need clarification on any details, please take a look in the "/test" folder too. Every available command is used in the examples "testXYZ.bas"

FBSound is a free soundlib primarily for games and demos. The API interface is written for the open source FreeBASIC compiler. It supports dynamically loadable plugin's.
Linux x86 (alsa, dsp, arts)
libplug-alsa-32.so, libplug-dsp-32.so, libplug-arts-32.so

Linux x86_64 (alsa)
libplug-alsa-64.so

Windows x86 (DirectSound, MultiMediaDevice,)
plug-ds-32.dll, plug-mm-32.dll

Windows x86_64 (DirectSound, MultiMediaDevice,)
plug-ds-64.dll, plug-mm-64.dll

Install FBSound.

Extract your FBSound download included with the folder tree.

fbsound-1.0/
fbsound-1.0/data
fbsound-1.0/doc
fbsound-1.0/lib
fbsound-1.0/inc
fbsound-1.0/src
fbsound-1.0/tests

Explanation about the type of parameters in this document.

nParam means a number (integer)
sParam means a floating point number (single)
blnParam means boolean (true or false)
strParam means chars (string)
@nParam means a ptr on number (integer)
@sParam means a ptr on floating point number (single)
@lpParam means a ptr on short ptr (16 bit samples)
@blnParam means a ptr on boolean (true or false)
[Param] means this param are optional

Function Summary: init/start/stop/exit FBSound.
fbs_Init([nRate],[nChannels],[nBuffers],[nFrames],[nIndex]) as boolean
     Setup any available playback devices. (internal: fbs_Start() will be called too)
fbs_Start() as boolean
     (optional) Start the sound threads.
fbs_Stop() as boolean
     (optional) Stop all running threads.
fbs_Exit() as boolean
     (optional) Remove FBSound (internal: fbs_Stop() will be called if any threads are running)

 

Function Summary: get/set plugin values.
fbs_Set_PlugPath(strNewPath) as boolean
     (optional) Set the folder where fbs_Init() should load dynamic plugins from (default = exepath).
fbs_Get_PlugPath() as string
     Get the folder where fbs_Init() load dynamic plugins from.
fbs_Get_PlugOuts() as integer
     Get the number of available playback plugins.
fbs_Get_PlugName() as string
     Get the name of the current pluin.
fbs_Get_PlugDevice() as string
     Get the name of the current playback device.
fbs_Get_PlugError() as string
     Get the error description if fbs_Init() returns False.
fbs_Get_PlugRate() as integer
     Get the current playback rate in Hz.
fbs_Get_PlugBits() as integer
     Get the number of bits per sample. (16 bit)
fbs_Get_PlugChannels() as integer
     Get the current number of playback channels (1 = mono, 2 = stereo).
fbs_Get_PlugBuffers() as integer
     Get the current number of playback buffers. (3 = default).
fbs_Get_PlugFrames() as integer
     Get the current number of frames per buffer. (2048 = default).
fbs_Get_PlugChannels() as integer
     Get the current buffersize in bytes. (default: 2048 * 16bits * 2Channels) \ 8bits = 8192 bytes)
fbs_Get_PlugFramesize() as integer
     Get the current framesize in bytes. (default: (16bits * 2Channels) \ 8bits = 4 bytes)
fbs_Get_PlugRunning() as boolean
     Get the current state of the playback device True/False. See: fbs_Start() / fbs_Stop() (default: True after calling fbs_Init())

 

Function Summary: get/set properties of FBSound.
fbs_Get_PlayingSounds() as integer
     Get the number of current playing sounds. See: fbs_Play_Wave() / fbs_Play_Sound().
fbs_Get_PlayingStreams() as integer
     Get the number of current playing Streams. See: fbs_Play_MP3Stream().
fbs_Get_PlayedBytes() as integer
     Get the number of played bytes since fbs_Init() was called.
fbs_Get_PlayedSamples() as integer
     Get the number of played samples since fbs_Init() was called.
fbs_Get_PlayTime() as double
     Get the high resolution play time in seconds since fbs_Init() was called.
fbs_Get_MasterVolume(@sVolume) as boolean
     Get the current main volume (default = 1.0) after fbs_Init() was called.
fbs_Set_MasterVolume(sVolume) as boolean
     Set the main volume.
fbs_Get_MaxChannels(@nChannels) as boolean
     Get the current number of playable sounds/channels in parallel. (default = 128).
fbs_Set_MaxChannels(nChannels) as boolean
     Set the number of playable sounds in parallel.
fbs_Set_MasterFilter(nFilter,sHZ,sDB,[sOctave],[blnEnabled]) as boolean
     Set params of 10 posible master band EQ's.
fbs_Enable_MasterFilter(nFilter) as boolean
     Enable one of 10 posible master band EQ's.
fbs_Disable_MasterFilter(nFilter) as boolean
     Disable one of 10 posible master band EQ's.
fbs_Volume_2_dB(sVolume) as single
     Tanslate volume in dB.
fbs_dB_2_Volume(sdB) as single
     Tanslate dB in volume.

 

Function Summary: load/create/remove wave objects in memory.
fbs_Load_WAVFile(strFilename,@hWave) as boolean
     Load any *.wav file in memory.
fbs_Load_MP3File(strFilename,@hWave [,strTmpFile as string]) as boolean
     Load any *.mp3 *.mp2 *.mp file in memory
fbs_Load_OGGFile(strFilename,@hWave [,strTmpFile as string]) as boolean
     Load any *.ogg file in memory
fbs_Load_MODFile(strFilename,@hWave [,strTmpFile as string]) as boolean
     Load any *.mod *.xm *.ts *.s3m file in memory
fbs_Create_Wave(nSamples,@hWave,@lpSamples) as boolean
     Create a new wave with nSamples samples in memory.
fbs_Create_Sound(hWave,@hSound) as boolean
     Create a playable sound hSound from the loaded wave hWave.
fbs_Destroy_Sound(@hSound) as boolean
     Remove any sound from memory.
fbs_Destroy_Wave(@hWave) as boolean
     Remove any wave from memory.

 

Function Summary: create/remove a stream object.
fbs_Create_MP3Stream(strFilename) as boolean
     Prepare a Stream for real-time decoding.
fbs_End_MP3Stream() as boolean
     Stop and remove an MP3Stream and its real-time decoder

 

Function Summary: play waves/sounds/streams.
fbs_Play_Wave(hWave,[nLoops],[sSpeed],[sVolume],[sPan],[@hSound]) as boolean
     Play a loaded hWave object and create an optional hSound object from it.
fbs_Play_Sound(hSound,[nLoops]) as boolean
     Play a created hSound object.
fbs_Play_MP3Stream() as boolean
     Play a prepared stream object.

 

Function Summary: get properties of an wave object.
fbs_Get_WavePointers(hWave,[@lpStart],[@lpEnd],[@nChannels]) as boolean
     Get current pointers, channels, and samples from playing sound.
fbs_Get_WaveLength(hWave,@ms) as boolean
     Get length of sound in milli seconds ms/1000 = seconds.

 

Function Summary: get/set properties of an sound object.
fbs_Get_SoundLoops(hSound,@nLoops) as boolean
     Get the current number of sound loops.
fbs_Set_SoundLoops(hSound,nLoops) as boolean
     Set the new number of sound loops.
fbs_Get_SoundSpeed(hSound,@sSpeed) as boolean
     Get the passed sound's playback speed.
fbs_Set_SoundSpeed(hSound,sSpeed) as boolean
     Set the passed sound's playback speed.
fbs_Get_SoundVolume(hSound,@sVolume) as boolean
     Get the passed sound's volume.
fbs_Set_SoundVolume(hSound,sVolume) as boolean
     Set the passed sound's volume.
fbs_Get_SoundPan(hSound,@sPan) as boolean
     Get the passed sound's panning position.
fbs_Set_SoundPan(hSound,sPan) as boolean
     Set the passed sound's panning position.
fbs_Get_SoundMuted(hSound,@blnMuted) as boolean
     Get the passed sound's muted state.
fbs_Set_SoundMuted(hSound,blnMuted) as boolean
     Set the passed sound's muted state.
fbs_Get_SoundPaused(hSound,@blnPaused) as boolean
     Get the passed sound's pause state.
fbs_Set_SoundPaused(hSound,blnPaused) as boolean
     Set the passed sound's pause state.
fbs_Get_SoundPointers(hSound,[@lpStart],[@lpPlay],[@lpEnd]) as boolean
     Get pointers from sound object.
fbs_Set_SoundPointers(hSound,[lpStart],[lpPlay],[lpEnd]) as boolean
     Set pointers from sound object.
fbs_Get_SoundPosition(hSound,@sPosition) as boolean
     Get current playback position (0.0-1.0) from sound object.

Function Summary: get/set properties of a stream.
fbs_Get_StreamVolume(@sVolume) as boolean
     Get the passed stream's volume.
fbs_Set_StreamVolume(sVolume) as boolean
     Set the passed stream's volume.
fbs_Get_StreamPan(@sPan) as boolean
     Get the passed stream's panning position.
fbs_Set_StreamPan(sPan) as boolean
     Set the passed stream's panning position.
fbs_Get_StreamBuffer(@lpBuffer,@nChannels,@nSamples) as boolean
     Get the current stream's sample buffer.

 

Function Summary: set/enable/disable user defined buffer callbacks.
fbs_Set_MasterCallback(@Callback as BUFFERCALLBACK) as boolean
     Set a buffer callback sub for the master channel. (samples are processed after the mixer pipeline)
fbs_Enable_MasterCallback() as boolean
     Enable the master buffer callback.
fbs_Disable_MasterCallback() as boolean
     Disable the master buffer callback.
fbs_Set_SoundCallback(hSound,@Callback as BUFFERCALLBACK) as boolean
     Set a buffer callback sub for any sound. (samples are processed before the mixer pipeline)
fbs_Enable_SoundCallback(hSound) as boolean
     Enable sound buffer callback.
fbs_Disable_SoundCallback(hSound) as boolean
     Disable sound buffer callback.
fbs_Set_StreamCallback(@Callback as BUFFERCALLBACK) as boolean
     Set a buffer callback sub for a stream. (samples are processed before the mixer pipeline)
fbs_Enable_StreamCallback() as boolean
     Enable stream buffer callback.
fbs_Disable_StreamCallback() as boolean
     Disable stream buffer callback.

 

Function Summary: set/enable/disable user defined load callback.
fbs_Set_LoadCallback(@Callback as LOADCALLBACK) as boolean
     Set a load callback sub for file loading.
fbs_Enable_LoadCallback() as boolean
     Enable the load callback.
fbs_Disable_LoadCallback() as boolean
     Disable the load callback.

 

The FBSound API in more details:

function: fbs_Init()

fbs_Init([nRate],[nChannesl],[nBuffers],[nFrames],[nPluinIndex],[nDevceIndex]) as boolean
Setup a playback device. This function is the key to use FBSound and must be the first call.

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

const data_path = "../data/"

' only if not same as exe path
' fbs_Set_PlugPath("./path_of_plugins")

fbs_Init()


    Parameters:
      nRate as integer (Playback rate in Hz.)
             Range: (6000-11025, 22050, 32000, 44100, 48000, 96000)
             Default: 44100
 
      nChannels as integer (Number of playback channels.)
             Range: 1 = mono, 2 = stereo
             Default: 2 (stereo)
 
      nBuffers as integer (Number of playback buffers.)
             Default: 3
 
      nFrames as integer (Number of frames per buffer.)
             Default: 2048
 
      nIndex as integer (Specify which device should be used by default.)
             Range: 0 = first, 1 = second, ...
             Default: 0 (first device)
 

   Returns:
     False if an error occurred
     True otherwise

functions: fbs_Start(), fbs_Stop(), fbs_Exit().

There is normally no need to use these functions manually.
fbs_Start() will be called internally from
fbs_Init(). If any FreeBASIC program ends, fbs_Stop() and fbs_Exit() will be called internally too.

There is one reason to use these functions manually:
If you want to change the FBSound setup via fbs_Init() first call fbs_Stop() then fbs_Exit(). all playing waves/sounds/streams are stopped and removed from memory. Now use fbs_Init() with any new setup and reload/create waves/sounds/streams. (The old will be invalid)

    Parameters:
      none

    Returns:
     False if an error occurred
     True otherwise

sub: fbs_Set_PlugPath().

fbs_Set_PlugPath(strNewPath)
Normally
fbs_Init() searches in the exepath for available dynamically loadable plugins/outs. If the exepath is not the folder with the dynamic libs you must call fbs_Set_PlugPath("newpath/") before you can use fbs_Init().



fbs_Set_PlugPath("../lib/")
fbs_Init()

   Parameter:
      NewPath as string

functions: fbs_Load_WAVFile(), fbs_Load_MP3File(), fbs_Load_OGGFile(), fbs_Load_MODFile().

fbs_Load_WAVFile(strFileName,@hWave)
fbs_Load_MP3File(strFileName,@hWave)
fbs_Load_OGGFile(strFileName,@hWave)
fbs_Load_MODFile(strFileName,@hWave)
This functions load media files into the pool of memory waves.
The returned hWave is the ID of the new created wave and can be used with
fbs_Play_Wave().

dim as integer hWave1,hWave2,hWave2,hWave4
fbs_Init()
fbs_Load_WAVFile("anyfile.wav",@hWave1)
fbs_Load_MP3File("anyfile.mp3",@hWave2)
fbs_Load_OGGFile("anyfile.ogg",@hWave3)
fbs_Load_MODFile("anyfile.xxx",@hWave4)
' (.xxx) .mod, .it, .s3m or .xm
fbs_Play_Wave(hWave1)
fbs_Play_Wave(hWave2)
fbs_Play_Wave(hWave3)
fbs_Play_Wave(hWave4)
sleep

   Parameters:
      FileName as string (The file's name.)

      @hWave as integer (The hWave to create.)

   Returns:
     False if an error occurred
     True otherwise

function: fbs_Play_Wave().

fbs_Play_Wave(hWave, [nLoops], [sSpeed], [sVolume], [sPan],[@hSound])
Start playback of an hWave object.
The wave can be loaded from either
fbs_Load_WAVFile(), fbs_Load_MP3File(), fbs_Load_OGGFile(), fbs_Load_MODFile()
or created by fbs_Create_Wave(). Optional you can create an hSound object too. See get / set sound properties.

dim as integer hWave
fbs_Load_WAVFile("anywave.wav",@hWave)
fbs_Play_Wave(hWave)
Play an hWave object.

fbs_Play_Wave(hWave,3)
Loop hWave 3 times

fbs_Play_Wave(hWave,2,,,-1.0)
Loop hWave 2 times on the left channel

fbs_Play_Wave(hWave,,1.5)
Play hWave with 150% of the normal speed

   Parameters:
      hWave as integer (The ID of the wave in memory.)

      nLoops as integer (Number of times to loop the sample.)
   Range: 1 to 231
   Default: 1

      sSpeed as single (The sample's playback speed.)
   Range: -4.0 (400% speed backwards) to +4.0 (400% speed forwards)
   Default: 1.0 (100% speed forwards)

      sPan as single (The sample's stereo location.)
   Range: -1.0 (full left) to +1.0 (full right)
   Default: 0.0 (center)

      @hSound (optional: the new created hSound object.)

   Returns:
     False if an error occurred
     True otherwise

functions: fbs_Create_Sound() and fbs_Play_Sound().

fbs_Create_Sound(hWave,@hSound) as boolean
fbs_Play_Sound(hSound, [nLoops]) as boolean
Create an hSound object from loaded hWave object with fbs_Create_Sound().
Start the hSound playback with fbs_Play_Sound()

The main difference between playing an hWave handle with
fbs_Play_Wave() and playing an hSound handle with fbs_Play_Sound() is that you can change all properties of a playing hSound while it is playing/created/stopped or paused.

See get/set sound properties.

dim as integer hWave,hSound
fbs_Load_WAVFile("anywave.wav",@hWave)
fbs_Create_Sound(hWave,@hSound)
fbs_Play_Sound(hSound)
sleep

Create and play an hSound object:

fbs_Play_Sound(hSound,3)
Loop hSound 3 times

   Parameters:
      hWave as integer (The hWave to use.)
      @hSound as integer (The hSound to create.)

   Returns:
     False if an error occurred
     True otherwise

function: fbs_Get_WavePointes().

fbs_Get_WavePointers (hWave,@lpStart,@lpEnd,@nChannels) as boolean

   Parameters:
      hWave as integer (The hWave whose properties to get.)

      @lpStart as short ptr (The address of first sample in memory.)
   Default: NULL

      @lpEnd as short ptr (The address of last sample in memory.)
   Default: NULL

      @nChannesl integer ptr (The number of channels.)
   Default: NULL

   Returns:
     False if an error occurred
     True otherwise

functions: get/set sound properties.

fbs_Get_SoundLoops (hSound,@nLoops   ) as boolean
fbs_Set_SoundLoops (hSound, nLoops   ) as boolean
fbs_Get_SoundSpeed (hSound,@sSpeed   ) as boolean
fbs_Set_SoundSpeed (hSound, sSpeed   ) as boolean
fbs_Get_SoundVolume (hSound,@sVolume  ) as boolean
fbs_Set_SoundVolume (hSound, sVolume  ) as boolean
fbs_Get_SoundPan(hSound,@sPan     ) as boolean
fbs_Set_SoundPan(hSound, sPan     ) as boolean
fbs_Get_SoundMuted (hSound,@blnMuted ) as boolean
fbs_Set_SoundMuted (hSound, blnMuted ) as boolean
fbs_Get_SoundPaused (hSound,@blnPaused) as boolean
fbs_Set_SoundPaused (hSound, blnPaused) as boolean
fbs_Get_SoundPointers (hSound,@lpStart,@lpPlay,@lpEnd) as boolean
fbs_Set_SoundPointers (hSound,lpStart,lpPlay,lpEnd) as boolean
fbs_Get_SoundPosition (hSound,@sPosition) as boolean

After you have created an hSound object with
fbs_Create_Sound(), you can set any and all of its properties. It does not matter if the hSound is playing / freshly created / paused or muted, you can change the properties at any time.

The advantage: for an hWave object some of these properties can only be set once with fbs_Play_Wave()

   Parameters:
      hSound as integer (The hSound whose properties to set.)

      nLoops as integer (The number of times to loop the hSound.)
   Range: 1 to 231
   Default: 1

      sSpeed as single (The playback speed of the hSound.)
   Range: -4.0 (400% speed backwards) to +4.0 (400% speed forwards)
   Default: 1.0 (100% speed forwards)

      sVolume as single (The hSound's playback volume.)
   Range: 0.0 to 2.0
   Default: 1.0

      sPan as single (The hSound's stereo location.)
   Range: -1.0 (full left) to +1.0 (full right)
   Default: 0.0 (center)

      blnMuted as boolean (The muted state of the hSound.)
   Range: True (no sound while playing default) or False (sound on)
   Default: False

      blnPaused as boolean (The paused state of the hSound.)
   Range: True (paused) or False (normal play)
   Default: False

      lpStart as short ptr (The first playable sample of the hSound.)
   Default: NULL

      lpPlay as short ptr (The play position of the hSound.)
   Default: NULL

      lpEnd as short ptr (The last playable sample of the hSound.)
   Default: NULL

   Returns:
     False if an error occurred
     True otherwise

functions: create/play/end an MP3Stream.

fbs_Create_MP3Stream(strFilename) as boolean
fbs_Play_MP3Stream() as boolean
fbs_Get_PlayingStreams() as integer
fbs_End_MP3Stream() as boolean

Prepare an MP3Stream and it's real time decoder with fbs_Create_MP3Stream("filename") and begin stream playback with fbs_Play_MP3Stream()

' prepare the real time decoder
fbs_Create_MP3Stream("../data/legends.mp3)
' start stream playback
fbs_Play_MP3Stream()
sleep


' prepare the real time decoder
fbs_Create_MP3Stream("../data/legends.mp3)
' start stream playback
fbs_Play_MP3Stream()
' wait of first decoded sound buffer
while fbs_Get_PlayingStreams()=0:sleep 10:wend

print "wait on end of stream or hit any key to quit."
while inkey="" and fbs_Get_PlayingStreams()>0
sleep 1000
wend

   Parameters:
      strFilename as string (The *.mp3 *.mp2 or *.mp FileName.)

   Returns:
     False if an error occurred
     True otherwise

functions: get/set stream properties.

fbs_Get_StreamVolume(@sVolume) as boolean
fbs_Set_StreamVolume( sVolume) as boolean
fbs_Get_StreamPan(@sPan) as boolean
fbs_Set_StreamPan( sPan) as boolean

   Parameters:
      sVolume as single (The hSound's playback volume.)
   Range: 0.0 to 2.0
   Default: 1.0

      sPan as single (The hSound's stereo location.)
   Range: -1.0 (full left) to +1.0 (full right)
   Default: 0.0 (center)

   Returns:
     False if an error occurred
     True otherwise

functions: set/enable/disable user defined buffer callbacks.

type BUFFERCALLBACK as sub (
  byval lpSamples as short ptr, _
  byval nChannels as integer , _
  byval nSamples  as integer )

fbs_Set_MasterCallback(@YourCallback as BUFFERCALLBACK) as boolean
fbs_Enable_MasterCallback() as boolean
fbs_Disable_MasterCallback() as boolean

fbs_Set_SoundCallback(hSound,@YourCallback as BUFFERCALLBACK) as boolean
fbs_Enable_SoundCallback(hSound) as boolean
fbs_Disable_SoundCallback(hSound) as boolean

fbs_Set_StreamCallback(@YourCallback as BUFFERCALLBACK) as boolean
fbs_Enable_StreamCallback() as boolean
fbs_Disable_StreamCallback() as boolean

After you have set and enabled a buffer callback it will be called for every new sound buffer.

The master buffer callback is called with all ready to use samples and is processed after the dsp and mixer pipeline. After this callback the buffer is sent to the current playback device.

The sound buffer callback is called for any sound object passed as an hSound and is processed before the dsp and mixer pipeline.

The stream buffer callback is called after a new buffer is decoded and is processed before the dsp and mixer pipeline.

You can read and write any sample of a new buffer from inside the callback. But be sure your sub will need not too much time because the dsp and mixer thread will wait until your callback sub finishes.

   Parameters:
      hSound as integer (The sound ID.)

      @YourCallback as BUFFERCALLBACK (Defined in fbstypes.bi)

   Returns:
     False if an error occurred
     True otherwise

functions: set/enable/disable user defined load callback.

type LOADCALLBACK as sub (byval Percent as integer)

fbs_Set_LoadCallback(@YourLoadCallback as LOADCALLBACK) as boolean
fbs_Enable_LoadCallback() as boolean
fbs_Disable_LoadCallback() as boolean

After you have set and enabled a load callback it will be called for every active file loading.

Please take a look in the /test folder.

functions: set/enable/disable one of 10 posible master band EQ's.

fbs_Set_MasterFilter(nFilter,sHz,sdB,[sOctave=1.0],[blnEnable=False]) as boolean
fbs_Enable_MasterFilter(nFilter) as boolean
fbs_Disable_MasterFilter(nFilter) as boolean

You can set/enable/disable 10 posible master band EQ's.

The filters processed after the mixer pipeline. After this dsp section the buffer is sent to the current playback device.

    Parameters:
      nFilter as integer (The filter ID 0-9.)
      sHz as single (The center frequenc in Hz.)
      sdB as single (The band volume in dB.)
      sOctave as single (The optional band width in octaves default=1.0.)
      blnOnOff as boolean (The optional en/disable switch default=False)

   Returns:
     False if an error occurred
     True otherwise

functions: simple translatition between volume and dB

fbs_Volume_2_dB(byval Volume) as single
fbs_dB_2_Volume(byval dB as single) as single

   Parameters:
      dB as single (dB to volume)
      volume as single (volume to dB.)

   Returns:
     as single the translated value.