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

 This example shows how to play streaming audio from the internet
==============================================================================================='/
#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 sound = any
    dim as FMOD_CHANNEL    ptr channel = 0
    dim as FMOD_RESULT      result = 0
    dim as string             key
    dim as uinteger     version

    printf(!"===================================================================\n")
    printf(!"NetStream Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"===================================================================\n\n")

    if (__FB_ARGC__ < 2) then
        printf(!"Usage:   netstream <url>\n")
        printf(!"Example: netstream http:'www.fmod.org/stream.mp3\n\n")
        end -1
    end if

    /'
        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, 1, FMOD_INIT_NORMAL, 0)
    ERRCHECK(result)

    /'
        Bump up the file buffer size a little bit for netstreams (to account for lag).
    '/
    result = FMOD_System_SetStreamBufferSize(fsystem, 64*1024, FMOD_TIMEUNIT_RAWBYTES)
    ERRCHECK(result)

    result = FMOD_System_CreateSound(fsystem, __FB_ARGV__[1], FMOD_HARDWARE or FMOD_2D or FMOD_CREATESTREAM or FMOD_NONBLOCKING,  0, @sound)
    ERRCHECK(result)

    printf(!"Press space to pause, Esc to quit\n\n")

    /'
        Main loop
    '/
    do
        dim as uinteger    ms = 0, percent = 0
        dim as integer             playing = FALSE
        dim as integer             paused = FALSE
        dim as integer             starving = FALSE
        dim as FMOD_OPENSTATE  openstate = any

        if (channel = 0) then
            result = FMOD_System_PlaySound(fsystem, FMOD_CHANNEL_FREE, sound, FALSE, @channel)
        end if
            key = inkey

            select case key
                case " " :
                    if (channel) then

                        dim as integer paused = any
                        FMOD_Channel_GetPaused(channel, @paused)
paused xor=1
                        FMOD_Channel_SetPaused(channel, paused)
                    end if
        end select

        FMOD_System_Update(fsystem)

            dim as FMOD_TAG tag = any
while  (FMOD_Sound_GetTag(sound, 0, -1, @tag) = FMOD_OK)
            if (tag.datatype = FMOD_TAGDATATYPE_STRING) then
                printf(!"%s = %s (%d bytes)       \n", tag.name, tag.data, tag.datalen)
            ElseIf (tag.type = FMOD_TAGTYPE_FMOD) then
                if (strcmp(tag.name, "Sample Rate Change")) = 0 then
                    FMOD_Channel_SetFrequency(channel, *cast(float ptr, tag.data))
                end if
        end if
wend
        result = FMOD_Sound_GetOpenState(sound, @openstate, @percent, @starving, 0)
        ERRCHECK(result)

        if (channel) then
            result = FMOD_Channel_GetPaused(channel, @paused)
            ERRCHECK(result)
            result = FMOD_Channel_IsPlaying(channel, @playing)
            ERRCHECK(result)
            result = FMOD_Channel_GetPosition(channel, @ms, FMOD_TIMEUNIT_MS)
            ERRCHECK(result)
            result = FMOD_Channel_SetMute(channel, starving)
            ERRCHECK(result)
        end if

        printf(!"Time %02d:%02d:%02d : %s : (%3d%%%) %s     \r", _
ms \ 1000 \ 60, ms \ 1000 mod 60, ms \ 10 mod 100, _
iif(openstate = FMOD_OPENSTATE_BUFFERING,@"Buffering...",iif(openstate = FMOD_OPENSTATE_CONNECTING,@"Connecting...",iif(paused,@"Paused       ",iif(playing,@"Playing      ",@"Stopped      ")))), percent, _
iif(starving,@"STARVING",@"        "))

        Sleep(10)

    loop while (key <> chr(27))

    printf(!"\n")

    printf(!"Shutting down.\n")
    
    if (channel) then
        result = FMOD_Channel_Stop(channel)
        ERRCHECK(result)
    end if

    /'
        If we pressed escape before it is ready, wait for it to finish opening before we release it.
    '/
    do
        dim as FMOD_OPENSTATE openstate = any

        result = FMOD_Sound_GetOpenState(sound, @openstate, 0, 0, 0)
        ERRCHECK(result)

        if (openstate = FMOD_OPENSTATE_READY) then
            exit do
        end if

        printf(!"Waiting for sound to finish opening before trying to release it....\r")

        Sleep(10)
    loop while 1

    /'
        Shut down
    '/
    result = FMOD_Sound_Release(sound)
    ERRCHECK(result)
    result = FMOD_System_Close(fsystem)
    ERRCHECK(result)
    result = FMOD_System_Release(fsystem)
    ERRCHECK(result)
