endian big
getdstring IDENT 8
get UNKNOWN long
get UNKNOWN long
get FILESIZE long
get UNKNOWN long
get UNKNOWN long
get UNKNOWN long
get INFO1OFF long
get INFO2OFF long # unknown informatin

goto INFO1OFF
get UOFF long # unknown information
get CODEOFF long
get MSF long # MSFBANK

goto INFO2OFF
get UNKNOWNOFF long
get UNKNOWNOFF long
get DATASIZE long # size from MSFBANK till end

set OFF MSF
math OFF += 0x14
goto OFF

get FILES long
math OFF += 0x1c
set OFFPOS OFF
set SIZEPOS FILES
math SIZEPOS *= 4
math SIZEPOS += OFFPOS
math SIZEPOS += 0xc
get BNAME basename
string BNAME += "_"

for i = 1 <= FILES
    endian big
    goto OFFPOS
    get OFFSET long
    math OFFSET += MSF
   
    savepos OFFPOS
    goto SIZEPOS
    get SIZE long
    savepos SIZEPOS
    set NAME BNAME
    string NAME += i
    goto OFFSET
    callfunction writeAT3 1
next i

startfunction writeAT3
   
    # read info from header and adjust size
    goto OFFSET
    getDstring MSIG 4 # "MSF0" or "MSFC"
    get BITSIGN long
    get UNK long
    get SIZE long
    get FREQ long
    math OFFSET += 0x40
   
    endian little
    set MEMORY_FILE binary "\x52\x49\x46\x46\x88\xb3\x6d\x0\x57\x41\x56\x45\x66\x6d\x74\x20\x20\x0\x0\x0\x70\x2\x2\x0\x80\xbb\x0\x0\x9a\x40\x0\x0\x80\x1\x0\x0\xe\x0\x1\x0\x0\x10\x0\x0\x0\x0\x0\x0\x1\x0\x0\x0\x66\x61\x63\x74\x8\x0\x0\x0\x91\x81\x24\x1\x1b\x4\x0\x0\x64\x61\x74\x61\x88\xb3\x6d\x0"
    string NAME += ".at3"
    set RIFFSIZE SIZE
    math RIFFSIZE += 0x44
    if BITSIGN == 4 # 66kbps
        putVarChr MEMORY_FILE 0x1c 0x204d long
        putVarChr MEMORY_FILE 0x20 0xc0 long
    elif BITSIGN == 5 # 105 kbps
        putVarChr MEMORY_FILE 0x1c 0x3324 long
        putVarChr MEMORY_FILE 0x20 0x130 long
    elif BITSIGN == 6 # 132 kbps
        putVarChr MEMORY_FILE 0x1c 0x409a long
        putVarChr MEMORY_FILE 0x20 0x180 long
    endif
    putVarChr MEMORY_FILE 0x04 RIFFSIZE long
    putVarChr MEMORY_FILE 0x16 0x2 byte
    putVarChr MEMORY_FILE 0x18 FREQ long
    putVarChr MEMORY_FILE 0x48 SIZE long
    append
    log MEMORY_FILE OFFSET SIZE
    append
    math SIZE += 0x4c
    log NAME 0 SIZE MEMORY_FILE
    endian big
endfunction