File indexing completed on 2024-12-22 05:33:12

0001 <?php
0002 /////////////////////////////////////////////////////////////////
0003 /// getID3() by James Heinrich <info@getid3.org>               //
0004 //  available at http://getid3.sourceforge.net                 //
0005 //            or http://www.getid3.org                         //
0006 //          also https://github.com/JamesHeinrich/getID3       //
0007 /////////////////////////////////////////////////////////////////
0008 // See readme.txt for more details                             //
0009 /////////////////////////////////////////////////////////////////
0010 //                                                             //
0011 // module.audio.avr.php                                        //
0012 // module for analyzing AVR Audio files                        //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_avr extends getid3_handler
0019 {
0020 
0021   public function Analyze() {
0022     $info = &$this->getid3->info;
0023 
0024     // http://cui.unige.ch/OSG/info/AudioFormats/ap11.html
0025     // http://www.btinternet.com/~AnthonyJ/Atari/programming/avr_format.html
0026     // offset    type    length    name        comments
0027     // ---------------------------------------------------------------------
0028     // 0    char    4    ID        format ID == "2BIT"
0029     // 4    char    8    name        sample name (unused space filled with 0)
0030     // 12    short    1    mono/stereo    0=mono, -1 (0xFFFF)=stereo
0031     //                     With stereo, samples are alternated,
0032     //                     the first voice is the left :
0033     //                     (LRLRLRLRLRLRLRLRLR...)
0034     // 14    short    1    resolution    8, 12 or 16 (bits)
0035     // 16    short    1    signed or not    0=unsigned, -1 (0xFFFF)=signed
0036     // 18    short    1    loop or not    0=no loop, -1 (0xFFFF)=loop on
0037     // 20    short    1    MIDI note    0xFFnn, where 0 <= nn <= 127
0038     //                     0xFFFF means "no MIDI note defined"
0039     // 22    byte    1    Replay speed    Frequence in the Replay software
0040     //                     0=5.485 Khz, 1=8.084 Khz, 2=10.971 Khz,
0041     //                     3=16.168 Khz, 4=21.942 Khz, 5=32.336 Khz
0042     //                     6=43.885 Khz, 7=47.261 Khz
0043     //                     -1 (0xFF)=no defined Frequence
0044     // 23    byte    3    sample rate    in Hertz
0045     // 26    long    1    size in bytes (2 * bytes in stereo)
0046     // 30    long    1    loop begin    0 for no loop
0047     // 34    long    1    loop size    equal to 'size' for no loop
0048     // 38  short   2   Reserved, MIDI keyboard split */
0049     // 40  short   2   Reserved, sample compression */
0050     // 42  short   2   Reserved */
0051     // 44  char   20;  Additional filename space, used if (name[7] != 0)
0052     // 64    byte    64    user data
0053     // 128    bytes    ?    sample data    (12 bits samples are coded on 16 bits:
0054     //                     0000 xxxx xxxx xxxx)
0055     // ---------------------------------------------------------------------
0056 
0057     // Note that all values are in motorola (big-endian) format, and that long is
0058     // assumed to be 4 bytes, and short 2 bytes.
0059     // When reading the samples, you should handle both signed and unsigned data,
0060     // and be prepared to convert 16->8 bit, or mono->stereo if needed. To convert
0061     // 8-bit data between signed/unsigned just add 127 to the sample values.
0062     // Simularly for 16-bit data you should add 32769
0063 
0064     $info['fileformat'] = 'avr';
0065 
0066     $this->fseek($info['avdataoffset']);
0067     $AVRheader = $this->fread(128);
0068 
0069     $info['avr']['raw']['magic'] = substr($AVRheader,  0,  4);
0070     $magic = '2BIT';
0071     if ($info['avr']['raw']['magic'] != $magic) {
0072       $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['avr']['raw']['magic']).'"';
0073       unset($info['fileformat']);
0074       unset($info['avr']);
0075       return false;
0076     }
0077     $info['avdataoffset'] += 128;
0078 
0079     $info['avr']['sample_name']        =         rtrim(substr($AVRheader,  4,  8));
0080     $info['avr']['raw']['mono']        = getid3_lib::BigEndian2Int(substr($AVRheader, 12,  2));
0081     $info['avr']['bits_per_sample']    = getid3_lib::BigEndian2Int(substr($AVRheader, 14,  2));
0082     $info['avr']['raw']['signed']      = getid3_lib::BigEndian2Int(substr($AVRheader, 16,  2));
0083     $info['avr']['raw']['loop']        = getid3_lib::BigEndian2Int(substr($AVRheader, 18,  2));
0084     $info['avr']['raw']['midi']        = getid3_lib::BigEndian2Int(substr($AVRheader, 20,  2));
0085     $info['avr']['raw']['replay_freq'] = getid3_lib::BigEndian2Int(substr($AVRheader, 22,  1));
0086     $info['avr']['sample_rate']        = getid3_lib::BigEndian2Int(substr($AVRheader, 23,  3));
0087     $info['avr']['sample_length']      = getid3_lib::BigEndian2Int(substr($AVRheader, 26,  4));
0088     $info['avr']['loop_start']         = getid3_lib::BigEndian2Int(substr($AVRheader, 30,  4));
0089     $info['avr']['loop_end']           = getid3_lib::BigEndian2Int(substr($AVRheader, 34,  4));
0090     $info['avr']['midi_split']         = getid3_lib::BigEndian2Int(substr($AVRheader, 38,  2));
0091     $info['avr']['sample_compression'] = getid3_lib::BigEndian2Int(substr($AVRheader, 40,  2));
0092     $info['avr']['reserved']           = getid3_lib::BigEndian2Int(substr($AVRheader, 42,  2));
0093     $info['avr']['sample_name_extra']  =         rtrim(substr($AVRheader, 44, 20));
0094     $info['avr']['comment']            =         rtrim(substr($AVRheader, 64, 64));
0095 
0096     $info['avr']['flags']['stereo'] = (($info['avr']['raw']['mono']   == 0) ? false : true);
0097     $info['avr']['flags']['signed'] = (($info['avr']['raw']['signed'] == 0) ? false : true);
0098     $info['avr']['flags']['loop']   = (($info['avr']['raw']['loop']   == 0) ? false : true);
0099 
0100     $info['avr']['midi_notes'] = array();
0101     if (($info['avr']['raw']['midi'] & 0xFF00) != 0xFF00) {
0102       $info['avr']['midi_notes'][] = ($info['avr']['raw']['midi'] & 0xFF00) >> 8;
0103     }
0104     if (($info['avr']['raw']['midi'] & 0x00FF) != 0x00FF) {
0105       $info['avr']['midi_notes'][] = ($info['avr']['raw']['midi'] & 0x00FF);
0106     }
0107 
0108     if (($info['avdataend'] - $info['avdataoffset']) != ($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 1 : 2))) {
0109       $info['warning'][] = 'Probable truncated file: expecting '.($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 1 : 2)).' bytes of audio data, found '.($info['avdataend'] - $info['avdataoffset']);
0110     }
0111 
0112     $info['audio']['dataformat']      = 'avr';
0113     $info['audio']['lossless']        = true;
0114     $info['audio']['bitrate_mode']    = 'cbr';
0115     $info['audio']['bits_per_sample'] = $info['avr']['bits_per_sample'];
0116     $info['audio']['sample_rate']     = $info['avr']['sample_rate'];
0117     $info['audio']['channels']        = ($info['avr']['flags']['stereo'] ? 2 : 1);
0118     $info['playtime_seconds']         = ($info['avr']['sample_length'] / $info['audio']['channels']) / $info['avr']['sample_rate'];
0119     $info['audio']['bitrate']         = ($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 8 : 16)) / $info['playtime_seconds'];
0120 
0121 
0122     return true;
0123   }
0124 
0125 }