“即时"处理音频(C#, WP7) [英] Processing audio "on-fly" (C#, WP7)

查看:19
本文介绍了“即时"处理音频(C#, WP7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 C# 中,在 .NET 上即时"处理音频?例如,如果我想评估录音时音频的平均强度(为此,我需要最后几毫秒).

Is there a way, in a C#, on a .NET, to process audio "on-fly"? For example, if I want to evaluate average intensity of the audio AT the moment of recording (for that, I will need to have last couple of milliseconds).

推荐答案

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

Initialization of a microphone, and recorded sounds processing:

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();
}

整体信号的幅度.您不能在所有字节数组中找到平均值,而是在较小的窗口中找到平均值以获得幅度曲线:

Amplitude of the overall signal. You can find averages not in all byte array, but in smaller windows to get amplitude curve:

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;
}

用于动态生成声音的其他东西,可能在未来对您有所帮助:

Other stuff for generating sounds on-the-fly that possible help you in future:

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);
}

这篇关于“即时"处理音频(C#, WP7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