How to play raw adts AAC stream using MediaStreamSource? - c#

Does anyone have this working? I believe I have the CodecPrivateData messed up, but I can't find any WAVEFORMTEX FormatTags that seem to work. I tried 0xFF00, 0x1016, and 0x0116. I used the MSDN documentation to complete the rest of the string.
I found this in MMReg.h (WAVE_FORMAT_MPEG_ADTS_AAC 0x1600) which would have a FormatTag(0x0016). You would think this is what I should use, but that does not work either.
When using 0xFF if I avoid the adts headers in the stream GetSampleAsync get called repeatedly but there is no playback. I know the stream is good because I can play it in FooBar, VLC, and Window Media Player. When I stick the raw AAC into an MP4 container it works fine in silverlight so I know that the AAC setting are supported by silverlight.
Can someone supply me with a hex CodecPrivateData string that worked for them?
Edit:
This is an example of PrivateCodecData hex string that I think should work:
0x1016010080BB0000E02E0000010000000E000100FE0000000000000000001190
This string goes straight from OpenMediaAsync() to CloseMedia() without even trying a sample. The MSDN documentation example does the same thing found here. I would expect it to at least try a sample before closing.

Silverlight seems to be not compliant with AAC and ADTS wrapper...
If you analyze your MP4 file (with MediaInfo for example), you will see AAC for audio but without ADTS wrapper. This is why Silverlight can read in this case your AAC audio embedded in a MP4 file.
So you need to remove the ADTS header for each audio sample before calling ReportSampleCompleted() in the MediaStreamSource. The ADTS header consists of 7 or 9 bytes (without or with CRC). For more informations about the ADTS header, see this link.
Obviously, you need to adapt your CodecPrivateData string with the right FormatTag. Here is mine for a AAC LC 48KHz #96Kbps file : FF00020080BB0000E02E0000040010000200

Related

WP 8.1 Trimming MP3 FILES

Is there a way to trim a mp3 file?
I did some research and every search was leading me to NAudio.
However NAudio doesn't support WP8.1. Actually I don't think it support any version of windows phone.
Is there any other way to trim a mp3 file? MP3s are made of frames and ID3 tags.
Is there a helper that could read mp3 frames and then copy them into a new file?
An MP3 file is a collection of MPEG frames that you can manipulate fairly easily. If you read the NAudio source code (specifically the Mp3Frame class) you'll find a fairly good set of C# code for reading the individual frames. From there you can index the frames, figure out their positions in time and copy out only the ones you're interested in to the output file.
It may be a bit more complex than that, but have a look at Mark's code in and around the Mp3Frame class for some more information on how it works.
Oh, and don't forget to credit him if you use his code.

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 to stream online radio on WP7?

I am trying to create a radio app, which contains a collection of MP3-stream urls. However, I have run into some trouble streaming (or playing back) the audio.
I am trying to connect to a raw data stream like the ones you plug into VLC. An example url is http://mp3.ht-stream.net/;80 (just plugged this one and a few others into VLC and it worked perfectly). Basically your standard internet radio feed.
I have created the AudioPlaybackAgent, filled in what I think is needed to get this up and running, and everything works fine when I stream regular .mp3 files over the internet. But when I try to connect to these streams, it doesn't do (or play) anything.
I reckon it could be because I'm not using an AudioStreamingAgent (with a MediaStreamSource implementation), but that stuff is a bit too advanced for me, and as I understood after some hours searching the interwebs, mp3 streams could use the AudioPlaybackAgent instead.
Any advice on how I can make this work? Will I have to use an AudioStreamingAgent instead? Is there any open source examples as to how I implement this mysterious MediaStreamSource class? Should I scrap my great idea? Any answers will be greatly appreciated.
My AudioPlayer.cs code is available here if you'd like a peek - but it's mostly the standard stuff.
You can't easily play audio from the provided link because of this is not a regular audio file. This is SHOUTcast stream.
You can check some open source implementations and to figure out how to work with this audio stream. For example: Shoutcast MediaStreamSource.
So I can say that you need to implement a lot of stuff in your app to play this stream. There is no quick and easy way.
Look at the Background Audio Streamer sample.

Output procedurally generated (or externally streamed) audio on Windows Phone 7

I'm looking at developing an application for the WP7 platform which accepts an audio stream from a computer and outputs that stream on the phone speaker. This involves either dealing with the audio encoding / decoding myself, or somehow passing off an audio stream to the WP7 platform.
I've struggled so far to find any raw audio output API's and I am not sure what I have to do on the server (computer) side to get the phone to just deal with the audio stream.
I have looked at a few MSDN articles, but I can't quite tell if they do what I want. If somebody could point me in the right direction that would be great!
I think the MediaStreamSource class does what I'm looking for, and the MediaStreamSource.ReportGetSampleCompleted method appears to confirm this, but nowhere does it say clearly that it can be used for raw audio.
If you need any information, or if you have any suggestions of better ways to do this that would also be appreciated!
You should be able to use the XNA.Framework.Audio namespace SoundEffect to get a soundeffect instance from a raw audio stream. Which should usually be a standard PCM format.
This post on microphone by Charles Petzold could be useful.
An excerpt
In contrast, classes in the Microsoft.Xna.Framework.Audio namespace
work with uncompressed audio data in the standard PCM format, which is
the same method used for audio CDs and Windows WAV files.

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.

Categories

Resources