/// <summary> /// パラメータを指定して新しい OverSampling クラスのインスタンスを初期化します。 /// </summary> /// <param name="samplingRate">サンプリング周波数。これはオーバーサンプリング後の周波数となります。</param> /// <param name="magnification">サンプリング倍率。</param> /// <param name="stereo">ステレオである場合は true、モノラルである場合は false。</param> /// <param name="filterSize">フィルタサイズ。</param> public OverSampling(double samplingRate, int magnification, bool stereo, int filterSize) { if (samplingRate <= 0.0) throw new ArgumentOutOfRangeException("samplingRate"); if (magnification <= 0) throw new ArgumentOutOfRangeException("magnification"); if (filterSize <= 0) throw new ArgumentOutOfRangeException("filterSize"); if (filterSize % 2 != 0) throw new ArgumentException(); this.samplingRate = samplingRate; this.magnification = magnification; this.stereo = stereo; this.filterSize = filterSize; this.filter = new SoundFilter(stereo, filterSize); var filterGenerator = new LowPassFilter() { SamplingRate = samplingRate * magnification, CutoffFrequency = samplingRate / 2 - FiniteImpulseResponse.GetDelta(samplingRate * magnification, filterSize) }; double[] impulse = filterGenerator.Generate(filterSize / 2); Window.Blackman(impulse); filter.SetFilter(impulse); }
private void ExportLoop(ExportData data) { using (FileStream fs = new FileStream(this.textBox_saveto.Text, FileMode.Create)) using (WaveFormatWriter wfw = new WaveFormatWriter(fs, data.SamplingRate, data.Bit * 8, 2)) { const int bufferSize = 512; const int filterSize = 4096; float[] buffer = new float[bufferSize]; double[] buffer_double = new double[filterSize]; double[] bufferOut = new double[filterSize]; SoundFilter filter = new SoundFilter(true, filterSize); var filterGenerator = new LowPassFilter() { SamplingRate = data.SamplingRate * data.Oversampling, CutoffFrequency = data.SamplingRate / 2 - ImpulseResponse.GetDelta(data.SamplingRate * data.Oversampling, filterSize) }; double[] impulse = filterGenerator.Generate(filterSize / 2); Window.Hanning(impulse); filter.SetFilter(impulse); double bufferTime = (buffer.Length / 2.0) / (data.SamplingRate * data.Oversampling); var filterBuffer = new FilterBuffer<float>(filterSize, da => { if (data.Oversampling > 1) { for (int i = 0; i < filterSize; i++) bufferOut[i] = da[i]; filter.Filtering(bufferOut); for (int i = 0, j = 0; i < filterSize; i += data.Oversampling * 2) { buffer_double[j++] = bufferOut[i]; buffer_double[j++] = bufferOut[i + 1]; } wfw.Write(buffer_double, 0, (int)Math.Min(filterSize / data.Oversampling, data.FileSize - data.Output)); } else wfw.Write(da, 0, filterSize); data.Output = wfw.WrittenBytes; }); while (!this.reqEnd && data.FileSize > data.Output) { data.Connector.Sequencer.Progress(bufferTime); data.Connector.Master.Read(buffer, 0, bufferSize); filterBuffer.Push(buffer); if (data.SequenceEnded && data.Connector.Master.ToneCount == 0) this.reqEnd = true; } filterBuffer.Close(); this.reqEnd = true; this.Invoke(new Action(() => button_stop_Click(null, null))); } }