问题 使用Apple的新AudioEngine来改变AudioPlayer声音的音高


我目前正试图让Apple的新音频引擎与我目前的音频设置配合使用。具体来说,我试图用音频引擎改变音高,这显然是可能的 这个帖子

我还研究过其他改变音高的解决方案,包括Dirac和ObjectAL,但不幸的是,在使用Swift时,两者似乎都很混乱。

我的问题是如何使用Apple的新音频引擎更改音频文件的音高。我能够使用AVAudioPlayer播放声音,但我没有得到如何在audioEngine中引用该文件。在链接页面上的代码中有一个引用音频文件的“格式”,但我没有得到如何创建格式或它的作用。

我用这个简单的代码播放声音:

let path = NSBundle.mainBundle().pathForResource(String(randomNumber), ofType:"m4r")
let fileURL = NSURL(fileURLWithPath: path!)
player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
player.prepareToPlay()
player.play()

7015
2017-09-06 21:28


起源



答案:


您使用AVAudioPlayerNode,而不是AVAudioPlayer。

engine = AVAudioEngine()
playerNode = AVAudioPlayerNode()
engine.attachNode(playerNode)

然后你可以附加一个AVAudioUnitTimePitch。

var mixer = engine.mainMixerNode;
auTimePitch = AVAudioUnitTimePitch()
auTimePitch.pitch = 1200 // In cents. The default value is 1.0. The range of values is -2400 to 2400
auTimePitch.rate = 2 //The default value is 1.0. The range of supported values is 1/32 to 32.0.
engine.attachNode(auTimePitch)
engine.connect(playerNode, to: auTimePitch, format: mixer.outputFormatForBus(0))
engine.connect(auTimePitch, to: mixer, format: mixer.outputFormatForBus(0))

14
2017-09-09 11:37



对我来说完全是无稽之谈。我在哪里告诉我要播放的文件? - charlesrockbass
scheduleFile是要使用的功能。 developer.apple.com/library/ios/documentation/AVFoundation/... - Gene De Lisa


答案:


您使用AVAudioPlayerNode,而不是AVAudioPlayer。

engine = AVAudioEngine()
playerNode = AVAudioPlayerNode()
engine.attachNode(playerNode)

然后你可以附加一个AVAudioUnitTimePitch。

var mixer = engine.mainMixerNode;
auTimePitch = AVAudioUnitTimePitch()
auTimePitch.pitch = 1200 // In cents. The default value is 1.0. The range of values is -2400 to 2400
auTimePitch.rate = 2 //The default value is 1.0. The range of supported values is 1/32 to 32.0.
engine.attachNode(auTimePitch)
engine.connect(playerNode, to: auTimePitch, format: mixer.outputFormatForBus(0))
engine.connect(auTimePitch, to: mixer, format: mixer.outputFormatForBus(0))

14
2017-09-09 11:37



对我来说完全是无稽之谈。我在哪里告诉我要播放的文件? - charlesrockbass
scheduleFile是要使用的功能。 developer.apple.com/library/ios/documentation/AVFoundation/... - Gene De Lisa