简体   繁体   English

麦克风在分析频率时,AudioKit能播放声音吗?

[英]Can AudioKit play a sound while microphone is analyzing frequency?

I want to make an ear training app, so I want to make a sound while the microphone analyzes the frequency. 我想制作一个耳朵训练应用程序,所以我想在麦克风分析频率时发出声音。 I'm at the beginning to prove the concept, so for now, I just took AudioKit's sample app MicrophoneAnalisys and added some codes to make a sound. 我正在开始证明这个概念,所以现在,我只是采用了AudioKit的示例应用程序MicrophoneAnalisys并添加了一些代码来发出声音。

    import AudioKit
    import AudioKitUI
    import UIKit

    class ViewController: UIViewController {

        var oscillator1 = AKOscillator()
        var oscillator2 = AKOscillator()
        var mixer = AKMixer()

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

            mixer.volume = 0.5
            AudioKit.output = mixer
            do {
                try AudioKit.start()
            } catch {
                AKLog("AudioKit did not start!")
            }
        }

        var mic: AKMicrophone!
        var tracker: AKFrequencyTracker!
        var silence: AKBooster!

    override func viewDidLoad() {
        super.viewDidLoad()

        AKSettings.audioInputEnabled = true
        mic = AKMicrophone()
        tracker = AKFrequencyTracker(mic)
        silence = AKBooster(tracker, gain: 0)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        AudioKit.output = silence
        do {
            try AudioKit.start()
        } catch {
            AKLog("AudioKit did not start!")
        }
        setupPlot()
        Timer.scheduledTimer(timeInterval: 0.1,
                             target: self,
                             selector: #selector(ViewController.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)
    }


    @IBAction func didTapASound(_ sender: Any) {
        print("didTapASound")

        mixer = AKMixer(oscillator1, oscillator2)

        // Cut the volume in half since we have two oscillators
        mixer.volume = 0.5
        AudioKit.output = mixer
        do {
            try AudioKit.start()
        } catch {
            AKLog("AudioKit did not start!")
        }

        if oscillator1.isPlaying {
            oscillator1.stop()
            oscillator2.stop()
        } else {
            oscillator1.frequency = random(in: 220 ... 880)
            oscillator1.start()
            oscillator2.frequency = random(in: 220 ... 880)
            //            oscillator2.start()
        }

    }

I know I'm definitely doing wrong by trying to run 我知道我试图跑步肯定是错的

AudioKit.output = mixer
do {
  try AudioKit.start()
} catch {
  AKLog("AudioKit did not start!")
}

and

AudioKit.output = mixer
do {
    try AudioKit.start()
} catch {
    AKLog("AudioKit did not start!")
}

simultaneously. 同时。

I get the following errors. 我收到以下错误。

[avae] AVAEInternal.h:70:_AVAE_Check: required condition is false: [AVAudioIONodeImpl.mm:910:SetOutputFormat: (IsFormatSampleRateAndChannelCountValid(hwFormat))]
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(hwFormat)'

I read this article but couldn't understand whether the person asking the question was able to solve the problem. 我读过这篇文章,但无法理解提出问题的人是否能够解决问题。 Can someone tell me if I can make a sound while putting the mic on with AudioKit and point me to the right direction where I can learn how I can do it? 有人可以告诉我,如果我可以在使用AudioKit将麦克风打开的时候发出声音并指出我正确的方向,我可以学习如何做到这一点吗? Thanks! 谢谢!

I'm answering my own question. 我在回答我自己的问题。 Of course, it can make a sound and listen to a microphone simultaneously. 当然,它可以发出声音并同时收听麦克风。 All I had to do was to put them together with AKMixer. 我所要做的就是将它们与AKMixer放在一起。

var oscillator = AKOscillator()

var mic: AKMicrophone!
    var tracker: AKFrequencyTracker!
    var silence: AKBooster!

override func viewDidLoad() {
        super.viewDidLoad()

        AKSettings.audioInputEnabled = true
        mic = AKMicrophone()
        tracker = AKFrequencyTracker(mic)
        silence = AKBooster(tracker, gain: 0)
    }

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    AudioKit.output = AKMixer(silence,oscillator)
    //AudioKit.output = silence
    do {
        try AudioKit.start()
    } catch {
        AKLog("AudioKit did not start!")
    }
    setupPlot()
    Timer.scheduledTimer(timeInterval: 0.1,
                         target: self,
                         selector: #selector(ViewController.updateUI),
                         userInfo: nil,
                         repeats: true)
}

@IBAction func makeASound(_ sender: Any) {

    if oscillator.isPlaying {
        print("Stop A Sound")
        oscillator.stop()
    } else {
        print("Make A Sound")
        oscillator.amplitude = 1.0
        oscillator.frequency = 440.0
        oscillator.start()
    }

}

Just in case someone has the same question.. 以防有人有同样的问题..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM