Пример #1
0
 public void ApplyFilter(IAudioFilter filter)
 {
     foreach (var segment in segments)
     {
         segment.ApplyFilter(filter);
     }
 }
Пример #2
0
 private void btnStop_Click(object sender, EventArgs e)
 {
     soundOutDevice.Stop();
     tmrUpdateViz.Stop();
     tbarFilePosition.Enabled = false;
     lastFilterStep           = null;
 }
Пример #3
0
        public CrossoverFourWayFilter(IAudioFilter source, Crossover crossover)
        {
            if (source.NumberOfChannels != 2)
            {
                throw new ArgumentException("Audio source must have 2 channels", "source");
            }

            if (source.SampleRate != crossover.sampleRate)
            {
                throw new ArgumentException("Crossover sample rate does not match input source", "crossover");
            }

            if (crossover.crossoverType != Crossover.CROSSOVER_TYPES.FOUR_WAY)
            {
                throw new ArgumentException("Crossover must be a FOUR_WAY type", "crossover");
            }

            this.source    = source;
            this.crossover = crossover;

            this.currentState = States.Normal;

            this.lastOverallPosition     = 0;
            this.lastInputBufferPosition = -1;

            initializeFilterKernels();
        }
Пример #4
0
        public SixToEight(IAudioFilter source)
        {
            if (source.NumberOfChannels != 6)
            {
                throw new ArgumentException("source must have 6 channels, such as the CrossoverThreewayFilter");
            }

            this.source = source;
        }
Пример #5
0
        public void ApplyFilter(IAudioFilter filter)
        {
            List <Segment> .Enumerator var2 = this.segments.GetEnumerator();

            while (var2.MoveNext())
            {
                Segment segment = var2.Current;
                segment.ApplyFilter(filter);
            }
        }
Пример #6
0
 public NullEchoCancelFilter(
     int systemLatency,
     int filterLength,
     AudioFormat recordedAudioFormat,
     AudioFormat playedAudioFormat,
     IAudioFilter playedResampler   = null,
     IAudioFilter recordedResampler = null) :
     base(systemLatency, filterLength, recordedAudioFormat, playedAudioFormat, playedResampler, recordedResampler)
 {
 }
Пример #7
0
 public AudioContext(AudioFormat audioFormat, IAudioFilter resampler, IDtxFilter dtxFilter, IAudioTwoWayFilter speechEnhancer, IAudioEncoder encoder)
 {
     AudioFormat            = audioFormat;
     Resampler              = resampler;
     SpeechEnhancementStack = speechEnhancer;
     Encoder        = encoder;
     DtxFilter      = dtxFilter;
     ResampleBuffer = new byte[audioFormat.BytesPerFrame];
     CancelBuffer   = new short[audioFormat.SamplesPerFrame];
     EncodeBuffer   = new short[audioFormat.SamplesPerFrame];
     SendBuffer     = new short[audioFormat.SamplesPerFrame];
 }
Пример #8
0
        public SampleTap(IAudioFilter source, int numSamplesToSave)
        {
            this.source = source;

            this.numChannels = source.NumberOfChannels;

            this.samplesSaved = new double[this.numChannels][];
            for (int i = 0; i < this.samplesSaved.Length; i++)
            {
                this.samplesSaved[i] = new double[numSamplesToSave];
            }
        }
Пример #9
0
        public VolumeControl(IAudioFilter source)
        {
            this.source = source;

            this.numberOfChannels = source.NumberOfChannels;

            this.volume = new double[this.numberOfChannels];

            for (int i = 0; i < this.volume.Length; i++)
            {
                this.volume[i] = 1.0;
            }
        }
Пример #10
0
        public FilterToWaveSource(IAudioFilter source)
        {
            this.source = source;

            if (source.BitsPerSample != 16)
            {
                throw new ArgumentException("Only support 16 bits per sample");
            }

            if (source.NumberOfChannels != 2)
            {
                throw new ArgumentException("Only support 2 channels (stereo)");
            }
        }
Пример #11
0
        public IAudioFilter ApplyTo(IAudioFilter input)
        {
            if (m_Initialized)
            {
                throw new InvalidOperationException("AudioProcesses can only be used once.");
            }

            m_Initialized = true;
            Initialize();

            return(new AudioFilter(from _ in FilterBaseHelper.Bind(this)
                                   from value in input
                                   select Allocate(input.Output).Do(Render, value)));
        }
