美文网首页
Apple AudioUnit Effect

Apple AudioUnit Effect

作者: 程序员都是傻子呀 | 来源:发表于2020-03-14 10:14 被阅读0次
import Foundation
import AudioToolbox

/// Effect
/// Handles applying Core Audio effects to the sound generated by `Synthesizer`s
public enum Effect {
    /// Highpass
    /// Cuts off frequencies higher than the specified amount
    /// - cutoff: the frequency in Hertz to cutoff at
    case highPass(cutoff: Float32)
    /// Lowpass
    /// Cuts off frequencies lower than the specified amount
    /// - cutoff: the frequency in Hertz to cutoff at
    case lowPass(cutoff: Float32)
    /// Delay
    /// Repeats the audio similar to an echo
    /// - delayTime: the time in seconds after at which the first delay is heard
    /// - dryWetMix: the mix from 0 to 100 of "wet" and "dry" delay
    /// - feedback: the amount from 0 to 100 that each delay should feedback into the next one
    case delay(delayTime: Float32, wetDryMix: Float32, feedback: Float32)
    /// Reverb
    /// Causes audio to "linger", giving the effect of playing sound in a large space
    /// - smallLargeMix: the mix from 0 to 100 of the small and large reverbs
    /// - dryWetMix: the mix from 0 to 100 of "wet" and "dry" delay
    /// - preDelay: the time in seconds for the pre delay
    case reverb(smallLargeMix: Float32, dryWetMix: Float32, preDelay: Float32)
    /// Distortion
    /// Distorts the sound
    /// - decimation: the amount from 0 to 100 that the sound should be decimated by
    /// - decimationMix: the mix from 0 to 100 of the decimated sound
    /// - ringModMix: the mix from 0 to 100 of the ring modulation effect
    /// - finalMix: the mix from 0 to 100 of the original sound to the distorted sound
    case distortion(decimation: Float32, decimationMix: Float32, ringModMix: Float32, finalMix: Float32)
    
    var description: AudioComponentDescription {
        switch self {
        case .highPass:
            return AudioComponentDescription(componentType: kAudioUnitType_Effect,
                                             componentSubType: kAudioUnitSubType_HighPassFilter,
                                             componentManufacturer: kAudioUnitManufacturer_Apple,
                                             componentFlags: 0, componentFlagsMask: 0)
        case .lowPass:
            return AudioComponentDescription(componentType: kAudioUnitType_Effect,
                                             componentSubType: kAudioUnitSubType_LowPassFilter,
                                             componentManufacturer: kAudioUnitManufacturer_Apple,
                                             componentFlags: 0, componentFlagsMask: 0)
        case .delay:
            return AudioComponentDescription(componentType: kAudioUnitType_Effect,
                                             componentSubType: kAudioUnitSubType_Delay,
                                             componentManufacturer: kAudioUnitManufacturer_Apple,
                                             componentFlags: 0, componentFlagsMask: 0)
        case .reverb:
            return AudioComponentDescription(componentType: kAudioUnitType_Effect,
                                             componentSubType: kAudioUnitSubType_MatrixReverb,
                                             componentManufacturer: kAudioUnitManufacturer_Apple,
                                             componentFlags: 0, componentFlagsMask: 0)
        case .distortion:
            return AudioComponentDescription(componentType: kAudioUnitType_Effect,
                                             componentSubType: kAudioUnitSubType_Distortion,
                                             componentManufacturer: kAudioUnitManufacturer_Apple,
                                             componentFlags: 0, componentFlagsMask: 0)
        }
    }
    
    func setParameters(unit: inout AudioUnit) {
        switch self {
        case .highPass(let cutoff):
            AudioUnitSetParameter(unit, kHipassParam_CutoffFrequency, kAudioUnitScope_Global, 0, cutoff, 0)
        case .lowPass(let cutoff):
            AudioUnitSetParameter(unit, kLowPassParam_CutoffFrequency, kAudioUnitScope_Global, 0, cutoff, 0)
        case .delay(let delayTime, let wetDryMix, let feedback):
            AudioUnitSetParameter(unit, kDelayParam_DelayTime, kAudioUnitScope_Global, 0, delayTime, 0)
            AudioUnitSetParameter(unit, kDelayParam_WetDryMix, kAudioUnitScope_Global, 0, wetDryMix, 0)
            AudioUnitSetParameter(unit, kDelayParam_Feedback, kAudioUnitScope_Global, 0, feedback, 0)
        case .reverb(let smallLargeMix, let dryWetMix, let preDelay):
            AudioUnitSetParameter(unit, kReverbParam_SmallLargeMix, kAudioUnitScope_Global, 0, smallLargeMix, 0)
            AudioUnitSetParameter(unit, kReverbParam_DryWetMix, kAudioUnitScope_Global, 0, dryWetMix, 0)
            AudioUnitSetParameter(unit, kReverbParam_PreDelay, kAudioUnitScope_Global, 0, preDelay, 0)
        case .distortion(let decimation, let decimationMix, let ringModMix, let finalMix):
            AudioUnitSetParameter(unit, kDistortionParam_Decimation, kAudioUnitScope_Global, 0, decimation, 0)
            AudioUnitSetParameter(unit, kDistortionParam_DecimationMix, kAudioUnitScope_Global, 0, decimationMix, 0)
            AudioUnitSetParameter(unit, kDistortionParam_RingModMix, kAudioUnitScope_Global, 0, ringModMix, 0)
            AudioUnitSetParameter(unit, kDistortionParam_FinalMix, kAudioUnitScope_Global, 0, finalMix, 0)

        }
    }
}

相关文章

  • Apple AudioUnit Effect

  • 音频采集

    音频采集 音频采集的方式 AudioUnit音频单元 AudioUnit总结 最底层 AVFoundation...

  • AudioUnit 框架详细解析

    1. AudioUnit框架详细解析(一) —— 基本概览2. AudioUnit框架详细解析(二) —— 关于A...

  • iOS使用AudioUnit/AudioQueue实现耳返功能

    首先理清思路我这边使用AudioUnit录音,AudioQueue播放1、创建AudioUnit对象,并初始化设置...

  • CoreAudio基础概念

    1.AudioUnit 在所有API中,AudioUnit延迟最短,使用最灵活.代价很复杂。 2.Audio Fi...

  • iOS AudioUnit 总结

    iOS AudioUnit 总结 iOS 的 AudioUnit 功能十分强大,使用图的形式连接各个节点,来实现我...

  • iOS音频-audioUnit总结

    在看LFLiveKit代码的时候,看到音频部分使用的是audioUnit做的,所以把audioUnit学习了一下。...

  • AudioUnit简介

    AudioUnit简介 AudioUnit这个名字取得还是比较形象的,它的主体就是一系列的unit,不同unit能...

  • AudioUnit

    转至 :http://www.qingpingshan.com/m/view.php?aid=386657 声音的...

  • AudioUnit

    在iOS平台上,所有的音频框架底层都是基于AudioUnit实现的。较高层次的音频框架包括:MediaPlayer...

网友评论

      本文标题:Apple AudioUnit Effect

      本文链接:https://www.haomeiwen.com/subject/godmshtx.html