Back to index

eyed3  0.6.18
Public Member Functions | Public Attributes
mp3.Header Class Reference

List of all members.

Public Member Functions

def __init__
def decode

Public Attributes

 version
 layer
 errorProtection
 bitRate
 playTime
 sampleFreq
 padding
 privateBit
 copyright
 original
 emphasis
 mode
 modeExtension
 frameLength

Detailed Description

Definition at line 138 of file mp3.py.


Constructor & Destructor Documentation

def mp3.Header.__init__ (   self,
  header_data = None 
)

Definition at line 139 of file mp3.py.

00139 
00140    def __init__(self, header_data=None):
00141        self.version = None
00142        self.layer = None
00143        self.errorProtection = None
00144        self.bitRate = None
00145        self.playTime = None
00146        self.sampleFreq = None
00147        self.padding = None
00148        self.privateBit = None
00149        self.copyright = None
00150        self.original = None
00151        self.emphasis = None
00152        self.mode = None
00153        # This value is left as is: 0<=modeExtension<=3.
00154        # See http://www.dv.co.yu/mpgscript/mpeghdr.htm for how to interpret
00155        self.modeExtension = None
00156 
00157        if header_data:
00158            self.decode(header_data)


Member Function Documentation

def mp3.Header.decode (   self,
  header 
)

Definition at line 160 of file mp3.py.

