1<?php
2/////////////////////////////////////////////////////////////////
3/// getID3() by James Heinrich <info@getid3.org>               //
4//  available at https://github.com/JamesHeinrich/getID3       //
5//            or https://www.getid3.org                        //
6//            or http://getid3.sourceforge.net                 //
7//                                                             //
8// /demo/demo.audioinfo.class.php - part of getID3()           //
9//                                                            ///
10/////////////////////////////////////////////////////////////////
11// +----------------------------------------------------------------------+
12// | PHP version 4.1.0                                                    |
13// +----------------------------------------------------------------------+
14// | Placed in public domain by Allan Hansen, 2002. Share and enjoy!      |
15// +----------------------------------------------------------------------+
16// | /demo/demo.audioinfo.class.php                                       |
17// |                                                                      |
18// | Example wrapper class to extract information from audio files        |
19// | through getID3().                                                    |
20// |                                                                      |
21// | getID3() returns a lot of information. Much of this information is   |
22// | not needed for the end-application. It is also possible that some    |
23// | users want to extract specific info. Modifying getID3() files is a   |
24// | bad idea, as modifications needs to be done to future versions of    |
25// | getID3().                                                            |
26// |                                                                      |
27// | Modify this wrapper class instead. This example extracts certain     |
28// | fields only and adds a new root value - encoder_options if possible. |
29// | It also checks for mp3 files with wave headers.                      |
30// +----------------------------------------------------------------------+
31// | Example code:                                                        |
32// |   $au = new AudioInfo();                                             |
33// |   print_r($au->Info('file.flac');                                    |
34// +----------------------------------------------------------------------+
35// | Authors: Allan Hansen <ahØartemis*dk>                                |
36// +----------------------------------------------------------------------+
37
38
39
40/**
41* getID3() settings
42*/
43
44require_once('../getid3/getid3.php');
45
46
47
48
49/**
50* Class for extracting information from audio files with getID3().
51*/
52
53class AudioInfo {
54
55	/**
56	* Private variables
57	*/
58	private $result;
59	private $info;
60	private $getID3;
61
62
63	/**
64	* Constructor
65	*/
66
67	function __construct() {
68
69		// Initialize getID3 engine
70		$this->getID3 = new getID3;
71		$this->getID3->option_md5_data        = true;
72		$this->getID3->option_md5_data_source = true;
73		$this->getID3->encoding               = 'UTF-8';
74	}
75
76
77
78
79	/**
80	* Extract information - only public function
81	*
82	* @param    string  $file    Audio file to extract info from.
83	*
84	* @return array
85	*/
86
87	public function Info($file) {
88
89		// Analyze file
90		$this->info = $this->getID3->analyze($file);
91
92		// Exit here on error
93		if (isset($this->info['error'])) {
94			return array ('error' => $this->info['error']);
95		}
96
97		// Init wrapper object
98		$this->result = array();
99		$this->result['format_name']     = (isset($this->info['fileformat']) ? $this->info['fileformat'] : '').'/'.(isset($this->info['audio']['dataformat']) ? $this->info['audio']['dataformat'] : '').(isset($this->info['video']['dataformat']) ? '/'.$this->info['video']['dataformat'] : '');
100		$this->result['encoder_version'] = (isset($this->info['audio']['encoder'])         ? $this->info['audio']['encoder']         : '');
101		$this->result['encoder_options'] = (isset($this->info['audio']['encoder_options']) ? $this->info['audio']['encoder_options'] : '');
102		$this->result['bitrate_mode']    = (isset($this->info['audio']['bitrate_mode'])    ? $this->info['audio']['bitrate_mode']    : '');
103		$this->result['channels']        = (isset($this->info['audio']['channels'])        ? $this->info['audio']['channels']        : '');
104		$this->result['sample_rate']     = (isset($this->info['audio']['sample_rate'])     ? $this->info['audio']['sample_rate']     : '');
105		$this->result['bits_per_sample'] = (isset($this->info['audio']['bits_per_sample']) ? $this->info['audio']['bits_per_sample'] : '');
106		$this->result['playing_time']    = (isset($this->info['playtime_seconds'])         ? $this->info['playtime_seconds']         : '');
107		$this->result['avg_bit_rate']    = (isset($this->info['audio']['bitrate'])         ? $this->info['audio']['bitrate']         : '');
108		$this->result['tags']            = (isset($this->info['tags'])                     ? $this->info['tags']                     : '');
109		$this->result['comments']        = (isset($this->info['comments'])                 ? $this->info['comments']                 : '');
110		$this->result['warning']         = (isset($this->info['warning'])                  ? $this->info['warning']                  : '');
111		$this->result['md5']             = (isset($this->info['md5_data'])                 ? $this->info['md5_data']                 : '');
112
113		// Post getID3() data handling based on file format
114		$method = (isset($this->info['fileformat']) ? $this->info['fileformat'] : '').'Info';
115		if ($method && method_exists($this, $method)) {
116			$this->$method();
117		}
118
119		return $this->result;
120	}
121
122
123
124
125	/**
126	* post-getID3() data handling for AAC files.
127	*
128	*/
129
130	private function aacInfo() {
131		$this->result['format_name']     = 'AAC';
132	}
133
134
135
136
137	/**
138	* post-getID3() data handling for Wave files.
139	*
140	*/
141
142	private function riffInfo() {
143		if ($this->info['audio']['dataformat'] == 'wav') {
144
145			$this->result['format_name'] = 'Wave';
146
147		} elseif (preg_match('#^mp[1-3]$#', $this->info['audio']['dataformat'])) {
148
149			$this->result['format_name'] = strtoupper($this->info['audio']['dataformat']);
150
151		} else {
152
153			$this->result['format_name'] = 'riff/'.$this->info['audio']['dataformat'];
154
155		}
156	}
157
158
159
160
161	/**
162	* * post-getID3() data handling for FLAC files.
163	*
164	*/
165
166	private function flacInfo() {
167		$this->result['format_name']     = 'FLAC';
168	}
169
170
171
172
173
174	/**
175	* post-getID3() data handling for Monkey's Audio files.
176	*
177	*/
178
179	private function macInfo() {
180		$this->result['format_name']     = 'Monkey\'s Audio';
181	}
182
183
184
185
186
187	/**
188	 * post-getID3() data handling for Lossless Audio files.
189	 */
190	private function laInfo() {
191		$this->result['format_name']     = 'La';
192	}
193
194
195
196
197
198	/**
199	* post-getID3() data handling for Ogg Vorbis files.
200	*
201	* @access   private
202	*/
203
204	function oggInfo() {
205		if ($this->info['audio']['dataformat'] == 'vorbis') {
206
207			$this->result['format_name']     = 'Ogg Vorbis';
208
209		} else if ($this->info['audio']['dataformat'] == 'flac') {
210
211			$this->result['format_name'] = 'Ogg FLAC';
212
213		} else if ($this->info['audio']['dataformat'] == 'speex') {
214
215			$this->result['format_name'] = 'Ogg Speex';
216
217		} else {
218
219			$this->result['format_name'] = 'Ogg '.$this->info['audio']['dataformat'];
220
221		}
222	}
223
224
225
226
227	/**
228	* post-getID3() data handling for Musepack files.
229	*
230	* @access   private
231	*/
232
233	function mpcInfo() {
234		$this->result['format_name']     = 'Musepack';
235	}
236
237
238
239
240	/**
241	* post-getID3() data handling for MPEG files.
242	*
243	* @access   private
244	*/
245
246	function mp3Info() {
247		$this->result['format_name']     = 'MP3';
248	}
249
250
251
252
253	/**
254	* post-getID3() data handling for MPEG files.
255	*
256	* @access   private
257	*/
258
259	function mp2Info() {
260		$this->result['format_name']     = 'MP2';
261	}
262
263
264
265
266
267	/**
268	* post-getID3() data handling for MPEG files.
269	*
270	* @access   private
271	*/
272
273	function mp1Info() {
274		$this->result['format_name']     = 'MP1';
275	}
276
277
278
279
280	/**
281	* post-getID3() data handling for WMA files.
282	*
283	* @access   private
284	*/
285
286	function asfInfo() {
287		$this->result['format_name']     = strtoupper($this->info['audio']['dataformat']);
288	}
289
290
291
292	/**
293	* post-getID3() data handling for Real files.
294	*
295	* @access   private
296	*/
297
298	function realInfo() {
299		$this->result['format_name']     = 'Real';
300	}
301
302
303
304
305
306	/**
307	* post-getID3() data handling for VQF files.
308	*
309	* @access   private
310	*/
311
312	function vqfInfo() {
313		$this->result['format_name']     = 'VQF';
314	}
315
316}
317