使用Swift使用AVAudioEngine进行实时转换

前端之家收集整理的这篇文章主要介绍了使用Swift使用AVAudioEngine进行实时转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Swift在OSX上开发音频效果应用程序,我对集成音高效果感兴趣. @H_403_1@我希望能够实时地将音调调高或调高八度.目前我只收到一个干燥的信号.

@H_403_1@我不确定这是否可行,并且想知道这是否可行,或者任何人可能有任何帮助或建议.

@H_403_1@与该问题相关的当前代码如下:

import Cocoa
import AVFoundation


class ViewController: NSViewController {
       var engine = AVAudioEngine()
       var timePitch = AVAudioUnitTimePitch()


    override func viewDidLoad() {
         timePitch.pitch = 1200

        // Setup engine and node instances
        var mixer = engine.mainMixerNode
        var input = engine.inputNode
        var output = engine.outputNode
        var format = input.inputFormatForBus(0)
        var error:NSError?

       engine.attachNode(timePitch)

       engine.connect(input,to: timePitch,format: format)
       engine.connect(timePitch,to: output,format: format)

      engine.startAndReturnError(&error)

        super.viewDidLoad()
    }

   override var representedObject: AnyObject? {
        didSet {
        // Update the view,if already loaded.
        }
    }


}
timePitch.pitch = -500 //Rude man voice
timePitch.rate = 1.5 //In 1.5 times faster
@H_403_1@检查this tutorial.和direct link以示教程中的示例获取更多信息.

@H_403_1@Swift 2.0的示例:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var engine: AVAudioEngine!
    var player: AVAudioPlayerNode!

    var file = AVAudioFile()

    override func viewDidLoad() {
        super.viewDidLoad()

        engine = AVAudioEngine()
        player = AVAudioPlayerNode()
        player.volume = 1.0

        let path = NSBundle.mainBundle().pathForResource("in",ofType: "caf")!
        let url = NSURL.fileURLWithPath(path)

        let file = try? AVAudioFile(forReading: url)
        let buffer = AVAudioPCMBuffer(PCMFormat: file!.processingFormat,frameCapacity: AVAudioFrameCount(file!.length))
        do {
            try file!.readIntoBuffer(buffer)
        } catch _ {
        }

        let pitch = AVAudioUnitTimePitch()

        //
        pitch.pitch = -500 //Distortion
        pitch.rate = 1.5 //Voice speed
        //

        engine.attachNode(player)

        engine.attachNode(pitch)

        engine.connect(player,to: pitch,format: buffer.format)

        engine.connect(pitch,to: engine.mainMixerNode,format: buffer.format)
        player.scheduleBuffer(buffer,atTime: nil,options: AVAudioPlayerNodeBufferOptions.Loops,completionHandler: nil)

        engine.prepare()
        do {
            try engine.start()
        } catch _ {
        }

        player.play()

    }
}
原文链接:https://www.f2er.com/swift/319424.html

猜你在找的Swift相关文章