On Android, I have found to show the amplitude of the recording I can use getMaxAmplitude at a regular basis. Does iOS have the same function? I want to show the input voice volume meter while recording from the microphone.
Asked
Active
Viewed 1,573 times
1 Answers
0
Assuming you are using AVAudioRecorder
you can enable metering and read the average and peak volumes (in decibels) for each audio channel.
Sample Microphone 10 times a second:
# AVAudioSettings setup ...
~~~
avAudioRecorder = AVAudioRecorder.Create(this.audioFilePath, new AudioSettings(settings), out error);
avAudioRecorder.MeteringEnabled = true;
var peakPower = 0.0;
var averagePower = 0.0;
// Note: No await, run the following Task synchronously!
Task.Run(async() => // Personally I would create my own Thread, but this works for a quick example
{
while (true) // Do not create object/release objects or subtasks in this loop, it will cause poor performance due to GC.
{
if (!avAudioRecorder.Recording)
return;
avAudioRecorder.UpdateMeters();
peakPower = avAudioRecorder.PeakPower(0);
averagePower = avAudioRecorder.AveragePower(0);
Console.WriteLine($"{averagePower}:{peakPower}");
Thread.Sleep(100);
}
});
Output:
2017-01-14 19:26:16.271 Sound[33399:19078946] -52.9222717285156:-48.1419296264648
2017-01-14 19:26:17.271 Sound[33399:19078946] -53.7662239074707:-45.7719497680664
2017-01-14 19:26:18.274 Sound[33399:19078946] -49.105827331543:-43.4426803588867
2017-01-14 19:26:19.275 Sound[33399:19078946] -9.97611618041992:4.59128427505493
2017-01-14 19:26:20.276 Sound[33399:19078946] -37.2582626342773:5.28962087631226
2017-01-14 19:26:21.276 Sound[33399:19078946] -24.4357891082764:4.00279855728149
2017-01-14 19:26:22.277 Sound[33399:19078946] -11.7632217407227:4.34794473648071
2017-01-14 19:26:23.278 Sound[33399:19078946] -32.4504356384277:5.66976404190063
2017-01-14 19:26:24.279 Sound[33399:19078946] -18.5233268737793:0.0205818619579077
2017-01-14 19:26:25.280 Sound[33399:19078946] -28.790828704834:-5.83084678649902
Ref: https://developer.apple.com/reference/avfoundation/avaudiorecorder
in decibels, for the sound being recorded. A return value of 0 dB indicates full scale, or maximum power; a return value of -160 dB indicates minimum power (that is, near silence).
If the signal provided to the audio recorder exceeds ±full scale, then the return value may exceed 0 (that is, it may enter the positive range).

SushiHangover
- 73,120
- 10
- 106
- 165
-
Thanks I have tried. When doing the volume meter? Which one should I go for. The average or the peak. When I tried on my iPhone, even though I have not saying anyway I can see the number around 70 (currently I converted to percentage). Is that because the noise around me but is not in a audible sound though. – LittleFunny Jan 19 '17 at 01:35
-
@LittleFunny 70%? -160db to 0db, decibels are a logarithmic unit on a power of 2 scale... Yes, you will typically see a steady background level, i.e. input noise. This level will vary based upon background noise levels, microphone type (different iPhone models)... Avg or Peak, What should you use?....If you where creating a `VU Meter` you would use average with maybe a peak indictor lighting up of the peak exceeds some set value... – SushiHangover Jan 19 '17 at 01:58
-
@LittleFunny Really depends upon want you are trying to convey to the user, average in a simple linear "LED" array to show them that their voice fluctuations are being recorded, if average is might always close to peak value then the recording might be distorted and they should move the phone away from their mouth and to stop yelling at their phone ;-) – SushiHangover Jan 19 '17 at 02:00
-
I see.. I doing for android as well which I am using getMaxAmplitude so I assumed getMaxAmplitude is same idea as peak but how to get the average. – LittleFunny Jan 19 '17 at 02:04
-
@LittleFunny On Android using `getAudioSourceMax`, they totally leave it up to you to sample at a steady rate and calculate an average to display and thus you are responsible for store the "peak". The values returned are more like a "sound pressure", since it is uint it is a 16-bit digitalization of the analog to digital conversion. There are some great answers here : http://stackoverflow.com/questions/10655703/what-does-androids-getmaxamplitude-function-for-the-mediarecorder-actually-gi – SushiHangover Jan 19 '17 at 02:15