'' ----------------------------------------------------------------------------
'' IrrKlang Wrapper for Imperative Languages - Freebasic Examples
'' Frank Dodd (2009)
'' ----------------------------------------------------------------------------
'' Example 03: Play a sound in 3D space
'' This simple example moves an observer in sound space to effect the volume and
'' stereo output of the sound
'' ----------------------------------------------------------------------------

'' ////////////////////////////////////////////////////////////////////////////
'' Includes for extension libraries
#include "IrrKlangWrapper.bi"

'' ////////////////////////////////////////////////////////////////////////////
'' global variables
DIM Position as single = 0
DIM Direction as integer = 1
DIM LastTime as Double
DIM sound as irr_sound

'' ////////////////////////////////////////////////////////////////////////////
'' GDB debugger main() function

' start the irrklang sound engine
IrrKlangStart

' Set the position of the listener
IrrKlangSetListenerPosition( 0,0,-25, 0,0,0 )

' Play a sound in 3D space
sound = IrrKlangPlay3D ( "./media/getout.ogg", 0,0,0, _
        IRR_KLANG_FALSE, IRR_KLANG_FALSE, IRR_KLANG_TRUE )

IrrKlangSetMinDistance( sound, 100.0 )

' pan the sound from left to right
? "Press 'Q' to end"

Do
    ' we only want to update the sound position if 0.05 seconds have passed
    if LastTime + 0.05 < Timer then
        ' make a note of the current time so we know when to do this again
        LastTime = Timer

        ' if we are moving left
        if Direction = 1 then
            ' if we are at the left limit
            if Position > 8 then
                ' start to move right
                Direction = -1
            else
                ' keep moving left
                Position += 0.1
                ' set the position of the observer of the sound
                IrrKlangSetListenerPosition( Position,0,-25, 0,0,0 )

                ' NOTE: we could achieve the same effect by moving the sound
                ' with the command described below
                ' IrrKlangSetPosition( sound, position, 0, 0 )

                ' display the current position and rewind the cursor
                ? Position; "       "
                Locate 5, 0
            end if
        else
            ' we must be moving right
            ' if we are at the right limit
            if Position < -8 then
                ' start moving left
                Direction = 1
            else
                ' keep moving right
                Position -= 0.1
                ' set the position of the observer of the sound
                IrrKlangSetListenerPosition( Position,0,-25, 0,0,0 )

                ' NOTE: we could achieve the same effect by moving the sound
                ' with the command described below
                ' IrrKlangSetPosition( sound, position, 0, 0 )

                ' display the current position and rewind the cursor
                ? Position; "       "
                Locate 5, 0
            end if
        end if
    end if

Loop Until Inkey = "q"

' Stop the urrklang sound engine and release resources
IrrKlangStop
