Hope is a Dream. Dream is a Hope.

非公開ブログは再開しました。

NAudioで信号処理 (その5)

NAudioで信号処理 (目次) - Hope is a Dream. Dream is a Hope.

NAudioで信号処理 (その5)

f:id:hope_is_dream:20170506124450p:plain

エフェクト処理の準備

C# Audio Tutorial 5 - EffectStream Part 1

Tutorial 5ではエフェクト処理の準備をします。WaveFIleReaderを使ってロードしたデータに対して、自作関数EffectStream(WaveStream継承)を間に挟みます。 これにより、任意の信号処理の後にBLockAlignReductionStreamを通して、再生が可能になります。

※ 本チュートリアルでは、エフェクトはかけてません。

 // Audio Chain
WaveChannel32 wave = new WaveChannel32(new WaveFileReader(open.FileName));
EffectStream effect = new EffectStream(wave); // エフェクトをかけるためのパイプライン
stream = new BlockAlignReductionStream(effect);

output = new DirectSoundOut(200);
output.Init(stream);
output.Play();
using System;
using System.Windows.Forms;
using NAudio.Wave;

namespace Tutorial
{
    public partial class Form1 : Form
    {
        #region form
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// フォームの初期設定。
        /// </summary>
        private void Form1_Load(object sender, EventArgs e)
        {
            // ボタンの初期状態
            this.Text = @"Tutorial 6 - Audio Loopback using";
            linkLabel1.Text = "https://www.youtube.com/watch?v=BjnTgIdTXwI";
        }

        #endregion

        #region Member
        private DirectSoundOut output = null;
        private BlockAlignReductionStream stream = null;
        #endregion


        /// <summary>
        /// フォームのクロージング処理
        /// Wave関連オブジェクトのDispose処理を担当
        /// </summary>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            output?.Dispose();
            output = null;

            stream?.Dispose();
            stream = null;
        }

        private void button_OpenWave_Click(object sender, EventArgs e)
        {
            // WAV File Open
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "WAV File (*.wav)|*.wav;";
            if (open.ShowDialog() != DialogResult.OK) return;

            // Audio Chain
            WaveChannel32 wave = new WaveChannel32(new WaveFileReader(open.FileName));
            EffectStream effect = new EffectStream(wave); // エフェクトをかけるためのパイプライン
            stream = new BlockAlignReductionStream(effect);

            output = new DirectSoundOut(200);
            output.Init(stream);
            output.Play();

        }
    }

}

EffectStream.cs

using System;

using NAudio.Wave;

namespace Tutorial
{
    public class EffectStream : WaveStream
    {
        public WaveStream SourceStream { get; set; }

        public EffectStream(WaveStream stream)
        {
            this.SourceStream = stream;
        }

        public override WaveFormat WaveFormat
        {
            get { return this.SourceStream.WaveFormat; }
        }

        public override long Length
        {
            get { return SourceStream.Length; }
        }

        public override long Position
        {
            get { return this.SourceStream.Position; }
            set { this.SourceStream.Position = value; }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            //*****************************************************************//
            // ここで信号処理をする
            //*****************************************************************//
            Console.WriteLine("DirectSoundOut request {0} bytes", count);
            return this.SourceStream.Read(buffer, offset, count);
        }
    }
}