gpt4 book ai didi

Swift:断开 AVAudioUnit 与播放 AVAudioEngine 的连接

转载 作者:行者123 更新时间:2023-11-28 07:27:21 26 4
gpt4 key购买 nike

我正在播放一个带有某些效果的音频文件。这是代码:

 engine = AVAudioEngine()
playerB = AVAudioPlayerNode()

playerB.volume = 0.5

let path = Bundle.main.path(forResource: "ukulele", ofType: "wav")!
let url = NSURL.fileURL(withPath: path)

let file = try? AVAudioFile(forReading: url)
buffer = AVAudioPCMBuffer(pcmFormat: file!.processingFormat, frameCapacity: AVAudioFrameCount(file!.length))!
try! file!.read(into: buffer)

reverb.loadFactoryPreset(AVAudioUnitReverbPreset.cathedral)
reverb.wetDryMix = 50

distortion.loadFactoryPreset(AVAudioUnitDistortionPreset.speechRadioTower)
distortion.wetDryMix = 25

let delay = AVAudioUnitDistortion()
delay.loadFactoryPreset(AVAudioUnitDistortionPreset.speechAlienChatter)
delay.wetDryMix = 25


engine.attach(playerB)
engine.attach(reverb)
engine.attach(distortion)
engine.attach(delay)
engine.attach(pitch)
engine.attach(speedControl)

engine.connect(playerB, to: pitch, format: nil)

engine.connect(pitch, to: speedControl, format: nil)
engine.connect(speedControl, to: reverb, format: nil)

engine.connect(reverb, to: distortion, format: nil)
engine.connect(distortion, to: engine.mainMixerNode, format: buffer.format)

playerB.scheduleBuffer(buffer, at: nil, options: AVAudioPlayerNodeBufferOptions.loops, completionHandler: nil)



engine.prepare()
try! engine.start()

我想要的是在特定操作发生时断开其中一个 AVAudioUnit。然而,在移除 AVAudioUnit 之后,播放器完全没有声音。

例如,如果我想删除reverb,代码是:engine.disconnectNodeOutput(reverb)但是在这条线运行之后,玩家就沉默了。

我做错了什么?我只是想删除一个已经添加的效果。

最佳答案

这是一条链条,您断开了其中一个链环。这是您的代码:

engine.connect(pitch, to: speedControl, format: nil)
engine.connect(speedControl, to: reverb, format: nil)
engine.connect(reverb, to: distortion, format: nil)
engine.connect(distortion, to: engine.mainMixerNode, format: buffer.format)

如果您移除混响节点,速度控制和失真之间就会出现空洞。没有什么能越过那个洞。你需要连接它们。

关于Swift:断开 AVAudioUnit 与播放 AVAudioEngine 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56124540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com