代码拉取完成,页面将自动刷新
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using NAudio.Dsp;
using NAudio.Wave;
using SkyJukebox.Api.Playback;
// Modified code from NAudioWpfDemo
namespace SkyJukebox.NAudioFramework
{
public class Equalizer : ISampleProvider
{
private readonly ISampleProvider _sourceProvider;
private readonly ObservableCollection<IEqualizerBand> _bands;
private readonly List<BiQuadFilter[]> _filters;
private readonly int _channels;
private bool _updated;
private object _lockObj;
public bool Enabled { get; set; }
public Equalizer(ISampleProvider sourceProvider, ObservableCollection<IEqualizerBand> bands)
{
_sourceProvider = sourceProvider;
_bands = bands;
_channels = sourceProvider.WaveFormat.Channels;
_lockObj = new object();
foreach (IEqualizerBand band in _bands) band.PropertyChanged += EqualizerBandPropertyChanged;
_bands.CollectionChanged += BandsOnCollectionChanged;
_filters = new List<BiQuadFilter[]>();
CreateFilters();
}
private void BandsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (IEqualizerBand item in e.OldItems)
{
//Removed items
item.PropertyChanged -= EqualizerBandPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (IEqualizerBand item in e.NewItems)
{
//Added items
item.PropertyChanged += EqualizerBandPropertyChanged;
}
}
Update();
}
private void EqualizerBandPropertyChanged(object sender, PropertyChangedEventArgs e)
{
Update();
}
private void CreateFilters()
{
lock (_lockObj)
{
_filters.Clear();
foreach (var band in _bands)
{
var filter = new BiQuadFilter[_channels];
for (var n = 0; n < _channels; n++)
{
filter[n] = BiQuadFilter.PeakingEQ(_sourceProvider.WaveFormat.SampleRate, band.Frequency,
band.Bandwidth, band.Gain);
}
_filters.Add(filter);
}
}
}
public void Update()
{
_updated = true;
CreateFilters();
}
public WaveFormat WaveFormat
{
get { return _sourceProvider.WaveFormat; }
}
public int Read(float[] buffer, int offset, int count)
{
var samplesRead = _sourceProvider.Read(buffer, offset, count);
if (!Enabled) return samplesRead;
if (_updated)
{
CreateFilters();
_updated = false;
}
lock (_lockObj)
{
for (var n = 0; n < samplesRead; n++)
{
var ch = n % _channels;
for (var band = 0; band < _bands.Count; band++)
{
buffer[offset + n] = _filters[band][ch].Transform(buffer[offset + n]);
}
}
}
return samplesRead;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。