Exemple #1
0
 def __init__(self, file_name):
     super(audio_decoder, self).__init__()
     self.file_name = file_name
     ext = file_name.strip().split('.')[-1].lower()
     dm = muxer.Demuxer(ext)
     fin = open(file_name, 'rb')
     if not fin:
         raise "cannot find file %s" % file_name
     s = fin.read(3000000)
     r = dm.parse(s)
     
     print dm.streams
     self.decoder = None
     for aindex in xrange( len( dm.streams )):
       if dm.streams[ aindex ] and dm.streams[ aindex ][ 'type' ]== muxer.CODEC_TYPE_AUDIO:
         self.decoder = acodec.Decoder( dm.streams[ aindex ] )
         self.aindex = aindex
         break
     if not self.decoder:
         raise "no audio track found in given media file!"
     
     self.resampler = sound.Resampler( (dm.streams[ aindex ][ 'sample_rate' ], dm.streams[ aindex ][ 'channels' ]), 
                                       (constants.AUDIO_SAMPLE_RATE , 1) )
     self.ostream = data_stream(constants.AUDIO_SAMPLE_RATE, data_format = {'dtype':np.int16})
     self.odtype = np.int16
     self.demuxer = dm
     self.frames = r
     self.fin = fin
def aplayer(name, card, rate, tt):
    import pymedia.muxer as muxer, pymedia.audio.acodec as acodec, pymedia.audio.sound as sound
    import time
    dm = muxer.Demuxer(str.split(name, '.')[-1].lower())
    snds = sound.getODevices()
    if card not in range(len(snds)):
        raise 'Cannot play sound to non existent device %d out of %d' % (
            card + 1, len(snds))
    f = open(name, 'rb')
    snd = resampler = dec = None
    s = f.read(32000)
    t = 0
    while len(s):
        frames = dm.parse(s)
        if frames:
            for fr in frames:
                # Assume for now only audio streams

                if dec == None:
                    print dm.getInfo(), dm.streams
                    dec = acodec.Decoder(dm.streams[fr[0]])

                r = dec.decode(fr[1])
                if r and r.data:
                    if snd == None:
                        print 'Opening sound with %d channels -> %s' % (
                            r.channels, snds[card]['name'])
                        snd = sound.Output(int(r.sample_rate * rate),
                                           r.channels, sound.AFMT_S16_LE, card)
                        if rate < 1 or rate > 1:
                            resampler = sound.Resampler(
                                (r.sample_rate, r.channels),
                                (int(r.sample_rate / rate), r.channels))
                            print 'Sound resampling %d->%d' % (
                                r.sample_rate, r.sample_rate / rate)

                    data = r.data
                    if resampler:
                        data = resampler.resample(data)
                    if EMULATE:
                        # Calc delay we should wait to emulate snd.play()

                        d = len(data) / float(r.sample_rate * r.channels * 2)
                        time.sleep(d)
                        if int(t + d) != int(t):
                            print 'playing: %d sec\r' % (t + d),
                        t += d
                    else:
                        snd.play(data)
        if tt > 0:
            if snd and snd.getPosition() > tt:
                break

        s = f.read(512)

    while snd.isPlaying():
        time.sleep(.05)