Пример #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <param name="index">The channels to return, 0 for Woofer, 1 for Midrange, and 2 for Tweeter</param>
        public SelectCrossover(IAudioFilter source, int index)
        {
            if (source.NumberOfChannels != 6)
            {
                throw new ArgumentException("source must have 6 channels, such as the CrossoverFilter");
            }

            if (index < 0 || index >= 3)
            {
                throw new ArgumentOutOfRangeException("index", "" + index);
            }

            this.source = source;
            this.index  = index;
        }
Пример #13
0
        public ConvertBitsPerSample(IAudioFilter source, uint targetBPS)
        {
            if (targetBPS != 16 && targetBPS != 24 && targetBPS != 32)
            {
                throw new ArgumentException("targetBPS must be either 16, 24, or 32");
            }

            this.source    = source;
            this.targetBPS = targetBPS;

            if (targetBPS > source.BitsPerSample)
            {
                multiplier = 1 << ((int)targetBPS - (int)source.BitsPerSample);
            }
            else
            {
                multiplier = 1.0 / (1 << ((int)source.BitsPerSample - (int)targetBPS));
            }
        }
Пример #14
0
        /// <summary>
        /// Constructs an instance of the WebRtcFilter.
        /// </summary>
        /// <param name="expectedAudioLatency">The expected audio latency in milliseconds</param>
        /// <param name="filterLength">The length of the echo cancellation filter in milliseconds</param>
        /// <param name="recordedAudioFormat">The audio format into which the recorded audio has been transformed prior to encoding (e.g., not the raw audio)</param>
        /// <param name="playedAudioFormat">The audio format which the audio must be transformed after decoding and before playing</param>
        /// <param name="enableAec">Whether to enable acoustic echo cancellation</param>
        /// <param name="enableDenoise">Whether to enable denoising</param>
        /// <param name="enableAgc">Whether to enable automatic gain control</param>
        /// <param name="playedResampler">The resampler which should be used on played audio</param>
        /// <param name="recordedResampler">The resampler which should be used on recorded audio</param>
        public WebRtcFilter(int expectedAudioLatency, int filterLength,
                            AudioFormat recordedAudioFormat, AudioFormat playedAudioFormat,
                            bool enableAec, bool enableDenoise, bool enableAgc,
                            IAudioFilter playedResampler = null, IAudioFilter recordedResampler = null) :
            base(expectedAudioLatency, filterLength, recordedAudioFormat, playedAudioFormat, playedResampler, recordedResampler)
        {
            // Default settings.
            var aecConfig = new AecConfig(FilterLength, recordedAudioFormat.SamplesPerFrame, recordedAudioFormat.SamplesPerSecond)
            {
                NlpMode     = AecNlpMode.KAecNlpModerate,
                SkewMode    = false,
                MetricsMode = false
            };

            _ns  = new NoiseSuppressor(recordedAudioFormat);
            _aec = new AecCore(aecConfig);

            if (aecConfig.NlpMode != AecNlpMode.KAecNlpConservative &&
                aecConfig.NlpMode != AecNlpMode.KAecNlpModerate &&
                aecConfig.NlpMode != AecNlpMode.KAecNlpAggressive)
            {
                throw new ArgumentException();
            }

            _aec.targetSupp   = WebRtcConstants.targetSupp[(int)aecConfig.NlpMode];
            _aec.minOverDrive = WebRtcConstants.minOverDrive[(int)aecConfig.NlpMode];

            if (aecConfig.MetricsMode && aecConfig.MetricsMode != true)
            {
                throw new ArgumentException();
            }
            _aec.metricsMode = aecConfig.MetricsMode;
            if (_aec.metricsMode)
            {
                _aec.InitMetrics();
            }
            _enableAec     = enableAec;
            _enableDenoise = enableDenoise;
            _enableAgc     = enableAgc;

            _agc = new Agc(0, 255, Agc.AgcMode.AgcModeAdaptiveDigital, (uint)recordedAudioFormat.SamplesPerSecond);
        }
