『PongeRecoder』を作った


 PC上の音を録音するために自作した。
ボタンを押すと録音開始、もう一度押すと停止。
録音したwavデータは実行ファイルと同じ場所に保存される。

ソフト置き場

drive.google.com

コード

//NAudio 2.1.0
//Copyright 2020 Mark Heath
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using Microsoft.VisualBasic;
using Microsoft.VisualBasic.Devices;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using System;
using System.IO;
using System.Windows;

namespace WpfAppTest1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        WasapiLoopbackCapture? _WasapiLoopCapture;
        Audio? _Speaker;
        WaveFileWriter? _WaveFileWriter;

        string _SilenceSoundPath;
        string _OutputPath;

        public MainWindow()
        {
            this._OutputPath = Directory.GetCurrentDirectory();
            this._SilenceSoundPath = System.IO.Path.Combine(_OutputPath, @"resource\silence.wav");

            InitializeComponent();
        }

        private void Button_Start_Click(object sender, RoutedEventArgs e)
        {
            if (this.Btn_Record.Content.ToString() == "Start")
            {
                //保存ファイルの名の決定
                var fileName = "Sound";
                var fileType = ".wav";

                if (this.SaveFileName.Text != string.Empty) fileName = this.SaveFileName.Text;
                var outputFilePath = System.IO.Path.Combine(this._OutputPath, fileName + fileType);

                _WasapiLoopCapture = new WasapiLoopbackCapture();
                _WaveFileWriter = new WaveFileWriter(outputFilePath, _WasapiLoopCapture.WaveFormat);

                _WasapiLoopCapture.DataAvailable += (s, e) =>
                {
                    _WaveFileWriter.Write(e.Buffer, 0, e.BytesRecorded);
                };

                //キャプチャ終了時のイベント
                _WasapiLoopCapture.RecordingStopped += (s, e) =>
                {
                    _WaveFileWriter.Dispose();
                    _WaveFileWriter = null;
                    _Speaker?.Stop();
                    _WasapiLoopCapture.Dispose();
                };

                //無音を鳴らす
                this._Speaker = new Audio();
                new Audio().Play(this._SilenceSoundPath, AudioPlayMode.BackgroundLoop);

                _WasapiLoopCapture.StartRecording();

                this.RecodingStatus.Text = "Recoding";
                this.Btn_Record.Content = "Stop";
            }
            else if (this.Btn_Record.Content.ToString() == "Stop")
            {
                _WasapiLoopCapture?.StopRecording();

                this.RecodingStatus.Text = "No Recoding";
                this.Btn_Record.Content = "Start";
            }

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            _WasapiLoopCapture?.StopRecording();
        }
    }
}

naudio

github.com
 ライセンスはMIT。
C#でオーディオライブラリ使うならこれ、みたいな紹介を見かけたので使用。
WasapiLoopbackCaptureを使うことでPC上の音を取得できる。
ただし音が鳴ってない時は録音イベントが発生しないので、「無音」を再生しておく必要がある。

Costura.Fody

github.com
 dllとexeをまとめてしまうやつ。初めて使ったが何も考えなくても使えるのはラク
最新バージョンではビルド失敗したので安定バージョンの5.7.0を使用。