bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
680 stars 69 forks source link

Receiving single channel 16 bit audio, how to nicely play through a player object? #119

Closed JacobAsmuth closed 3 years ago

JacobAsmuth commented 3 years ago

My current code looks like this:

  def play_audio(self):
          with self.default_speaker.player(consts.SAMPLE_RATE) as player:
              while True:
                  try:
                      samples = self.voice_buffer.get_samples()
                      if samples is not None:
                          np_samples = np.frombuffer(samples, dtype=np.int16).astype(np.float32)
                          player.play(np_samples)
                  except Exception as e:
                      print("Error playing audio: %s" %  (e,))

'samples' is a one-dimensional byte array of 16 bit samples reprsenting a single audio channel. If I use scipy to save 'samples' as a .wav file, it sounds completely correct, and I can also play samples using PyAudio and it works. This makes me confident that my data represents valid audio. However, Running the above code produces pure static through my speakers. Any ideas?