naudio applying a simple envelop filter

Viewed 11

Hi I'm working with Naudio and dotnet framework 4.0 (I can't upgrade due some restrictions). I manage to record the audio but now I should use an envelop filter. To be honest I have red some documentation about NAudio but I just can't get my head around it. I have the following code:

using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.IO;

namespace SomeFancyProject
{
    public class AudioRecorder
    {
        private int _deviceNumber = 0;
        private WaveIn _waveSource;

        private WaveFileWriter _waveFile;
        private MemoryStream _stream;

        public AudioRecorder(string microphoneName)
        {
            SetMicrophoneGuid(microphoneName);
        }

        private void SetMicrophoneGuid(string microphoneName)
        {
            var waveInDevices = WaveIn.DeviceCount;
            for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
            {
                WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
                if (microphoneName == deviceInfo.ProductName)
                {
                    _deviceNumber = waveInDevice;
                    break;
                }
            }
        }

        public static IList<string> GetMicrophones()
        {
            var waveInDevices = WaveIn.DeviceCount;
            var devices = new List<string>();
            for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
            {
                WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
                devices.Add(deviceInfo.ProductName);
            }
            return devices;
        }


        public void Record()
        {
            _stream = new MemoryStream();
            _waveSource = new WaveIn();
            _waveSource.WaveFormat = new WaveFormat(44100, 1);
            _waveSource.DeviceNumber = _deviceNumber;
            _waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
            _waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);

            //_waveFile = new WaveFileWriter(@"C:\Temp\Test0001.wav", _waveSource.WaveFormat);
            _waveFile = new WaveFileWriter(_stream, _waveSource.WaveFormat);
            _waveSource.StartRecording();
        }

        void waveSource_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (_waveFile != null)
            {
                _waveFile.Write(e.Buffer, 0, e.BytesRecorded);
                _waveFile.Flush();
            }
        }

        void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
        {
            if (_waveSource != null)
            {
                _waveSource.Dispose();
                _waveSource = null;
            }

            if (_waveFile != null)
            {
                _waveFile.Dispose();
                _waveFile = null;
            }
        }

        public MemoryStream StopRecording()
        {
            _waveSource.StopRecording();
            return _stream;
        }

        public static MemoryStream GetStream(string filePath)
        {
            var stream = new MemoryStream(File.ReadAllBytes(filePath));
            return stream;
        }
    }
}

Somewhere else I call Record() and after a time I call StopRecording() which returns a memorystream from the audio. On this memorystream I should apply an envelop filter. I did this with Nwaves but this lib can't be used in dotnet framework 4.0. Is there a way to apply an envelop filter with naudio?

Thanks in advance

0 Answers
Related