FBSound API

This document describes the FBSound API version 1.1.
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.1/
fbsound-1.1/data
fbsound-1.1/doc
fbsound-1.1/lib
fbsound-1.1/inc
fbsound-1.1/tests

Explanation about the type of parameters in this document.

nParameter means a number (integer 32 or 64bit)
fParameter means a floating point number (single)
bParameter means a boolean (true or false)
sParameter means a string
hParameter means a handle (hWave or hSound integer 32 or 64bit)
@nParameter means a pointer on number (integer 32 or 64bit)
@fParameter means a pointer on floating point number (single)
@pParameter means a pointer on 16-bit samples (short)
@bParameter means a pointer on boolean (true or false)
@hParameter means a pointer on handle (hWave or hSound)
[Param] means this parameter are optional

Function Summary: init/start/stop/exit FBSound.
fbs_Init([nRate],[nChannels],[nBuffers],[nFrames],[nPluginIndex],[nDeviceIndex]) 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(sPath) 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(@fVolume) as boolean
     Get the current main volume (default = 1.0) after fbs_Init() was called.
fbs_Set_MasterVolume(fVolume) 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. (max = 512)
fbs_Set_MasterFilter(nFilterIndex, fHZ, fDB,[fOctave],[bEnabled]) as boolean
     Set parameters of 10 posible master band EQ's. (nFilterIndex 0..9)
fbs_Enable_MasterFilter(nFilterIndex) as boolean
     Enable one of 10 posible master band EQ's. (nFilterIndex 0..9)
fbs_Disable_MasterFilter(nFilter) as boolean
     Disable one of 10 posible master band EQ's. (nFilterIndex 0..9)
fbs_Volume_2_dB(fVolume) as single
     Tanslate volume in dB.
fbs_dB_2_Volume(fdB) as single
     Tanslate dB in volume.

 

Function Summary: load/create/remove wave objects in memory.
fbs_Load_WAVFile(strFilename, @hWave) as boolean
     Load none compressed *.wav file in memory (must be in 8 or 16bit mono or stereo).
fbs_Load_MP3File(strFilename, @hWave [,sTmpFile as string]) as boolean
     Load any *.mp3 *.mp2 *.mp file in memory
fbs_Load_OGGFile(strFilename, @hWave [,sTmpFile as string]) as boolean
     Load any *.ogg file in memory
fbs_Load_MODFile(strFilename, @hWave [,sTmpFile as string]) as boolean
     Load any *.mod *.xm *.ts *.s3m file in memory
fbs_Create_Wave(nSamples,@hWave, @pSamples) 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 hSound object from memory.
fbs_Destroy_Wave(@hWave) as boolean
     Remove hWave object from memory.

 

Function Summary: create/remove a stream object.
fbs_Create_MP3Stream(sFilename) 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=1],[fSpeed=1.0], [fVolume=1.0], [fPan=0.0], [@hSound]) as boolean
     Play a loaded hWave object and create an optional hSound object from it.
fbs_Play_Sound(hSound, [nLoops=1]) 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, [@pStart],[@pEnd], @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, @fSpeed) as boolean
     Get the passed sound's playback speed.
fbs_Set_SoundSpeed(hSound, fSpeed) as boolean
     Set the passed sound's playback speed.
fbs_Get_SoundVolume(hSound ,@fVolume) as boolean
     Get the passed sound's volume.
fbs_Set_SoundVolume(hSound,fVolume) as boolean
     Set the passed sound's volume.
fbs_Get_SoundPan(hSound, @fPan) as boolean
     Get the passed sound's panning position.
fbs_Set_SoundPan(hSound, fPan) as boolean
     Set the passed sound's panning position.
fbs_Get_SoundMuted(hSound, @bMuted) as boolean
     Get the passed sound's muted state.
fbs_Set_SoundMuted(hSound, bMuted) as boolean
     Set the passed sound's muted state.
fbs_Get_SoundPaused(hSound, @bPaused) as boolean
     Get the passed sound's pause state.