Exemple #3
0
    def _processAudioFrame(self, d):
        # Decode audio frame
        afr = self.ac.decode(d[1])
        if afr:
            # See if we have to set up the sound
            if self.snd == None:
                self.aBitRate = afr.bitrate
                self.aSampleRate = afr.sample_rate
                self.aChannels = afr.channels
                try:
                    # Hardcoded S16 ( 16 bit signed ) for now
                    #print 'Opening sound', afr.sample_rate, afr.channels, sound.AFMT_S16_LE, self.audioCard
                    self.snd = sound.Output(afr.sample_rate, afr.channels,
                                            sound.AFMT_S16_LE, self.audioCard)
                    self.resampler = None
                except:
                    try:
                        # Create a resampler when no multichannel sound is available
                        self.resampler = sound.Resampler(
                            (afr.sample_rate, afr.channels),
                            (afr.sample_rate, 2))
                        # Fallback to 2 channels
                        #print 'Falling back to', afr.sample_rate, 2, sound.AFMT_S16_LE, self.audioCard
                        self.snd = sound.Output(afr.sample_rate, 2,
                                                sound.AFMT_S16_LE,
                                                self.audioCard)
                    except:
                        self.err.append(sys.exc_info())
                        raise

                # Calc max buf size for better audio handling
                if self.maxBufSize == -1:
                    self.maxBufSize = self.snd.getSpace()

            # Handle the PTS accordingly
            snd = self.snd
            if d[3] > 0 and self.aDelta == 0 and snd:
                # set correction factor in case of PTS presence
                self.aDelta = (float(d[3]) /
                               90000) - snd.getPosition() - snd.getLeft()

            # Play the raw data if we have it
            if len(afr.data) > 0:
                # Split the audio data if the size of data chunk larger than the buffer size
                data = afr.data
                if len(data) > self.maxBufSize:
                    data = str(data)

                while len(data) > self.maxBufSize:
                    chunk = data[:self.maxBufSize]
                    data = data[self.maxBufSize:]
                    self.aDecodedFrames.append(
                        (chunk, afr.sample_rate, afr.channels))

                self.aDecodedFrames.append(
                    (data, afr.sample_rate, afr.channels))
                self._processAudio()
Exemple #4
0
def videoDecodeBenchmark( inFile, opt ):
  dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] )
  f= open( inFile, 'rb' )
  s= f.read( 400000 )
  r= dm.parse( s )
  v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams )
  if len( v )== 0:
    raise 'There is no video stream in a file %s' % inFile
  
  v_id= v[ 0 ][ 'index' ]
  print 'Assume video stream at %d index: ' % v_id
  
  a= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_AUDIO, dm.streams )
  if len( a )== 0:
    print 'There is no audio stream in a file %s. Ignoring audio.' % inFile
    opt= 'noaudio'
  else:
    a_id= a[ 0 ][ 'index' ]
  
  t= time.time()
  
  vc= vcodec.Decoder( dm.streams[ v_id ] )
  print dm.streams[ v_id ]
  if opt!= 'noaudio':
    import pymedia.audio.acodec as acodec
    import pymedia.audio.sound as sound
    ac= acodec.Decoder( dm.streams[ a_id ] )
  resampler= None
  frames= 0
  while len( s )> 0:
    for fr in r:
      if fr[ 0 ]== v_id:
        d= vc.decode( fr[ 1 ] )
        if fr[ 3 ]> 0:
          print 'Video PTS', fr[ 3 ]
        if d and d.data:
          frames+= 1
          #ff= open( 'c:\\test', 'wb' )
          #ff.write( d.data[ 0 ] )
          #ff.close()
      elif opt!= 'noaudio' and fr[ 0 ]== a_id:
        d= ac.decode( fr[ 1 ] )
        if resampler== None:
          if d and d.channels> 2:
            resampler= sound.Resampler( (d.sample_rate,d.channels), (d.sample_rate,2) )
        else:
          data= resampler.resample( d.data )

    s= f.read( 400000 )
    r= dm.parse( s )
    
    tt= time.time()- t
    print '%d frames in %d secs( %.02f fps )' % ( frames, tt, float(frames)/tt )
Exemple #5
0
    def processAudioFrame(self, d):
        # Decode audio frame
        afr = self.ac.decode(d[1])
        if afr:
            # See if we set up the sound
            if self.snd == None:
                self.aBitRate = afr.bitrate
                #print 'Sound: ', afr.sample_rate, afr.channels, afr.bitrate
                try:
                    # Hardcoded S16 ( 16 bit signed ) for now
                    self.snd = sound.Output(afr.sample_rate, afr.channels,
                                            sound.AFMT_S16_LE)
                    self.resampler = None
                except:
                    try:
                        # Create a resampler when no multichannel sound is available
                        self.resampler = sound.Resampler(
                            (afr.sample_rate, afr.channels),
                            (afr.sample_rate, 2))
                        # Fallback to 2 channels
                        self.snd = sound.Output(afr.sample_rate, 2,
                                                sound.AFMT_S16_LE)
                    except:
                        traceback.print_exc()
                        self.err.append(sys.exc_info()[1])
                        return

            # See if we need to resample the audio data
            s = afr.data
            if self.resampler:
                s = self.resampler.resample(s)

            # Handle the PTS accordingly
            if d[3] > 0 and self.aDelta == 0:
                # set correction factor in case of PTS presence
                self.aDelta = (float(d[3]) / 90000
                               ) - self.snd.getPosition() - self.snd.getLeft()

            # Play the raw data if we have it
            if len(s) > 0:
                self.aDecodedFrames.append(s)
                while len(self.aDecodedFrames):
                    # See if we can play sound chunk without clashing with the video frame
                    if len(s) > self.snd.getSpace():
                        break

                    #print 'LEFT:', self.snd.getLeft(), len( s ), self.snd.getSpace()
                    self.snd.play(self.aDecodedFrames.pop(0))
