Can I calculate the bufferSize on .installTap to equal X seconds

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)")
    }

Hello,

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?

AVAudioNode.h states the following about the bufferSize parameter:

the requested size of the incoming buffers in sample frames. Supported range is [100, 400] ms.

Additionally, the documentation states:

The size of the incoming buffers. The implementation may choose another size.

Given this info, you would not be able do what you want, certainly you will not be able to get a 10-15 second buffer, but also you should not rely on the api always respecting the bufferSize that you pass in anyway.

All that being said, for your high-level goal of recording 10-15 seconds of audio you might find this api simpler to use: https://developer.apple.com/documentation/avfaudio/avaudiorecorder/1389378-record

All that being said, for your high-level goal of recording 10-15 seconds of audio you might find this api simpler to use: https://developer.apple.com/documentation/avfaudio/avaudiorecorder/1389378-record

As I will be recoding unknown lengths, and trying to use "background" samples to remove background noise, I am guessing that using the same functions to record would be the wiser?

The size of the incoming buffers. The implementation may choose another size.

Does this also apply to the same instance call of .installTap ? In other words, once running, will I start getting different buffer sizes in the same instance?

Can I calculate the bufferSize on .installTap to equal X seconds
 
 
Q