1<?php
2
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/////////////////////////////////////////////////////////////////
10//                                                             //
11// module.audio.au.php                                         //
12// module for analyzing AU files                               //
13// dependencies: NONE                                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20
21class getid3_au extends getid3_handler
22{
23	/**
24	 * @return bool
25	 */
26	public function Analyze() {
27		$info = &$this->getid3->info;
28
29		$this->fseek($info['avdataoffset']);
30		$AUheader  = $this->fread(8);
31
32		$magic = '.snd';
33		if (substr($AUheader, 0, 4) != $magic) {
34			$this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" (".snd") at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($AUheader, 0, 4)).'"');
35			return false;
36		}
37
38		// shortcut
39		$info['au'] = array();
40		$thisfile_au        = &$info['au'];
41
42		$info['fileformat']            = 'au';
43		$info['audio']['dataformat']   = 'au';
44		$info['audio']['bitrate_mode'] = 'cbr';
45		$thisfile_au['encoding']               = 'ISO-8859-1';
46
47		$thisfile_au['header_length']   = getid3_lib::BigEndian2Int(substr($AUheader,  4, 4));
48		$AUheader .= $this->fread($thisfile_au['header_length'] - 8);
49		$info['avdataoffset'] += $thisfile_au['header_length'];
50
51		$thisfile_au['data_size']             = getid3_lib::BigEndian2Int(substr($AUheader,  8, 4));
52		$thisfile_au['data_format_id']        = getid3_lib::BigEndian2Int(substr($AUheader, 12, 4));
53		$thisfile_au['sample_rate']           = getid3_lib::BigEndian2Int(substr($AUheader, 16, 4));
54		$thisfile_au['channels']              = getid3_lib::BigEndian2Int(substr($AUheader, 20, 4));
55		$thisfile_au['comments']['comment'][] =                      trim(substr($AUheader, 24));
56
57		$thisfile_au['data_format'] = $this->AUdataFormatNameLookup($thisfile_au['data_format_id']);
58		$thisfile_au['used_bits_per_sample'] = $this->AUdataFormatUsedBitsPerSampleLookup($thisfile_au['data_format_id']);
59		if ($thisfile_au['bits_per_sample'] = $this->AUdataFormatBitsPerSampleLookup($thisfile_au['data_format_id'])) {
60			$info['audio']['bits_per_sample'] = $thisfile_au['bits_per_sample'];
61		} else {
62			unset($thisfile_au['bits_per_sample']);
63		}
64
65		$info['audio']['sample_rate']  = $thisfile_au['sample_rate'];
66		$info['audio']['channels']     = $thisfile_au['channels'];
67
68		if (($info['avdataoffset'] + $thisfile_au['data_size']) > $info['avdataend']) {
69			$this->warning('Possible truncated file - expecting "'.$thisfile_au['data_size'].'" bytes of audio data, only found '.($info['avdataend'] - $info['avdataoffset']).' bytes"');
70		}
71
72		$info['playtime_seconds'] = $thisfile_au['data_size'] / ($thisfile_au['sample_rate'] * $thisfile_au['channels'] * ($thisfile_au['used_bits_per_sample'] / 8));
73		$info['audio']['bitrate'] = ($thisfile_au['data_size'] * 8) / $info['playtime_seconds'];
74
75		return true;
76	}
77
78	/**
79	 * @param int $id
80	 *
81	 * @return string|false
82	 */
83	public function AUdataFormatNameLookup($id) {
84		static $AUdataFormatNameLookup = array(
85			0  => 'unspecified format',
86			1  => '8-bit mu-law',
87			2  => '8-bit linear',
88			3  => '16-bit linear',
89			4  => '24-bit linear',
90			5  => '32-bit linear',
91			6  => 'floating-point',
92			7  => 'double-precision float',
93			8  => 'fragmented sampled data',
94			9  => 'SUN_FORMAT_NESTED',
95			10 => 'DSP program',
96			11 => '8-bit fixed-point',
97			12 => '16-bit fixed-point',
98			13 => '24-bit fixed-point',
99			14 => '32-bit fixed-point',
100
101			16 => 'non-audio display data',
102			17 => 'SND_FORMAT_MULAW_SQUELCH',
103			18 => '16-bit linear with emphasis',
104			19 => '16-bit linear with compression',
105			20 => '16-bit linear with emphasis + compression',
106			21 => 'Music Kit DSP commands',
107			22 => 'SND_FORMAT_DSP_COMMANDS_SAMPLES',
108			23 => 'CCITT g.721 4-bit ADPCM',
109			24 => 'CCITT g.722 ADPCM',
110			25 => 'CCITT g.723 3-bit ADPCM',
111			26 => 'CCITT g.723 5-bit ADPCM',
112			27 => 'A-Law 8-bit'
113		);
114		return (isset($AUdataFormatNameLookup[$id]) ? $AUdataFormatNameLookup[$id] : false);
115	}
116
117	/**
118	 * @param int $id
119	 *
120	 * @return int|false
121	 */
122	public function AUdataFormatBitsPerSampleLookup($id) {
123		static $AUdataFormatBitsPerSampleLookup = array(
124			1  => 8,
125			2  => 8,
126			3  => 16,
127			4  => 24,
128			5  => 32,
129			6  => 32,
130			7  => 64,
131
132			11 => 8,
133			12 => 16,
134			13 => 24,
135			14 => 32,
136
137			18 => 16,
138			19 => 16,
139			20 => 16,
140
141			23 => 16,
142
143			25 => 16,
144			26 => 16,
145			27 => 8
146		);
147		return (isset($AUdataFormatBitsPerSampleLookup[$id]) ? $AUdataFormatBitsPerSampleLookup[$id] : false);
148	}
149
150	/**
151	 * @param int $id
152	 *
153	 * @return int|false
154	 */
155	public function AUdataFormatUsedBitsPerSampleLookup($id) {
156		static $AUdataFormatUsedBitsPerSampleLookup = array(
157			1  => 8,
158			2  => 8,
159			3  => 16,
160			4  => 24,
161			5  => 32,
162			6  => 32,
163			7  => 64,
164
165			11 => 8,
166			12 => 16,
167			13 => 24,
168			14 => 32,
169
170			18 => 16,
171			19 => 16,
172			20 => 16,
173
174			23 => 4,
175
176			25 => 3,
177			26 => 5,
178			27 => 8,
179		);
180		return (isset($AUdataFormatUsedBitsPerSampleLookup[$id]) ? $AUdataFormatUsedBitsPerSampleLookup[$id] : false);
181	}
182
183}
184