fbs_Set_SoundPaused(hSound, bPaused) as boolean
     Set the passed sound's pause state.
fbs_Get_SoundPointers(hSound, [@pStart], [@pPlay], [@pEnd]) as boolean
     Get pointers from sound object.
fbs_Set_SoundPointers(hSound, [lpStart], [pPlay] ,[pEnd]) as boolean
     Set pointers from sound object.
fbs_Get_SoundPosition(hSound, @fPosition) as boolean
     Get current playback position (0.0-1.0) from sound object.

Function Summary: get/set properties of a stream.
fbs_Get_StreamVolume(@fVolume) as boolean
     Get the passed stream's volume.
fbs_Set_StreamVolume(fVolume) as boolean
     Set the passed stream's volume.
fbs_Get_StreamPan(@fPan) as boolean
     Get the passed stream's panning position.
fbs_Set_StreamPan(fPan) as boolean
     Set the passed stream's panning position.
fbs_Get_StreamBuffer(@pBuffer, @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
 
      nPluinIndex as integer (Specify which plugout should be used by default.)
             Range: 0 = first, 1 = second, ...
             Default: 0 (first plugout)
 
      nDeviceIndex as integer (Specify which playback 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(sNewPath)
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(sFileName,@hWave)
fbs_Load_MP3File(sFileName,@hWave)
fbs_Load_OGGFile(sFileName,@hWave)
fbs_Load_MODFile(sFileName,@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], [fSpeed=1.0], [fVolume=1.0], [fPan=0.0], [@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

      fSpeed 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)

      fPan 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, @pStart, @pEnd, @nChannels) as boolean

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

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

      @pEnd 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, @fSpeed   ) as boolean
fbs_Set_SoundSpeed (hSound, fSpeed   ) as boolean
fbs_Get_SoundVolume (hSound, @fVolume  ) as boolean
fbs_Set_SoundVolume (hSound, fVolume  ) as boolean
fbs_Get_SoundPan(hSound, @fPan     ) as boolean
fbs_Set_SoundPan(hSound, fPan     ) as boolean
fbs_Get_SoundMuted (hSound, @bMuted ) as boolean
fbs_Set_SoundMuted (hSound, bMuted ) as boolean
fbs_Get_SoundPaused (hSound, @bPaused) as boolean
fbs_Set_SoundPaused (hSound, bPaused) as boolean
fbs_Get_SoundPointers (hSound, @pStart, @pPlay, @pEnd) as boolean
fbs_Set_SoundPointers (hSound, pStart, pPlay, pEnd) as boolean
fbs_Get_SoundPosition (hSound, @fPosition) 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

      fSpeed 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)

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

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

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

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

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

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

      pEnd 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(sFilename) 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:
      sFilename as string (The *.mp3 *.mp2 or *.mp FileName.)

   Returns:
     False if an error occurred
     True otherwise

functions: get/set stream properties.

fbs_Get_StreamVolume(@fVolume) as boolean
fbs_Set_StreamVolume(fVolume) as boolean
fbs_Get_StreamPan(@fPan) as boolean
fbs_Set_StreamPan(fPan) as boolean

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

      fPan 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 pSamples 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 boolean
fbs_Enable_SoundCallback(hSound) as boolean
fbs_Disable_SoundCallback(hSound) as boolean

fbs_Set_StreamCallback(@YourCallback) 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 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, fHz, fdB,[fOctave=1.0],[bEnable=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:
      nFilterIndex as integer (0 .. 9)
      fHz as single (The center frequenc in Hz.)
      fdB as single (The band volume in dB.)
      fOctave as single (The optional band width in octaves default=1.0.)
      bOnOff as boolean (The optional en/dis-able switch default=False)

   Returns:
     false if an error occurred
     true otherwise

functions: simple translatition between volume and dB

fbs_Volume_2_dB(fVolume) as single
fbs_dB_2_Volume(fdB) as single

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

   Returns:
     as single the translated value.