Exemple #6
0
def aPlayer( name ):
  global sampleFreqs, exit, snd
  # Open analyzer for a mono signal
  analyzer= sound.SpectrAnalyzer( 1, SAMPLES, NUM_FREQS )
  resampler= None
  
  dec= acodec.Decoder( str.split( name, '.' )[ -1 ].lower() )
  f= open( name, 'rb' )
  snd= None
  s= f.read( 20000 )
  while len( s ) and exit== 0:
    r= dec.decode( s )
    if snd== None:
      print 'Opening sound with sample rate %d and channels %d' % ( r.sample_rate, r.channels )
      snd= sound.Output( r.sample_rate, r.channels, sound.AFMT_S16_LE )
      resampler= sound.Resampler( (r.sample_rate,r.channels), (r.sample_rate,1) )
    
    if r and len( r.data ):
      s1= resampler.resample( r.data )
      fTmp= analyzer.asBands( BANDS, s1 )
      sampleFreqs.append( ( snd.getPosition()+ snd.getLeft(), fTmp ) )
      snd.play( r.data )
      pp= snd.getPosition()
      x= 0
      while x< len( sampleFreqs ) and pp> sampleFreqs[ x ][ 0 ]:
        x+= 1
      
      for i in range( 0, x- 1 ):
        del( sampleFreqs[ i ] )
      
    s= f.read( 512 )
  
  # Wait until complete all sounds
  while snd.isPlaying():
    time.sleep( .05 )
  
  exit= 1
Exemple #7
0
def videoDecodeBenchmark(inFile, opt):
    pygame.init()
    pygame.display.set_mode((800, 600), 0)
    ovl = None

    dm = muxer.Demuxer(inFile.split('.')[-1])
    f = open(inFile, 'rb')
    s = f.read(400000)
    r = dm.parse(s)
    v = filter(lambda x: x['type'] == muxer.CODEC_TYPE_VIDEO, dm.streams)
    if len(v) == 0:
        raise 'There is no video stream in a file %s' % inFile

    v_id = v[0]['index']
    print 'Assume video stream at %d index: ' % v_id

    a = filter(lambda x: x['type'] == muxer.CODEC_TYPE_AUDIO, dm.streams)
    if len(a) == 0:
        print 'There is no audio stream in a file %s. Ignoring audio.' % inFile
        opt = 'noaudio'
    else:
        a_id = a[0]['index']

    t = time.time()

    vc = vcodec.Decoder(dm.streams[v_id])
    print dm.streams[v_id]
    if opt != 'noaudio':
        ac = acodec.Decoder(dm.streams[a_id])
    resampler = None
    frames = 0
    q = []
    while len(s) > 0:
        for fr in r:
            if fr[0] == v_id:
                d = vc.decode(fr[1])
                if d and d.data:
                    frames += 1
                    #ff= open( 'c:\\test', 'wb' )
                    #ff.write( d.data[ 0 ] )
                    #ff.close()
                    if not ovl:
                        ovl = pygame.Overlay(YV12, d.size)
                    q.append(d)
                    if len(q) > 4:
                        try:
                            ovl.set_data(q[0].data)
                            ovl.display()
                        except:
                            ovl.display(q[0].data)

                        del (q[0])
            elif opt != 'noaudio' and fr[0] == a_id:
                d = ac.decode(fr[1])
                if resampler == None:
                    if d and d.channels > 2:
                        resampler = sound.Resampler(
                            (d.sample_rate, d.channels), (d.sample_rate, 2))
                else:
                    data = resampler.resample(d.data)

        s = f.read(400000)
        r = dm.parse(s)

        tt = time.time() - t
        print '%d frames in %d secs( %.02f fps )' % (frames, tt,
                                                     float(frames) / tt)

        ev = pygame.event.get()
        for e in ev:
            if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
                s = ''
                break
