0

I have a script that reads an audio media file with pydub and then outputs it to a pyaudio stream. Using the code below it plays through super quickly and I only hear snippets of the audio. I believe the issue is that pydub using milliseconds and pyaudio wanting frames.

  def _load_media(self, media_location) :
    self.media = AudioSegment.from_file(media_location)
    self.stream = self.p.open( format=self.p.get_format_from_width(self.media.sample_width),
                          channels=self.media.channels,
                          rate=self.media.frame_rate,
                          output=True,
                          stream_callback=self._load_frames)
    self.media_loaded = True

  def _load_frames(self, in_data, frame_count, time_info, status) :
    data = self.media[:frame_count].raw_data
    self.media = self.media[frame_count:]
     
    return (data, pyaudio.paContinue)
Tom Haines
  • 21
  • 5

1 Answers1

0

I had the same problem as you, and figured it out. You had the right idea, pyDub is using ms where pyAudio feeds it frames. You just need to convert it like so:

def _load_frames(self, in_data, frame_count, time_info, status) :

    time = ( frame_count / self.media.frame_rate ) * 1000.0

    data = self.media[:time].raw_data
    self.media = self.media[time:]

    return (data, pyaudio.paContinue)