Below is a quick snippet of where I record audio. I would like to get a sampling of the background audio so that later I can filter out background noise. I figure 10 to 15 seconds should be a good amount of time.
Although I am assuming that it can change depending on the iOS device, the format returned from .inputFormat is :
<AVAudioFormat 0x600003e8d9a0: 1 ch, 48000 Hz, Float32>
Based on the format info, is it possible to make bufferSize for .installTap be just the write size for whatever time I wish to record for?
I realize I can create a timer for 10 seconds, stop recording, paster the files I have together, etc. etc. But if I can avoid all that extra coding it would be nice.
let node = audioEngine.inputNode
let recordingFormat = node.inputFormat(forBus: 0)
makeFile(format: recordingFormat)
node.installTap(onBus: 0, bufferSize: 8192, format: recordingFormat, block: {
[self]
(buffer, _) in
audioMetering(buffer: buffer)
print ("\(self.averagePowerForChannel0) \(self.averagePowerForChannel1)")
if self.averagePowerForChannel0 < -50 && self.averagePowerForChannel1 < -50 {
...
} else {
...
}
do {
... write audio file
} catch {return};})
audioEngine.prepare()
do {
try audioEngine.start()
} catch let error {
print ("oh catch \(error)")
}