PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 11ms RepoModel.GetById 30ms app.codeStats 0ms

/sys/plugins/id3/getid3/module.audio.vqf.php

https://bitbucket.org/DESURE/dcms
PHP | 160 lines | 119 code | 23 blank | 18 comment | 14 complexity | 2531a6c00ced28b96b4ae12b04244c5c MD5 | raw file
  1<?php
  2/////////////////////////////////////////////////////////////////
  3/// getID3() by James Heinrich <info@getid3.org>               //
  4//  available at http://getid3.sourceforge.net                 //
  5//            or http://www.getid3.org                         //
  6//          also https://github.com/JamesHeinrich/getID3       //
  7/////////////////////////////////////////////////////////////////
  8// See readme.txt for more details                             //
  9/////////////////////////////////////////////////////////////////
 10//                                                             //
 11// module.audio.vqf.php                                        //
 12// module for analyzing VQF audio files                        //
 13// dependencies: NONE                                          //
 14//                                                            ///
 15/////////////////////////////////////////////////////////////////
 16
 17
 18class getid3_vqf extends getid3_handler
 19{
 20	public function Analyze() {
 21		$info = &$this->getid3->info;
 22
 23		// based loosely on code from TTwinVQ by Jurgen Faul <jfaulØgmx*de>
 24		// http://jfaul.de/atl  or  http://j-faul.virtualave.net/atl/atl.html
 25
 26		$info['fileformat']            = 'vqf';
 27		$info['audio']['dataformat']   = 'vqf';
 28		$info['audio']['bitrate_mode'] = 'cbr';
 29		$info['audio']['lossless']     = false;
 30
 31		// shortcut
 32		$info['vqf']['raw'] = array();
 33		$thisfile_vqf               = &$info['vqf'];
 34		$thisfile_vqf_raw           = &$thisfile_vqf['raw'];
 35
 36		$this->fseek($info['avdataoffset']);
 37		$VQFheaderData = $this->fread(16);
 38
 39		$offset = 0;
 40		$thisfile_vqf_raw['header_tag'] = substr($VQFheaderData, $offset, 4);
 41		$magic = 'TWIN';
 42		if ($thisfile_vqf_raw['header_tag'] != $magic) {
 43			$info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_vqf_raw['header_tag']).'"';
 44			unset($info['vqf']);
 45			unset($info['fileformat']);
 46			return false;
 47		}
 48		$offset += 4;
 49		$thisfile_vqf_raw['version'] =                           substr($VQFheaderData, $offset, 8);
 50		$offset += 8;
 51		$thisfile_vqf_raw['size']    = getid3_lib::BigEndian2Int(substr($VQFheaderData, $offset, 4));
 52		$offset += 4;
 53
 54		while ($this->ftell() < $info['avdataend']) {
 55
 56			$ChunkBaseOffset = $this->ftell();
 57			$chunkoffset = 0;
 58			$ChunkData = $this->fread(8);
 59			$ChunkName = substr($ChunkData, $chunkoffset, 4);
 60			if ($ChunkName == 'DATA') {
 61				$info['avdataoffset'] = $ChunkBaseOffset;
 62				break;
 63			}
 64			$chunkoffset += 4;
 65			$ChunkSize = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
 66			$chunkoffset += 4;
 67			if ($ChunkSize > ($info['avdataend'] - $this->ftell())) {
 68				$info['error'][] = 'Invalid chunk size ('.$ChunkSize.') for chunk "'.$ChunkName.'" at offset '.$ChunkBaseOffset;
 69				break;
 70			}
 71			if ($ChunkSize > 0) {
 72				$ChunkData .= $this->fread($ChunkSize);
 73			}
 74
 75			switch ($ChunkName) {
 76				case 'COMM':
 77					// shortcut
 78					$thisfile_vqf['COMM'] = array();
 79					$thisfile_vqf_COMM    = &$thisfile_vqf['COMM'];
 80
 81					$thisfile_vqf_COMM['channel_mode']   = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
 82					$chunkoffset += 4;
 83					$thisfile_vqf_COMM['bitrate']        = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
 84					$chunkoffset += 4;
 85					$thisfile_vqf_COMM['sample_rate']    = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
 86					$chunkoffset += 4;
 87					$thisfile_vqf_COMM['security_level'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
 88					$chunkoffset += 4;
 89
 90					$info['audio']['channels']        = $thisfile_vqf_COMM['channel_mode'] + 1;
 91					$info['audio']['sample_rate']     = $this->VQFchannelFrequencyLookup($thisfile_vqf_COMM['sample_rate']);
 92					$info['audio']['bitrate']         = $thisfile_vqf_COMM['bitrate'] * 1000;
 93					$info['audio']['encoder_options'] = 'CBR' . ceil($info['audio']['bitrate']/1000);
 94
 95					if ($info['audio']['bitrate'] == 0) {
 96						$info['error'][] = 'Corrupt VQF file: bitrate_audio == zero';
 97						return false;
 98					}
 99					break;
100
101				case 'NAME':
102				case 'AUTH':
103				case '(c) ':
104				case 'FILE':
105				case 'COMT':
106				case 'ALBM':
107					$thisfile_vqf['comments'][$this->VQFcommentNiceNameLookup($ChunkName)][] = trim(substr($ChunkData, 8));
108					break;
109
110				case 'DSIZ':
111					$thisfile_vqf['DSIZ'] = getid3_lib::BigEndian2Int(substr($ChunkData, 8, 4));
112					break;
113
114				default:
115					$info['warning'][] = 'Unhandled chunk type "'.$ChunkName.'" at offset '.$ChunkBaseOffset;
116					break;
117			}
118		}
119
120		$info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate'];
121
122		if (isset($thisfile_vqf['DSIZ']) && (($thisfile_vqf['DSIZ'] != ($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'))))) {
123			switch ($thisfile_vqf['DSIZ']) {
124				case 0:
125				case 1:
126					$info['warning'][] = 'Invalid DSIZ value "'.$thisfile_vqf['DSIZ'].'". This is known to happen with VQF files encoded by Ahead Nero, and seems to be its way of saying this is TwinVQF v'.($thisfile_vqf['DSIZ'] + 1).'.0';
127					$info['audio']['encoder'] = 'Ahead Nero';
128					break;
129
130				default:
131					$info['warning'][] = 'Probable corrupted file - should be '.$thisfile_vqf['DSIZ'].' bytes, actually '.($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'));
132					break;
133			}
134		}
135
136		return true;
137	}
138
139	public function VQFchannelFrequencyLookup($frequencyid) {
140		static $VQFchannelFrequencyLookup = array(
141			11 => 11025,
142			22 => 22050,
143			44 => 44100
144		);
145		return (isset($VQFchannelFrequencyLookup[$frequencyid]) ? $VQFchannelFrequencyLookup[$frequencyid] : $frequencyid * 1000);
146	}
147
148	public function VQFcommentNiceNameLookup($shortname) {
149		static $VQFcommentNiceNameLookup = array(
150			'NAME' => 'title',
151			'AUTH' => 'artist',
152			'(c) ' => 'copyright',
153			'FILE' => 'filename',
154			'COMT' => 'comment',
155			'ALBM' => 'album'
156		);
157		return (isset($VQFcommentNiceNameLookup[$shortname]) ? $VQFcommentNiceNameLookup[$shortname] : $shortname);
158	}
159
160}