annotate e2gallerypro/e2upload/Backend/Assets/getid3/module.audio.voc.php @ 11:ed6ee381b8fd judyates

[svn r12] compressed photos and assembled them into a working web gallery
author rlm
date Mon, 12 Apr 2010 02:31:43 -0400
parents 3f6b44aa6b35
children
rev   line source
rlm@3 1 <?php
rlm@3 2 // +----------------------------------------------------------------------+
rlm@3 3 // | PHP version 5 |
rlm@3 4 // +----------------------------------------------------------------------+
rlm@3 5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
rlm@3 6 // +----------------------------------------------------------------------+
rlm@3 7 // | This source file is subject to version 2 of the GPL license, |
rlm@3 8 // | that is bundled with this package in the file license.txt and is |
rlm@3 9 // | available through the world-wide-web at the following url: |
rlm@3 10 // | http://www.gnu.org/copyleft/gpl.html |
rlm@3 11 // +----------------------------------------------------------------------+
rlm@3 12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
rlm@3 13 // +----------------------------------------------------------------------+
rlm@3 14 // | Authors: James Heinrich <infoØgetid3*org> |
rlm@3 15 // | Allan Hansen <ahØartemis*dk> |
rlm@3 16 // +----------------------------------------------------------------------+
rlm@3 17 // | module.audio.voc.php |
rlm@3 18 // | Module for analyzing Creative VOC Audio files. |
rlm@3 19 // | dependencies: NONE |
rlm@3 20 // +----------------------------------------------------------------------+
rlm@3 21 //
rlm@3 22 // $Id: module.audio.voc.php,v 1.3 2006/11/02 10:48:02 ah Exp $
rlm@3 23
rlm@3 24
rlm@3 25
rlm@3 26 class getid3_voc extends getid3_handler
rlm@3 27 {
rlm@3 28
rlm@3 29 public function Analyze() {
rlm@3 30
rlm@3 31 $getid3 = $this->getid3;
rlm@3 32
rlm@3 33 $original_av_data_offset = $getid3->info['avdataoffset'];
rlm@3 34
rlm@3 35 fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
rlm@3 36 $voc_header= fread($getid3->fp, 26);
rlm@3 37
rlm@3 38 // Magic bytes: 'Creative Voice File'
rlm@3 39
rlm@3 40 $info_audio = &$getid3->info['audio'];
rlm@3 41 $getid3->info['voc'] = array ();
rlm@3 42 $info_voc = &$getid3->info['voc'];
rlm@3 43
rlm@3 44 $getid3->info['fileformat'] = 'voc';
rlm@3 45 $info_audio['dataformat'] = 'voc';
rlm@3 46 $info_audio['bitrate_mode'] = 'cbr';
rlm@3 47 $info_audio['lossless'] = true;
rlm@3 48 $info_audio['channels'] = 1; // might be overriden below
rlm@3 49 $info_audio['bits_per_sample'] = 8; // might be overriden below
rlm@3 50
rlm@3 51 // byte # Description
rlm@3 52 // ------ ------------------------------------------
rlm@3 53 // 00-12 'Creative Voice File'
rlm@3 54 // 13 1A (eof to abort printing of file)
rlm@3 55 // 14-15 Offset of first datablock in .voc file (std 1A 00 in Intel Notation)
rlm@3 56 // 16-17 Version number (minor,major) (VOC-HDR puts 0A 01)
rlm@3 57 // 18-19 2's Comp of Ver. # + 1234h (VOC-HDR puts 29 11)
rlm@3 58
rlm@3 59 getid3_lib::ReadSequence('LittleEndian2Int', $info_voc['header'], $voc_header, 20,
rlm@3 60 array (
rlm@3 61 'datablock_offset' => 2,
rlm@3 62 'minor_version' => 1,
rlm@3 63 'major_version' => 1
rlm@3 64 )
rlm@3 65 );
rlm@3 66
rlm@3 67 do {
rlm@3 68 $block_offset = ftell($getid3->fp);
rlm@3 69 $block_data = fread($getid3->fp, 4);
rlm@3 70 $block_type = ord($block_data{0});
rlm@3 71 $block_size = getid3_lib::LittleEndian2Int(substr($block_data, 1, 3));
rlm@3 72 $this_block = array ();
rlm@3 73
rlm@3 74 @$info_voc['blocktypes'][$block_type]++;
rlm@3 75
rlm@3 76 switch ($block_type) {
rlm@3 77
rlm@3 78 case 0: // Terminator
rlm@3 79 // do nothing, we'll break out of the loop down below
rlm@3 80 break;
rlm@3 81
rlm@3 82 case 1: // Sound data
rlm@3 83 $block_data .= fread($getid3->fp, 2);
rlm@3 84 if ($getid3->info['avdataoffset'] <= $original_av_data_offset) {
rlm@3 85 $getid3->info['avdataoffset'] = ftell($getid3->fp);
rlm@3 86 }
rlm@3 87 fseek($getid3->fp, $block_size - 2, SEEK_CUR);
rlm@3 88
rlm@3 89 getid3_lib::ReadSequence('LittleEndian2Int', $this_block, $block_data, 4,
rlm@3 90 array (
rlm@3 91 'sample_rate_id' => 1,
rlm@3 92 'compression_type' => 1
rlm@3 93 )
rlm@3 94 );
rlm@3 95
rlm@3 96 $this_block['compression_name'] = getid3_voc::VOCcompressionTypeLookup($this_block['compression_type']);
rlm@3 97 if ($this_block['compression_type'] <= 3) {
rlm@3 98 $info_voc['compressed_bits_per_sample'] = (int)(str_replace('-bit', '', $this_block['compression_name']));
rlm@3 99 }
rlm@3 100
rlm@3 101 // Less accurate sample_rate calculation than the Extended block (#8) data (but better than nothing if Extended Block is not available)
rlm@3 102 if (empty($info_audio['sample_rate'])) {
rlm@3 103 // SR byte = 256 - (1000000 / sample_rate)
rlm@3 104 $info_audio['sample_rate'] = (int)floor((1000000 / (256 - $this_block['sample_rate_id'])) / $info_audio['channels']);
rlm@3 105 }
rlm@3 106 break;
rlm@3 107
rlm@3 108 case 2: // Sound continue
rlm@3 109 case 3: // Silence
rlm@3 110 case 4: // Marker
rlm@3 111 case 6: // Repeat
rlm@3 112 case 7: // End repeat
rlm@3 113 // nothing useful, just skip
rlm@3 114 fseek($getid3->fp, $block_size, SEEK_CUR);
rlm@3 115 break;
rlm@3 116
rlm@3 117 case 8: // Extended
rlm@3 118 $block_data .= fread($getid3->fp, 4);
rlm@3 119
rlm@3 120 //00-01 Time Constant:
rlm@3 121 // Mono: 65536 - (256000000 / sample_rate)
rlm@3 122 // Stereo: 65536 - (256000000 / (sample_rate * 2))
rlm@3 123 getid3_lib::ReadSequence('LittleEndian2Int', $this_block, $block_data, 4,
rlm@3 124 array (
rlm@3 125 'time_constant' => 2,
rlm@3 126 'pack_method' => 1,
rlm@3 127 'stereo' => 1
rlm@3 128 )
rlm@3 129 );
rlm@3 130 $this_block['stereo'] = (bool)$this_block['stereo'];
rlm@3 131
rlm@3 132 $info_audio['channels'] = ($this_block['stereo'] ? 2 : 1);
rlm@3 133 $info_audio['sample_rate'] = (int)floor((256000000 / (65536 - $this_block['time_constant'])) / $info_audio['channels']);
rlm@3 134 break;
rlm@3 135
rlm@3 136 case 9: // data block that supersedes blocks 1 and 8. Used for stereo, 16 bit
rlm@3 137 $block_data .= fread($getid3->fp, 12);
rlm@3 138 if ($getid3->info['avdataoffset'] <= $original_av_data_offset) {
rlm@3 139 $getid3->info['avdataoffset'] = ftell($getid3->fp);
rlm@3 140 }
rlm@3 141 fseek($getid3->fp, $block_size - 12, SEEK_CUR);
rlm@3 142
rlm@3 143 getid3_lib::ReadSequence('LittleEndian2Int', $this_block, $block_data, 4,
rlm@3 144 array (
rlm@3 145 'sample_rate' => 4,
rlm@3 146 'bits_per_sample' => 1,
rlm@3 147 'channels' => 1,
rlm@3 148 'wFormat' => 2
rlm@3 149 )
rlm@3 150 );
rlm@3 151
rlm@3 152 $this_block['compression_name'] = getid3_voc::VOCwFormatLookup($this_block['wFormat']);
rlm@3 153 if (getid3_voc::VOCwFormatActualBitsPerSampleLookup($this_block['wFormat'])) {
rlm@3 154 $info_voc['compressed_bits_per_sample'] = getid3_voc::VOCwFormatActualBitsPerSampleLookup($this_block['wFormat']);
rlm@3 155 }
rlm@3 156
rlm@3 157 $info_audio['sample_rate'] = $this_block['sample_rate'];
rlm@3 158 $info_audio['bits_per_sample'] = $this_block['bits_per_sample'];
rlm@3 159 $info_audio['channels'] = $this_block['channels'];
rlm@3 160 break;
rlm@3 161
rlm@3 162 default:
rlm@3 163 $getid3->warning('Unhandled block type "'.$block_type.'" at offset '.$block_offset);
rlm@3 164 fseek($getid3->fp, $block_size, SEEK_CUR);
rlm@3 165 break;
rlm@3 166 }
rlm@3 167
rlm@3 168 if (!empty($this_block)) {
rlm@3 169 $this_block['block_offset'] = $block_offset;
rlm@3 170 $this_block['block_size'] = $block_size;
rlm@3 171 $this_block['block_type_id'] = $block_type;
rlm@3 172 $info_voc['blocks'][] = $this_block;
rlm@3 173 }
rlm@3 174
rlm@3 175 } while (!feof($getid3->fp) && ($block_type != 0));
rlm@3 176
rlm@3 177 // Terminator block doesn't have size field, so seek back 3 spaces
rlm@3 178 fseek($getid3->fp, -3, SEEK_CUR);
rlm@3 179
rlm@3 180 ksort($info_voc['blocktypes']);
rlm@3 181
rlm@3 182 if (!empty($info_voc['compressed_bits_per_sample'])) {
rlm@3 183 $getid3->info['playtime_seconds'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / ($info_voc['compressed_bits_per_sample'] * $info_audio['channels'] * $info_audio['sample_rate']);
rlm@3 184 $info_audio['bitrate'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / $getid3->info['playtime_seconds'];
rlm@3 185 }
rlm@3 186
rlm@3 187 return true;
rlm@3 188 }
rlm@3 189
rlm@3 190
rlm@3 191
rlm@3 192 public static function VOCcompressionTypeLookup($index) {
rlm@3 193
rlm@3 194 static $lookup = array (
rlm@3 195 0 => '8-bit',
rlm@3 196 1 => '4-bit',
rlm@3 197 2 => '2.6-bit',
rlm@3 198 3 => '2-bit'
rlm@3 199 );
rlm@3 200 return (isset($lookup[$index]) ? $lookup[$index] : 'Multi DAC ('.($index - 3).') channels');
rlm@3 201 }
rlm@3 202
rlm@3 203
rlm@3 204
rlm@3 205 public static function VOCwFormatLookup($index) {
rlm@3 206
rlm@3 207 static $lookup = array (
rlm@3 208 0x0000 => '8-bit unsigned PCM',
rlm@3 209 0x0001 => 'Creative 8-bit to 4-bit ADPCM',
rlm@3 210 0x0002 => 'Creative 8-bit to 3-bit ADPCM',
rlm@3 211 0x0003 => 'Creative 8-bit to 2-bit ADPCM',
rlm@3 212 0x0004 => '16-bit signed PCM',
rlm@3 213 0x0006 => 'CCITT a-Law',
rlm@3 214 0x0007 => 'CCITT u-Law',
rlm@3 215 0x2000 => 'Creative 16-bit to 4-bit ADPCM'
rlm@3 216 );
rlm@3 217 return (isset($lookup[$index]) ? $lookup[$index] : false);
rlm@3 218 }
rlm@3 219
rlm@3 220
rlm@3 221
rlm@3 222 public static function VOCwFormatActualBitsPerSampleLookup($index) {
rlm@3 223
rlm@3 224 static $lookup = array (
rlm@3 225 0x0000 => 8,
rlm@3 226 0x0001 => 4,
rlm@3 227 0x0002 => 3,
rlm@3 228 0x0003 => 2,
rlm@3 229 0x0004 => 16,
rlm@3 230 0x0006 => 8,
rlm@3 231 0x0007 => 8,
rlm@3 232 0x2000 => 4
rlm@3 233 );
rlm@3 234 return (isset($lookup[$index]) ? $lookup[$index] : false);
rlm@3 235 }
rlm@3 236
rlm@3 237 }
rlm@3 238
rlm@3 239
rlm@3 240 ?>