使用Apple的新AudioEngine更改AudioPlayer声音的音高 [英] Using Apple's new AudioEngine to change Pitch of AudioPlayer sound
问题描述
我目前正在尝试使Apple的新音频引擎与我当前的音频设置一起使用.具体来说,我正在尝试使用音频引擎更改音高,根据
I am currently trying to get Apple's new audio engine working with my current audio setup. Specifically, I am trying to change the pitch with Audio Engine, which apparently is possible according to this post.
我还研究了其他音高变化解决方案,包括Dirac和ObjectAL,但不幸的是,就我正在使用的Swift而言,两者似乎都搞砸了.
I have also looked into other pitch changing solutions including Dirac and ObjectAL, but unfortunately both seem to be pretty messed up in terms of working with Swift, which I am using.
我的问题是如何使用Apple的新音频引擎更改音频文件的音高.我可以使用AVAudioPlayer播放声音,但无法获得如何在audioEngine中引用该文件.链接页面上的代码中有一个格式",它指向音频文件,但是我没有得到如何创建格式或它的功能.
My question is how do I change the pitch of an audio file using Apple's new audio engine. I am able to play sounds using AVAudioPlayer, but I am not getting how the file is referenced in audioEngine. In the code on the linked page there is a 'format' that refers to audio file, but I am not getting how to create a format, or what it does.
我正在用以下简单代码播放声音:
I am playing sounds with this simple code:
let path = NSBundle.mainBundle().pathForResource(String(randomNumber), ofType:"m4r")
let fileURL = NSURL(fileURLWithPath: path!)
player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
player.prepareToPlay()
player.play()
推荐答案
您使用的是AVAudioPlayerNode,而不是AVAudioPlayer.
You use an AVAudioPlayerNode, not an AVAudioPlayer.
engine = AVAudioEngine()
playerNode = AVAudioPlayerNode()
engine.attachNode(playerNode)
然后,您可以附加一个AVAudioUnitTimePitch.
Then you can attach an 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))
这篇关于使用Apple的新AudioEngine更改AudioPlayer声音的音高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!