// DIE's signature file
// Author: Levis <levintaeyeon@live.com>
//         Jason Hood <jadoxa@yahoo.com.au>

init("audio","WAV Audio File");

includeScript("duration");

function detect(bShowType,bShowVersion,bShowOptions)
{
    if(Binary.getSize()>=36)
    {
        if(Binary.compare("'WAVEfmt '",8))
        {
            bDetected=1;
            var nFormat=Binary.readWord(20);
            var nChannels=Binary.readWord(22);
            var nRate=Binary.readDword(24);
            var nBPS=Binary.readWord(34);
            // There's a lot of formats, so I'll just copy what file uses.
            switch(nFormat)
            {
            case      1: sOptions="PCM";         break;
            case      2: sOptions="ADPCM";       break;
            case      6: sOptions="A-law";       break;
            case      7: sOptions="mu-law";      break;
            case      8: sOptions="DTS";         break;
            case     17: sOptions="IMA ADPCM";   break;
            case     20: sOptions="G.723 ADPCM"; break;
            case     49: sOptions="GSM 6.10";    break;
            case     64: sOptions="G.721 ADPCM"; break;
            case     80: sOptions="MPEG";        break;
            case     85: sOptions="MP3";         break;
            case 0x2001: sOptions="DTS";         break;
            }
            sOptions=sOptions.append(nRate+"Hz");
            switch(nChannels)
            {
            case 1: sOptions=sOptions.append("Mono");   break;
            case 2: sOptions=sOptions.append("Stereo"); break;
            }
            if(nBPS)
            {
                sOptions=sOptions.append(nBPS+"-bit");
                if(nFormat==1&&(nBPS==8||nBPS==16))
                {
                    var nOffset=Binary.findString(0x24,128,"data");
                    if(nOffset!=-1)
                    {
                        var nSeconds=Binary.readDword(nOffset+4)/(nBPS/8)/nRate;
                        sOptions=sOptions.append(duration(nSeconds));
                    }
                }
            }
        }
    }

    return result(bShowType,bShowVersion,bShowOptions);
}
