如何记录具有2个以上通道的输入设备到MP3格式

发布于 2025-02-11 14:56:43 字数 2270 浏览 1 评论 0原文

我正在构建一个录制软件,用于将所有连接的设备录制到MP3格式中。 这是我的代码:

IWaveIn _captureInstance = inputDevice.DataFlow == DataFlow.Render ?
                        new WasapiLoopbackCapture(inputDevice) : new WasapiCapture(inputDevice)

var waveFormatToUse = _captureInstance.WaveFormat;
var sampleRateToUse = waveFormatToUse.SampleRate;
var channelsToUse = waveFormatToUse.Channels;

if (sampleRateToUse > 48000)        // LameMP3FileWriter doesn't support a rate more than 48000Hz
{
    sampleRateToUse = 48000;
}
else if (sampleRateToUse < 8000)    // LameMP3FileWriter doesn't support a rate less than 8000Hz
{
    sampleRateToUse = 8000;
}

if (channelsToUse > 2)              // LameMP3FileWriter doesn't support a number of channels more than 2
{
    channelsToUse = 2;
}

waveFormatToUse = WaveFormat.CreateCustomFormat(_captureInstance.WaveFormat.Encoding,
                                                sampleRateToUse,
                                                channelsToUse,
                                                _captureInstance.WaveFormat.AverageBytesPerSecond,
                                                _captureInstance.WaveFormat.BlockAlign,
                                                _captureInstance.WaveFormat.BitsPerSample);

_mp3FileWriter = new LameMP3FileWriter(_currentStream, waveFormatToUse, 32);

此代码正常工作,除了连接的设备(也称为Steelseries Sonar)具有2个以上的频道的情况。 在有2个以上的频道的情况下,所有录音仅具有噪声。

我该如何解决这个问题?不需要仅使用lamemp3filewriter,我只需要它将mp3或任何具有良好压缩的格式。另外,如果可以在磁盘上保存中间文件(内存中的所有处理),则只有带有音频的最终文件。

我的录制代码:

// When the capturer receives audio, start writing the buffer into the mentioned file
    _captureInstance.DataAvailable += (s, a) =>
    {
        lock (_writerLock)
        {
            // Write buffer into the file of the writer instance
            _mp3FileWriter?.Write(a.Buffer, 0, a.BytesRecorded);
        }
    };

    // When the Capturer Stops, dispose instances of the capturer and writer
    _captureInstance.RecordingStopped += (s, a) =>
    {
        lock (_writerLock)
        {
            _mp3FileWriter?.Dispose();
        }

        _captureInstance?.Dispose();
    };

    // Start audio recording
    _captureInstance.StartRecording();

I am building a recording software for recording all connected devices to PC into mp3 format.
Here is my code:

IWaveIn _captureInstance = inputDevice.DataFlow == DataFlow.Render ?
                        new WasapiLoopbackCapture(inputDevice) : new WasapiCapture(inputDevice)

var waveFormatToUse = _captureInstance.WaveFormat;
var sampleRateToUse = waveFormatToUse.SampleRate;
var channelsToUse = waveFormatToUse.Channels;

if (sampleRateToUse > 48000)        // LameMP3FileWriter doesn't support a rate more than 48000Hz
{
    sampleRateToUse = 48000;
}
else if (sampleRateToUse < 8000)    // LameMP3FileWriter doesn't support a rate less than 8000Hz
{
    sampleRateToUse = 8000;
}

if (channelsToUse > 2)              // LameMP3FileWriter doesn't support a number of channels more than 2
{
    channelsToUse = 2;
}

waveFormatToUse = WaveFormat.CreateCustomFormat(_captureInstance.WaveFormat.Encoding,
                                                sampleRateToUse,
                                                channelsToUse,
                                                _captureInstance.WaveFormat.AverageBytesPerSecond,
                                                _captureInstance.WaveFormat.BlockAlign,
                                                _captureInstance.WaveFormat.BitsPerSample);

_mp3FileWriter = new LameMP3FileWriter(_currentStream, waveFormatToUse, 32);

This code works properly, except the cases when a connected device (also virtual as SteelSeries Sonar) has more than 2 channels.
In the case with more than 2 channels all recordings with noise only.

How can I solve this issue? It isn't required to use only LameMP3FileWriter, I only need it will mp3 or any format with good compression. Also if it's possible without saving intermediate files on the disk (all processing in memory), only the final file with audio.

My recording code:

// When the capturer receives audio, start writing the buffer into the mentioned file
    _captureInstance.DataAvailable += (s, a) =>
    {
        lock (_writerLock)
        {
            // Write buffer into the file of the writer instance
            _mp3FileWriter?.Write(a.Buffer, 0, a.BytesRecorded);
        }
    };

    // When the Capturer Stops, dispose instances of the capturer and writer
    _captureInstance.RecordingStopped += (s, a) =>
    {
        lock (_writerLock)
        {
            _mp3FileWriter?.Dispose();
        }

        _captureInstance?.Dispose();
    };

    // Start audio recording
    _captureInstance.StartRecording();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-02-18 14:56:46

如果la脚不支持两个以上的频道,则无法将此编码器用于您的目的。您是否使用 fraunhofer环绕mp3编码器?

链接:

https://download.cnet.com/mp3-surround-encoder/3000-2140_4-165541.html在大多数音频格式(带有C#代码示例)之间: https://www.codeproject.com/articles/501521/how-to-convert-bet-most-most-most-audio-formats-in-net-in-net

If LAME doesn't support more than 2 channels, you can't use this encoder for your purpose. Have you tried it with the Fraunhofer surround MP3 encoder?

Link: https://download.cnet.com/mp3-surround-encoder/3000-2140_4-165541.html

Also, here's a nice article discussing how to convert between most audio formats (with C# code samples): https://www.codeproject.com/articles/501521/how-to-convert-between-most-audio-formats-in-net

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文