Пример #15
0
        public VolumeControl(IAudioFilter source, double startingVolume, double leftBassAdjust, double leftMidAdjust, double leftMidUpperAdjust, double leftTrebAdjust, double rightBassAdjust, double rightMidAdjust, double rightMidUpperAdjust, double rightTrebAdjust)
        {
            this.source = source;

            this.numberOfChannels = source.NumberOfChannels;

            this.volume = new double[this.numberOfChannels];

            for (int i = 0; i < this.volume.Length; i++)  //volume[0] = LW; volume[1] = RW; volume[2] = LML; volume[3] = RML; volume[4] = LMU; volume[5] = RMU; volume[6] = LT; volume[7] = RT
            {
                volume[0] = startingVolume * leftBassAdjust;
                volume[1] = startingVolume * rightBassAdjust;
                volume[2] = startingVolume * leftMidAdjust;
                volume[3] = startingVolume * rightMidAdjust;
                volume[4] = startingVolume * leftMidUpperAdjust;
                volume[5] = startingVolume * rightMidUpperAdjust;
                volume[6] = startingVolume * leftTrebAdjust;
                volume[7] = startingVolume * rightTrebAdjust;
            }
        }
Пример #16
0
        /// <summary>
        /// Initializes the echo canceller.
        /// </summary>
        /// <param name="systemLatency">The amount of latency that the operating environment adds (in milliseconds).
        /// Determines how long a played frame is held before being submitted to the echo canceller.
        /// For Silverlight v4, this is typically ~150ms.</param>
        /// <param name="filterLength">The length of the echo cancellation filter in milliseconds (typically ~150).</param>
        /// <param name="recordedAudioFormat">The format of the recorded audio</param>
        /// <param name="playedAudioFormat">The format of the played audio</param>
        /// <param name="playedResampler">An instance of an IAudioFilter&lt;short&gt; which can be used to resample or synchronize played frames.</param>
        /// <param name="recordedResampler">An instance of an IAudioFilter&lt;short&gt; which can be used to resample or synchronize played frames.</param>
        protected EchoCancelFilter(int systemLatency, int filterLength, AudioFormat recordedAudioFormat, AudioFormat playedAudioFormat, IAudioFilter playedResampler = null, IAudioFilter recordedResampler = null)
        {
            _recordedAudioFormat = recordedAudioFormat;
            _playedAudioFormat   = playedAudioFormat;

            // We need to resample the audio we play (typically 16Khz) so that it matches the audio we
            // get from the AudioSinkAdapter (sometimes 16Khz, but often 8Khz); otherwise, the echo cancellation
            // wouldn't work.
            if (playedResampler == null)
            {
                playedResampler = new ResampleFilter(playedAudioFormat, recordedAudioFormat);
                playedResampler.InstanceName = "EchoCanceller_PlayedResampler";
            }

            // We don't typically need to resample the audio we get from the AudioSinkAdapter, but
            // this is here for historical reasons, as we have at times in the past tried to experiment with
            // synchronizing the played and the recorded streams, to account for differences in clock speed.
            // In general, that didn't seem to work, but I like the architectural ability to specify
            // a resampler here, so I've kept it in the pipeline.
            if (recordedResampler == null)
            {
                recordedResampler = new NullAudioFilter(recordedAudioFormat.SamplesPerFrame * sizeof(short));
                recordedResampler.InstanceName = "EchoCanceller_RecordedResampler";
            }

            _logger          = new EchoCancelFilterLogger();
            SystemLatency    = systemLatency;
            FilterLength     = filterLength * (recordedAudioFormat.SamplesPerSecond / 1000);
            SamplesPerFrame  = recordedAudioFormat.SamplesPerFrame;
            SamplesPerSecond = recordedAudioFormat.SamplesPerSecond;
            _recorded        = new short[SamplesPerFrame];

            // Configure the latency queue.
            QueueSize     = Math.Max(systemLatency / recordedAudioFormat.MillisecondsPerFrame, 1);
            _maxQueueSize = QueueSize + 1;
            _playedQueue  = new Queue <short[]>();

            _playedResampler   = playedResampler;
            _recordedResampler = recordedResampler;
        }
