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

 This example shows how to simply load and play multiple sounds.  This is about the simplest
 use of FMOD.
 This makes FMOD decode the into memory when it loads.  If the sounds are big and possibly take
 up a lot of ram, then it would be better to use the FMOD_CREATESTREAM flag so that it is 
 streamed in realtime as it plays.
==============================================================================================='/
#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 systemA = any, systemB = any
    dim as FMOD_SOUND     ptr soundA = any, soundB = any
    dim as FMOD_CHANNEL   ptr channelA = 0, channelB = 0
    dim as FMOD_RESULT     result = any
    dim as string             key
    dim as integer count = any, numdrivers = any, driver = any
    dim as uinteger    version = any

    /'
        Create Sound Card A
    '/
    result = FMOD_System_Create(@systemA)
    ERRCHECK(result)

    result = FMOD_System_GetVersion(systemA, @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 0
    end if

    result = FMOD_System_GetNumDrivers(systemA, @numdrivers)
    ERRCHECK(result)

    printf(!"---------------------------------------------------------\n")    
    printf(!"Select soundcard A\n")
    printf(!"---------------------------------------------------------\n")    
    for count=0 to numdrivers-1
        dim as zstring * 256 sname = any

        result = FMOD_System_GetDriverInfo(systemA, count, @sname, 256, 0)
        ERRCHECK(result)

        printf(!"%d : %s\n", count + 1, sname)
    next
    printf(!"---------------------------------------------------------\n")
    printf(!"Press a corresponding number or ESC to quit\n")

    do
key = inkey
        if (key = chr(27)) then
            end 0
        end if
        driver = asc(key) - asc("1")
sleep(10)
    loop while (driver < 0 or driver >= numdrivers)

    printf(!"\n")

    result = FMOD_System_SetDriver(systemA, driver)
    ERRCHECK(result)

    result = FMOD_System_Init(systemA, 32, FMOD_INIT_NORMAL, NULL)
    ERRCHECK(result)

    /'
        Create Sound Card B
    '/
    result = FMOD_System_Create(@systemB)
    ERRCHECK(result)

    result = FMOD_System_GetVersion(systemB, @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 0
    end if

    result = FMOD_System_GetNumDrivers(systemB, @numdrivers)
    ERRCHECK(result)

    printf(!"---------------------------------------------------------\n")    
    printf(!"Select soundcard B\n")
    printf(!"---------------------------------------------------------\n")    
    for count=0 to numdrivers-1
        dim as zstring * 256 sname = any

        result = FMOD_System_GetDriverInfo(systemB, count, @sname, 256, 0)
        ERRCHECK(result)

        printf(!"%d : %s\n", count + 1, sname)
    next
    printf(!"---------------------------------------------------------\n")
    printf(!"Press a corresponding number or ESC to quit\n")

    do
        key = inkey
        if (key = chr(27)) then
            end 0
        end if
        driver = asc(key) - asc("1")
sleep(10)
    loop while (driver < 0 or driver >= numdrivers)

    printf(!"\n")

    result = FMOD_System_SetDriver(systemB, driver)
    ERRCHECK(result)

    result = FMOD_System_Init(systemB, 32, FMOD_INIT_NORMAL, NULL)
    ERRCHECK(result)

    /'
        Load 1 sample into each soundcard.
    '/
    result = FMOD_System_CreateSound(systemA, "media/drumloop.wav", FMOD_HARDWARE, 0, @soundA)
    ERRCHECK(result)
    result = FMOD_Sound_SetMode(soundA, FMOD_LOOP_OFF)
    ERRCHECK(result)

    result = FMOD_System_CreateSound(systemB, "media/jaguar.wav", FMOD_HARDWARE, 0, @soundB)
    ERRCHECK(result)

    printf(!"===========================================================================\n")
    printf(!"MultipleSoundCard Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"===========================================================================\n")
    printf(!"\n")
    printf(!"Press '1' to play a sound on soundcard A\n")
    printf(!"Press '2' to play a sound on soundcard B\n")
    printf(!"Press 'Esc' to quit\n")
    printf(!"\n")

    /'
        Main loop.
    '/
    do

        dim as integer  channelsplayingA = 0
        dim as integer  channelsplayingB = 0

            key = inkey
            select case key
                case "1" :
                    result = FMOD_System_PlaySound(systemA, FMOD_CHANNEL_FREE, soundA, 0, @channelA)
                    ERRCHECK(result)
                case "2" :
                    result = FMOD_System_PlaySound(systemB, FMOD_CHANNEL_FREE, soundB, 0, @channelB)
                    ERRCHECK(result)
        end select

        FMOD_System_Update(systemA)
        FMOD_System_Update(systemB)

        FMOD_System_GetChannelsPlaying(systemA, @channelsplayingA)
        FMOD_System_GetChannelsPlaying(systemB, @channelsplayingB)

        printf(!"Channels Playing on A %2d.   Channels Playing on B %2d.\r", channelsplayingA, channelsplayingB)

        Sleep(10)

    loop while (key <> chr(27))

    printf(!"\n")

    /'
        Shut down
    '/
    result = FMOD_Sound_Release(soundA)
    ERRCHECK(result)
    result = FMOD_System_Close(systemA)
    ERRCHECK(result)
    result = FMOD_System_Release(systemA)
    ERRCHECK(result)

    result = FMOD_Sound_Release(soundB)
    ERRCHECK(result)
    result = FMOD_System_Close(systemB)
    ERRCHECK(result)
    result = FMOD_System_Release(systemB)
    ERRCHECK(result)
