简体   繁体   English

NumPy数组,保存SoundDevice的.wav音频数据

[英]NumPy array holding .wav audio data for sounddevice

I would like to use sounddevice's playrec feature. 我想使用sounddevice的playrec功能。 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. 首先,我想让sd.play()开始工作,我是Python的新手,并且从未与NumPy一起工作过,我已经获得了使用pyaudio播放音频的功能,但是我需要sounddevice中的同时播放记录功能。 When I try to play an audio .wav file I get: TypeError: Unsupported data type: 'string288'. 当我尝试播放音频.wav文件时,我得到:TypeError:不支持的数据类型:'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. 我认为这与必须将.wav文件存储在numpy数组中有关,但是我不知道如何执行此操作。 Here is what I have: 这是我所拥有的:

import sounddevice as sd
import numpy as np

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

The documentation of sounddevice.play() says: sounddevice.play()的文档说:

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. 首先必须读取音频文件,并将其解释为numpy数组。 This code should work: 此代码应工作:

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

You'll find more examples here . 您将在此处找到更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM