Annotation of /trunk/lib/getid3/module.audio.voc.php
Parent Directory
|
Revision Log
Revision 2 - (view) (download)
| 1 : | andphe | 2 | <?php |
| 2 : | ///////////////////////////////////////////////////////////////// | ||
| 3 : | /// getID3() by James Heinrich <info@getid3.org> // | ||
| 4 : | // available at http://getid3.sourceforge.net // | ||
| 5 : | // or http://www.getid3.org // | ||
| 6 : | ///////////////////////////////////////////////////////////////// | ||
| 7 : | // See readme.txt for more details // | ||
| 8 : | ///////////////////////////////////////////////////////////////// | ||
| 9 : | // // | ||
| 10 : | // module.audio.voc.php // | ||
| 11 : | // module for analyzing Creative VOC Audio files // | ||
| 12 : | // dependencies: NONE // | ||
| 13 : | // /// | ||
| 14 : | ///////////////////////////////////////////////////////////////// | ||
| 15 : | // MOS Intruder Alerts | ||
| 16 : | defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); | ||
| 17 : | |||
| 18 : | class getid3_voc | ||
| 19 : | { | ||
| 20 : | |||
| 21 : | function getid3_voc(&$fd, &$ThisFileInfo) { | ||
| 22 : | |||
| 23 : | $OriginalAVdataOffset = $ThisFileInfo['avdataoffset']; | ||
| 24 : | fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET); | ||
| 25 : | $VOCheader = fread($fd, 26); | ||
| 26 : | |||
| 27 : | if (substr($VOCheader, 0, 19) != 'Creative Voice File') { | ||
| 28 : | $ThisFileInfo['error'][] = 'Expecting "Creative Voice File" at offset '.$ThisFileInfo['avdataoffset'].', found "'.substr($VOCheader, 0, 19).'"'; | ||
| 29 : | return false; | ||
| 30 : | } | ||
| 31 : | |||
| 32 : | // shortcuts | ||
| 33 : | $thisfile_audio = &$ThisFileInfo['audio']; | ||
| 34 : | $ThisFileInfo['voc'] = array(); | ||
| 35 : | $thisfile_voc = &$ThisFileInfo['voc']; | ||
| 36 : | |||
| 37 : | $ThisFileInfo['fileformat'] = 'voc'; | ||
| 38 : | $thisfile_audio['dataformat'] = 'voc'; | ||
| 39 : | $thisfile_audio['bitrate_mode'] = 'cbr'; | ||
| 40 : | $thisfile_audio['lossless'] = true; | ||
| 41 : | $thisfile_audio['channels'] = 1; // might be overriden below | ||
| 42 : | $thisfile_audio['bits_per_sample'] = 8; // might be overriden below | ||
| 43 : | |||
| 44 : | // byte # Description | ||
| 45 : | // ------ ------------------------------------------ | ||
| 46 : | // 00-12 'Creative Voice File' | ||
| 47 : | // 13 1A (eof to abort printing of file) | ||
| 48 : | // 14-15 Offset of first datablock in .voc file (std 1A 00 in Intel Notation) | ||
| 49 : | // 16-17 Version number (minor,major) (VOC-HDR puts 0A 01) | ||
| 50 : | // 18-19 2's Comp of Ver. # + 1234h (VOC-HDR puts 29 11) | ||
| 51 : | |||
| 52 : | $thisfile_voc['header']['datablock_offset'] = getid3_lib::LittleEndian2Int(substr($VOCheader, 20, 2)); | ||
| 53 : | $thisfile_voc['header']['minor_version'] = getid3_lib::LittleEndian2Int(substr($VOCheader, 22, 1)); | ||
| 54 : | $thisfile_voc['header']['major_version'] = getid3_lib::LittleEndian2Int(substr($VOCheader, 23, 1)); | ||
| 55 : | |||
| 56 : | do { | ||
| 57 : | |||
| 58 : | $BlockOffset = ftell($fd); | ||
| 59 : | $BlockData = fread($fd, 4); | ||
| 60 : | $BlockType = ord($BlockData{0}); | ||
| 61 : | $BlockSize = getid3_lib::LittleEndian2Int(substr($BlockData, 1, 3)); | ||
| 62 : | $ThisBlock = array(); | ||
| 63 : | |||
| 64 : | @$thisfile_voc['blocktypes'][$BlockType]++; | ||
| 65 : | switch ($BlockType) { | ||
| 66 : | case 0: // Terminator | ||
| 67 : | // do nothing, we'll break out of the loop down below | ||
| 68 : | break; | ||
| 69 : | |||
| 70 : | case 1: // Sound data | ||
| 71 : | $BlockData .= fread($fd, 2); | ||
| 72 : | if ($ThisFileInfo['avdataoffset'] <= $OriginalAVdataOffset) { | ||
| 73 : | $ThisFileInfo['avdataoffset'] = ftell($fd); | ||
| 74 : | } | ||
| 75 : | fseek($fd, $BlockSize - 2, SEEK_CUR); | ||
| 76 : | |||
| 77 : | $ThisBlock['sample_rate_id'] = getid3_lib::LittleEndian2Int(substr($BlockData, 4, 1)); | ||
| 78 : | $ThisBlock['compression_type'] = getid3_lib::LittleEndian2Int(substr($BlockData, 5, 1)); | ||
| 79 : | |||
| 80 : | $ThisBlock['compression_name'] = $this->VOCcompressionTypeLookup($ThisBlock['compression_type']); | ||
| 81 : | if ($ThisBlock['compression_type'] <= 3) { | ||
| 82 : | $thisfile_voc['compressed_bits_per_sample'] = getid3_lib::CastAsInt(str_replace('-bit', '', $ThisBlock['compression_name'])); | ||
| 83 : | } | ||
| 84 : | |||
| 85 : | // Less accurate sample_rate calculation than the Extended block (#8) data (but better than nothing if Extended Block is not available) | ||
| 86 : | if (empty($thisfile_audio['sample_rate'])) { | ||
| 87 : | // SR byte = 256 - (1000000 / sample_rate) | ||
| 88 : | $thisfile_audio['sample_rate'] = getid3_lib::trunc((1000000 / (256 - $ThisBlock['sample_rate_id'])) / $thisfile_audio['channels']); | ||
| 89 : | } | ||
| 90 : | break; | ||
| 91 : | |||
| 92 : | case 2: // Sound continue | ||
| 93 : | case 3: // Silence | ||
| 94 : | case 4: // Marker | ||
| 95 : | case 6: // Repeat | ||
| 96 : | case 7: // End repeat | ||
| 97 : | // nothing useful, just skip | ||
| 98 : | fseek($fd, $BlockSize, SEEK_CUR); | ||
| 99 : | break; | ||
| 100 : | |||
| 101 : | case 8: // Extended | ||
| 102 : | $BlockData .= fread($fd, 4); | ||
| 103 : | |||
| 104 : | //00-01 Time Constant: | ||
| 105 : | // Mono: 65536 - (256000000 / sample_rate) | ||
| 106 : | // Stereo: 65536 - (256000000 / (sample_rate * 2)) | ||
| 107 : | $ThisBlock['time_constant'] = getid3_lib::LittleEndian2Int(substr($BlockData, 4, 2)); | ||
| 108 : | $ThisBlock['pack_method'] = getid3_lib::LittleEndian2Int(substr($BlockData, 6, 1)); | ||
| 109 : | $ThisBlock['stereo'] = (bool) getid3_lib::LittleEndian2Int(substr($BlockData, 7, 1)); | ||
| 110 : | |||
| 111 : | $thisfile_audio['channels'] = ($ThisBlock['stereo'] ? 2 : 1); | ||
| 112 : | $thisfile_audio['sample_rate'] = getid3_lib::trunc((256000000 / (65536 - $ThisBlock['time_constant'])) / $thisfile_audio['channels']); | ||
| 113 : | break; | ||
| 114 : | |||
| 115 : | case 9: // data block that supersedes blocks 1 and 8. Used for stereo, 16 bit | ||
| 116 : | $BlockData .= fread($fd, 12); | ||
| 117 : | if ($ThisFileInfo['avdataoffset'] <= $OriginalAVdataOffset) { | ||
| 118 : | $ThisFileInfo['avdataoffset'] = ftell($fd); | ||
| 119 : | } | ||
| 120 : | fseek($fd, $BlockSize - 12, SEEK_CUR); | ||
| 121 : | |||
| 122 : | $ThisBlock['sample_rate'] = getid3_lib::LittleEndian2Int(substr($BlockData, 4, 4)); | ||
| 123 : | $ThisBlock['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($BlockData, 8, 1)); | ||
| 124 : | $ThisBlock['channels'] = getid3_lib::LittleEndian2Int(substr($BlockData, 9, 1)); | ||
| 125 : | $ThisBlock['wFormat'] = getid3_lib::LittleEndian2Int(substr($BlockData, 10, 2)); | ||
| 126 : | |||
| 127 : | $ThisBlock['compression_name'] = $this->VOCwFormatLookup($ThisBlock['wFormat']); | ||
| 128 : | if ($this->VOCwFormatActualBitsPerSampleLookup($ThisBlock['wFormat'])) { | ||
| 129 : | $thisfile_voc['compressed_bits_per_sample'] = $this->VOCwFormatActualBitsPerSampleLookup($ThisBlock['wFormat']); | ||
| 130 : | } | ||
| 131 : | |||
| 132 : | $thisfile_audio['sample_rate'] = $ThisBlock['sample_rate']; | ||
| 133 : | $thisfile_audio['bits_per_sample'] = $ThisBlock['bits_per_sample']; | ||
| 134 : | $thisfile_audio['channels'] = $ThisBlock['channels']; | ||
| 135 : | break; | ||
| 136 : | |||
| 137 : | default: | ||
| 138 : | $ThisFileInfo['warning'][] = 'Unhandled block type "'.$BlockType.'" at offset '.$BlockOffset; | ||
| 139 : | fseek($fd, $BlockSize, SEEK_CUR); | ||
| 140 : | break; | ||
| 141 : | } | ||
| 142 : | |||
| 143 : | if (!empty($ThisBlock)) { | ||
| 144 : | $ThisBlock['block_offset'] = $BlockOffset; | ||
| 145 : | $ThisBlock['block_size'] = $BlockSize; | ||
| 146 : | $ThisBlock['block_type_id'] = $BlockType; | ||
| 147 : | $thisfile_voc['blocks'][] = $ThisBlock; | ||
| 148 : | } | ||
| 149 : | |||
| 150 : | } while (!feof($fd) && ($BlockType != 0)); | ||
| 151 : | |||
| 152 : | // Terminator block doesn't have size field, so seek back 3 spaces | ||
| 153 : | fseek($fd, -3, SEEK_CUR); | ||
| 154 : | |||
| 155 : | ksort($thisfile_voc['blocktypes']); | ||
| 156 : | |||
| 157 : | if (!empty($thisfile_voc['compressed_bits_per_sample'])) { | ||
| 158 : | $ThisFileInfo['playtime_seconds'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / ($thisfile_voc['compressed_bits_per_sample'] * $thisfile_audio['channels'] * $thisfile_audio['sample_rate']); | ||
| 159 : | $thisfile_audio['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds']; | ||
| 160 : | } | ||
| 161 : | |||
| 162 : | return true; | ||
| 163 : | } | ||
| 164 : | |||
| 165 : | function VOCcompressionTypeLookup($index) { | ||
| 166 : | static $VOCcompressionTypeLookup = array( | ||
| 167 : | 0 => '8-bit', | ||
| 168 : | 1 => '4-bit', | ||
| 169 : | 2 => '2.6-bit', | ||
| 170 : | 3 => '2-bit' | ||
| 171 : | ); | ||
| 172 : | return (isset($VOCcompressionTypeLookup[$index]) ? $VOCcompressionTypeLookup[$index] : 'Multi DAC ('.($index - 3).') channels'); | ||
| 173 : | } | ||
| 174 : | |||
| 175 : | function VOCwFormatLookup($index) { | ||
| 176 : | static $VOCwFormatLookup = array( | ||
| 177 : | 0x0000 => '8-bit unsigned PCM', | ||
| 178 : | 0x0001 => 'Creative 8-bit to 4-bit ADPCM', | ||
| 179 : | 0x0002 => 'Creative 8-bit to 3-bit ADPCM', | ||
| 180 : | 0x0003 => 'Creative 8-bit to 2-bit ADPCM', | ||
| 181 : | 0x0004 => '16-bit signed PCM', | ||
| 182 : | 0x0006 => 'CCITT a-Law', | ||
| 183 : | 0x0007 => 'CCITT u-Law', | ||
| 184 : | 0x2000 => 'Creative 16-bit to 4-bit ADPCM' | ||
| 185 : | ); | ||
| 186 : | return (isset($VOCwFormatLookup[$index]) ? $VOCwFormatLookup[$index] : false); | ||
| 187 : | } | ||
| 188 : | |||
| 189 : | function VOCwFormatActualBitsPerSampleLookup($index) { | ||
| 190 : | static $VOCwFormatLookup = array( | ||
| 191 : | 0x0000 => 8, | ||
| 192 : | 0x0001 => 4, | ||
| 193 : | 0x0002 => 3, | ||
| 194 : | 0x0003 => 2, | ||
| 195 : | 0x0004 => 16, | ||
| 196 : | 0x0006 => 8, | ||
| 197 : | 0x0007 => 8, | ||
| 198 : | 0x2000 => 4 | ||
| 199 : | ); | ||
| 200 : | return (isset($VOCwFormatLookup[$index]) ? $VOCwFormatLookup[$index] : false); | ||
| 201 : | } | ||
| 202 : | |||
| 203 : | } | ||
| 204 : | |||
| 205 : | |||
| 206 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