Пример #17
0
        public DeemphasisFilter(IAudioFilter source, Deemphasis deemphasis, bool filterEnabled)
        {
            if (source.NumberOfChannels != 2)
            {
                throw new ArgumentException("Audio source must have 2 channels", "source");
            }

            if (source.SampleRate != deemphasis.sampleRate)
            {
                throw new ArgumentException("Crossover sample rate does not match input source", "crossover");
            }

            this.source            = source;
            this.deemphasis        = deemphasis;
            this.filterEnabled     = filterEnabled;
            this.lastFilterEnabled = filterEnabled;

            this.currentState = States.Normal;

            this.lastOverallPosition     = 0;
            this.lastInputBufferPosition = -1;

            initializeFilterKernels();
        }
Пример #18
0
 public abstract void ApplyFilter(IAudioFilter filter);
Пример #19
0
 public override void ApplyFilter(IAudioFilter filter)
 {
     filter.Apply(this);
 }
Пример #20
0
        /// <summary>
        /// Initializes the echo canceller.
        /// </summary>
        /// <param name="systemLatency">The amount of latency that the operating environment adds (in milliseconds).
        /// Determines how long a played frame is held before being submitted to the echo canceller.
        /// For Silverlight v4, this is typically ~150ms.</param>
        /// <param name="filterLength">The length of the echo cancellation filter in milliseconds (typically ~150).</param>
        /// <param name="recordedAudioFormat">The format of the recorded audio</param>
        /// <param name="playedAudioFormat">The format of the played audio</param>
        /// <param name="playedResampler">An instance of an IAudioFilter&lt;short&gt; which can be used to resample or synchronize played frames.</param>
        /// <param name="recordedResampler">An instance of an IAudioFilter&lt;short&gt; which can be used to resample or synchronize played frames.</param>
        protected EchoCancelFilter(int systemLatency, int filterLength, AudioFormat recordedAudioFormat, AudioFormat playedAudioFormat, IAudioFilter playedResampler = null, IAudioFilter recordedResampler = null)
        {
            this.recordedAudioFormat = recordedAudioFormat;
            this.playedAudioFormat   = playedAudioFormat;

            if (playedResampler == null)
            {
                // playedResampler = new NullAudioFilter(recordedAudioFormat.SamplesPerFrame * sizeof(short)) { InstanceName = "EchoCanceller_played" };
                playedResampler = new ResampleFilter(playedAudioFormat, recordedAudioFormat);
                // var pr = new ResampleFilter<short>(sizeof(short), samplesPerSecond, channels, sizeof(short), samplesPerSecond, channels, samplesPerFrame * sizeof(short));
            }

            if (recordedResampler == null)
            {
                recordedResampler = new NullAudioFilter(recordedAudioFormat.SamplesPerFrame * sizeof(short));
                // var rr = new ResampleFilter(sizeof(short), samplesPerSecond, channels, sizeof(short), samplesPerSecond, channels, samplesPerFrame * sizeof(short));
                // recordedResampler = new ResampleFilter(playedAudioFormat, recordedAudioFormat);
                recordedResampler.InstanceName = "EchoCanceller_recorded";
            }

            logger           = new EchoCancelFilterLogger();
            SystemLatency    = systemLatency;
            FilterLength     = filterLength * (recordedAudioFormat.SamplesPerSecond / 1000);
            SamplesPerFrame  = recordedAudioFormat.SamplesPerFrame;
            SamplesPerSecond = recordedAudioFormat.SamplesPerSecond;
            recorded         = new short[SamplesPerFrame];

            // Configure the latency queue.
            QueueSize    = Math.Max(systemLatency / recordedAudioFormat.MillisecondsPerFrame, 1);
            maxQueueSize = QueueSize + 1;
            playedQueue  = new Queue <short[]>();

            this.playedResampler   = playedResampler;
            this.recordedResampler = recordedResampler;
        }
