处理音频”;“飞行中”;(C#,WP7)

本文关键字:WP7 音频 飞行 处理 飞行中 | 更新日期: 2023-09-27 18:25:15

在C#中,在.NET上,有没有一种方法可以"动态"处理音频?例如,如果我想在录制时评估音频的平均强度(为此,我需要最后几毫秒)。

处理音频”;“飞行中”;(C#,WP7)

麦克风初始化和录音处理:

private void Initialize()
{
    Microphone microphone = Microphone.Default;
    // 100 ms is a minimum buffer duration
    microphone.BufferDuration = TimeSpan.FromMilliseconds(100);  
    DispatcherTimer updateTimer = new DispatcherTimer()
    {
        Interval = TimeSpan.FromMilliseconds(0.1)
    };
    updateTimer.Tick += (s, e) =>
    {
        FrameworkDispatcher.Update();
    };
    updateTimer.Start();
    byte[] microphoneSignal = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
    microphone.BufferReady += (s, e) =>
    {
        int microphoneDataSize = microphone.GetData(microphoneSignal);
        double amplitude = GetSignalAmplitude(microphoneSignal);
        // do your stuff with amplitude here
    };
    microphone.Start();
}

整体信号的振幅。你可以在不是所有字节数组中找到平均值,而是在较小的窗口中找到振幅曲线:

private double GetSignalAmplitude(byte[] signal)
{
    int BytesInSample = 2;
    int signalSize = signal.Length / BytesInSample;
    double Sum = 0.0;
    for (int i = 0; i < signalSize; i++)
    {
        int sample = Math.Abs(BitConverter.ToInt16(signal, i * BytesInSample));
        Sum += sample;
    }            
    double amplitude = Sum / signalSize; 
    return amplitude;
}

其他可以在飞行中产生声音的东西,可能会在未来帮助你:

DynamicSoundEffectInstance generatedSound = new DynamicSoundEffectInstance(SampleRate, AudioChannels.Mono);
generatedSound.SubmitBuffer(buffer);
private void Int16ToTwoBytes(byte[] output, Int16 value, int offset)
{
    output[offset + 1] = (byte)(value >> 8);
    output[offset] = (byte)(value & 0x00FF);
}