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

 This example shows how to load a playlist and play the sounds in a playlist.
==============================================================================================='/
#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 playlist = any
    dim as FMOD_SOUND       ptr sound    = any
    dim as FMOD_CHANNEL     ptr channel  = 0
    dim as FMOD_TAG          tag = any
    dim as FMOD_RESULT       result = any
    dim as FMOD_SOUND_TYPE   soundtype
    dim as FMOD_BOOL         isplaylist = 0
    dim as zstring             ptr title = NULL
    dim as integer               count = 0
    dim as string               key
    dim as uinteger      version = any
    dim as zstring * 128              file = 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 0
    end if

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

    result = FMOD_System_CreateSound(fsystem, "media/playlist.m3u", FMOD_DEFAULT, 0, @playlist)
    ERRCHECK(result)

    result = FMOD_Sound_GetFormat(playlist, @soundtype, 0, 0, 0)
    ERRCHECK(result)

    if(soundtype = FMOD_SOUND_TYPE_PLAYLIST) then isplaylist = TRUE

    printf(!"===================================================================\n")
    printf(!"PlayList Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"===================================================================\n")
    printf(!"\n")
    printf(!"Press 'n'     to play next sound in playlist\n")
    printf(!"Press 'space' to pause/unpause current sound\n")
    printf(!"Press 'Esc'   to quit\n")
    printf(!"\n")

    if (isplaylist) then
        printf(!"PLAYLIST loaded.\n")
        /'
            Get the first song in the playlist, create the sound and then play it.
        '/
        result = FMOD_Sound_GetTag(playlist, "FILE", count, @tag)
        ERRCHECK(result)

        sprintf(file, "media/%s", tag.data)

        result = FMOD_System_CreateSound(fsystem, file, FMOD_DEFAULT, 0, @sound)
        ERRCHECK(result)

        result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound, 0, @channel)
        ERRCHECK(result)

        FMOD_Sound_GetTag(playlist, "TITLE", count, @tag)
        title = tag.data

        count+=1
    else
        printf(!"SOUND loaded.\n")

        /'
            This is just a normal sound, so just play it.
        '/
        sound = playlist

        result = FMOD_Sound_SetMode(sound, FMOD_LOOP_NORMAL)
        ERRCHECK(result)

        result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound, 0, @channel)
        ERRCHECK(result)
    end if

    printf(!"\n")

    /'
        Main loop.
    '/
    do
        dim as FMOD_BOOL  isplaying = 0

        if (channel <>0  and isplaylist <> 0) then
            /'
                When sound has finished playing, play the next sound in the playlist
            '/

            FMOD_Channel_IsPlaying(channel, @isplaying)
            if (isplaying = 0) then
                if (sound) then
                    FMOD_Sound_Release(sound)

                    sound = NULL
                end if

                result = FMOD_Sound_GetTag(playlist, "FILE", count, @tag)
                if (result <> FMOD_OK) then
                    count = 0
                else
                    printf(!"playing next song in playlist...\n")

                    sprintf(file, "media/%s", tag.data)

                    result = FMOD_System_CreateSound(fsystem, file, FMOD_DEFAULT, 0, @sound)
                    ERRCHECK(result)

                    result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound, 0, @channel)
                    ERRCHECK(result)

                    FMOD_Sound_GetTag(playlist, "TITLE", count, @tag)
                    title = tag.data

                    count+=1
                end if
            end if
        end if

            key = inkey
            select case key
                case "n" :
                    /'
                        Play the next song in the playlist
                    '/
                    if (channel <> 0 and isplaylist <> 0) then
                        FMOD_Channel_Stop(channel)
                    end if
                case " " :
                    if (channel) then
                        dim as FMOD_BOOL paused = any

                        FMOD_Channel_GetPaused(channel, @paused)
paused xor=1
                        FMOD_Channel_SetPaused(channel, paused)
                    end if
        end select

        FMOD_System_Update(fsystem)

        scope
            dim as uinteger ms = 0
            dim as uinteger lenms = 0
            dim as FMOD_BOOL    paused = 0

            if (channel) then
                if (sound) then
                    result = FMOD_Sound_GetLength(sound, @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

                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
            end if

            printf(!"Time %02d:%02d:%02d/%02d:%02d:%02d : %s : %s\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 ",@"Playing "), title)

        end scope

        Sleep(10)

    loop while (key <> chr(27))

    printf(!"\n")

    /'
        Shut down
    '/
    if (sound) then
        result = FMOD_Sound_Release(sound)
        ERRCHECK(result)
    end if
    if (isplaylist) then
        result = FMOD_Sound_Release(playlist)
        ERRCHECK(result)
    end if
    result = FMOD_System_Close(fsystem)
    ERRCHECK(result)
    result = FMOD_System_Release(fsystem)
    ERRCHECK(result)
