6

How can I convert an audio sample that came out of pyaudio, which is a bytes object, into a numpy array of signed-int16 samples?

ArekBulski
  • 4,520
  • 4
  • 39
  • 61
  • 5
    you can use `np.frombuffer`. do you want to combine two bytes into int16 or one int16 for each byte? first case use `.view`. second case use `.astype`- I think you can even specify the dtype in `frombuffer` but not sure. That would work in the first case. – Paul Panzer Feb 16 '17 at 14:45
  • 5
    Solution: `numpy.frombuffer(x, numpy.int16)` – ArekBulski Feb 16 '17 at 14:53
  • 1
    Alternatively, you could use the [sounddevice](http://python-sounddevice.readthedocs.io/) module, which directly supports NumPy arrays and does the conversion for you if you specify `dtype='int16'`. By default it uses `dtype='float32'`, which is useful if you want to do further processing on the audio signals. – Matthias Feb 17 '17 at 08:21

0 Answers0