Python Pyaudio+麦克风+实时过滤器

2024-06-15 15:16:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,我需要遵循三个先决条件

  1. 使用麦克风进行实时录音
  2. 实时收听计算机扬声器输出的麦克风
  3. 通过在流的执行期间执行带通滤波器,对此流进行修改

我仍然找不到在回调执行期间操纵音频的任何示例,我相信这个过程因可能的操纵而被阻止。我不是python专家。谁能给我指路吗

目前,我已经有了麦克风,可以在启动脚本时听到声音

# -*- coding: utf-8 -*- 

import pyaudio

FORMATO = pyaudio.paInt16 # Pode ser pyaudio.paFloat32 também
CHUNK = 1024
WIDTH = 2
CANAL = 1
RATE = 44100
FRAMESWAV = []
p = pyaudio.PyAudio()


def loopback(in_data, frame_count, time_info, status):                      
    return (in_data, pyaudio.paContinue)
            


# Abrindo Canal para ouvir o microfone.          
stream = p.open(format=p.get_format_from_width(WIDTH), channels=CANAL, rate=RATE, 
            input=True, 
            output=True, 
            frames_per_buffer=CHUNK,
            stream_callback=loopback)

stream.start_stream()   

while True:    
    
    i = input("Pressione Enter SAIR")
    if not i:    
        stream.stop_stream()
        stream.close()        
        p.terminate()        
        print("Voce pressionou para SAIR.")
        break
    else:   
        continue

Tags: 项目intrueformatinputdatastreamrate
1条回答
网友
1楼 · 发布于 2024-06-15 15:16:55

我有一个类似的问题,我发现这篇论文对我很有帮助。下面是代码的一部分,讨论如何收集、处理数据,然后将数据传输回演讲者

# define a pass through, y = x, callback
def callback(in_data, frame_length, time_info,status):

globalDSP_IO, b, a, zi#no widgets yet
DSP_IO.DSP_callback_tic()#log entering time
# convert audio byte data to an int16 ndarray
in_data_nda = np.frombuffer(in_data,dtype=np.int16)
#***********************************************# 
Begin DSP operations here
# for this app cast int16 to float32
x = in_data_nda.astype(float32)
y = x # pass input to output
# Typically more DSP code here
# Optionally apply a linear filter to the input
#y, zi = signal.lfilter(b,a,x,zi=zi)
#***********************************************
# Save data for later analysis
# accumulate a new frame of samples if enabled
# with Tcapture
DSP_IO.DSP_capture_add_samples(y)
#***********************************************
# Convert from float back to int16
y = y.astype(int16)
DSP_IO.DSP_callback_toc()#log departure time
# Convert ndarray back to bytes
return y.tobytes(), pah.pyaudio.paContinue

Real-Time Digital Signal Processing Usingpyaudio_helper and the ipywidgets

希望这对你也有帮助

相关问题 更多 >