#include "fbgfx.bi"
Using FB

#include "fmod.bi"

CONST number_of_sound_effect = 10
CONST FALSE = 0
CONST TRUE = 1

DECLARE SUB setSoundVolume
DECLARE SUB playsample (plsnd as integer ptr)
DECLARE SUB playStreamMusic (musc As String)
DECLARE SUB stopStreamMusic()

DIM SHARED PlaySound AS INTEGER
DIM SHARED sample(number_of_sound_effect) AS INTEGER PTR
DIM SHARED music_handle AS INTEGER PTR
DIM SHARED SFXVolume AS INTEGER, MusicVolume AS INTEGER

PlaySound = TRUE
SFXVolume = 3
MusicVolume = 3

FSOUND_Init(44100, number_of_sound_effect+1, 0) 
IF FSOUND_Init(44100, number_of_sound_effect+1, 0) = 0 THEN PlaySound = FALSE

IF PlaySound = TRUE THEN
    sample(1) = FSOUND_SAMPLE_Load(FSOUND_FREE,"sound1.wav",0,0,0)
    sample(2) = FSOUND_SAMPLE_Load(FSOUND_FREE,"sound2.wav",0,0,0)
END IF

playStreamMusic ("river.ogg")

SCREENRES 640, 480, 32, 1, GFX_WINDOWED

DO
    
IF PlaySound = TRUE THEN FSOUND_Update 

LOCATE 1,1
PRINT "PUSH ESC TO END PROGRAM"
PRINT "PUSH 1 AND 2 TO TEST SOUND EFFECTS"
PRINT "PUSH F1 AND F2 TO CHANGE THE WATER SOUND VOLUME"

IF MULTIKEY(SC_1) THEN 
    playsample (sample(1))
    SLEEP 1000
END IF
IF MULTIKEY(SC_2) THEN 
    playsample (sample(2))
    SLEEP 1000
END IF
IF MULTIKEY(SC_F1) THEN
    MusicVolume = MusicVolume - 1
    IF MusicVolume < 0 THEN MusicVolume = 0
    setSoundVolume
    SLEEP 1000
END IF 
IF MULTIKEY(SC_F2) THEN
    MusicVolume = MusicVolume + 1
    IF MusicVolume > 4 THEN MusicVolume = 4
    setSoundVolume
    SLEEP 1000
END IF

SLEEP 10
   
LOOP UNTIL MULTIKEY(SC_ESCAPE)
FSOUND_Close

SUB playStreamMusic (musc As String)
    
    DIM SetVol AS INTEGER

    IF PlaySound = TRUE THEN
        music_handle = FSOUND_Stream_Open(musc, FSOUND_LOOP_NORMAL, 0, 0 ) 
        FSOUND_Stream_Play(1, music_handle)
        IF MusicVolume = 0 THEN SetVol = 0
        IF MusicVolume = 1 THEN SetVol = 63
        IF MusicVolume = 2 THEN SetVol = 126
        IF MusicVolume = 3 THEN SetVol = 190
        IF MusicVolume = 4 THEN SetVol = 255
        FSOUND_SetVolumeAbsolute(1, SetVol)
    END IF	

END SUB

SUB stopStreamMusic()
    
    IF PlaySound = TRUE THEN
        FSOUND_Stream_Stop music_handle
        FSOUND_Stream_Close music_handle
    END IF
    
END SUB


SUB setSoundVolume
    
    DIM SetVol AS INTEGER
    
    IF PlaySound = TRUE THEN
        IF SFXVolume = 0 THEN SetVol = 0
        IF SFXVolume = 1 THEN SetVol = 63
        IF SFXVolume = 2 THEN SetVol = 126
        IF SFXVolume = 3 THEN SetVol = 190
        IF SFXVolume = 4 THEN SetVol = 255
        FSOUND_SetSFXMasterVolume(SetVol)

        IF MusicVolume = 0 THEN SetVol = 0
        IF MusicVolume = 1 THEN SetVol = 63
        IF MusicVolume = 2 THEN SetVol = 126
        IF MusicVolume = 3 THEN SetVol = 190
        IF MusicVolume = 4 THEN SetVol = 255
        FSOUND_SetVolumeAbsolute(1, SetVol)
    END IF
    
END SUB

SUB playsample (plsnd as integer ptr)

    IF PlaySound = TRUE THEN 
        FSOUND_PlaySound (FSOUND_FREE, plsnd)
        setSoundVolume
    END IF
    
END SUB