8d5c5d4bed9223cd8528def8f9aeff13e3db8730
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 /////////////////////////////////////////////////////////////////
11 // module.audio.shorten.php //
12 // module for analyzing Shorten Audio files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
18 class getid3_shorten
extends getid3_handler
21 public function Analyze() {
22 $info = &$this->getid3
->info
;
24 $this->fseek($info['avdataoffset']);
26 $ShortenHeader = $this->fread(8);
28 if (substr($ShortenHeader, 0, 4) != $magic) {
29 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes(substr($ShortenHeader, 0, 4)).'"');
32 $info['fileformat'] = 'shn';
33 $info['audio']['dataformat'] = 'shn';
34 $info['audio']['lossless'] = true;
35 $info['audio']['bitrate_mode'] = 'vbr';
37 $info['shn']['version'] = getid3_lib
::LittleEndian2Int(substr($ShortenHeader, 4, 1));
39 $this->fseek($info['avdataend'] - 12);
40 $SeekTableSignatureTest = $this->fread(12);
41 $info['shn']['seektable']['present'] = (bool) (substr($SeekTableSignatureTest, 4, 8) == 'SHNAMPSK');
42 if ($info['shn']['seektable']['present']) {
43 $info['shn']['seektable']['length'] = getid3_lib
::LittleEndian2Int(substr($SeekTableSignatureTest, 0, 4));
44 $info['shn']['seektable']['offset'] = $info['avdataend'] - $info['shn']['seektable']['length'];
45 $this->fseek($info['shn']['seektable']['offset']);
46 $SeekTableMagic = $this->fread(4);
48 if ($SeekTableMagic != $magic) {
50 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['shn']['seektable']['offset'].', found "'.getid3_lib
::PrintHexBytes($SeekTableMagic).'"');
55 // typedef struct tag_TSeekEntry
57 // unsigned long SampleNumber;
58 // unsigned long SHNFileByteOffset;
59 // unsigned long SHNLastBufferReadPosition;
60 // unsigned short SHNByteGet;
61 // unsigned short SHNBufferOffset;
62 // unsigned short SHNFileBitOffset;
63 // unsigned long SHNGBuffer;
64 // unsigned short SHNBitShift;
71 $SeekTableData = $this->fread($info['shn']['seektable']['length'] - 16);
72 $info['shn']['seektable']['entry_count'] = floor(strlen($SeekTableData) / 80);
73 //$info['shn']['seektable']['entries'] = array();
74 //$SeekTableOffset = 0;
75 //for ($i = 0; $i < $info['shn']['seektable']['entry_count']; $i++) {
76 // $SeekTableEntry['sample_number'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
77 // $SeekTableOffset += 4;
78 // $SeekTableEntry['shn_file_byte_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
79 // $SeekTableOffset += 4;
80 // $SeekTableEntry['shn_last_buffer_read_position'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
81 // $SeekTableOffset += 4;
82 // $SeekTableEntry['shn_byte_get'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
83 // $SeekTableOffset += 2;
84 // $SeekTableEntry['shn_buffer_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
85 // $SeekTableOffset += 2;
86 // $SeekTableEntry['shn_file_bit_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
87 // $SeekTableOffset += 2;
88 // $SeekTableEntry['shn_gbuffer'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
89 // $SeekTableOffset += 4;
90 // $SeekTableEntry['shn_bit_shift'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
91 // $SeekTableOffset += 2;
92 // for ($j = 0; $j < 3; $j++) {
93 // $SeekTableEntry['cbuf0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
94 // $SeekTableOffset += 4;
96 // for ($j = 0; $j < 3; $j++) {
97 // $SeekTableEntry['cbuf1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
98 // $SeekTableOffset += 4;
100 // for ($j = 0; $j < 4; $j++) {
101 // $SeekTableEntry['offset0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
102 // $SeekTableOffset += 4;
104 // for ($j = 0; $j < 4; $j++) {
105 // $SeekTableEntry['offset1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
106 // $SeekTableOffset += 4;
109 // $info['shn']['seektable']['entries'][] = $SeekTableEntry;
116 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
117 $this->error('PHP running in Safe Mode - backtick operator not available, cannot run shntool to analyze Shorten files');
121 if (GETID3_OS_ISWINDOWS
) {
123 $RequiredFiles = array('shorten.exe', 'cygwin1.dll', 'head.exe');
124 foreach ($RequiredFiles as $required_file) {
125 if (!is_readable(GETID3_HELPERAPPSDIR
.$required_file)) {
126 $this->error(GETID3_HELPERAPPSDIR
.$required_file.' does not exist');
130 $commandline = GETID3_HELPERAPPSDIR
.'shorten.exe -x "'.$info['filenamepath'].'" - | '.GETID3_HELPERAPPSDIR
.'head.exe -c 64';
131 $commandline = str_replace('/', '\\', $commandline);
135 static $shorten_present;
136 if (!isset($shorten_present)) {
137 $shorten_present = file_exists('/usr/local/bin/shorten') || `which shorten`
;
139 if (!$shorten_present) {
140 $this->error('shorten binary was not found in path or /usr/local/bin');
143 $commandline = (file_exists('/usr/local/bin/shorten') ?
'/usr/local/bin/' : '' ) . 'shorten -x '.escapeshellarg($info['filenamepath']).' - | head -c 64';
147 $output = `
$commandline`
;
149 if (!empty($output) && (substr($output, 12, 4) == 'fmt ')) {
151 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.audio-video.riff.php', __FILE__
, true);
153 $fmt_size = getid3_lib
::LittleEndian2Int(substr($output, 16, 4));
154 $DecodedWAVFORMATEX = getid3_riff
::parseWAVEFORMATex(substr($output, 20, $fmt_size));
155 $info['audio']['channels'] = $DecodedWAVFORMATEX['channels'];
156 $info['audio']['bits_per_sample'] = $DecodedWAVFORMATEX['bits_per_sample'];
157 $info['audio']['sample_rate'] = $DecodedWAVFORMATEX['sample_rate'];
159 if (substr($output, 20 +
$fmt_size, 4) == 'data') {
161 $info['playtime_seconds'] = getid3_lib
::LittleEndian2Int(substr($output, 20 +
4 +
$fmt_size, 4)) / $DecodedWAVFORMATEX['raw']['nAvgBytesPerSec'];
165 $this->error('shorten failed to decode DATA chunk to expected location, cannot determine playtime');
170 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8;
174 $this->error('shorten failed to decode file to WAV for parsing');