簡體   English   中英

pyaudio錄音python

[英]pyaudio audio recording python

我正在嘗試使用 Python 從麥克風錄制音頻。 我有以下代碼:

import pyaudio
import wave
import threading

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
WAVE_OUTPUT_FILENAME = "file.wav"

stop_ = False
audio = pyaudio.PyAudio()

stream = audio.open(format=FORMAT, channels=CHANNELS,
                    rate=RATE, input=True,
                    frames_per_buffer=CHUNK)


def stop():
    global stop_
    while True:
        if not input('Press Enter >>>'):
            print('exit')
            stop_ = True


t = threading.Thread(target=stop, daemon=True).start()
frames = []

while True:
    data = stream.read(CHUNK)
    frames.append(data)
    if stop_:
        break

stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()

我的代碼工作正常,但是當我播放我的錄音時,我在最終輸出文件 ( file.wav ) 中聽不到任何聲音。

為什么這里會出現問題,我該如何解決?

您的代碼運行良好。 您面臨的問題是由於管理員權限。 音頻文件的數據為常量 0,因此,您無法在生成的 wav 文件中收聽聲音。 我想您的麥克風設備已安裝並正常工作。 如果您不確定音頻安裝狀態,則根據操作系統執行以下步驟:

MAC OS:系統偏好設置->聲音->輸入,在那里您可以將條形可視化為發出聲音。 確保所選設備類型為內置。

在此處輸入圖片說明

Windos OS:聲音設置和測試麥克風通過單擊收聽此設備,您可以稍后取消選中它,因為它會將您的聲音回傳到揚聲器並會產生很大的噪音。

在此處輸入圖片說明

很可能您正在使用 Mac OS。 我遇到了類似的問題,因為我使用 Atom 編輯器來運行 python 代碼。 嘗試從 Mac OS 的終端(或 Power Shell,如果您使用的是 Windows)運行您的代碼,(如果出現用於在 Mac OS 上訪問麥克風的彈出窗口,請按 OK)。 就是這樣! 您的代碼將記錄良好。 作為測試人員,請運行下面的代碼以檢查您是否可以可視化聲音,並確保通過終端(無編輯器或 IDE)運行它。

import queue
import sys
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import numpy as np
import sounddevice as sd

# Lets define audio variables
# We will use the default PC or Laptop mic to input the sound

device = 0 # id of the audio device by default
window = 1000 # window for the data
downsample = 1 # how much samples to drop
channels = [1] # a list of audio channels
interval = 30 # this is update interval in miliseconds for plot

# lets make a queue
q = queue.Queue()
# Please note that this sd.query_devices has an s in the end.
device_info =  sd.query_devices(device, 'input')
samplerate = device_info['default_samplerate']
length  = int(window*samplerate/(1000*downsample))

# lets print it 
print("Sample Rate: ", samplerate)

# Typical sample rate is 44100 so lets see.

# Ok so lets move forward

# Now we require a variable to hold the samples 

plotdata =  np.zeros((length,len(channels)))
# Lets look at the shape of this plotdata 
print("plotdata shape: ", plotdata.shape)
# So its vector of length 44100
# Or we can also say that its a matrix of rows 44100 and cols 1

# next is to make fig and axis of matplotlib plt
fig,ax = plt.subplots(figsize=(8,4))

# lets set the title
ax.set_title("PyShine")

# Make a matplotlib.lines.Line2D plot item of color green
# R,G,B = 0,1,0.29

lines = ax.plot(plotdata,color = (0,1,0.29))

# We will use an audio call back function to put the data in queue

def audio_callback(indata,frames,time,status):
    q.put(indata[::downsample,[0]])

# now we will use an another function 
# It will take frame of audio samples from the queue and update
# to the lines

def update_plot(frame):
    global plotdata
    while True:
        try: 
            data = q.get_nowait()
        except queue.Empty:
            break
        shift = len(data)
        plotdata = np.roll(plotdata, -shift,axis = 0)
        # Elements that roll beyond the last position are 
        # re-introduced 
        plotdata[-shift:,:] = data
    for column, line in enumerate(lines):
        line.set_ydata(plotdata[:,column])
    return lines
ax.set_facecolor((0,0,0))
# Lets add the grid
ax.set_yticks([0])
ax.yaxis.grid(True)

""" INPUT FROM MIC """

stream  = sd.InputStream( device = device, channels = max(channels), samplerate = samplerate, callback  = audio_callback)


""" OUTPUT """      

ani  = FuncAnimation(fig,update_plot, interval=interval,blit=True)
with stream:
    plt.show()

將此文件作為 voice.py 保存到文件夾中(比如說 AUDIO)。 然后從終端命令 cd 到 AUDIO 文件夾,然后使用以下命令執行它:

python3語音.py

或者

蟒蛇語音.py

取決於你的 python env 名稱。

在此處輸入圖片說明

通過使用print(sd.query_devices()) ,我看到如下設備列表:

  1. Microsoft Sound Mapper - 輸入,MME(2 進,0 出)
  2. 麥克風(AudioHubNano2D_V1.5,MME(2進0出)
  3. 內置麥克風(科勝訊 S、MME(2 進,0 出)
  4. ...

但是,如果我使用device = 0 ,我仍然可以從設備號 1 的 USB 麥克風接收聲音。默認情況下,所有音頻信號都進入聲音映射器嗎? 這意味着如果我使用device = 0 ,我將從所有音頻輸入獲取所有音頻信號; 如果我只想從一個特定設備輸入音頻,我需要選擇它的編號 x 作為device = x

我還有一個問題:是否可以在一個應用程序中以不同的方式從設備 1 和 2 捕獲音頻信號?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM