Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

swift - AudioKit (iOS) - Add observer for frequency / amplitude change

I'm using the AudioKit framework for developing a small application that is able to listen for the microphone input frequency & amplitude and I want to trigger an event (obviously I want to trigger a function) every time the frequency & the amplitude got specific values -> An event for a specific tone.

Right now I'm using this simple Swift code for my application:

mic = AKMicrophone()
tracker = AKFrequencyTracker(mic)
silence = AKBooster(tracker, gain: 1)
AKSettings.audioInputEnabled = true
AudioKit.output = silence
AudioKit.start()

let t = Timer.scheduledTimer( timeInterval: 0.05, target: self, selector: #selector(self.checkFrequencyAmplitude), userInfo: nil, repeats: true)

func checkFrequencyAmplitude() {
   let frequency = tracker.frequency,
       amplitude = tracker.amplitude
   if (frequency > 1000 && frequency < 1200 && amplitude > 0 && amplitude < 0.2) {
      // do stuff if the specific tone appeared
   }
}

But to be honest I got massive problems using a timeInterval with 0.05s, like my iPhone heats up super fast & thats absolutely not this what I want.

  • Keep in mind: Just using another timeInterval for example one second (timeInterval: 1.0) is not possibility because my app must be able to detect the specific sound as fast as possible. (Some of the input sound could be pretty short [0.5 seconds or less].)

Maybe the AudioKit developer added some event can be triggered if the input gets a specific frequency / amplitude.

Or maybe it is possible to add something like an Swift observer to a AKFrequencyTracker-subclass..

I'm clueless how to go on and would be super thankful for some help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of using a timer, you should let the DSP check for your conditions for you. To do this you'll have to edit the frequency tracker or create your own copy. The good news is that AudioKit's amplitude tracker already does something like this, calling a function whenever a certain volume is heard ie. an amplitude threshold is reached:

https://github.com/AudioKit/AudioKit/blob/master/AudioKit/Common/Nodes/Analysis/Amplitude%20Tracker/AKAmplitudeTrackerDSPKernel.hpp#L97

If you do it in a general way, you could even make a pull request and commit the code back to AudioKit. In fact, if you want to do that, I can help steer you in the right direction further. But, I think you can get pretty far using the amplitude tracker as a template.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...