/'
    This example builds the FSB's required for the Designer API examples.
    NOTE: the order of files here represents the output order of Designer.
'/

#include "crt.bi"
#include "fmodex/fsbank.bi"


dim shared as zstring ptr gTutorialFileNames(0 to ...) = _
{ _
    @"media/music/flsh-idle-05.ogg", _
    @"media/cycle-stop.ogg", _
    @"media/music/layer-guitar-d.ogg", _
    @"media/note.ogg", _
    @"media/ambient_two.ogg", _
    @"media/music/state-race-finish.ogg", _
    @"media/music/layer-guitar-a.ogg", _
    @"media/max_play_behavior_two.ogg", _
    @"media/music/state-idle-fadeout.ogg", _
    @"media/car/onmid.ogg", _
    @"media/sequence-three.ogg", _
    @"media/car/offhigh.ogg", _
    @"media/music/layer-drums-d.ogg", _
    @"media/car/idle.ogg", _
    @"media/car/offlow.ogg", _
    @"media/music/layer-guitar-c.ogg", _
    @"media/music/state-race-02a.ogg", _
    @"media/music/flsh-idle-02.ogg", _
    @"media/drum-loop.ogg", _
    @"media/music/state-idle-01.ogg", _
    @"media/sequence-start.ogg", _
    @"media/music/state-race-01a.ogg", _
    @"media/ambient_one_four.ogg", _
    @"media/music/flsh-idle-01.ogg", _
    @"media/max_play_behavior_one.ogg", _
    @"media/cycle-start.ogg", _
    @"media/music/layer-guitar-alt-c.ogg", _
    @"media/music/state-idle-02.ogg", _
    @"media/ambient_three.ogg", _
    @"media/car/offmid.ogg", _
    @"media/music/excited.ogg", _
    @"media/music/flsh-idle-04.ogg", _
    @"media/car/onlow.ogg", _
    @"media/music/state-idle-03.ogg", _
    @"media/sequence-four.ogg", _
    @"media/music/state-race-02b.ogg", _
    @"media/sequence-one.ogg", _
    @"media/music/layer-drums-a.ogg", _
    @"media/music/layer-guitar-alt-d.ogg", _
    @"media/music/flsh-idle-03.ogg", _
    @"media/ambient_one_loop.ogg", _
    @"media/music/layer-bass-a.ogg", _
    @"media/interactive_music_2.ogg", _
    @"media/music/layer-guitar-b.ogg", _
    @"media/music/layer-guitar-alt-a.ogg", _
    @"media/max_play_behavior_four.ogg", _
    @"media/cycle-sustain.ogg", _
    @"media/music/state-race-start.ogg", _
    @"media/music/relaxed.ogg", _
    @"media/music/state-race-01b.ogg", _
    @"media/car/onhigh.ogg", _
    @"media/sequence-two.ogg", _
    @"media/music/layer-drums-b.ogg", _
    @"media/interactive_music_3.ogg", _
    @"media/sequence-end.ogg", _
    @"media/max_play_behavior_three.ogg", _
    @"media/interactive_music_1.ogg", _
    @"media/music/layer-drums-c.ogg", _
    @"media/music/layer-guitar-alt-b.ogg" _
}

dim shared as zstring ptr gStreamingFileNames(0 to ...) = _
{ _
    @"media/interactive_music_6channel.ogg" _
}

dim shared as const uinteger gNumTutorialFileNames  = uBound(gTutorialFileNames) - lBound(gTutorialFileNames) + 1
dim shared as const uinteger gNumStreamingFileNames = uBound(gStreamingFileNames) - lBound(gStreamingFileNames) + 1

#define CHECK_RESULT(_x) scope:dim as FSBANK_RESULT _result = (_x):if(_result <> FSBANK_OK) then:printf(!"\nBuild failed, Press any key to exit.\n"):sleep:end _result:end if:end scope

function printErrors(byval subSounds as const FSBANK_SUBSOUND ptr) as FSBANK_RESULT
    dim as FSBANK_RESULT result                    = FSBANK_OK
    dim as const         FSBANK_PROGRESSITEM ptr item = NULL

    printf(!"Build Error!\n")

    do
        result = FSBank_FetchNextProgressItem(@item)
        CHECK_RESULT(result)

        if (item) then
            if (item->state = FSBANK_STATE_FAILED) then
                dim as const FSBANK_STATEDATA_FAILED ptr failedStateData = cast(const FSBANK_STATEDATA_FAILED ptr,item->stateData)
                printf(!"Error: %s -- \"%s\"\n", failedStateData->errorString, iif(item->subSoundIndex < 0, @"System", subSounds[item->subSoundIndex].fileNames[0]))
            end if

            result = FSBank_ReleaseProgressItem(item)
            CHECK_RESULT(result)
        end if
    loop while (item <> NULL)

    return FSBANK_OK
end function


function buildBank(byval fileNames as  zstring ptr ptr, byval numFileNames as uinteger, byval outputFileName as zstring ptr) as FSBANK_RESULT
    dim as FSBANK_RESULT    result    = FSBANK_OK
    dim as FSBANK_SUBSOUND ptr subSounds = allocate(numFileNames * sizeof(FSBANK_SUBSOUND))

    for i as uinteger = 0 to numFileNames-1
        memset(@subSounds[i], 0, sizeof(FSBANK_SUBSOUND))

        subSounds[i].fileNames          = @fileNames[i]
        subSounds[i].numFileNames       = 1
        subSounds[i].speakerMap         = FSBANK_SPEAKERMAP_DEFAULT        
        subSounds[i].overrideFlags      = FSBANK_BUILD_DEFAULT
        subSounds[i].overrideQuality    = 0
        subSounds[i].desiredSampleRate  = 0
    next

    printf(!"Building %s...\n", outputFileName)

    result = FSBank_Build(subSounds, numFileNames, FSBANK_FORMAT_MP3, FSBANK_BUILD_DEFAULT or FSBANK_BUILD_DONTLOOP, 0, NULL, outputFileName)
    if (result <> FSBANK_OK) then
        printErrors(subSounds)
        return result
    end if

    printf(!"done.\n")
deallocate(subSounds )
    return FSBANK_OK
end function


' program start
    dim as FSBANK_RESULT result = FSBANK_OK

result = FSBank_Init(FSBANK_FSBVERSION_FSB4, FSBANK_INIT_NORMAL or FSBANK_INIT_GENERATEPROGRESSITEMS, 2, NULL)
    CHECK_RESULT(result)

    result = buildBank(@gTutorialFileNames(0), gNumTutorialFileNames, "tutorial_bank.fsb")
    CHECK_RESULT(result)

    result = buildBank(@gStreamingFileNames(0), gNumStreamingFileNames, "streaming_bank.fsb")
    CHECK_RESULT(result)

    result = FSBank_Release()
    CHECK_RESULT(result)

    printf(!"\nBuild complete, Press any key to exit.\n")
sleep
