处理音频"对飞" (C#,WP7) [英] Processing audio "on-fly" (C#, WP7)

查看:172
本文介绍了处理音频"对飞" (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);
}

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

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