Converting mp3 bitrates using lame_enc.dll or similar - c#

I need to programatically convert mp3's of any bitrate to a standard bitrate for streaming audio using c#.
Currently a buffer is populated with mp3 data from disk and then send out to the "listeners" at what should be a constant speed (the broadcast), but the mp3's could be of any bitrate. This makes timing extremely difficult and should rather be streamed at a standard bitrate instead of a bitrate dictated by the mp3 itself.
Lame seems to be the right encoder for the job, but any documentation or sample code only seems to be concerned with converting from wav samples to mp3. Not mp3 to mp3. The exe wrapper can do the bitrate conversion, but completely without any clue as to what gets passed to beEncodeChunk().
Has anyone had any experience in doing this kind of thing with lame or any similar encoder?
Do i need to decode to wav then encode back to mp3 to achieve what i'm after?
I welcome any links or advice with open arms.
Thanks

you have to decode the mp3 to wav, then re-encode it to the new bitrate

Related

converting Rtsp stream to .wav and parallel y to bytes while stream is running

Thanks in advance ,I am converting rtsp live stream to .wav file using ffmpeg.Its converting good but i want to convert .wav file to byte stream parallely at the time of converting rtsp to .wav file
1)Launch ffmpeg as a Proceas with the option to decode to standard output in the Arguments (usually a '-' at the end of the arguments)
2) Read the standard output from that Process in accordance with the output format being written.
This keeps the output in memory and allows you to consume it just after its been provided to stdout.
See this question for an example commanf of how to get raw audio from ffmpeg.
Can ffmpeg convert audio to raw PCM? If so, how?
You can't expect a player to understand the datastrucutes without the wav headers because the sample size and rate are not defined in a raw pcm stream.
Finally I think you'll see that it's better to output wav format and read the wav format which will contain everything a player needs to playback the audio.
You would them provide wav audio instead of pcm which will work in naudio as well as many other players quite easily.

Convert PCM to MP3/OGG

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:
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.
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.
<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.
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.
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();
}

How can I use mp3 files in Windows Phone 8?

in my windows phone gaming apps, I have to use lots of sound. Now I have seen that Windows phone does not support mp3 files. So I need to use the wav files. Any mp3 file which is just 500 kb size in mp3 format, when convert that to ".wav" it becomes min 2.5MB. It's actually eating up my apps size and unnecessarily the size of my apps is getting bigger.
Anyone know how can I use the mp3 file? In my solution I have a Asset folder and inside this folder all the ".wav" files are located.
How I am doing this let me write a code
SoundEffect effect;
Iinside constructor-
{
...
var soundFile = "Assets/Jump.wav";
Stream stream = TitleContainer.OpenStream(soundFile);
effect = SoundEffect.FromStream(stream);
And in the code
effect.Play();
Is there any better approach. In some thread I come to know that doing this is not a better way coding as it creates object and used up the system space. Please suggest what to do, how do I add mp3 files and write better code for working with sound files.
you can use BackgroundAudioPlayer to play your wav and mp3 files. SoundEffect class cannot play mp3 data
Go through this it's an entire app on it's own.
Background Audio WP
To use an MP3 file you would have to decode the MP3 into PCM in memory, and use that resulting stream in the SoundEffect.FromStream method.
Another thing you could try is encoding the wav files as ADPCM. This usually compresses the wave file at ratio of 4:1. If you can't use the stream directly, decoding of ADPCM is much more straightforward than decoding an mp3 file.
And one more thing you could try to save space is converting the uncompressed wave files into mono, and a lower sampling rate. You can perform listening tests on the sounds after the conversion to make sure that the loss in quality is acceptable or not.

How to convert AMR sound stream to PCM uncompressed stream in .NET

I need to convert an AMR (Adaptive Multi-Rate) audio file recorded in a phone (as a Stream object) to a PCM uncompressed wav audio Stream so it can be processed afterwards for speech recognition. The Speech Recognition doesn't like the AMR format. This is going to be a server application using the Microsoft Speech Platform. I am not sure about using the ffdshow or similar libraries in a .
Right now I am researching NAudio and DirectShowNet to see if they can help me accomplish this but was hoping someone can point in the right direction.
After a lot of searching for a solution for this, I am going to use ffmpeg. It provides the AMR-NB (NB=Narrow Band) decoder. There are a lot of c# wrappers for ffmpeg around; most of them abandoned efforts and one that is up to date but is not free. Just running ffmpeg with the basic parameters provides what I need, plus it is really fast.
I don't like the idea of calling an external process to do the conversion, plus I need to save the AMR stream as a file so it can be converted to a wav file but I believe I can make it work efficiently.

Audio Conversion C#

What is the best way to convert various audio formats to PCM?
For example: mp3, evrc, ogg vox.
Is there a library out there that will allow me to implement this relatively easily?
EDIT:
I guess my initial question wasn't really what I needed. Most of the libs I have found are file converters. What I need is a block converter, where I pass in a 1Kb block of vox data and it returns its converted PCM block. Of course I’ll have to tell the converter what type of data it is and various pieces of codec information.
The solution I am going for is to save and VOIP formats into a common wav format and to play that conformed file in real time. I thought there should be an easy way to do this because all audio is eventually turned into PCM before it is outputted anyways.
You can use NAudio to pass blocks of compressed audio into any ACM codecs you have installed on your machine. You do need to know how to create the appropriate WAVEFORMAT structure to describe the compressed audio type correctly though.
Check out AVBlocks SDK for .NET. It supports several audio formats, and audio transforms like Multi-channel audio to Stereo audio, resampling and bitrate conversion.
Try modified code from http://alvas.net/alvas.audio,tips.aspx#tip91
static void AnyToWav(string fileName)
{
DsReader dr = new DsReader(fileName);
IntPtr formatPcm = dr.ReadFormat();
byte[] dataPcm = dr.ReadData();
dr.Close();
WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"), AudioCompressionManager.FormatBytes(formatPcm));
ww.WriteData(formatPcm);
ww.Close();
}
Don't know any lib that does it all but we do mp3->wav using madxlib.
It's free but I suggest paying the $10 for the sdk as it comes with documentation and examples.
There is a c# tutorial on youtube that might be helpful to you. It shows how to use a specific audio library called alvas.audio that really does some neat things with audio. I found the video to be very educational. The audio library is completely written in c#. Watch the video for more details: http://www.youtube.com/watch?v=2DIQECXFPeU

Categories

Resources