Processing audio “on-fly” (C#, WP7)

后端 未结 1 687
南旧
南旧 2020-12-13 23:07

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 wi

相关标签:
1条回答
  • 2020-12-13 23:16

    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);
    }
    
    0 讨论(0)
提交回复
热议问题