建立一个简单的均衡器 [英] Build a simple Equalizer
问题描述
我想使用AVAudioEngine
制作5频段音频均衡器(60Hz,230Hz,910Hz,4kHz,14kHz).我想通过垂直滑块让用户输入每个频段的增益,并据此调整正在播放的音频.我尝试使用AVAudioUnitEQ
来执行此操作,但是播放音频时没有听到任何区别.我试图对值进行硬编码以指定每个频率的增益,但是仍然无法正常工作.这是我的代码:
I would like to make a 5-band audio equalizer (60Hz, 230Hz, 910Hz, 4kHz, 14kHz) using AVAudioEngine
. I would like to have the user input gain per band through a vertical slider and accordingly adjust the audio that is playing. I tried using AVAudioUnitEQ
to do this, but I hear no difference when playing the audio. I tried to hardcode in values to specify a gain at each frequency, but it still does not work. Here is the code I have:
var audioEngine: AVAudioEngine = AVAudioEngine()
var equalizer: AVAudioUnitEQ!
var audioPlayerNode: AVAudioPlayerNode = AVAudioPlayerNode()
var audioFile: AVAudioFile!
// in viewDidLoad():
equalizer = AVAudioUnitEQ(numberOfBands: 5)
audioEngine.attach(audioPlayerNode)
audioEngine.attach(equalizer)
let bands = equalizer.bands
let freqs = [60, 230, 910, 4000, 14000]
audioEngine.connect(audioPlayerNode, to: equalizer, format: nil)
audioEngine.connect(equalizer, to: audioEngine.outputNode, format: nil)
for i in 0...(bands.count - 1) {
bands[i].frequency = Float(freqs[i])
}
bands[0].gain = -10.0
bands[0].filterType = .lowShelf
bands[1].gain = -10.0
bands[1].filterType = .lowShelf
bands[2].gain = -10.0
bands[2].filterType = .lowShelf
bands[3].gain = 10.0
bands[3].filterType = .highShelf
bands[4].gain = 10.0
bands[4].filterType = .highShelf
do {
if let filepath = Bundle.main.path(forResource: "song", ofType: "mp3") {
let filepathURL = NSURL.fileURL(withPath: filepath)
audioFile = try AVAudioFile(forReading: filepathURL)
audioEngine.prepare()
try audioEngine.start()
audioPlayerNode.scheduleFile(audioFile, at: nil, completionHandler: nil)
audioPlayerNode.play()
}
} catch _ {}
由于低频增益为-10,而高频增益为10,因此播放任何媒体时都应该有非常明显的差异.但是,当媒体开始播放时,其声音与未连接任何均衡器的情况下播放的声音相同.
Since the low frequencies have a gain of -10 and the high frequencies have a gain of 10, there should be a very noticeable difference when playing any media. However, when the media starts playing, it sounds the same as if played without any equalizer attached.
我不确定为什么会这样,但是我尝试了几种不同的方法进行调试.我以为这可能是功能的顺序,所以我尝试将其切换,以便在调整所有频段之后调用audioEngine.connect,但这也没有任何区别.
I'm not sure why this is happening, but I tried several different things to debug. I thought that it might be the order of the functions so I tried switching it so that audioEngine.connect is called after adjusting all of the bands, but that did not make a difference either.
我使用AVAudioUnitTimePitch
尝试了相同的代码,并且效果很好,所以我对为什么它不适用于AVAudioUnitEQ
感到震惊.
I tried this same code with using an AVAudioUnitTimePitch
, and it worked perfectly, so I am dumbfounded as to why it does not work with AVAudioUnitEQ
.
我不想为此项目使用任何第三方库或可可豆荚,我想单独使用AVFoundation
来实现.
I do not want to use any third-party libraries or cocoa pods for this project, I would like to do it using AVFoundation
alone.
任何帮助将不胜感激!
谢谢.
推荐答案
AVAudioUnitEQFilterParameters
浏览文档时,我注意到除bypass
之外,我已经弄乱了所有参数,并且似乎更改此标志可以修复所有问题!
AVAudioUnitEQFilterParameters
Looking through the documentation, I noticed that I had messed with all of the parameters except bypass
and it seems that changing this flag fixed everything!
因此,我认为这里的主要问题是,所提供的系统值(而不是程序员设置的值)一定不能绕过每个AVAudioUnitEQ频段.
So, I believe the main issue here is that each AVAudioUnitEQ band must not be bypassed by the provided system values rather than the values the programmer sets.
所以,我改变了
for i in 0...(bands.count - 1) {
bands[i].frequency = Float(freqs[i])
}
到
for i in 0...(bands.count - 1) {
bands[i].frequency = Float(freqs[i])
bands[i].bypass = false
bands[i].filtertype = .parametric
}
,一切开始起作用.此外,为了制作有效的均衡器,允许用户修改各个频率,每个频段的filtertype
应设置为.parametric
.
and everything started working. Furthermore, to make an effective equalizer that allows the user to modify individual frequencies the filtertype
for each band should be set to .parametric
.
我仍然不确定应该将bandwith设置为什么,但是我可能可以在线检查它,也可以将其弄乱,直到声音与其他均衡器应用程序匹配为止.
I am still unsure on what I should set the bandwith to, but I can probably check online for that or just mess with it until the sound matches a different equalizer application.
这篇关于建立一个简单的均衡器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!