	/'===============================================================================================
 FSB Example
 Copyright (c), Firelight Technologies Pty, Ltd 2004-2011.

 This example demonstrates the usage of the FSB format, generated by the FSBank tool.
 It is simply a packed file format, with sounds contained within.  This is a cross platform
 format that is useful to keep the data format optimal for the platform you are targeting.
 (ie PS2, Xbox, Gamecube have native compression formats)
==============================================================================================='/
#include "windows.bi"
#include "crt.bi"

#include "fmodex/fmodex.bi"
#include "fmodex/fmod_errors.bi"

sub ERRCHECK(byval result as FMOD_RESULT)
    if (result <> FMOD_OK) then
        printf(!"FMOD error! (%d) %s\n", result, FMOD_ErrorString(result))
        end -1
    end if
end sub

' program start
    dim as FMOD_SYSTEM    ptr fsystem = any
    dim as FMOD_SOUND     ptr fsb = any
    dim as FMOD_CHANNEL   ptr channel = 0
    dim as FMOD_RESULT     result = any
    dim as string             key
    dim as integer count = any, numsubsounds = any
    dim as uinteger    version = any

    /'
        Create a System object and initialize.
    '/
    result = FMOD_System_Create(@fsystem)
    ERRCHECK(result)

    result = FMOD_System_GetVersion(fsystem, @version)
    ERRCHECK(result)

    if (version < FMOD_VERSION) then
        printf(!"Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION)
        end 
    end if

    result = FMOD_System_Init(fsystem, 32, FMOD_INIT_NORMAL, 0)
    ERRCHECK(result)

    result = FMOD_System_CreateSound(fsystem, "media/example.fsb", FMOD_DEFAULT, 0, @fsb)
    ERRCHECK(result)

    printf(!"===================================================================\n")
    printf(!"FSB Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"===================================================================\n")
    printf(!"\n")

    result = FMOD_Sound_GetNumSubSounds(fsb, @numsubsounds)
    ERRCHECK(result)

    for count = 0 to numsubsounds-1
        dim as FMOD_SOUND ptr subsound
        dim as zstring * 256 sname = any

        result = FMOD_Sound_GetSubSound(fsb, count, @subsound)
        ERRCHECK(result)

        result = FMOD_Sound_GetName(subsound, @sname, 256)
        ERRCHECK(result)

        printf(!"Press '%c' to play \"%s\"\n", asc("1") + count, @sname)
    next
    printf(!"Press 'Esc' to quit\n")
    printf(!"\n")

    /'
        Main loop.
    '/
    do
key = inkey
            if (key >= "1" and key < str(asc("1") + numsubsounds)) then
                dim as FMOD_SOUND ptr subsound = any
                dim as integer index = asc(key) - asc("1")

                FMOD_Sound_GetSubSound(fsb, index, @subsound)
               
                result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, subsound, FALSE, @channel)
                ERRCHECK(result)
            end if

        FMOD_System_Update(fsystem)

        scope
            dim as uinteger ms = 0
            dim as uinteger lenms = 0
            dim as integer          playing = 0
            dim as integer          paused = 0
            dim as integer          channelsplaying = 0

            if (channel) then
                dim as FMOD_SOUND ptr currentsound = 0

                result = FMOD_Channel_IsPlaying(channel, @playing)
                if ((result <> FMOD_OK) and (result <> FMOD_ERR_INVALID_HANDLE) and (result <> FMOD_ERR_CHANNEL_STOLEN)) then
                    ERRCHECK(result)
                end if

                result = FMOD_Channel_GetPaused(channel, @paused)
                if ((result <> FMOD_OK) and (result <> FMOD_ERR_INVALID_HANDLE) and (result <> FMOD_ERR_CHANNEL_STOLEN)) then
                    ERRCHECK(result)
                end if

                result = FMOD_Channel_GetPosition(channel, @ms, FMOD_TIMEUNIT_MS)
                if ((result <> FMOD_OK) and (result <> FMOD_ERR_INVALID_HANDLE) and (result <> FMOD_ERR_CHANNEL_STOLEN)) then
                    ERRCHECK(result)
                end if
               
                FMOD_Channel_GetCurrentSound(channel, @currentsound)
                if (currentsound) then
                    result = FMOD_Sound_GetLength(currentsound, @lenms, FMOD_TIMEUNIT_MS)
                    if ((result <> FMOD_OK) and (result <> FMOD_ERR_INVALID_HANDLE) and (result <> FMOD_ERR_CHANNEL_STOLEN)) then
                        ERRCHECK(result)
                    end if
                end if
            end if

            FMOD_System_GetChannelsPlaying(fsystem, @channelsplaying)

            printf(!"Time %02d:%02d:%02d/%02d:%02d:%02d : %s : Channels Playing %2d\r", _
ms \ 1000 \ 60, ms \ 1000 mod 60, ms \ 10 mod 100, lenms \ 1000 \ 60, lenms \ 1000 mod 60, lenms \ 10 mod 100, _
iif(paused,@"Paused ", iif(playing,@"Playing",@"Stopped")), channelsplaying)
        end scope

        Sleep(10)

    loop while (key <> chr(27))

    printf(!"\n")

    /'
        Shut down
    '/
    result = FMOD_Sound_Release(fsb)
    ERRCHECK(result)
    result = FMOD_System_Close(fsystem)
    ERRCHECK(result)
    result = FMOD_System_Release(fsystem)
    ERRCHECK(result)