Exemple #8
0
    def run(self):
        """ plays a god-damn song """

        #local mp3 playback
        print self.file_name.encode('utf-8')
        self.parent.current_song.status = 'loading'
        while os.path.isfile(self.file_name) != True:
            time.sleep(1)

        get_buffer = options.GetSetting('buffer-size-bytes',
                                        self.parent.FILEDB)
        if get_buffer != False:
            buffer_sz = int(get_buffer)
        else:
            buffer_sz = BUFFER_SIZE
        while os.path.getsize(self.file_name) < buffer_sz:
            time.sleep(2)
            #print os.path.getsize(file_name)
        self.parent.time_count = -1
        self.parent.current_song.status = 'playing'

        card = int(self.output_device)
        rate = 1
        tt = -1
        #dm= muxer.Demuxer( str.split( name, '.' )[ -1 ].lower() )
        dm = muxer.Demuxer('mp3')
        snds = sound.getODevices()
        if card not in range(len(snds)):
            raise 'Cannot play sound to non existent device %d out of %d' % (
                card + 1, len(snds))
        f = open(self.file_name, 'rb')
        self.snd = resampler = dec = None
        s = f.read(32000)
        t = 0
        while (len(s)):
            #print self.local_play_status
            frames = dm.parse(s)
            if frames:
                for fr in frames:
                    # Assume for now only audio streams

                    if dec == None:
                        #print dm.getHeaderInfo(), dm.streams
                        dec = acodec.Decoder(dm.streams[fr[0]])

                    r = dec.decode(fr[1])
                    if r and r.data:
                        if self.snd == None:
                            #print 'Opening sound with %d channels -> %s' % ( r.channels, snds[ card ][ 'name' ] )
                            self.snd = sound.Output(int(r.sample_rate * rate),
                                                    r.channels,
                                                    sound.AFMT_S16_LE, card)
                            #print r.channels
                            if rate < 1 or rate > 1:
                                resampler = sound.Resampler(
                                    (r.sample_rate, r.channels),
                                    (int(r.sample_rate / rate), r.channels))
                                #print 'Sound resampling %d->%d' % ( r.sample_rate, r.sample_rate/rate )

                        data = r.data
                        if resampler:
                            data = resampler.resample(data)
                        if EMULATE:
                            # Calc delay we should wait to emulate snd.play()

                            d = len(data) / float(
                                r.sample_rate * r.channels * 2)
                            time.sleep(d)
                            #if int( t+d )!= int( t ):
                            #    print 'playing: %d sec\r' % ( t+d ),
                            t += d
                        else:
                            self.snd.play(data)
                        #print snd.getPosition()
            if tt > 0:
                if self.snd and self.snd.getPosition() < tt:
                    break

            s = f.read(512)
            if self.local_play_status == False:
                break

        while self.snd.isPlaying():
            time.sleep(0.05)
Exemple #9
0
    def __init__(self, sample_rate, channels, to_sample_rate, to_channels):
        super(Resample, self).__init__()

        self.resampler = sound.Resampler((sample_rate, channels),
                                         (to_sample_rate, to_channels))
Exemple #10
0
    def __init__(self, from_sample_rate, from_channels, to_sample_rate,
                 to_channels):
        super(RawResampleTo, self).__init__()

        self.resampler = sound.Resampler((from_sample_rate, from_channels),
                                         (to_sample_rate, to_channels))
