Get audio volume from microphone

Hello. We are trying to get audio volume from microphone.

We have 2 questions.

1. Can anyone tell me about AVAudioEngine.InputNode.volume?

AVAudioEngine.InputNode.volume

Return 0 in the silence, Return float type value within 1.0 depending on the
volume are expected work, but it looks 1.0 (default value) is returned at any time.

Which case does it return 0.5 or 0?

Sample code is below. Microphone works correctly.

// instance member
private var engine: AVAudioEngine!
private var node: AVAudioInputNode!

// start method
self.engine = .init()
self.node = engine.inputNode
engine.prepare()
try! engine.start()

// volume getter
print(\(self.node.volume))

2. What is the best practice to get audio volume from microphone?

Requirements are:

  • Without AVAudioRecorder. We use it for streaming audio.
  • it should withstand high frequency access.

Testing info

  • device: iPhone XR
  • OS version: iOS 18

Best Regards.

Hello @Gazzy, thank you for your post.

  1. Can anyone tell me about AVAudioEngine.InputNode.volume?

The documentation for volume mentions that this property is only implemented in AVAudioEnvironmentNode and AVAudioMixerNode, and therefore it is not implemented in AVAudioInputNode.

  1. What is the best practice to get audio volume from microphone?

On macOS you can use Core Audio to access the underlying audio device directly and set the kAudioDevicePropertyVolumeScalar, for example. On iOS you need to call isInputGainSettable before trying to set the input gain with setInputGain(_:). To simply know what the input gain is, you can access the inputGain property of AVAudioSession.

@Engineer

Thank you for your response.

We have two more question.

What is the best practice to get decibel audio from microphone?

You suggested inputGain to get audio gain from microphone.

However it looks the api to get the settings of microphone.

We hope to get audio level that input from microphone.

Do you have any ideas?

About AVAudioRecorder

We can find some sample using AVAudioRecorder to get audio gain.

Can we use it on the case of streaming?

Can we use it with AudioOutputUnitStart?

Best Regards.

Get audio volume from microphone
 
 
Q