NAudio | 参考 - peace098beat/windows_applicaciton GitHub Wiki

Youtubeチュートリアル

C# Audio Tutorial 1 - Wave File with NAudio
C# Audio Tutorial 2 - MP3/WAV File with NAudio
C# Audio Tutorial 3 - Convert MP3 File to Wave File
C# Audio Tutorial 4 - Custom WaveStream Object
C# Audio Tutorial 5 - EffectStream Part 1
C# Audio Tutorial 6 - Audio Loopback using NAudio
C# Audio Tutorial 7 : Recording to a Wave File
C# Audio Tutorial 8 - EffectStream Part 2
C# Audio Tutorial 9 - EffectStream Part 3 (Echo!)
C# Audio Tutorial 10 - Plotting Audio Waveforms
C# Audio Tutorial 11 - Plotting Audio Waveforms Part 2

.NETで音声処理を試してみる NAudio編 第1回

NAudioはマイクからの入力やスピーカーへの出力をキャプチャしてファイルに出力したりできます。 そうです。録音ソフトを作成できます。今回は、ヘッドフォンやスピーカーへの出力をファイルに書き出すサンプルを作りました。

MMDeviceEnumerator, DataAvailable, WasapiLoopbackCapture, WaveFileWriter,

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.CoreAudioApi;
using NAudio.Wave;

namespace NAudioDemo.Models
{

    public sealed class AudioOutputWriter : IDisposable
    {

        #region イベント

        public event EventHandler<WaveInEventArgs> DataAvailable;

        #endregion

        #region フィールド

        private readonly string _FileName;

        private WasapiLoopbackCapture _WaveIn;

        private Stream _Stream;

        private WaveFileWriter _WaveFileWriter;

        #endregion

        #region コンストラクタ

        public AudioOutputWriter(string fileName, MMDevice device)
        {
            if (device == null)
                throw new ArgumentNullException(nameof(device));

            this._FileName = fileName;
            this._WaveIn = new WasapiLoopbackCapture(device);
            this._WaveIn.DataAvailable += this.WaveInOnDataAvailable;
            this._WaveIn.RecordingStopped += this.WaveInOnRecordingStopped;

            this._Stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Write);
            this._WaveFileWriter = new WaveFileWriter(this._Stream, this._WaveIn.WaveFormat);
        }

        #endregion

        #region プロパティ

        public bool IsRecording
        {
            get;
            private set;
        }

        #endregion

        #region メソッド

        public void Start()
        {
            this.IsRecording = true;
            this._WaveIn.StartRecording();
        }

        public void Stop()
        {
            this.IsRecording = false;
            this._WaveIn.StopRecording();
        }

        #region オーバーライド
        #endregion

        #region イベントハンドラ

        private void WaveInOnRecordingStopped(object sender, StoppedEventArgs e)
        {
            if (this._WaveFileWriter != null)
            {
                this._WaveFileWriter.Close();
                this._WaveFileWriter = null;
            }

            if (this._Stream != null)
            {
                this._Stream.Close();
                this._Stream = null;
            }

            this.Dispose();
        }

        private void WaveInOnDataAvailable(object sender, WaveInEventArgs e)
        {
            this._WaveFileWriter.Write(e.Buffer, 0, e.BytesRecorded);
            this.DataAvailable?.Invoke(this, e);
        }

        #endregion

        #region ヘルパーメソッド
        #endregion

        #endregion

        #region IDisposable メンバー

        public void Dispose()
        {
            this._WaveIn.DataAvailable -= this.WaveInOnDataAvailable;
            this._WaveIn.RecordingStopped -= this.WaveInOnRecordingStopped;

            this._WaveIn?.Dispose();
            this._WaveFileWriter?.Dispose();
            this._Stream?.Dispose();
        }

        #endregion

    }

}

【C#】PCのマイクで拾った音をPepperに送って再生させる【qi Framework】

C#で声の入力を拾う方法ですが、音声出力のプログラムでもお世話になったNAudioは当然と言わんばかりに音声入力にも対応しているため、再びNAudioを使います。入力を拾うにはWaveInEventクラスを利用すればよく、使い方もそんなに複雑ではありません。例えばPCにマイク入力を入れ、そのままエコーバック的に出力する場合は次のようなコードを書きます。

WaveInEvent, DataAvailable, StartRecording, StopRecording


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using NAudio.Wave;
using NAudio.CoreAudioApi;
 
namespace NAudioEchoBack
{
    class Program
    {
        static void Main(string[] args)
        {
            var mmDevice = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
 
            using (var waveIn = new WaveInEvent())
            using (var wavPlayer = new WasapiOut(mmDevice, AudioClientShareMode.Shared, false, 300))
            {
                //フォーマットを入力側に合わせないと速度やらなんやらおかしくなるので注意!
                var wavProvider = new BufferedWaveProvider(waveIn.WaveFormat);
                //NOTE: うるさいと思ったらボリュームを絞る
                //wavPlayer.Volume = 0.01f;
                wavPlayer.Init(new VolumeWaveProvider16(wavProvider));
                wavPlayer.Play();
 
                waveIn.DataAvailable += (_, e) =>
                {
                    wavProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
                };
                waveIn.StartRecording();
                Console.WriteLine("Press ENTER to quit...");
                Console.ReadLine();
                waveIn.StopRecording();
            }
            Console.WriteLine("Program ended successfully.");
        }
    }
}

using (var waveIn = new WaveInEvent())
{
    waveIn.DataAvailable += (_, e) =>
    {
        //byte[]であるe.Bufferのうち先頭からe.BytesRecorded個が有効な録音データなので
        //それを使って何かする
    };
    //音声の取得開始
    waveIn.StartRecording();
 
    //他の処理
 
    //終了
    waveIn.StopRecording();
}