Exemple #1
0
def CreateWaveHeaderFromFile (name):
    """Returns the wave header string which can be used in force header argument of the open() function.
    The name argument is the path to the audio file which can be recognized by fileinfo module.
    (*.mp1, *.mp2, *.mp3, *.mp4, *.m4a, *.m4b, *.aac, *.wma, *.ogg, *.flac, *.wav, *.aif)
    Note that this is not accurate. In most cases all will be OK, but, for instance,
    when you have the two channeled MP3 file encoded with 24 bits depth, you will not get the correct header.
    The sample width will be assumed as for 16 bits depth i.e. sw = 2,
    which will lead into incorrect reading of stdout and wrong representation of the raw data.
    If you do know what specifications the output of the external decoder is going to have,
    then rather use the CreateWaveHeader() instead.
    I would be delighted if there is someone who can help fix this."""
    import cStringIO
    wh = cStringIO.StringIO()
    wf = wave.open(wh, "w")
    inf = info(name).info
    try: chn = inf.channels
    except: chn = int(inf.mode!=3)+1
    wf.setnchannels(chn)
    try: bps = inf.bits_per_sample # Only MP4 has bits_per_sample
    except:
        bps = 16 # This is stupid, completely mental, really
    wf.setsampwidth((bps+7)//8)
    wf.setframerate(inf.sample_rate)
    wf.setnframes(0)
    wf.writeframes("")
    wh.seek(0)
    return wh.read()
Exemple #2
0
 def initfp (self):
     """Extracts data in the first 44 bytes in a WAVE stdout"""
     file = self.obj.stdout
     # Read in all data
     header = file.read(44)
     # Verify that the correct identifiers are present
     if (header[0:4] != "RIFF") or (header[12:16] != "fmt "):
         if self.forceheader: header = self.forceheader
         else: raise Error, "file does not start with RIFF id or fmt chunk missing"
     self._chunksize = struct.unpack('<L', header[4:8])[0]
     self._format = header[8:12]
     self._subchunk1size = struct.unpack('<L', header[16:20])[0]
     self._audioformat = struct.unpack('<H', header[20:22])[0]
     self._nchannels = struct.unpack('<H', header[22:24])[0]
     self._framerate = struct.unpack('<L', header[24:28])[0]
     self._byterate = struct.unpack('<L', header[28:32])[0]
     self._blockalign = struct.unpack('<H', header[32:34])[0]
     self._bitspersample = struct.unpack('<H', header[34:36])[0]
     self._sampwidth = (self._bitspersample+7)//8
     self._framesize = self._nchannels*self._sampwidth
     self._dataloc = header.find("data")
     self._nframes = int(self._framerate*info(self.obj.filename).info.length)