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

 This example shows how to read tags from sound files
==============================================================================================='/
#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_RESULT      result = any
    dim as FMOD_TAG         tag = any
    dim as integer              numtags = any, numtagsupdated = any, count = any
    dim as uinteger     version = any

    printf(!"==================================================================\n")
    printf(!"ReadTags Example.  Copyright (c) Firelight Technologies 2004-2011.\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, NULL)
    ERRCHECK(result)

    /'
        Open the specified file. Use FMOD_CREATESTREAM and FMOD_OPENONLY so it opens quickly
    '/
    result = FMOD_System_CreateSound(fsystem, "media/wave.mp3", FMOD_SOFTWARE or FMOD_2D or FMOD_CREATESTREAM or FMOD_OPENONLY, 0, @sound)
    ERRCHECK(result)

    /'
        Read and display all tags associated with this file
    '/
    while  (FMOD_Sound_GetTag(sound, 0, -1, @tag) = FMOD_OK)
        /'
            An index of -1 means "get the first tag that's new or updated".
            If no tags are new or updated then getTag will end FMOD_ERR_TAGNOTFOUND.
            This is the first time we've read any tags so they'll all be new but after we've read them, 
            they won't be new any more.
        '/
        if (tag.datatype = FMOD_TAGDATATYPE_STRING) then
            printf(!"%s = %s (%d bytes)\n", tag.name, tag.data, tag.datalen)
        else
            printf(!"%s = <binary> (%d bytes)\n", tag.name, tag.datalen)
        end if
    wend
    printf(!"\n")

    /'
        Read all the tags regardless of whether they're updated or not. Also show the tag type.
    '/
    result = FMOD_Sound_GetNumTags(sound, @numtags, @numtagsupdated)
    ERRCHECK(result)
    for count=0 to numtags-1
        result = FMOD_Sound_GetTag(sound, 0, count, @tag)
        ERRCHECK(result)

        select case tag.type
            case FMOD_TAGTYPE_UNKNOWN :
                printf(!"FMOD_TAGTYPE_UNKNOWN  ")
            case FMOD_TAGTYPE_ID3V1 :
                printf(!"FMOD_TAGTYPE_ID3V1  ")
            case FMOD_TAGTYPE_ID3V2 :
                printf(!"FMOD_TAGTYPE_ID3V2  ")

           case FMOD_TAGTYPE_VORBISCOMMENT :
                printf(!"FMOD_TAGTYPE_VORBISCOMMENT  ")
            case FMOD_TAGTYPE_SHOUTCAST :
                printf(!"FMOD_TAGTYPE_SHOUTCAST  ")
            case FMOD_TAGTYPE_ICECAST :
                printf(!"FMOD_TAGTYPE_ICECAST  ")
            case FMOD_TAGTYPE_ASF :
                printf(!"FMOD_TAGTYPE_ASF  ")
            case FMOD_TAGTYPE_FMOD :
                printf(!"FMOD_TAGTYPE_FMOD  ")
            case FMOD_TAGTYPE_USER :
                printf(!"FMOD_TAGTYPE_USER  ")
        end select

        if (tag.datatype = FMOD_TAGDATATYPE_STRING) then
            printf(!"%s = %s (%d bytes)\n", tag.name, tag.data, tag.datalen)
        else
            printf(!"%s = ??? (%d bytes)\n", tag.name, tag.datalen)
        end if
    next
    printf(!"\n")

    /'
        Find a specific tag by name. Specify an index > 0 to get access to multiple tags of the same name.
    '/
    result = FMOD_Sound_GetTag(sound, "ARTIST", 0, @tag)
    ERRCHECK(result)
    printf(!"%s = %s (%d bytes)\n", tag.name, tag.data, tag.datalen)
    printf(!"\n")
sleep
    /'
        Shut down
    '/
    result = FMOD_Sound_Release(sound)
    ERRCHECK(result)
    result = FMOD_System_Close(fsystem)
    ERRCHECK(result)
    result = FMOD_System_Release(fsystem)
    ERRCHECK(result)
