我在我的应用程序中使用AudioKit麦克风分析来检测声音频率。一切工作正常,但是,我想捕获头几个频率(可能是20到30个)并将其存储在数组中。我可以使用print(tracker.frequency)打印频率。我正在尝试在空数组上使用insert来存储
这是我的视图控制器中的代码
@objc func updateUI() {
if tracker.amplitude > 0.01 {
frequencyLbl.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
}
// This is not working. I need an array like [23.111, 234.555, 345.65.. etc]
var dynamicFrequency: [Float] = [frequency]
for f in dynamicFrequency {
dynamicFrequency.insert(f, at: 0)
}
print(dynamicFrequency)
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))
noteNameLbl.text = "\(noteNamesWithSharps[index])\(octave)"
noteNameFlatsLbl.text = "\(noteNamesWithFlats[index])\(octave)"
}
amplitudeLbl.text = String(format: "%0.2f", tracker.amplitude)
}
输出显示在下面
AKMicrophone.swift:init():45:Mixer inputs 8
Audio Kit Started
[27.1829815, 27.1829815]
[16.7672386, 16.7672386]
[32.2606049, 32.2606049]
[31.2773647, 31.2773647]
[31.2035122, 31.2035122]
[31.5194035, 31.5194035]
[32.4384956, 32.4384956]
[25.3139915, 25.3139915]
[28.1445961, 28.1445961]
但是,我希望输出为
[27.1829815,16.7672386,32.2606049,31.2773647,31.2035122,31.5194035,32.43849 56,25.3139915,28.1445961]
我在做什么错了?
答案 0 :(得分:0)
您似乎经常拨打 String dirPath = Environment.getExternalStorageDirectory().toString();
dirPath+="/Android/data/"+context.getPackageName()+"/";
File file = new File(dirPath)
file.mkdirs();
。这就是updateUI()
被多次打印的原因。
要获得所需的输出,您需要做的是在dynamicFrequency
外部声明dynamicFrequency
,并在20个左右的频率上打印其内容:
updateUI()