Exemple #11
0
    def aud(self, queue):
        self.snd = None
        resampler = None
        ac = None
        print 'VPlayer: Audio thread has started'
        try:
            while 1:
                d = queue.get()
                if type(d) == str:
                    if d == 'SEEK':
                        if ac: ac.reset()
                        #if self.snd:
                        #  self.snd.stop()
                    if d == 'STOP':
                        break
                    if d == 'START':
                        params = queue.get()
                        ac = acodec.Decoder(params)
                        self.snd = None
                        self.aDelta = 0
                    continue

                # Skip queue items when not playing or some errors detected
                if self.playingFile == None or len(self.err) > 0:
                    if self.snd:
                        self.snd.stop()
                        self.snd = None
                    continue
                if ac == None:
                    print 'Audio codec is not set. Need to run START first.'
                    continue

                afr = ac.decode(d[1])
                if self.snd == None and afr:
                    self.aBitRate = afr.bitrate
                    print 'Sound: ', afr.sample_rate, afr.channels, afr.bitrate
                    try:
                        # Hardcoded S16 ( 16 bit signed ) for now
                        self.snd = sound.Output(afr.sample_rate, afr.channels,
                                                0x10)
                        resampler = None
                    except:
                        # Fallback to 2 channels
                        try:
                            resampler = sound.Resampler(
                                (afr.sample_rate, afr.channels),
                                (afr.sample_rate, 2))
                            self.snd = sound.Output(afr.sample_rate, 2, 0x10)
                        except:
                            self.err.append(sys.exc_info()[1])
                            continue

                if afr:
                    s = afr.data
                    if resampler:
                        s = resampler.resample(s)

                    if d[3] > 0:
                        # set correction factor in case of PTS presence
                        self.aDelta = (
                            float(d[3]) / 90000
                        ) - self.snd.getPosition() - self.snd.getLeft()
                        #print 'AUDIO: ', d[3], self.aDelta, queue.qsize(), self.snd.getLeft()

                    # See if we have anything to play
                    if len(s) > 0 and self.seek == 0:
                        self.snd.play(s)
        except:
            traceback.print_exc()

        print 'VPlayer: Audio thread has stopped'
Exemple #12
0
  def aud( self, queue ):
    self.snd= None
    resampler= None
    ac= None
    tstsDelay=stsDelay=lastAPTS  = Dlinna=0
    print 'VPlayer: Audio thread has started'

    try: 
      while 1:
        d= queue.get()
        if type( d )== str:
          if d== 'SEEK':
            if ac: ac.reset()
            #if self.snd:
            #  self.snd.stop()
          if d== 'STOP':
            break
          if d== 'START':
            params= queue.get()
            ac= acodec.Decoder( params )
            self.snd= None
            self.aDelta= 0
          continue
          
        # Skip queue items when not playing or some errors detected
        if self.playingFile== None or len( self.err )> 0:
          if self.snd:
            self.snd.stop()
            self.snd= None
          continue
        if ac== None:
          print 'Audio codec is not set. Need to run START first.'
          continue
        
        afr= ac.decode( d[ 1 ] )
        if self.snd== None and afr:
          self.aBitRate= afr.bitrate
          print 'Sound: ', afr.sample_rate, afr.channels, afr.bitrate
          try:
            # Hardcoded S16 ( 16 bit signed ) for now
            self.snd= sound.Output( afr.sample_rate, afr.channels, 0x10 )
            resampler= None
          except:
            # Fallback to 2 channels
            try:
              resampler= sound.Resampler( (afr.sample_rate,afr.channels), (afr.sample_rate,2) )
              self.snd= sound.Output( afr.sample_rate, 2, 0x10 )
            except:
              self.err.append( sys.exc_info()[1] )
              continue        
        if afr:
          s= afr.data
          if resampler:
            s= resampler.resample( s )
          # See if we have anything to play
        if len( s ) > 0 and self.seek== 0:
            
            
            if lastAPTS == 0 : lastAPTS = time.time()
            stsDelay =  float(tstsDelay - time.time()+lastAPTS)
            print 'APTS', self.getAPTS(), time.time()- lastAPTS,float(len(s)/(2*2)), float(len(s)/(2*2))/afr.sample_rate,tstsDelay,stsDelay
            if (stsDelay > 0) : time.sleep (stsDelay)
            self.snd.play( s )
            tstsDelay = tstsDelay+ float(len(s)/(2*2))/afr.sample_rate
    except:
      traceback.print_exc()
        
    print 'VPlayer: Audio thread has stopped'