简体   繁体   English

xamarin iOS:如何在录制时显示语音的音频幅度

[英]xamarin iOS: How to show the audio amplitude of the voice when recording

On Android, I have found to show the amplitude of the recording I can use getMaxAmplitude at a regular basis. 在Android上,我发现显示记录的幅度我可以定期使用getMaxAmplitude。 Does iOS have the same function? iOS是否具有相同的功能? I want to show the input voice volume meter while recording from the microphone. 我想在麦克风录音时显示输入的语音音量表。

Assuming you are using AVAudioRecorder you can enable metering and read the average and peak volumes (in decibels) for each audio channel. 假设您正在使用AVAudioRecorder您可以启用计量并读取每个音频通道的平均和峰值音量(以分贝为单位)。

Sample Microphone 10 times a second: 样品麦克风每秒10次:

# 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 参考: 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; 返回值0 dB表示满量程或最大功率; a return value of -160 dB indicates minimum power (that is, near silence). 返回值为-160 dB表示最小功率(即接近静音)。

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). 如果提供给录音机的信号超过±满量程,则返回值可能超过0(即,它可能进入正范围)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM