简体   繁体   中英

How to record an input device with more than 2 channels to mp3 format

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM