Tuesday, 25 August 2020

Getting strange frequency readings on AKFrequencyTracker attached to a Focusrite 2i2

I'm seeing an issue when using an AKFrequencyTracker that when connected to an audio interface I see erroneous high frequency readings around 47kHz while playing a connected guitar. I don't have to play anything and I see the high frequencies. The amplitude seems to track to what it should be based on the input from the interface.

I've been able to reproduce this in the microphone input playground as well. Here is the slightly modified version.

//: ## Tracking Microphone Input
//:
import AudioKitPlaygrounds
import AudioKit

// 0 is mic
// 1 is focusrite

let selectedInput = 1

if let inputs = AudioKit.inputDevices {
    print("\(inputs.count) inputs found")
    for input in inputs {
        print("\(input.name): \(input.description)")
    }
    print("Chosen Input: \(inputs[selectedInput].name): \(inputs[selectedInput].description)")
    try AudioKit.setInputDevice(inputs[selectedInput])
}

print("Sample Rate Before: \(AKSettings.sampleRate)")
let sampleRate = AudioKit.engine.inputNode.inputFormat(forBus: selectedInput).sampleRate
print("Changing Sample Rate to \(sampleRate)")
AKSettings.sampleRate = sampleRate

let mic = AKMicrophone()

let micCopy1 = AKBooster(mic)
let micCopy2 = AKBooster(mic)
let micCopy3 = AKBooster(mic)

let tracker = AKFrequencyTracker(micCopy2, hopSize: 4096, peakCount: 20)
let silence = AKBooster(tracker, gain: 0)

//: The frequency tracker passes its input to the output,
//: so we can insert into the signal chain at the bottom
AudioKit.output = silence
try AudioKit.start()

//: User Interface
import AudioKitUI

class LiveView: AKLiveViewController {

    var trackedAmplitudeSlider = AKSlider(property: "Tracked Amplitude", range: 0 ... 0.8) { _ in
        // Do nothing, just for display
    }
    var trackedFrequencySlider = AKSlider(property: "Tracked Frequency",
                                      range: 0 ... 2_400,
                                      format: "%0.3f Hz"
    ) { _ in
        // Do nothing, just for display
    }

    override func viewDidLoad() {

        addTitle("Tracking Microphone Input")
        addView(trackedAmplitudeSlider)
        addView(trackedFrequencySlider)

        AKPlaygroundLoop(every: 0.1) {
            self.trackedAmplitudeSlider.value = tracker.amplitude
            self.trackedFrequencySlider.value = tracker.frequency
            if tracker.amplitude > 0.01 {
                print("Frequency: \(tracker.frequency)\tAmplitude: \(tracker.amplitude)")
            }
        }


        let fftPlot = AKNodeFFTPlot(micCopy1, frame: CGRect(x: 0, y: 0, width: 500, height: 200))
        fftPlot.shouldFill = true
        fftPlot.shouldMirror = false
        fftPlot.shouldCenterYAxis = false
        fftPlot.color = AKColor.purple
        fftPlot.gain = 100
        addView(fftPlot)

        let rollingPlot = AKNodeOutputPlot(micCopy2, frame: CGRect(x: 0, y: 0, width: 440, height: 200))
        rollingPlot.plotType = .rolling
        rollingPlot.shouldFill = true
        rollingPlot.shouldMirror = true
        rollingPlot.color = AKColor.red
        rollingPlot.gain = 2
        addView(rollingPlot)

        let plot = AKNodeOutputPlot(micCopy3, frame: CGRect(x: 0, y: 0, width: 440, height: 200))
        plot.plotType = .buffer
        plot.shouldFill = true
        plot.shouldMirror = true
        plot.color = AKColor.blue
        plot.gain = 2
        addView(plot)

    }
}

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = LiveView()

The output from playing a single A note looks like this.

2 inputs found
Built-in Microphone: <Device: Built-in Microphone (45)>
Scarlett 2i2 USB: <Device: Scarlett 2i2 USB (41)>
Chosen Input: Scarlett 2i2 USB: <Device: Scarlett 2i2 USB (41)>
Sample Rate Before: 44100.0
Changing Sample Rate to 48000.0
Frequency: 0.0  Amplitude: 0.0
Frequency: 0.0  Amplitude: 0.0
Frequency: 200.0    Amplitude: 0.0010132051538676023
Frequency: 200.0    Amplitude: 0.0009377096430398524
Frequency: 23873.810546875  Amplitude: 0.0011335075832903385
Frequency: 23960.720703125  Amplitude: 0.0020712288096547127
Frequency: 11976.5791015625 Amplitude: 0.0018204051302745938
Frequency: 23956.021484375  Amplitude: 0.001121549867093563
Frequency: 23812.46484375   Amplitude: 0.0011597403790801764
Frequency: 23812.46484375   Amplitude: 0.0010917051695287228
Frequency: 23812.46484375   Amplitude: 0.0010660385014489293
Frequency: 23812.46484375   Amplitude: 0.001134994556196034
Frequency: 23812.46484375   Amplitude: 0.001019841991364956
Frequency: 23812.46484375   Amplitude: 0.0009903700556606054
Frequency: 23812.46484375   Amplitude: 0.0009054119000211358
Frequency: 23812.46484375   Amplitude: 0.0009549797978252172
Frequency: 23812.46484375   Amplitude: 0.0009998299647122622
Frequency: 23812.46484375   Amplitude: 0.0009498257422819734
Frequency: 23812.46484375   Amplitude: 0.0009073034743778408
Frequency: 23812.46484375   Amplitude: 0.0009004760067909956
Frequency: 23812.46484375   Amplitude: 0.0009699893998913467
Frequency: 23812.46484375   Amplitude: 0.0010407709050923586
Frequency: 23812.46484375   Amplitude: 0.0010262386640533805
Frequency: 23812.46484375   Amplitude: 0.0009718178189359605
Frequency: 23812.46484375   Amplitude: 0.009978151880204678
Frequency: 47177.0234375    Amplitude: 0.003072592196986079
Frequency: 47127.61328125   Amplitude: 0.0013660960830748081
Frequency: 47735.60546875   Amplitude: 0.001907210098579526
Frequency: 46763.9609375    Amplitude: 0.0021022947039455175
Frequency: 47023.5390625    Amplitude: 0.0015598429599776864
Frequency: 47597.9140625    Amplitude: 0.0015960222808644176
Frequency: 47480.890625 Amplitude: 0.0016460377955809236
Frequency: 47610.85546875   Amplitude: 0.002251675818115473
Frequency: 23986.009765625  Amplitude: 0.0037994058802723885
Frequency: 47241.359375 Amplitude: 0.0015401218552142382
Frequency: 81.2586898803711 Amplitude: 0.0019702680874615908
Frequency: 47823.76171875   Amplitude: 0.0017958006355911493
Frequency: 47340.4140625    Amplitude: 0.2594001889228821
Frequency: 46485.42578125   Amplitude: 0.2443418949842453
Frequency: 439.1202697753906    Amplitude: 0.1977834701538086
Frequency: 438.7035827636719    Amplitude: 0.178997203707695
Frequency: 438.89056396484375   Amplitude: 0.14854535460472107
Frequency: 439.0594177246094    Amplitude: 0.13751377165317535
Frequency: 438.93438720703125   Amplitude: 0.11730749160051346
Frequency: 439.00408935546875   Amplitude: 0.1075168028473854
Frequency: 439.0429382324219    Amplitude: 0.09269371628761292
Frequency: 438.9091796875   Amplitude: 0.08598381280899048
Frequency: 439.02099609375  Amplitude: 0.08173725008964539
Frequency: 438.84759521484375   Amplitude: 0.07284277677536011
Frequency: 439.15087890625  Amplitude: 0.06797594577074051
Frequency: 438.9859619140625    Amplitude: 0.06045695021748543
Frequency: 439.0673828125   Amplitude: 0.05586790665984154
Frequency: 438.60552978515625   Amplitude: 0.051378797739744186
Frequency: 438.995361328125 Amplitude: 0.048340748995542526
Frequency: 438.2513732910156    Amplitude: 0.046707604080438614
Frequency: 438.7973327636719    Amplitude: 0.04262331500649452
Frequency: 46400.02734375   Amplitude: 0.03838739171624184
Frequency: 438.7486572265625    Amplitude: 0.03510402515530586
Frequency: 46454.44140625   Amplitude: 0.033189449459314346
Frequency: 438.2769470214844    Amplitude: 0.031524695456027985
Frequency: 438.5264587402344    Amplitude: 0.02920457161962986
Frequency: 46384.69140625   Amplitude: 0.026590649038553238
Frequency: 438.8437805175781    Amplitude: 0.022203370928764343
Frequency: 46476.07421875   Amplitude: 0.021149421110749245
Frequency: 438.9068603515625    Amplitude: 0.01986468769609928
Frequency: 46408.11328125   Amplitude: 0.011579773388803005
Frequency: 47087.5  Amplitude: 0.003355375723913312
Frequency: 438.4395751953125    Amplitude: 0.0027760120574384928
Frequency: 47169.25 Amplitude: 0.0029691671952605247
Frequency: 47137.53515625   Amplitude: 0.0037814872339367867
Frequency: 47239.609375 Amplitude: 0.0036421294789761305
Frequency: 47280.1875   Amplitude: 0.00348213710822165
Frequency: 47243.96484375   Amplitude: 0.00349710276350379
Frequency: 47200.53515625   Amplitude: 0.003506213193759322
Frequency: 47199.453125 Amplitude: 0.0034976035822182894
Frequency: 47144.86328125   Amplitude: 0.0033620402682572603
Frequency: 47194.66796875   Amplitude: 0.003223164239898324
Frequency: 47233.7734375    Amplitude: 0.0029081928078085184
Frequency: 47167.11328125   Amplitude: 0.002717009512707591
Frequency: 47226.33984375   Amplitude: 0.0026167919859290123
Frequency: 47141.22265625   Amplitude: 0.00268756877630949
Frequency: 47199.5546875    Amplitude: 0.0027781547978520393
Frequency: 47346.63671875   Amplitude: 0.0027468751650303602
Frequency: 47168.578125 Amplitude: 0.0026313727721571922
Frequency: 47183.4921875    Amplitude: 0.0024406039156019688
Frequency: 47240.8984375    Amplitude: 0.0022249151952564716
Frequency: 47455.91796875   Amplitude: 0.0021174440626055002
Frequency: 47284.98046875   Amplitude: 0.002093059942126274
Frequency: 47233.26953125   Amplitude: 0.002134564332664013
Frequency: 47414.72265625   Amplitude: 0.0021047494374215603
Frequency: 47152.109375 Amplitude: 0.002083567436784506
Frequency: 47366.0703125    Amplitude: 0.002048497786745429

You can see that it starts with "noise" with very minimal amplitude and then it detects the appropriate frequency ~440 Hz in there with an appropriate amplitude but also has a lot of the "noise" ~47 kHz readings.

I haven't been able to find much information about using AudioKit like this with an audio interface. I've reproduce this issue on an iMac, iOS Simulator and iPad Pro.

Not sure if it adds anything valuable to this, but when doing similar tests with just the build in microphone for the iMac using the simulator or an iPhone or iPad it works as expected and gives valid frequency readings the entire time.

I don't really think this is a problem with the 2i2 interface precisely as I've seen the same issues when using other audio interfaces from Tascam and even a Fractal AxeFx 3.

Any help is greatly appreciated.



from Getting strange frequency readings on AKFrequencyTracker attached to a Focusrite 2i2

No comments:

Post a Comment