.NET オーディオ/MIDIライブラリ「NAudio」 を使って、C#言語でイコライザを作ります。なお、開発環境はVisual Studio 2013 ExpressでWindows 7 Proです。
NAudioライブラリのインストールおよび設定
NAudioライブラリは「naudio/NAudio」から「NAudio-Release.zip」をダウンロードし、解凍したライブラリを、Visual Studio 2013 Expressの「参照の追加」によりプロジェクトに追加します。
イコライザソフトの作成
イコライザソフトは、デモコード「NAudio/NAudioWpfDemo/EqualizationDemo/」を参照して作成します。デモソフトはWPF上で動作し、今回はWindows Formで動作します。
- 17行目から24行目はイコライザの部分で、それぞれの周波数のゲインを指定します。
- 32行目で再生したいファイル名「sample.wav」を指定します。再生したいファイル名を「sample.wav」として実行フォルダに設定してください。
Form1.cs
using NAudio.Dsp;
using NAudio.Wave;
public partial class Form1 : Form
{
private AudioFileReader reader;
private IWavePlayer player;
private Equalizer equalizer;
private string selectedFile;
private readonly EqualizerBand[] bands;
public Form1()
{
InitializeComponent();
bands = new EqualizerBand[]
{
new EqualizerBand {Bandwidth = 0.8f, Frequency = 100, Gain =17},
new EqualizerBand {Bandwidth = 0.8f, Frequency = 200, Gain =7},
new EqualizerBand {Bandwidth = 0.8f, Frequency = 400, Gain =3},
new EqualizerBand {Bandwidth = 0.8f, Frequency = 800, Gain =4},
new EqualizerBand {Bandwidth = 0.8f, Frequency = 1200, Gain = -1},
new EqualizerBand {Bandwidth = 0.8f, Frequency = 2400, Gain = -3},
new EqualizerBand {Bandwidth = 0.8f, Frequency = 4800, Gain = 7},
new EqualizerBand {Bandwidth = 0.8f, Frequency = 9600, Gain = -13},
};
}
private void buttonPlay_Click(object sender, EventArgs e)
{
selectedFile = "sample.wav";
reader = new AudioFileReader(selectedFile);
equalizer = new Equalizer(reader, bands);
player = new WaveOutEvent();
player.Init(equalizer);
player.Play();
}
}
デモコードは、48行目に示すコメントのように記述されています。これを、49行目から55行目のように書き換えます。
Equalizer.cs
class Equalizer : ISampleProvider
{
/// <summary>
/// Basic example of a multi-band eq
/// uses the same settings for both channels in stereo audio
/// Call Update after you've updated the bands
/// Potentially to be added to NAudio in a future version
/// </summary>
class Equalizer : ISampleProvider
{
private readonly ISampleProvider sourceProvider;
private readonly EqualizerBand[] bands;
private readonly BiQuadFilter[,] filters;
private readonly int channels;
private readonly int bandCount;
private bool updated;
public Equalizer(ISampleProvider sourceProvider, EqualizerBand[] bands)
{
this.sourceProvider = sourceProvider;
this.bands = bands;
channels = sourceProvider.WaveFormat.Channels;
bandCount = bands.Length;
filters = new BiQuadFilter[channels, bands.Length];
CreateFilters();
}
private void CreateFilters()
{
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++)
{
var band = bands[bandIndex];
for (int i = 0; i < channels; i++)
{
if (filters[i, bandIndex] == null)
filters[i, bandIndex] = BiQuadFilter.PeakingEQ(sourceProvider.WaveFormat.SampleRate, band.Frequency, band.Bandwidth, band.Gain);
else
filters[i, bandIndex].SetPeakingEq(sourceProvider.WaveFormat.SampleRate, band.Frequency, band.Bandwidth, band.Gain);
}
}
}
public void Update()
{
updated = true;
CreateFilters();
}
// public WaveFormat WaveFormat => sourceProvider.WaveFormat;
public WaveFormat WaveFormat
{
get
{
return sourceProvider.WaveFormat;
}
}
public int Read(float[] buffer, int offset, int count)
{
int samplesRead = sourceProvider.Read(buffer, offset, count);
if (updated)
{
CreateFilters();
updated = false;
}
for (int sample = 0; sample <samplesRead; sample++)
{
int ch = sample % channels;
for (int band = 0; band < bandCount; band++)
{
buffer[offset + sample] = filters[ch, band].Transform(buffer[offset + sample]);
}
}
return samplesRead;
}
}
EqualizerBand.cs
class EqualizerBand
{
public float Frequency { get; set; }
public float Gain { get; set; }
public float Bandwidth { get; set; }
}
イコライザソフトの実行
イコライザソフトを実行すると、次の画面が表示されます。
「Play」ボタンを押すと、sample.wavファイルが再生されます。イコライザの定義の「new EqualizerBand {Bandwidth = 0.8f, Frequency = 100, Gain =17}」のゲインを上げて再生すると低音が響きます。イコライザの定義「 new EqualizerBand {Bandwidth = 0.8f, Frequency = 9600, Gain = -13}」のゲインを上げて再生すると高音が響きます
