Detecting blowing on the iPhone microphone?

前端 未结 5 2094
再見小時候
再見小時候 2020-12-23 19:01

I am trying to detect when the user is blowing into the mic of an iPhone. Right now I am using the SCListener class from Stephen Celis to call

if ([[SCListen         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 19:53

    I would recommend low-pass filtering the power signal first. There is always going to be some amount of transient noise that will mess with instantaneous readings; low-pass filtering helps mitigate that. A nice and easy low-pass filter would be something like this:

    // Make this a global variable, or a member of your class:
    double micPower = 0.0;
    // Tweak this value to your liking (must be between 0 and 1)
    const double ALPHA = 0.05;
    
    // Do this every 'tick' of your application (e.g. every 1/30 of a second)
    double instantaneousPower = [[SCListener sharedListener] peakPower];
    
    // This is the key line in computing the low-pass filtered value
    micPower = ALPHA * instantaneousPower + (1.0 - ALPHA) * micPower;
    
    if(micPower > THRESHOLD)  // 0.99, in your example
        // User is blowing on the microphone
    

提交回复
热议问题