2016-08-10 53 views
1

我正在嘗試創建一個程序來一次回話。我似乎無法得到它的工作。有些網站說使用numpy數組,但我不知道如何。同時使用pyaudio播放和錄製聲音

import pyaudio 
import wave 
import time 
import multiprocessing as mp 
import pyaudio 
import numpy as np 
import sounddevice as sd 

fs = 44100 
FORMAT = pyaudio.paInt16 
CHANNELS = 2 
RATE = 44100 
CHUNK = 1024 
audio = pyaudio.PyAudio() 
RECORD_SECONDS = 5 
stream = audio.open(format=FORMAT, channels=CHANNELS, 
       rate=RATE, input=True, 
       frames_per_buffer=CHUNK) 
myarray = [] 
for i in range(0, int(RATE/CHUNK * RECORD_SECONDS)): 
    data = stream.read(CHUNK) 
    myarray.append(data) 

    myrecording = sd.play(myarray, fs, channels=2) 

Traceback (most recent call last): File "SoundTest.py", line 24, in myrecording = sd.play(myarray, fs, channels=2) line 2170, in check_data dtype = _check_dtype(data.dtype) File "/home/lordvile/.local/lib/python2.7/site-packages/sounddevice.py", line 2316, in _check_dtype raise TypeError('Unsupported data type: ' + repr(dtype)) TypeError: Unsupported data type: 'string32768'

+0

什麼是不workng?說明。 – Michael

+0

@Michael我真的不知道。這是錯誤。我刪除了幾行,因爲它不適合...回溯(最近呼叫最後): 文件「SoundTest.py」,第24行,在 myrecording = sd.play(myarray,fs,channels = 2) line 2170,in check_data dtype = _check_dtype(data.dtype) 文件「/home/lordvile/.local/lib/python2.7/site-packages/sounddevice.py」,第2316行,在_check_dtype中 raise TypeError('Unsupported數據類型:'+ repr(dtype)) TypeError:不支持的數據類型:'string32768' –

+0

@SubhadityaMukherjee請用您的Tracebak更新您的問題。 –

回答

1

不知道,如果你有需要使用pyaudio,但這裏是使用sounddevice一個例子。

下面的示例根據變量記錄來自麥克風的音頻,您可以根據您的要求進行修改。

使用標準音頻輸出(揚聲器)播放相同的內容。

更多關於此here

工作守則

import sounddevice as sd 
import numpy as np 
import scipy.io.wavfile as wav 

fs=44100 
duration = 10 # seconds 
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2, dtype='float64') 
print "Recording Audio for %s seconds" %(duration) 
sd.wait() 
print "Audio recording complete , Playing recorded Audio" 
sd.play(myrecording, fs) 
sd.wait() 
print "Play Audio Complete" 

輸出

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> 
Recording Audio for 10 seconds 
Audio recording complete , Playing recorded Audio 
Play Audio Complete 
>>>