3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
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
23 public function Analyze() {
24 $info = &$this->getid3
->info
;
26 $this->fseek($info['avdataoffset']);
28 $ShortenHeader = $this->fread(8);
30 if (substr($ShortenHeader, 0, 4) != $magic) {
31 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes(substr($ShortenHeader, 0, 4)).'"');
34 $info['fileformat'] = 'shn';
35 $info['audio']['dataformat'] = 'shn';
36 $info['audio']['lossless'] = true;
37 $info['audio']['bitrate_mode'] = 'vbr';
39 $info['shn']['version'] = getid3_lib
::LittleEndian2Int(substr($ShortenHeader, 4, 1));
41 $this->fseek($info['avdataend'] - 12);
42 $SeekTableSignatureTest = $this->fread(12);
43 $info['shn']['seektable']['present'] = substr($SeekTableSignatureTest, 4, 8) == 'SHNAMPSK';
44 if ($info['shn']['seektable']['present']) {
45 $info['shn']['seektable']['length'] = getid3_lib
::LittleEndian2Int(substr($SeekTableSignatureTest, 0, 4));
46 $info['shn']['seektable']['offset'] = $info['avdataend'] - $info['shn']['seektable']['length'];
47 $this->fseek($info['shn']['seektable']['offset']);
48 $SeekTableMagic = $this->fread(4);
50 if ($SeekTableMagic != $magic) {
52 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['shn']['seektable']['offset'].', found "'.getid3_lib
::PrintHexBytes($SeekTableMagic).'"');
57 // typedef struct tag_TSeekEntry
59 // unsigned long SampleNumber;
60 // unsigned long SHNFileByteOffset;
61 // unsigned long SHNLastBufferReadPosition;
62 // unsigned short SHNByteGet;
63 // unsigned short SHNBufferOffset;
64 // unsigned short SHNFileBitOffset;
65 // unsigned long SHNGBuffer;
66 // unsigned short SHNBitShift;
73 $SeekTableData = $this->fread($info['shn']['seektable']['length'] - 16);
74 $info['shn']['seektable']['entry_count'] = floor(strlen($SeekTableData) / 80);
75 //$info['shn']['seektable']['entries'] = array();
76 //$SeekTableOffset = 0;
77 //for ($i = 0; $i < $info['shn']['seektable']['entry_count']; $i++) {
78 // $SeekTableEntry['sample_number'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
79 // $SeekTableOffset += 4;
80 // $SeekTableEntry['shn_file_byte_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
81 // $SeekTableOffset += 4;
82 // $SeekTableEntry['shn_last_buffer_read_position'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
83 // $SeekTableOffset += 4;
84 // $SeekTableEntry['shn_byte_get'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
85 // $SeekTableOffset += 2;
86 // $SeekTableEntry['shn_buffer_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
87 // $SeekTableOffset += 2;
88 // $SeekTableEntry['shn_file_bit_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
89 // $SeekTableOffset += 2;
90 // $SeekTableEntry['shn_gbuffer'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
91 // $SeekTableOffset += 4;
92 // $SeekTableEntry['shn_bit_shift'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
93 // $SeekTableOffset += 2;
94 // for ($j = 0; $j < 3; $j++) {
95 // $SeekTableEntry['cbuf0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
96 // $SeekTableOffset += 4;
98 // for ($j = 0; $j < 3; $j++) {
99 // $SeekTableEntry['cbuf1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
100 // $SeekTableOffset += 4;
102 // for ($j = 0; $j < 4; $j++) {
103 // $SeekTableEntry['offset0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
104 // $SeekTableOffset += 4;
106 // for ($j = 0; $j < 4; $j++) {
107 // $SeekTableEntry['offset1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
108 // $SeekTableOffset += 4;
111 // $info['shn']['seektable']['entries'][] = $SeekTableEntry;
118 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
119 $this->error('PHP running in Safe Mode - backtick operator not available, cannot run shntool to analyze Shorten files');
123 if (GETID3_OS_ISWINDOWS
) {
125 $RequiredFiles = array('shorten.exe', 'cygwin1.dll', 'head.exe');
126 foreach ($RequiredFiles as $required_file) {
127 if (!is_readable(GETID3_HELPERAPPSDIR
.$required_file)) {
128 $this->error(GETID3_HELPERAPPSDIR
.$required_file.' does not exist');
132 $commandline = GETID3_HELPERAPPSDIR
.'shorten.exe -x "'.$info['filenamepath'].'" - | '.GETID3_HELPERAPPSDIR
.'head.exe -c 64';
133 $commandline = str_replace('/', '\\', $commandline);
137 static $shorten_present;
138 if (!isset($shorten_present)) {
139 $shorten_present = file_exists('/usr/local/bin/shorten') || `which shorten`
;
141 if (!$shorten_present) {
142 $this->error('shorten binary was not found in path or /usr/local/bin');
145 $commandline = (file_exists('/usr/local/bin/shorten') ?
'/usr/local/bin/' : '' ) . 'shorten -x '.escapeshellarg($info['filenamepath']).' - | head -c 64';
149 $output = `
$commandline`
;
151 if (!empty($output) && (substr($output, 12, 4) == 'fmt ')) {
153 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.audio-video.riff.php', __FILE__
, true);
155 $fmt_size = getid3_lib
::LittleEndian2Int(substr($output, 16, 4));
156 $DecodedWAVFORMATEX = getid3_riff
::parseWAVEFORMATex(substr($output, 20, $fmt_size));
157 $info['audio']['channels'] = $DecodedWAVFORMATEX['channels'];
158 $info['audio']['bits_per_sample'] = $DecodedWAVFORMATEX['bits_per_sample'];
159 $info['audio']['sample_rate'] = $DecodedWAVFORMATEX['sample_rate'];
161 if (substr($output, 20 +
$fmt_size, 4) == 'data') {
163 $info['playtime_seconds'] = getid3_lib
::LittleEndian2Int(substr($output, 20 +
4 +
$fmt_size, 4)) / $DecodedWAVFORMATEX['raw']['nAvgBytesPerSec'];
167 $this->error('shorten failed to decode DATA chunk to expected location, cannot determine playtime');
172 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8;
176 $this->error('shorten failed to decode file to WAV for parsing');