我的意思是每当我在电脑上播放任何音频时,我都希望低通滤波器实时过滤音频,并在音频放大器的左声道(我的低音炮)上传递该音频,高通滤波器将音频传递到我的音频放大器的右声道(高音扬声器)并同时在左右声道上播放它们。
我成功地使用 Python 创建了一个高通滤波器,但它在两个通道上播放,并且在播放之前必须将其保存到 wav。我希望它从我的计算机的音频中实时获取音频,然后将其实时转换为相应的右或左音频通道。
这是我在 stackoverflow 上找到的高通滤波器的代码:
import matplotlib.pyplot as plt
import numpy as np
import wave
import sys
import math
import conteib
fname = 'Lil Nas X Industry Baby Lyrics ft Jack Harlow.wav'
outname = 'filtered.wav'
cutOffFrequency = 400.0
def running_mean(x, windowSize):
um = np.um(np.insert(x, 0, 0))
return (um[windowSize:] - um[:-windowSize]) / windowSize
def interpret_wav(raw_bytes, n_frames, n_channels, sample_width, interleaved = True):
if sample_width == 1:
dtype = np.uint8 # unsigned char
elif sample_width == 2:
dtype = np.int16 # signed 2-byte short
else:
raise ValueError("Only supports 8 and 16 bit audio formats.")
channels = np.fromstring(raw_bytes, dtype=dtype)
if interleaved:
# channels are interleaved, i.e. sample N of channel M follows sample N of channel M-1 in raw data
channels.shape = (n_frames, n_channels)
channels = channels.T
else:
# channels are not interleaved. All samples from channel M occur before all samples from channel M-1
channels.shape = (n_channels, n_frames)
return channels
with conteib.closing(wave.open(fname,'rb')) as spf:
sampleRate = spf.getframerate()
ampWidth = spf.getsampwidth()
nChannels = spf.getnchannels()
nFrames = spf.getnframes()
signal = spf.readframes(nFrames*nChannels)
spf.close()
channels = interpret_wav(signal, nFrames, nChannels, ampWidth, True)
freqRatio = (cutOffFrequency/sampleRate)
N = int(math.sqrt(0.196196 + freqRatio**2)/freqRatio)
filtered = running_mean(channels[0], N).astype(channels.dtype)
wav_file = wave.open(outname, "w")
wav_file.setparams((1, ampWidth, sampleRate, nFrames, spf.getcomptype(), spf.getcompname()))
wav_file.writeframes(filtered.tobytes('C'))
wav_file.close()
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(65条)