在AudioKit中,一切都在模拟器中工作,尽管在手机上运行时我必须注释掉一些代码

时间:2018-05-26 03:45:27

标签: audiokit

我一直在使用名为AudioKit的第三方框架开发应用程序。我已经在应用程序的这一部分实现了两件事。一,它接收频率并显示可视化器。二,它播放频率。这些功能在我使用模拟器时都有效,但是当我将它下载到我的手机时除非我禁用播放频率的段,否则它不起作用。我提前为这个解释不清的问题道歉,我对此很陌生。

以下是错误: engine @ 0x1d0017ae0:无法初始化,错误= -10875

这是我的代码:

import UIKit
import AudioKitUI
import AudioKit


class SDViewController: UIViewController {
    // Lables
    @IBOutlet private var frequencyLabel: UILabel!
    @IBOutlet private var amplitudeLabel: UILabel!
    @IBOutlet private var noteNameWithSharpsLabel: UILabel!
    @IBOutlet private var noteNameWithFlatsLabel: UILabel!
    //Audio Visualier
    @IBOutlet private var audioInputPlot: EZAudioPlot!

    // Variables for Frequency
    var mic: AKMicrophone!
    var tracker: AKFrequencyTracker!
    var silence: AKBooster!
    var invertedfrequency : Float!
    //To Control Volume
    var amplitude = Double(0.5)

    let noteFrequencies = [16.35, 17.32, 18.35, 19.45, 20.6, 21.83, 23.12, 24.5, 25.96, 27.5, 29.14, 30.87]
    let noteNamesWithSharps = ["C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯", "A", "A♯", "B"]
    let noteNamesWithFlats = ["C", "D♭", "D", "E♭", "E", "F", "G♭", "G", "A♭", "A", "B♭", "B"]

    func setupPlot() {
        // Configures the Audio Visualizer

        //Audiokits versions of Apple's Node
        let plot = AKNodeOutputPlot(mic, frame: audioInputPlot.bounds)
        plot.plotType = .rolling
        plot.shouldFill = true
        plot.shouldMirror = true
        plot.color = UIColor.black
        audioInputPlot.addSubview(plot)
        audioInputPlot.backgroundColor = UIColor.black.withAlphaComponent(0.5)



    }



    override func viewDidLoad() {
        super.viewDidLoad()
        // Sets up the variables
        AKSettings.audioInputEnabled = true
        mic = AKMicrophone()
        tracker = AKFrequencyTracker(mic)
        silence = AKBooster(tracker, gain: 0)

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // Audiokit starts if it is silent
        AudioKit.output = silence
        do {
            try AudioKit.start()
        } catch {
            AKLog("AudioKit did not start!")
        }
        setupPlot()
        Timer.scheduledTimer(timeInterval: 0.1,
                             target: self,
                             selector: #selector(SDViewController.updateUI),
                             userInfo: nil,
                             repeats: true)
    }
    @objc func updateUI() {
        if tracker.amplitude > 0.1 {
            frequencyLabel.text = String(format: "%0.1f", tracker.frequency)

            var frequency = Float(tracker.frequency)
            while frequency > Float(noteFrequencies[noteFrequencies.count - 1]) {
                frequency /= 2.0
            }
            while frequency < Float(noteFrequencies[0]) {
                frequency *= 2.0
            }

            var minDistance: Float = 10_000.0
            var index = 0

            for i in 0..<noteFrequencies.count {
                let distance = fabsf(Float(noteFrequencies[i]) - frequency)
                if distance < minDistance {
                    index = i
                    minDistance = distance
                }
            }
            let octave = Int(log2f(Float(tracker.frequency) / frequency))
            noteNameWithSharpsLabel.text = "\(noteNamesWithSharps[index])\(octave)"
            noteNameWithFlatsLabel.text = "\(noteNamesWithFlats[index])\(octave)"
        }
        amplitudeLabel.text = String(format: "%0.2f", tracker.amplitude)
    }


  // Frequency Player Starts Here Orignal
    var oscillator1 = AKOscillator()
    var mixer = AKMixer()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        mixer = AKMixer(oscillator1)

        // Cut the volume in half since we have two oscillators
        mixer.volume = amplitude
        AudioKit.output = mixer

    }


    @IBAction func toggleSound(_ sender: UIButton) {
        if oscillator1.isPlaying {
            oscillator1.stop()
            sender.setTitle("Enable Active Noise Cancellation", for: .normal)
        }else {
           //Getting Inverted Frequency set by the Microphone Analysis portion
             oscillator1.frequency = (10000)
            oscillator1.start()
            sender.setTitle("Disable Active Noise Cancellation", for: .normal)
        }
    }
   // Frequency Player Ends Here Original
    @IBOutlet weak var lbl: UILabel!
    @IBAction func slider(_ sender: UISlider) {
        lbl.text = "\(Int(sender.value))%"
        amplitude = Double(sender.value/100)
        print(amplitude)


    }


}

0 个答案:

没有答案