Я пытаюсь создать приложение, которое анализирует потоки FFT и сравнивает оригинал с последующим потоком.Для этого мне нужно иметь возможность выключить и заново инициировать поток.
На симуляторе это работает без проблем.На физическом устройстве (iPad 12.9 1-го поколения) только первый сеанс дает действительные данные, тогда как начиная со второго я получаю только нули.
Я создал пример проекта Xcode, который продемонстрировал проблему.Его можно скачать здесь: https://drive.google.com/file/d/1rR2zWPREwXbXfZFocubwMgQ8SyFrXt2V/view?usp=sharing
Вот код ViewController:
import UIKit
import AudioKit
class ViewController: UIViewController, UIApplicationDelegate {
@IBOutlet weak var toggleLearn: UIButton!
var listenTimer : Timer?
let mic = AKMicrophone()
var compressor = AKCompressor()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func listen() {
compressor.start()
compressor = AKCompressor(mic)
if let inputs = AudioKit.inputDevices {
do {
try AudioKit.setInputDevice(inputs[0])
} catch {
print ("Could not set audio inputs: \(error)")
}
do {
try mic.setDevice(inputs[0])
} catch {
print ("Could not set the audio input device to the AKMic: \(error)")
}
}
AudioKit.output = AKBooster(compressor, gain: 0)
if !AudioKit.engine.isRunning {
do {
try AudioKit.start()
} catch {
print ("Could not start AudioKit: \(error)")
}
}
compressor.threshold = 3
compressor.headRoom = 3
compressor.masterGain = 1
compressor.attackDuration = 0.001
compressor.releaseDuration = 0.01
mic.start()
let fft = AKFFTTap(compressor)
if listenTimer == nil {
listenTimer = Timer.scheduledTimer(withTimeInterval: 0.08, repeats: true, block: { _ in
let i = fft.fftData
print (i[100...200])
})
}
}
@IBAction func learnPage(_ sender: UIButton) {
if AudioKit.engine.isRunning {
if listenTimer != nil {
listenTimer?.invalidate()
listenTimer = nil
}
mic.stop()
compressor.stop()
try! AudioKit.stop()
toggleLearn.setTitle("Listen", for: .normal)
} else {
toggleLearn.setTitle("Stop", for: .normal)
listen()
}
}
}
Помощь будет принята с благодарностью.