NAudio 和 Powershell

问题描述 投票:0回答:1

有人尝试过使用 NAudio 并直接在 Powershell 中录音吗?

我尝试将 C# 代码翻译成 powershell.. 大部分都有效,但我很难在 Powershell 中翻译它:

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

有什么想法吗?

总的来说..我想将这个c#代码翻译成powershell:

using System;
using NAudio.Wave;

namespace RecordAudioCSharpNAudio
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a filename");

            string filename = Console.ReadLine();

            filename += ".wav";

            Console.WriteLine("Press any key to start recording...");

            Console.ReadKey();

            var waveFormat = new WaveFormat(44100, 1);

            using (var waveFile = new WaveFileWriter(filename, waveFormat))
            {
                using (var waveIn = new WaveInEvent())
                {
                    waveIn.WaveFormat = waveFormat;

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

                    waveIn.StartRecording();
                    Console.WriteLine("Recording... Press any key to stop.");
                    Console.ReadKey();

                    waveIn.StopRecording();
                    Console.WriteLine("Recording stopped.");
                }
            }
            Console.WriteLine("Audio recorded and saved as " + filename);

        }
    }
}
c# powershell naudio
1个回答
0
投票

线路:

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

正在订阅名为

DataAvailable
的事件。在这种情况下,PowerShell 翻译将通过
Register-ObjectEvent
:

try {
    $waveFormat = [NAudio.Wave.WaveFormat]::new(44100, 1)
    $waveFile = [NAudio.Wave.WaveFileWriter]::new($filename, $waveFormat)
    $waveIn = [NAudio.Wave.WaveInEvent]::new()
    $waveIn.WaveFormat = $waveFormat

    $evt = Register-ObjectEvent -InputObject $waveIn -EventName DataAvailable -Action {
        param($s, $e)
        $waveFile.Write($e.Buffer, 0, $e.BytesRecorded)
    }

    $waveIn.StartRecording()
    [Console]::WriteLine('Recording... Press any key to stop.')
    [Console]::ReadKey()
    $waveIn.StopRecording()
    [Console]::WriteLine('Recording stopped.')
}
finally {
    $waveIn.Dispose()
    $waveFormat.Dispose()
    $evt | Unregister-Event
}

[Console]::WriteLine('Audio recorded and saved as ' + $filename)
© www.soinside.com 2019 - 2024. All rights reserved.