00160 
00161    def decode(self, header):
00162       if not is_valid_mp_header(header):
00163          raise Mp3Exception("Invalid MPEG header");
00164 
00165       # MPEG audio version from bits 19 and 20.
00166       version = (header >> 19) & 0x3
00167       self.version = [2.5, None, 2.0, 1.0][version]
00168       if self.version is None:
00169          raise Mp3Exception("Illegal MPEG version");
00170 
00171       # MPEG layer
00172       self.layer = 4 - ((header >> 17) & 0x3)
00173       if self.layer == 4:
00174          raise Mp3Exception("Illegal MPEG layer");
00175 
00176       # Decode some simple values.
00177       self.errorProtection = not (header >> 16) & 0x1;
00178       self.padding = (header >> 9) & 0x1;
00179       self.privateBit = (header >> 8) & 0x1;
00180       self.copyright = (header >> 3) & 0x1;
00181       self.original = (header >> 2) & 0x1;
00182 
00183       # Obtain sampling frequency.
00184       sampleBits = (header >> 10) & 0x3;
00185       if self.version == 2.5:
00186          freqCol = 2;
00187       else:
00188          freqCol = int(self.version - 1);
00189       self.sampleFreq = SAMPLE_FREQ_TABLE[sampleBits][freqCol];
00190       if not self.sampleFreq:
00191          raise Mp3Exception("Illegal MPEG sampling frequency");
00192 
00193       # Compute bitrate.
00194       bitRateIndex = (header >> 12) & 0xf;
00195       if int(self.version) == 1 and self.layer == 1:
00196          bitRateCol = 0;
00197       elif int(self.version) == 1 and self.layer == 2:
00198          bitRateCol = 1;
00199       elif int(self.version) == 1 and self.layer == 3:
00200          bitRateCol = 2;
00201       elif int(self.version) == 2 and self.layer == 1:
00202          bitRateCol = 3;
00203       elif int(self.version) == 2 and (self.layer == 2 or \
00204                                        self.layer == 3):
00205          bitRateCol = 4;
00206       else:
00207          raise Mp3Exception("Mp3 version %f and layer %d is an invalid "\
00208                             "combination" % (self.version, self.layer));
00209       self.bitRate = BIT_RATE_TABLE[bitRateIndex][bitRateCol];
00210       if self.bitRate == None:
00211          raise Mp3Exception("Invalid bit rate");
00212       # We know know the bit rate specified in this frame, but if the file
00213       # is VBR we need to obtain the average from the Xing header.
00214       # This is done by the caller since right now all we have is the frame
00215       # header.
00216 
00217       # Emphasis; whatever that means??
00218       emph = header & 0x3;
00219       if emph == 0:
00220          self.emphasis = EMPHASIS_NONE;
00221       elif emph == 1:
00222          self.emphasis = EMPHASIS_5015;
00223       elif emph == 2:
00224          self.emphasis = EMPHASIS_CCIT;
00225       elif strictID3():
00226          raise Mp3Exception("Illegal mp3 emphasis value: %d" % emph);
00227 
00228       # Channel mode.
00229       modeBits = (header >> 6) & 0x3;
00230       if modeBits == 0:
00231          self.mode = MODE_STEREO;
00232       elif modeBits == 1:
00233          self.mode = MODE_JOINT_STEREO;
00234       elif modeBits == 2:
00235          self.mode = MODE_DUAL_CHANNEL_STEREO;
00236       else:
00237          self.mode = MODE_MONO;
00238       self.modeExtension = (header >> 4) & 0x3;
00239 
00240       # Layer II has restrictions wrt to mode and bit rate.  This code
00241       # enforces them.
00242       if self.layer == 2:
00243          m = self.mode;
00244          br = self.bitRate;
00245          if (br == 32 or br == 48 or br == 56 or br == 80) and \
00246             (m != MODE_MONO):
00247             raise Mp3Exception("Invalid mode/bitrate combination for layer "\
00248                                "II");
00249          if (br == 224 or br == 256 or br == 320 or br == 384) and \
00250             (m == MODE_MONO):
00251             raise Mp3Exception("Invalid mode/bitrate combination for layer "\
00252                                "II");
00253 
00254       br = self.bitRate * 1000;
00255       sf = self.sampleFreq;
00256       p  = self.padding;
00257       if self.layer == 1:
00258          # Layer 1 uses 32 bit slots for padding.
00259          p  = self.padding * 4;
00260          self.frameLength = int((((12 * br) / sf) + p) * 4);
00261       else:
00262          # Layer 2 and 3 uses 8 bit slots for padding.
00263          p  = self.padding * 1;
00264          self.frameLength = int(((144 * br) / sf) + p);
00265 
00266       # Dump the state.
00267       TRACE_MSG("MPEG audio version: " + str(self.version));
00268       TRACE_MSG("MPEG audio layer: " + ("I" * self.layer));
00269       TRACE_MSG("MPEG sampling frequency: " + str(self.sampleFreq));
00270       TRACE_MSG("MPEG bit rate: " + str(self.bitRate));
00271       TRACE_MSG("MPEG channel mode: " + self.mode);
00272       TRACE_MSG("MPEG channel mode extension: " + str(self.modeExtension));
00273       TRACE_MSG("MPEG CRC error protection: " + str(self.errorProtection));
00274       TRACE_MSG("MPEG original: " + str(self.original));
00275       TRACE_MSG("MPEG copyright: " + str(self.copyright));
00276       TRACE_MSG("MPEG private bit: " + str(self.privateBit));
00277       TRACE_MSG("MPEG padding: " + str(self.padding));
00278       TRACE_MSG("MPEG emphasis: " + str(self.emphasis));
00279       TRACE_MSG("MPEG frame length: " + str(self.frameLength));

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 143 of file mp3.py.

Definition at line 148 of file mp3.py.

Definition at line 150 of file mp3.py.

Definition at line 142 of file mp3.py.

Definition at line 259 of file mp3.py.

Definition at line 141 of file mp3.py.

Definition at line 151 of file mp3.py.

Definition at line 154 of file mp3.py.

Definition at line 149 of file mp3.py.

Definition at line 146 of file mp3.py.

Definition at line 144 of file mp3.py.

Definition at line 147 of file mp3.py.

Definition at line 145 of file mp3.py.

Definition at line 140 of file mp3.py.


The documentation for this class was generated from the following file: