Jay
Jay

Reputation: 41

AudioKit tap skips over time intervals

I am building an app that uses microphone input to detect sounds and trigger events. I based my code on AKAmplitudeTap, but I when I ran it, I found that I was only obtaining sample data for intervals with missing sections.

The tap code looks like this (with the guts ripped out and simply keeping track of how many samples would have been processed):

open class MyTap {
//   internal let bufferSize: UInt32 = 1_024  // 8-9 kSamples/sec
     internal let bufferSize: UInt32 = 4096   // 39.6 kSamples/sec   
//   internal let bufferSize: UInt32 = 16536  // 43.3 kSamples/sec

public init(_ input: AKNode?) {
    input?.avAudioNode.installTap(onBus: 0, bufferSize: bufferSize, format: nil ) { buffer, _ in

        sampleCount += self.bufferSize

    }
}

I initialize the tap with:

func afterLoad() {
    assert(!loaded)
    AKSettings.audioInputEnabled = true
    do {
        try AKSettings.setSession(category: .playAndRecord, with: .allowBluetoothA2DP)
    } catch {
        print("Could not set session category.")
    }
    mic = AKMicrophone()
    myTap = MyTap(mic)  // seriously, can it be that easy?  

    loaded = true
}

The original tap code was capturing samples to a buffer, but I saw that big chunks of time were missing with a buffer size of 1024. I suspected that the processing time for the sample buffer might be excessive, so...

I simplified the code to simply keep track of how many samples were being passed to the tap. In another part of the code, I simply print out sampleCount/elapsedTime and, as noted in the comments after 'bufferSize' I get different amounts of samples per second.

The sample rate converges on 43.1 KSamples/sec with a 16K buffer, and only collects about 20% of the samples with a 1K buffer. I would prefer to use the small buffer size to obtain near real-time response to detected sounds. As I've been writing this, the 4K buffer version has been running and has stabilized at 39678 samples/sec.

Am I missing something? Can a tap with a small buffer size actually capture 44.1 Khz sample data?

Upvotes: 3

Views: 422

Answers (1)

Jay
Jay

Reputation: 41

Problem resolved... the tap requires this line of code

buffer.frameLength = self.bufferSize

... and suddenly all the samples appear. I obviously stripped out a bit too much code from the code I obviously didn't understand.

Upvotes: 1

Related Questions