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

 This example shows how to use the sound group behavior modes
==============================================================================================='/

#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 sound1 = any, sound2 = any, sound3 = any
    dim as FMOD_SOUNDGROUP    ptr soundgroup
    dim as FMOD_CHANNEL       ptr channel(3-1) = any
    dim as FMOD_RESULT         result = any
    dim as string                 key
    dim as integer                 mode=1
    dim as uinteger        version

    printf(!"======================================================================\n")
    printf(!"soundgroups Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"======================================================================\n")
    printf(!"This example plays 3 sounds in a sound group, demonstrating the effect\n")
    printf(!"of the three different sound group behavior modes\n")
    printf(!"======================================================================\n\n")

    /'
        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 0
    end if

    result = FMOD_System_Init(fsystem, 100, FMOD_INIT_NORMAL, 0)
    ERRCHECK(result)
    	
	/'
	    Load some sound files from the hard disk.
	'/
	result = FMOD_System_CreateSound(fsystem, "media/drumloop.wav", FMOD_HARDWARE or FMOD_LOOP_NORMAL, 0, @sound1)
	ERRCHECK(result)

	result = FMOD_System_CreateSound(fsystem, "media/jaguar.wav", FMOD_HARDWARE or FMOD_LOOP_NORMAL, 0, @sound2)
	ERRCHECK(result)

	result = FMOD_System_CreateSound(fsystem, "media/swish.wav", FMOD_HARDWARE or FMOD_LOOP_NORMAL, 0, @sound3)
	ERRCHECK(result)
    
    /' 
        Create the sound group with the following attributes:
          Name       = MyGroup
          MaxAudible = 1
          Behavior   = Mute 
    '/
	result = FMOD_System_CreateSoundGroup(fsystem, "MyGroup", @soundgroup)
	ERRCHECK(result)

    result = FMOD_SoundGroup_SetMaxAudible(soundgroup, 1)
    ERRCHECK(result)

    result = FMOD_SoundGroup_SetMaxAudibleBehavior(soundgroup, FMOD_SOUNDGROUP_BEHAVIOR_MUTE)
    ERRCHECK(result)

    result = FMOD_SoundGroup_SetMuteFadeSpeed(soundgroup, 2)
    ERRCHECK(result)

    /'
        Put the sounds in the sound group
    '/
	result = FMOD_Sound_SetSoundGroup(sound1, soundgroup)
	ERRCHECK(result)

	result = FMOD_Sound_SetSoundGroup(sound2, soundgroup)
	ERRCHECK(result)

	result = FMOD_Sound_SetSoundGroup(sound3, soundgroup)
	ERRCHECK(result)
	
	/'
        Play the sounds (two will be muted because of the behavior mode)
    '/
	result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound1, FALSE, @channel(0))
	ERRCHECK(result)	

	result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound2, FALSE, @channel(1))
	ERRCHECK(result)

	result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound3, FALSE, @channel(2))
	ERRCHECK(result)
    
    /'
        Display help
    '/
    printf(!"=========================================================================\n")
    printf(!"Press 1        BEAVIOR_FAIL \n")
    printf(!"      2        BEAVIOR_MUTE \n")
    printf(!"      3        BEAVIOR_STEALLOWEST\n")
    printf(!"      Q        Play/stop drumloop sound\n")
    printf(!"      W        Play/stop Jaguar sound\n")
    printf(!"      E        Play/stop shwish sound\n")
    printf(!"      ESC      Quit\n")
    printf(!"=========================================================================\n")
    
	do
        dim as float audibility = any
        dim as integer index = any
			key = inkey

			if( key = "1" ) then
                result = FMOD_SoundGroup_SetMaxAudibleBehavior(soundgroup, FMOD_SOUNDGROUP_BEHAVIOR_FAIL)
                ERRCHECK(result)
			end if

            if( key = "2" ) then
                result = FMOD_SoundGroup_SetMaxAudibleBehavior(soundgroup, FMOD_SOUNDGROUP_BEHAVIOR_MUTE)
                ERRCHECK(result)
			end if

            if( key = "3" ) then
                result = FMOD_SoundGroup_SetMaxAudibleBehavior(soundgroup, FMOD_SOUNDGROUP_BEHAVIOR_STEALLOWEST)
                ERRCHECK(result)
			end if

            if( key =  lcase("q") ) then
                result = FMOD_Channel_GetIndex(channel(0),@index)

                if (index) = 0 then
                    result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound1, FALSE, @channel(0))
	                if (result <> FMOD_ERR_MAXAUDIBLE) then
                        ERRCHECK(result)
                    end if
                else
                    result = FMOD_Channel_Stop(channel(0))
                    ERRCHECK(result)
                end if
			end if

            if( key = lcase("w") ) then
                result = FMOD_Channel_GetIndex(channel(1),@index)

                if (index) = 0 then
                    result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound2, FALSE,@channel(1))
                    if (result <> FMOD_ERR_MAXAUDIBLE) then
	                    ERRCHECK(result)
                    end if
                else
                    result = FMOD_Channel_Stop(channel(1))
                    ERRCHECK(result)
                end if
			end if

            if( key = lcase("e") ) then
                result = FMOD_Channel_GetIndex(channel(2),@index)

                if (index) = 0 then
                    result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound3, FALSE,@channel(2))
                    if (result <> FMOD_ERR_MAXAUDIBLE) then
	                    ERRCHECK(result)
                    end if
                else
                    result = FMOD_Channel_Stop(channel(2))
                    ERRCHECK(result)
                end if
			end if


        ' print out a small visual display
        scope
            dim as zstring * 80 s = any
            dim as zstring * 6 s1 = any
            dim as zstring * 6 s2(2) = any
            dim as integer i = any
            dim as FMOD_SOUNDGROUP_BEHAVIOR behavior = any
            FMOD_SoundGroup_GetMaxAudibleBehavior(soundgroup, @behavior)
            
            select case behavior
                case FMOD_SOUNDGROUP_BEHAVIOR_FAIL : 
                    sprintf(@s1,!"FAIL")
                case FMOD_SOUNDGROUP_BEHAVIOR_MUTE : 
                    sprintf(@s1,!"MUTE")
                case FMOD_SOUNDGROUP_BEHAVIOR_STEALLOWEST : 
                    sprintf(@s1,!"STEAL")
            end select
            
            for i=0 to 2
                FMOD_Channel_GetAudibility(channel(i), @audibility)
                
                if (audibility) = 0 then
                    result = FMOD_Channel_GetIndex(channel(i), @index)
                    
                    if (index) = 0 then
                        sprintf(@s2(i), !"STOP")
                    else
                        sprintf(@s2(i), !"MUTE")
                    end if
                else
                    sprintf(@s2(i), !"PLAY")
                end if

            
            sprintf(@s, !"MODE:%6s      | SOUND1: %s | SOUND2: %s  | SOUND3: %s | ", @s1, @s2(0), @s2(1), @s2(2))
            printf(!"%s\r", @s)
        next
end scope
		result = FMOD_System_Update(fsystem)
		ERRCHECK(result)
        Sleep(10)
	loop while (key <> chr(27))


    printf(!"\n")

    /'
        Shut down
    '/
    result = FMOD_SoundGroup_Release(soundgroup)
    ERRCHECK(result)
    result = FMOD_Sound_Release(sound3)
    ERRCHECK(result)
    result = FMOD_Sound_Release(sound2)
    ERRCHECK(result)
    result = FMOD_Sound_Release(sound1)
    ERRCHECK(result)

    result = FMOD_System_Close(fsystem)
    ERRCHECK(result)
    result = FMOD_System_Release(fsystem)
    ERRCHECK(result)
