views:

197

answers:

0

I'm writing an application that requires the user be in a quiet environment. To do this, I periodically check the power reading off the microphone. (I'm aware of the returned value being in dBFS or, in this case, a float in the interval [0, 1]. )

My problem is that the below code works just fine... except when it returns 18466064732283753157623808.00000. I see no NSLog output indicating AudioQueueGetProperty returning a failure. The weird value is always the mentioned value.

-(float)instantaneousPeakPower {
  UInt32 dataSize = sizeof(AudioQueueLevelMeterState) * recordFormat.mChannelsPerFrame;
  AudioQueueLevelMeterState *levels = (AudioQueueLevelMeterState*)malloc(dataSize);

  OSStatus rc = AudioQueueGetProperty(audioQueue, kAudioQueueProperty_CurrentLevelMeter, levels, &dataSize);
  if (rc) {
    NSLog(@"NoiseLeveMeter>>takeSample - AudioQueueGetProperty(CurrentLevelMeter) returned %@", rc);
  }

  float channelAvg = 0;
  for (int i = 0; i < recordFormat.mChannelsPerFrame; i++) {
    channelAvg += levels[i].mPeakPower;
  }
  free(levels);

  // This works because in this particular case one channel always has an mAveragePower of 0.
  return channelAvg;
}

What gives? Does the bit pattern of the value perhaps give a clue?