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.au.php                                         //
0012 // module for analyzing AU files                               //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_au extends getid3_handler
0019 {
0020 
0021   public function Analyze() {
0022     $info = &$this->getid3->info;
0023 
0024     $this->fseek($info['avdataoffset']);
0025     $AUheader  = $this->fread(8);
0026 
0027     $magic = '.snd';
0028     if (substr($AUheader, 0, 4) != $magic) {
0029       $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" (".snd") at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($AUheader, 0, 4)).'"';
0030       return false;
0031     }
0032 
0033     // shortcut
0034     $info['au'] = array();
0035     $thisfile_au        = &$info['au'];
0036 
0037     $info['fileformat']            = 'au';
0038     $info['audio']['dataformat']   = 'au';
0039     $info['audio']['bitrate_mode'] = 'cbr';
0040     $thisfile_au['encoding']               = 'ISO-8859-1';
0041 
0042     $thisfile_au['header_length']   = getid3_lib::BigEndian2Int(substr($AUheader,  4, 4));
0043     $AUheader .= $this->fread($thisfile_au['header_length'] - 8);
0044     $info['avdataoffset'] += $thisfile_au['header_length'];
0045 
0046     $thisfile_au['data_size']             = getid3_lib::BigEndian2Int(substr($AUheader,  8, 4));
0047     $thisfile_au['data_format_id']        = getid3_lib::BigEndian2Int(substr($AUheader, 12, 4));
0048     $thisfile_au['sample_rate']           = getid3_lib::BigEndian2Int(substr($AUheader, 16, 4));
0049     $thisfile_au['channels']              = getid3_lib::BigEndian2Int(substr($AUheader, 20, 4));
0050     $thisfile_au['comments']['comment'][] =                      trim(substr($AUheader, 24));
0051 
0052     $thisfile_au['data_format'] = $this->AUdataFormatNameLookup($thisfile_au['data_format_id']);
0053     $thisfile_au['used_bits_per_sample'] = $this->AUdataFormatUsedBitsPerSampleLookup($thisfile_au['data_format_id']);
0054     if ($thisfile_au['bits_per_sample'] = $this->AUdataFormatBitsPerSampleLookup($thisfile_au['data_format_id'])) {
0055       $info['audio']['bits_per_sample'] = $thisfile_au['bits_per_sample'];
0056     } else {
0057       unset($thisfile_au['bits_per_sample']);
0058     }
0059 
0060     $info['audio']['sample_rate']  = $thisfile_au['sample_rate'];
0061     $info['audio']['channels']     = $thisfile_au['channels'];
0062 
0063     if (($info['avdataoffset'] + $thisfile_au['data_size']) > $info['avdataend']) {
0064       $info['warning'][] = 'Possible truncated file - expecting "'.$thisfile_au['data_size'].'" bytes of audio data, only found '.($info['avdataend'] - $info['avdataoffset']).' bytes"';
0065     }
0066 
0067     $info['playtime_seconds'] = $thisfile_au['data_size'] / ($thisfile_au['sample_rate'] * $thisfile_au['channels'] * ($thisfile_au['used_bits_per_sample'] / 8));
0068     $info['audio']['bitrate'] = ($thisfile_au['data_size'] * 8) / $info['playtime_seconds'];
0069 
0070     return true;
0071   }
0072 
0073   public function AUdataFormatNameLookup($id) {
0074     static $AUdataFormatNameLookup = array(
0075       0  => 'unspecified format',
0076       1  => '8-bit mu-law',
0077       2  => '8-bit linear',
0078       3  => '16-bit linear',
0079       4  => '24-bit linear',
0080       5  => '32-bit linear',
0081       6  => 'floating-point',
0082       7  => 'double-precision float',
0083       8  => 'fragmented sampled data',
0084       9  => 'SUN_FORMAT_NESTED',
0085       10 => 'DSP program',
0086       11 => '8-bit fixed-point',
0087       12 => '16-bit fixed-point',
0088       13 => '24-bit fixed-point',
0089       14 => '32-bit fixed-point',
0090 
0091       16 => 'non-audio display data',
0092       17 => 'SND_FORMAT_MULAW_SQUELCH',
0093       18 => '16-bit linear with emphasis',
0094       19 => '16-bit linear with compression',
0095       20 => '16-bit linear with emphasis + compression',
0096       21 => 'Music Kit DSP commands',
0097       22 => 'SND_FORMAT_DSP_COMMANDS_SAMPLES',
0098       23 => 'CCITT g.721 4-bit ADPCM',
0099       24 => 'CCITT g.722 ADPCM',
0100       25 => 'CCITT g.723 3-bit ADPCM',
0101       26 => 'CCITT g.723 5-bit ADPCM',
0102       27 => 'A-Law 8-bit'
0103     );
0104     return (isset($AUdataFormatNameLookup[$id]) ? $AUdataFormatNameLookup[$id] : false);
0105   }
0106 
0107   public function AUdataFormatBitsPerSampleLookup($id) {
0108     static $AUdataFormatBitsPerSampleLookup = array(
0109       1  => 8,
0110       2  => 8,
0111       3  => 16,
0112       4  => 24,
0113       5  => 32,
0114       6  => 32,
0115       7  => 64,
0116 
0117       11 => 8,
0118       12 => 16,
0119       13 => 24,
0120       14 => 32,
0121 
0122       18 => 16,
0123       19 => 16,
0124       20 => 16,
0125 
0126       23 => 16,
0127 
0128       25 => 16,
0129       26 => 16,
0130       27 => 8
0131     );
0132     return (isset($AUdataFormatBitsPerSampleLookup[$id]) ? $AUdataFormatBitsPerSampleLookup[$id] : false);
0133   }
0134 
0135   public function AUdataFormatUsedBitsPerSampleLookup($id) {
0136     static $AUdataFormatUsedBitsPerSampleLookup = array(
0137       1  => 8,
0138       2  => 8,
0139       3  => 16,
0140       4  => 24,
0141       5  => 32,
0142       6  => 32,
0143       7  => 64,
0144 
0145       11 => 8,
0146       12 => 16,
0147       13 => 24,
0148       14 => 32,
0149 
0150       18 => 16,
0151       19 => 16,
0152       20 => 16,
0153 
0154       23 => 4,
0155 
0156       25 => 3,
0157       26 => 5,
0158       27 => 8,
0159     );
0160     return (isset($AUdataFormatUsedBitsPerSampleLookup[$id]) ? $AUdataFormatUsedBitsPerSampleLookup[$id] : false);
0161   }
0162 
0163 }