处理音频"对飞" (C#,WP7)音频、QUOT

2023-09-02 20:49:20 作者:没你,依然精彩

有没有一种方法,在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);
}
 
精彩推荐