虽然 Novocaine 允许您轻松地读取/播放音频,但仍然相当难将滤镜应用于音频。这个(Objective-C++)类将允许您通过几行代码应用各种类型的滤镜(高通滤波器、带通滤波器、峰值均衡器、 shelving 均衡器等)。
NVDSP 包含多种音频滤镜
为了开始,我建议您获取一份 Novocaine 的新副本,并打开 Novocaine 优秀的示例项目。然后导入 NVDSP 和 Filters 文件夹,开始您的滤波之旅。
// ... import Novocaine and audioFilerReader
#import "NVDSP/NVDSP.h"
#import "NVDSP/Filters/NVHighpassFilter.h"
// init Novocaine audioManager
audioManager = [Novocaine audioManager];
float samplingRate = audioManager.samplingRate;
// init fileReader which we will later fetch audio from
NSURL *inputFileURL = [[NSBundle mainBundle] URLForResource:@"Trentemoller-Miss-You" withExtension:@"mp3"];
fileReader = [[AudioFileReader alloc]
initWithAudioFileURL:inputFileURL
samplingRate:audioManager.samplingRate
numChannels:audioManager.numOutputChannels];
// setup Highpass filter
NVHighpassFilter *HPF;
HPF = [[NVHighpassFilter alloc] initWithSamplingRate:samplingRate];
HPF.cornerFrequency = 2000.0f;
HPF.Q = 0.5f;
// setup audio output block
[fileReader play];
[audioManager setOutputBlock:^(float *outData, UInt32 numFrames, UInt32 numChannels) {
[fileReader retrieveFreshAudio:outData numFrames:numFrames numChannels:numChannels];
[HPF filterData:outData numFrames:numFrames numChannels:numChannels];
}];
NVPeakingEQFilter *PEF = [[NVPeakingEQFilter alloc] initWithSamplingRate:audioManager.samplingRate];
PEF.centerFrequency = 1000.0f;
PEF.Q = 3.0f;
PEF.G = 20.0f;
[PEF filterData:data numFrames:numFrames numChannels:numChannels];
// import Novocaine.h and NVDSP.h
#import "NVDSP/Filter/NVLowpassFilter.h"
NVLowpassFilter *LPF = [[NVLowpassFilter alloc] initWithSamplingRate:audioManager.samplingRate];
LPF.cornerFrequency = 800.0f;
LPF.Q = 0.8f;
[LPF filterData:data numFrames:numFrames numChannels:numChannels];
// import Novocaine.h and NVDSP.h
#import "NVDSP/Filter/NVNotchFilter.h"
NVNotchFilter *NF = [[NVNotchFilter alloc] initWithSamplingRate:audioManager.samplingRate];
NF.centerFrequency = 3000.0f;
NF.Q = 0.8f;
[NF filterData:data numFrames:numFrames numChannels:numChannels];
有两种类型的带通滤波器
* 0 dB gain bandpass filter (NVBandpassFilter.h)
* Peak gain Q bandpass filter (NVBandpassQPeakGainFilter.h)
// import Novocaine.h and NVDSP.h
#import "NVDSP/Filter/NVBandpassFilter.h"
NVBandpassFilter *BPF = [[NVBandpassFilter alloc] initWithSamplingRate:audioManager.samplingRate];
BPF.centerFrequency = 2500.0f;
BPF.Q = 0.9f;
[BPF filterData:data numFrames:numFrames numChannels:numChannels];
// import Novocaine.h and NVDSP.h
#import "NVDSP/Utilities/NVSoundLevelMeter.h"
NVSoundLevelMeter *SLM = [[NVSoundLevelMeter alloc] init];
float dB = [SLM getdBLevel:outData numFrames:numFrames numChannels:numChannels];
NSLog(@"dB level: %f", dB);
// NSLogging in an output loop will most likely cause hickups/clicky noises, but it does log the dB level!
// To get a proper dB value, you have to call the getdBLevel method a few times (it has memory of previous values)
// You call this inside the input or outputBlock: [audioManager setOutputBlock:^...
所有样本值(通常在不削波时为 -1.0f .. 1.0f)都乘以增益值。
// import Novocaine.h and NVDSP.h
NVDSP *generalDSP = [[NVDSP alloc] init];
[generalDSP applyGain:outData length:numFrames*numChannels gain:0.8];
多个具有高增益的峰值均衡器可能导致削波。削波基本上是超出 1.0f 或 -1.0f 的最大或最小值的样本数据。削波将产生非常响亮和脏乱的噪声,就像坏过载效果一样。您可以使用 counterClipping
方法来防止削波(它将降低音量水平)。
// import Novocaine.h and NVDSP.h
#import "NVDSP/Utilities/NVClippingDetection.h"
NVClippingDetection *CDT = [[NVClippingDetection alloc] init];
// ... possible clipped outData ...//
[CDT counterClipping:outData numFrames:numFrames numChannels:numChannels];
// ... outData is now safe ...//
// or get the amount of clipped samples:
- (float) getClippedSamples:(float *)data numFrames:(UInt32)numFrames numChannels:(UInt32)numChannels;
// or get the percentage of clipped samples:
- (float) getClippedPercentage:(float*)data numFrames:(UInt32)numFrames numChannels:(UInt32)numChannels;
// or get the maximum value of a clipped sample that was found
- (float) getClippingSample:(float *)data numFrames:(UInt32)numFrames numChannels:(UInt32)numChannels;
请参阅 /Examples/NVDSPExample
了解一个简单的iOS XCodeProject示例。请注意,其中的Novocaine可能过时。
NVDSP类是用C++编写的,因此使用它的类将需要是Objective-C++。将所有使用NVDSP的文件从MyClass.m更改为MyClass.mm。
iHearYou Swarmy发送pull请求并将您的应用程序添加到此列表!
请查看此篇Medium博客
Alex Wiltschko - Novocaine的创造者
Yasoshima - 这篇文章的作者,揭示了vDSP_deq22的工作原理。(以及谷歌翻译,我不会说日语)
hrnt - 在IRC #iphonedev(freenode)上提供帮助