AudioKit中的Clicks / Distortion

时间:2018-01-05 18:57:40

标签: ios swift audiokit

当我添加一堆(20-40)样本同时播放和重叠时,它们会开始变形,然后一些挥动,振荡和点击开始发生。当样本正在播放应用程序崩溃时会发出类似的声音 - 听起来像一个突然,松脆的停止。

注意波纹在0:05到0:10之间开始;讨厌的点击从0:15左右开始。

Listen Here

我怎样才能让它更顺畅?我正在生成播放4-8秒.wav文件的AKPlayer对象(从4.1开始)。那些进入AKBoosters的AKBixers进入最终的AKMixer输出。

修改

许多PenAudioNodes插入AudioReceiver单例的混音器。

这是我的AudioReceiver单身人士:

class AudioReceiver {
    static var sharedInstance = AudioReceiver()
    private var audioNodes = [UUID : AudioNode]()
    private let mixer = AKMixer()
    private let queue = DispatchQueue(label: "audio-queue")

    //MARK: - Setup & Teardown

    init() {
        AudioKit.output = mixer //peakLimiter
        AudioKit.start()
    }

    //MARK: - Public

    func audioNodeBegan(_ message : AudioNodeMessage) {
        queue.async {
            var audioNode: AudioNode?
            switch message.senderType {
            case .pen:
                audioNode = PenAudioNode()
            case .home:
                audioNode = LoopingAudioNode(with: AudioHelper.homeLoopFile())
            default:
                break
            }
            if let audioNode = audioNode {
                self.audioNodes[message.senderId] = audioNode
                self.mixer.connect(input: audioNode.output)
                audioNode.start(message)
            }
        }
    }

    func audioNodeMoved(_ message : AudioNodeMessage) {
        queue.async {
            if let audioNode = self.audioNodes[message.senderId] {
                audioNode.update(message)
            }
        }
    }

    func audioNodeEnded(_ message : AudioNodeMessage) {
        queue.async {
            if let audioNode = self.audioNodes[message.senderId] {
                audioNode.stop(message)
            }
            self.audioNodes[message.senderId] = nil
        }
    }
}

这是我的PenAudioNode:

class PenAudioNode {
    fileprivate var mixer: AKMixer?
    fileprivate var playersBoosters = [AKPlayer : AKBooster]()
    fileprivate var finalOutput: AKNode?
    fileprivate let file: AKAudioFile = AudioHelper.randomBellSampleFile()

    //MARK: - Setup & Teardown

    init() {
        mixer = AKMixer()
        finalOutput = mixer!
    }
}

extension PenAudioNode: AudioNode {
    var output: AKNode {
        return finalOutput!
    }

    func start(_ message: AudioNodeMessage) {
    }

    func update(_ message: AudioNodeMessage) {
        if let velocity = message.velocity {
            let newVolume = Swift.min((velocity / 50) + 0.1, 1)
            mixer!.volume = newVolume
        }
        if let isClimactic = message.isClimactic, isClimactic {
            let player = AKPlayer(audioFile: file)
            player.completionHandler = { [weak self] in
                self?.playerCompleted(player)
            }
            let booster = AKBooster(player)
            playersBoosters[player] = booster
            booster.rampTime = 1
            booster.gain = 0
            mixer!.connect(input: booster)
            player.play()
            booster.gain = 1
        }
    }

    func stop(_ message: AudioNodeMessage) {
        for (_, booster) in playersBoosters {
            booster.gain = 0
        }
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 1) {
            self.mixer!.stop()
            self.output.disconnectOutput()
        }
    }

    private func playerCompleted(_ player: AKPlayer) {
        playersBoosters.removeValue(forKey: player)
    }
}

1 个答案:

答案 0 :(得分:0)

这听起来像是您没有释放对象,并且最终使音频引擎过载,图中连接了太多处理节点实例。特别是不释放AKBoosters会引起类似的问题。我无法真正确定您的代码在做什么,但是如果您在未正确释放对象的情况下生成对象,则会导致音频混乱。

您要尽可能节省对象,并确保使用的是基于AKNode的绝对最小数量的处理。

有多种调试方法,但是您可以首先打印出AVAudioEngine的当前状态:

AudioKit.engine.description

这将显示您在给定时刻在图中已连接的节点数。