Pergunta

I need to convert a continuous stream of PCM, or encoded audio (ADPCM, uLaw, Opus), into MP3/OGG format so that it can be streamed to a browser (using html's audio tag). I have the "stream-mp3/ogg-using-audio-tag" part working, now I need to develop the conversion layer.

I have two questions:

  1. How can I convert PCM into MP3/OGG using NAudio and/or some other C# library/framework? I assume there is a code snippet or two in the NAudio demo app that may be doing this, but I haven't been able to find it.
  2. Do I have to convert the encoded data (ADPCM, uLaw, OPUS) into PCM (which I can) before I convert it into MP3/OGG, or can the MP3/OGG 'containers' accept the encoded data?

NOTE: I understand there my be licensing issues with MP3 so we are open to using OGG.

Thanks.

Foi útil?

Solução 2

To answer your first question, to create MP3 or OGG you need an encoder. NAudio does not include an MP3 or an OGG encoder. All it does is give you ways to access encoders that are already installed on your computer (such as ACM or Media Foundation Transforms). However, with both MP3 and OGG you'll find that the easiest way is to find an unmanaged DLL or a command line utility and access that from .NET. The article I wrote which you linked to above includes a brief explanation of how you can use LAME.exe with stdin and stdout to convert PCM to MP3 on the fly.

As for your second question, the answer is yes. Whenever you transcode, you first decode to PCM, then re-encode in the target codec. I think theoretically you can put audio encoded in any format into an OGG container, but in practice, audio in an OGG container is usually encoded with Vorbis. FLAC and OPUS may be options, but once again you'd need to find an application or library that can write the OGG container format for you, as I'm not aware of any fully managed OGG writers.

Outras dicas

<Shameless Plug>
I wrote an addon for NAudio that uses libmp3lame from the LAME Encoder suite to handle MP3 encoding. It's on NuGet as NAudio.Lame, and the source is on GitHub.
</Shameless Plug>

Sadly the licensing issues remain if you are planning to use this for anything other than personal use. LAME itself is licensed under the LGPL, but the patents it implements still require licencing from Frauenhofer/Thompson according to the LAME Wikipedia entry. If you're planning to produce something for others this can get expensive.

The Vorbis compressor is unencumbered by patents and such, so it's a reasonable alternative. At some point I plan to do a similar wrapper to the OGG/Vorbis format. In the meantime, a quick Google Search turns up the Ogg Vorbis Interop Library which might be useful to you.

And yes, you will need PCM as an intermediate format in pretty much any conversion. NAudio gives you the tools to get PCM from a wide variety of audio formats.

1) PCM to OGG

string fileName = @"e:\Down\male.wav";
Sox.Convert(@"sox.exe", fileName, fileName + ".ogg");

2) PCM to MP3

    static void AnyToMp3(string fileName)
    {
        DsReader dr = new DsReader(fileName);
        IntPtr formatPcm = dr.ReadFormat();
        byte[] dataPcm = dr.ReadData();
        dr.Close();
        IntPtr formatMp3 = AudioCompressionManager.GetCompatibleFormat(formatPcm,
            AudioCompressionManager.MpegLayer3FormatTag);
        byte[] dataMp3 = AudioCompressionManager.Convert(formatPcm, formatMp3, dataPcm, false);
        Mp3Writer mw = new Mp3Writer(File.Create(fileName + ".mp3"));
        mw.WriteData(dataMp3);
        mw.Close();
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top