Пример #21
0
        private void btnPlay_Click(object sender, EventArgs e)
        {
            IAudioFilter waveRdr;

            // For the purposes of testing we process .raw files as
            // containing 16-bit stereo PCM data at 44.1KHz.  This is much
            // like a .wav file but without the header.
            if (lblFileName.Text.EndsWith(".raw"))
            {
                waveRdr = new RawFileReader(lblFileName.Text, 16, 44100, 2);
            }
            else
            {
                IWaveSource inputFile = CodecFactory.Instance.GetCodec(lblFileName.Text);

                waveRdr = new WaveSourceReader(inputFile);
            }

            // Convert to 24 bit samples (if needed)
            ConvertBitsPerSample bpsConvert = new ConvertBitsPerSample(waveRdr, 24);

            // Create a crossover and catch any errors in case the frequency parameters
            // are not valid
            Crossover crossover;

            try
            {
                crossover = Crossover.createThreeWayCrossover(Int32.Parse(txtLowerFreq.Text),
                                                              Int32.Parse(txtUpperFreq.Text), Int32.Parse(txtLowerTrans.Text), Int32.Parse(txtUpperTrans.Text),
                                                              waveRdr.SampleRate, true, false, 10.0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Error building crossover: " + ex.Message);
                return;
            }

            // Start creating filters...
            CrossoverThreeWayFilter filter = new CrossoverThreeWayFilter(bpsConvert, crossover);

            sampleTap = new SampleTap(filter, SAMPLE_LEN);



            int crossoverIdx = 1;

            if (rdoPlayWoofer.Checked)
            {
                crossoverIdx = 0;
            }
            else if (rdoPlayMidrange.Checked)
            {
                crossoverIdx = 1;
            }
            else if (rdoPlayTweeter.Checked)
            {
                crossoverIdx = 2;
            }

            channelSelectFilter = new SelectCrossover(sampleTap, crossoverIdx);

            volumeControlFilter = new VolumeControl(channelSelectFilter);

            // Only needed for playback through Windows
            lastFilterStep = new ConvertBitsPerSample(volumeControlFilter, 16);

            // Done creating filters...



            tbarFilePosition.Value = 0;
            // Max in seconds
            tbarFilePosition.Maximum = (int)(lastFilterStep.Length / lastFilterStep.SampleRate / lastFilterStep.NumberOfChannels);
            tbarFilePosition.Enabled = true;


            // Playback through Windows
            IWaveSource finalWaveSource = new FilterToWaveSource(lastFilterStep);

            //_soundOut = new WasapiOut();
            soundOutDevice = new WaveOut();
            soundOutDevice.Initialize(finalWaveSource);
            soundOutDevice.Play();

            tmrUpdateViz.Start();
        }
Пример #22
0
 public override IAudioFilter Process(IAudioFilter input)
 {
     return(ConfigHelper.MakeXMLDuplicate(FilterProcess).ApplyTo(input));
 }
Пример #23
0
 public TimeDomainEchoCancelFilter(int systemLatency, int filterLength, AudioFormat recordedAudioFormat, AudioFormat playedAudioFormat, IAudioFilter playedResampler = null, IAudioFilter recordedResampler = null) :
     base(systemLatency, filterLength, recordedAudioFormat, playedAudioFormat, playedResampler, recordedResampler)
 {
     TimeDomainInit(recordedAudioFormat.SamplesPerSecond);
 }
Пример #24
0
        public SpeexEchoCancelFilter(int systemLatency, int filterLength, AudioFormat recordedAudioFormat, AudioFormat playedAudioFormat, IAudioFilter playedResampler = null, IAudioFilter recordedResampler = null) :
            base(systemLatency, filterLength, recordedAudioFormat, playedAudioFormat, playedResampler, recordedResampler)
        {
            _speexEchoState = SpeexEchoCanceller.speex_echo_state_init(recordedAudioFormat.SamplesPerFrame, filterLength, _logger);
            object sampleRate = recordedAudioFormat.SamplesPerSecond;

            SpeexEchoCanceller.speex_echo_ctl(_speexEchoState, EchoControlCommand.SetSamplingRate, ref sampleRate);
        }
Пример #25
0
 /// <summary>
 /// Adds an audio filter to the chain.
 /// </summary>
 /// <param name="filter"></param>
 public void Add(IAudioFilter filter)
 {
     lock(_filters)
         _filters.Add(filter);
     if (_filters.Count == 1)
         InputAudioFormat = filter.InputAudioFormat;
     OutputAudioFormat = filter.OutputAudioFormat;
 }
Пример #26
0
 public abstract void ApplyFilter(IAudioFilter var1);
Пример #27
0
 public FilterToSampleSource(IAudioFilter source)
 {
     this.source = source;
 }