Jeff
Jeff

Reputation: 1004

NumPy array holding .wav audio data for sounddevice

I would like to use sounddevice's playrec feature. To start I would like to just get sd.play() to work, I am new to Python and have never worked with NumPy, I have gotten audio to play using pyaudio, but I need the simultaneous play record feature in sounddevice. When I try to play an audio .wav file I get: TypeError: Unsupported data type: 'string288'. I think it has something to do with having to store the .wav file in a numpy array, but I have no idea how to do that. Here is what I have:

import sounddevice as sd
import numpy as np

sd.default.samplerate = 44100
sd.play('test.wav')
sd.wait

Upvotes: 0

Views: 2830

Answers (1)

PatriceG
PatriceG

Reputation: 4063

The documentation of sounddevice.play() says:

sounddevice.play(data, samplerate=None, mapping=None, blocking=False, loop=False, **kwargs)

where data is an "array-like".

It can't work with an audio file name, as you tried. The audio file has first to be read, and interpreted as a numpy array. This code should work:

data, fs = sf.read(filename, dtype='float32')
sd.play(data, fs)

You'll find more examples here.

Upvotes: 2

Related Questions