source: extensions/charlies_content/getid3/getid3/module.audio.avr.php @ 3544

Last change on this file since 3544 was 3544, checked in by vdigital, 15 years ago

Change: getid3 upgraded to -> 1.7.9

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