サウンドファイルを再生してピッチを変更するプログラムをMac用に作成しようとしています。 は、私は多くのことができます以下の記事を見つけた:私はこれまでのところだのはここ Xcode 8 Swift 3 Pitch-altering soundsサウンドファイルを再生してピッチを適用する - Xcode 8 - Mac
は次のとおりです。
import Cocoa
import AVFoundation
let engine = AVAudioEngine()
let audioPlayerNode = AVAudioPlayerNode()
let changeAudioUnitTime = AVAudioUnitTimePitch()
var file = AVAudioFile()
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification)
{do
{
if let audioFileURL = Bundle.main.url(forResource: "/Volumes/.../file", withExtension: "m4v")
{
file = try AVAudioFile(forReading: audioFileURL)
audioPlayerNode.scheduleFile(file, at: nil, completionHandler: nil) // File is an AVAudioFile defined previously
}
}catch let error as NSError {
print(error.localizedDescription)
}
}
func setupAudioEngine()
{
engine.attach(audioPlayerNode)
engine.attach(changeAudioUnitTime)
engine.connect(audioPlayerNode, to: changeAudioUnitTime, format: nil)
engine.connect(changeAudioUnitTime, to: engine.outputNode, format: nil)
try? engine.start()
audioPlayerNode.play()
}
func hitSound(value: Float)
{
changeAudioUnitTime.pitch = value
}
@IBAction func lanch(_ sender: NSButton)
{
setupAudioEngine()
hitSound(value: 0)
}
@IBAction func stop(_ sender: NSButton)
{
exit(0)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
しかし、ハラス、それは動作しません。 2017-04-02 01:39:44.254866 AVAudioEngine [5508:159993] [中央] 54:エラー:> avae> AVAudioEngine.mm:283:AttachNode:エラーが発生しました。必要な条件は偽です!nodeimpl-> HasEngineImpl() 2017年4月2日01:39:44.255148 AVAudioEngine [5508:159993] [一般]に必要な条件は偽です!nodeimpl-> HasEngineImpl()
誰場合私を助けることができる、私は感謝します。
この行は、間違っているようだ:audioPlayerNode.scheduleFile(時ファイル、:nilに、completionHandler:ゼロ)。アプリをクラッシュさせることがあります。 – Fredo
このコードではファイルを再生していますが、ピッチは適用されません。 applicationDidFinishLaunching(_ aNotification:通知) { レット・パス= "/Volumes/.../Musique/Hind/Musique indienne.m4a" URL = URL(fileURLWithPath:パスを)聞かせて { オーディオファイルは= AVAudioFileを試しください(forReading:url、commonFormat:AVAudioCommonFormat.pcmFormatInt16、interleaved:false) } NSError {print(error.localizedDescription)}のようにcatchエラーを発生させる } – Fredo