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.avr.php                                        //
12// module for analyzing AVR Audio 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_avr extends getid3_handler
22{
23	/**
24	 * @return bool
25	 */
26	public function Analyze() {
27		$info = &$this->getid3->info;
28
29		// http://cui.unige.ch/OSG/info/AudioFormats/ap11.html
30		// http://www.btinternet.com/~AnthonyJ/Atari/programming/avr_format.html
31		// offset    type    length    name        comments
32		// ---------------------------------------------------------------------
33		// 0    char    4    ID        format ID == "2BIT"
34		// 4    char    8    name        sample name (unused space filled with 0)
35		// 12    short    1    mono/stereo    0=mono, -1 (0xFFFF)=stereo
36		//                     With stereo, samples are alternated,
37		//                     the first voice is the left :
38		//                     (LRLRLRLRLRLRLRLRLR...)
39		// 14    short    1    resolution    8, 12 or 16 (bits)
40		// 16    short    1    signed or not    0=unsigned, -1 (0xFFFF)=signed
41		// 18    short    1    loop or not    0=no loop, -1 (0xFFFF)=loop on
42		// 20    short    1    MIDI note    0xFFnn, where 0 <= nn <= 127
43		//                     0xFFFF means "no MIDI note defined"
44		// 22    byte    1    Replay speed    Frequence in the Replay software
45		//                     0=5.485 Khz, 1=8.084 Khz, 2=10.971 Khz,
46		//                     3=16.168 Khz, 4=21.942 Khz, 5=32.336 Khz
47		//                     6=43.885 Khz, 7=47.261 Khz
48		//                     -1 (0xFF)=no defined Frequence
49		// 23    byte    3    sample rate    in Hertz
50		// 26    long    1    size in bytes (2 * bytes in stereo)
51		// 30    long    1    loop begin    0 for no loop
52		// 34    long    1    loop size    equal to 'size' for no loop
53		// 38  short   2   Reserved, MIDI keyboard split */
54		// 40  short   2   Reserved, sample compression */
55		// 42  short   2   Reserved */
56		// 44  char   20;  Additional filename space, used if (name[7] != 0)
57		// 64    byte    64    user data
58		// 128    bytes    ?    sample data    (12 bits samples are coded on 16 bits:
59		//                     0000 xxxx xxxx xxxx)
60		// ---------------------------------------------------------------------
61
62		// Note that all values are in motorola (big-endian) format, and that long is
63		// assumed to be 4 bytes, and short 2 bytes.
64		// When reading the samples, you should handle both signed and unsigned data,
65		// and be prepared to convert 16->8 bit, or mono->stereo if needed. To convert
66		// 8-bit data between signed/unsigned just add 127 to the sample values.
67		// Simularly for 16-bit data you should add 32769
68
69		$info['fileformat'] = 'avr';
70
71		$this->fseek($info['avdataoffset']);
72		$AVRheader = $this->fread(128);
73
74		$info['avr']['raw']['magic'] = substr($AVRheader,  0,  4);
75		$magic = '2BIT';
76		if ($info['avr']['raw']['magic'] != $magic) {
77			$this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['avr']['raw']['magic']).'"');
78			unset($info['fileformat']);
79			unset($info['avr']);
80			return false;
81		}
82		$info['avdataoffset'] += 128;
83
84		$info['avr']['sample_name']        =         rtrim(substr($AVRheader,  4,  8));
85		$info['avr']['raw']['mono']        = getid3_lib::BigEndian2Int(substr($AVRheader, 12,  2));
86		$info['avr']['bits_per_sample']    = getid3_lib::BigEndian2Int(substr($AVRheader, 14,  2));
87		$info['avr']['raw']['signed']      = getid3_lib::BigEndian2Int(substr($AVRheader, 16,  2));
88		$info['avr']['raw']['loop']        = getid3_lib::BigEndian2Int(substr($AVRheader, 18,  2));
89		$info['avr']['raw']['midi']        = getid3_lib::BigEndian2Int(substr($AVRheader, 20,  2));
90		$info['avr']['raw']['replay_freq'] = getid3_lib::BigEndian2Int(substr($AVRheader, 22,  1));
91		$info['avr']['sample_rate']        = getid3_lib::BigEndian2Int(substr($AVRheader, 23,  3));
92		$info['avr']['sample_length']      = getid3_lib::BigEndian2Int(substr($AVRheader, 26,  4));
93		$info['avr']['loop_start']         = getid3_lib::BigEndian2Int(substr($AVRheader, 30,  4));
94		$info['avr']['loop_end']           = getid3_lib::BigEndian2Int(substr($AVRheader, 34,  4));
95		$info['avr']['midi_split']         = getid3_lib::BigEndian2Int(substr($AVRheader, 38,  2));
96		$info['avr']['sample_compression'] = getid3_lib::BigEndian2Int(substr($AVRheader, 40,  2));
97		$info['avr']['reserved']           = getid3_lib::BigEndian2Int(substr($AVRheader, 42,  2));
98		$info['avr']['sample_name_extra']  =         rtrim(substr($AVRheader, 44, 20));
99		$info['avr']['comment']            =         rtrim(substr($AVRheader, 64, 64));
100
101		$info['avr']['flags']['stereo'] = (($info['avr']['raw']['mono']   == 0) ? false : true);
102		$info['avr']['flags']['signed'] = (($info['avr']['raw']['signed'] == 0) ? false : true);
103		$info['avr']['flags']['loop']   = (($info['avr']['raw']['loop']   == 0) ? false : true);
104
105		$info['avr']['midi_notes'] = array();
106		if (($info['avr']['raw']['midi'] & 0xFF00) != 0xFF00) {
107			$info['avr']['midi_notes'][] = ($info['avr']['raw']['midi'] & 0xFF00) >> 8;
108		}
109		if (($info['avr']['raw']['midi'] & 0x00FF) != 0x00FF) {
110			$info['avr']['midi_notes'][] = ($info['avr']['raw']['midi'] & 0x00FF);
111		}
112
113		if (($info['avdataend'] - $info['avdataoffset']) != ($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 1 : 2))) {
114			$this->warning('Probable truncated file: expecting '.($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 1 : 2)).' bytes of audio data, found '.($info['avdataend'] - $info['avdataoffset']));
115		}
116
117		$info['audio']['dataformat']      = 'avr';
118		$info['audio']['lossless']        = true;
119		$info['audio']['bitrate_mode']    = 'cbr';
120		$info['audio']['bits_per_sample'] = $info['avr']['bits_per_sample'];
121		$info['audio']['sample_rate']     = $info['avr']['sample_rate'];
122		$info['audio']['channels']        = ($info['avr']['flags']['stereo'] ? 2 : 1);
123		$info['playtime_seconds']         = ($info['avr']['sample_length'] / $info['audio']['channels']) / $info['avr']['sample_rate'];
124		$info['audio']['bitrate']         = ($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 8 : 16)) / $info['playtime_seconds'];
125
126
127		return true;
128	}
129
130}
131