gpt4 book ai didi

python - Python麦克风输入和低通滤镜效果实时

转载 作者:行者123 更新时间:2023-12-03 01:55:50 24 4
gpt4 key购买 nike

我正在尝试使用覆盆子pi接收麦克风输入,并添加一种效果,以对麦克风输入实时输出低通音频滤波器效果。他们那里有许多python音频库,例如声音pi / py音频。如果有人可以帮助我实现这一目标。

亲切的问候。

最佳答案

我没有树莓派,但是我已经在Windows上尝试了类似python的方法。我假设您可以修改此程序以适合您的需求。

为此,您将需要安装pyaudiopip install pyaudio
可以从here下载的python sounddevice
该程序:
记录麦克风的音频
用限制器限制音频
播放修改后的音频
绘制原始和修改后的音频

Python代码

from pyaudio import PyAudio, paContinue, paFloat32
from time import sleep
from numpy import array, random, arange, float32, float64, zeros
import matplotlib.pyplot as plt
import sounddevice as sd

################################### Constants ##################################

fs = 44100 # Hz
threshold = 0.8 # absolute gain
delay = 40 # samples
signal_length = 1 # second
release_coeff = 0.5555 # release time factor
attack_coeff = 0.5 # attack time factor
dtype = float32 # default data type
block_length = 1024 # samples

############################# Implementation of Limiter ########################

class Limiter:
def __init__(self, attack_coeff, release_coeff, delay, dtype=float32):
self.delay_index = 0
self.envelope = 0
self.gain = 1
self.delay = delay
self.delay_line = zeros(delay, dtype=dtype)
self.release_coeff = release_coeff
self.attack_coeff = attack_coeff

def limit(self, signal, threshold):
for i in arange(len(signal)):
self.delay_line[self.delay_index] = signal[i]
self.delay_index = (self.delay_index + 1) % self.delay

# calculate an envelope of the signal
self.envelope *= self.release_coeff
self.envelope = max(abs(signal[i]), self.envelope)

# have self.gain go towards a desired limiter gain
if self.envelope > threshold:
target_gain = (1+threshold-self.envelope)
else:
target_gain = 1.0
self.gain = ( self.gain*self.attack_coeff +
target_gain*(1-self.attack_coeff) )

# limit the delayed signal
signal[i] = self.delay_line[self.delay_index] * self.gain



################### Record Audio from Microphone #########################

print "Recording Audio"
signal = sd.rec(signal_length * fs, samplerate=fs, channels=1, dtype=dtype)
sd.wait()
print "Audio recording complete , Play Audio"



################################# Play the Audio ###############################

original_signal = array(signal, copy=True, dtype=dtype)
limiter = Limiter(attack_coeff, release_coeff, delay, dtype)

def callback(in_data, frame_count, time_info, flag):
if flag:
print("Playback Error: %i" % flag)
played_frames = callback.counter
callback.counter += frame_count
limiter.limit(signal[played_frames:callback.counter], threshold)
return signal[played_frames:callback.counter], paContinue

callback.counter = 0

pa = PyAudio()

stream = pa.open(format = paFloat32,
channels = 1,
rate = fs,
frames_per_buffer = block_length,
output = True,
stream_callback = callback)

while stream.is_active():
sleep(0.1)

stream.close()
pa.terminate()

############################## Plot results ####################################

plt.figure()
plt.plot(original_signal, color='green', label='original signal')
plt.plot(signal, color='red', label='limited signal')
plt.legend()
plt.show(block=True)
############################## End Program ####################################

终端输出
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
Audio recording complete , Play Audio
>>>

影像输出

Output Image

关于python - Python麦克风输入和低通滤镜效果实时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35764018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com