Convert PCM to MP3 / OGG

I need to convert a continuous PCM stream or encoded sound (ADPCM, uLaw, Opus) to MP3 / OGG format so that it can be transferred to the browser (using the html audio tag). I have the "stream-mp3 / ogg-use-audio-tag" part, now I need to develop a conversion level.

I have two questions:

  • How to convert PCM to MP3 / OGG using NAudio and / or some other C # libraries? I suppose there is a piece of code or two in the NAudio demo application, but I could not find it.
  • Do I have to convert encoded data (ADPCM, uLaw, OPUS) to PCM (what can I) before converting it to MP3 / OGG, or can MP3 / OGG containers accept encoded data?

NOTE. I understand that I have problems licensing in MP3, so we are open to using OGG.

Thanks.

+2
source share
3 answers

To answer your first question, you need an encoder to create an MP3 or OGG. NAudio does not include MP3 or OGG encoder. All he does is provide you with ways to access all the encoders that are already installed on your computer (for example, ACM or Media Foundation Transforms). However, in both MP3 and OGG, you will find that the easiest way is to find an unmanaged DLL or command line utility and access it from .NET. In the article I wrote above, you will find 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, yes. Whenever you transcode, you first decode in PCM and then transcode in the target codec. I think that 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 parameters, but again you will need to find an application or library that can write the OGG container format for you, because I do not know about fully controlled OGG autographs.

+1
source

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

Unfortunately, licensing issues remain if you plan to use this for anything other than personal use. LAME itself is licensed under the LGPL , but the patents that it holds still require licensing from Frauenhofer / Thompson according to the LAME Wikipedia entry . If you plan to produce something for others, it can become expensive .

The Vorbis compressor is unencumbered by patents and is thus a reasonable alternative. At some point, I plan to make a similar shell in the OGG / Vorbis format. Meanwhile, a quick Google Search includes the Ogg Vorbis Interop Library , which may be of use to you.

And yes, you will need PCM as an intermediate format in almost any conversion. NAudio provides you with PCM tools from a wide variety of audio formats.

+2
source

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(); } 
0
source

All Articles