Play PCM data in .NET Framework 2? - c#

I have a stream of u-Law compressed PCM data I am extracting from a Camera, I need to play this out the speakers? Anybody know how? I've tried decoding the u-Law into normal WAV Data and then use SoundPlayer but it never seems to work! Always SoundPlayer only supports PCM Data?
I know the sounds ok, because I have saved it to a file (using a custom createWavHeader method) and iTunes can play it.

Windows comes with an ACM codec to convert u-law to PCM. You can use NAudio and use the WaveFileReader and the WaveFormatConversionStream to get a PCM stream you can play easily.

Similarly, there is a freeware library lizPlay providing PCM playing capabilities. Worth considering as developers of lizPlay pay special attention to ensure there is a version that is free of patent infringement issues.

Related

Play and record 16-bit PCM (raw) audio bytes in WPF

Are there any standard Microsoft components that will allow the following from a WPF application:
Record (default) PC microphone input to 16-bit PCM audio byte array chunks, and
Play 16-bit PCM audio byte array chunks on the (default) PC speaker
I have tried to investigate whether I could somehow use the SoundPlayer, MediaElement or the MediaPlayer components, but I don't really think any of them fit the bill. If I'm wrong, please provide a bit of input on how to go about this.
I can use the great NAudio library by Mark Heath to accomplish it, but if at all possible I would like to avoid taking on a dependency outside of the .NET Framework.
Thanks in advance.
I'm afraid there's nothing built into the .NET framework to do this, which is why libraries like NAudio exist.

How to play MP3 from Stream using DirectSound

I'm writing a program which at some point downloads an MP3 and stores it into a byte array. Then I create Stream from the bytes.
Not sure how to play the Stream. I don't want to use any dlls except for DirectSound.
Are there any better ways to play the Stream? (not another dll)
If yes, how? And if not, how to play the Stream by DirectSound?
It seems that this is not possible. See the quote from a similar MSDN forum post:
Raw DirectSound isn't capable of playing mp3-compressed audio. I recommend using DirectShow for this purpose, as it supports a wide variety of formats. You could also use a streaming dsound buffer and do the decompression yourself, but this is probably overkill in your situation.
Note that I don't recommend using mp3 in your project in any case, since it is not a royalty-free format. Ogg Vorbis is a good alternative.
Mp3 not royalty free so you will struggle to do it without a 3rd party lib and even so it is not recommended.

How to play a pure PCM raw stream with C#?

I was looking into System.Media.SoundPlayer and NAudio, and AFAIK they only play WAVE streams. The WAVE file is composed of the pure PCM data plus a format header.
I don't want to provide this header in the WAVE format. I want to pass the parameters (bitrate, etc) programatically
As I said in the comment above, using the native Windows API (waveOutOpen, waveOutPrepareHeader, waveOutWrite, and so on), it is possible to play the waveform data of your choice. I make this an answer so that it can be accepted in case it actually is what the OP is looking for.

Change Audio Format in C#

I want to change an audio file format into another. Specifically, I want to convert any audio file into 6khz, 16 bit, mono, PCM wav format.
How can I cope with this problem.
Thanks again in advance.
You can also do this using the open source C# audio library NAudio. Have a look at the NAudioDemo project for an example of passing WAV files through the ACM codecs installed on your machine to convert to another format. If your input file is not WAV or MP3, you will first need something that converts it to WAV though.
I would use the BASS Library. It has many possibilities to do format conversions using the built in encoding/decoding capabilities. It also has a .NET wrapper availabe.
I'm not entirely sure whether you'll be able to do this as well as you may like.
To start with refer to the windows API for dealing with RIFF files (that's the file group for WAV files.)
You'll need to read the headers, extract the data and uncompress it to get the raw data format. I beleive that the header data will tell you what codec was used for compression.
You'll need to perform some processing on the raw data. Conversion to mono and 16bit may not be a problem, but I'm not too sure about changes to the sampling rate.
You can then recompress using your specified codec.

Audio output from Silverlight

I'm looking to develop a Silverlight application which will take a stream of data (not an audio stream as such) from a web server.
The data stream would then be manipulated to give audio of a certain format (G.711 a-Law for example) which would then be converted into PCM so that additional effects can be applied (such as boosting the volume).
I'm OK up to this point. I've got my data, converted the G.711 into PCM but my problem is being able to output this PCM audio to the sound card.
I basing a solution on some C# code intended for a .Net application but in Silverlight there is a problem with trying to take a copy of a delegate (function pointer) which will be the topic of a separate question once I've produced a simple code sample.
So, the question is... How can I output the PCM audio that I have held in a data structure (currently an array) in my Silverlight to the user? (Please don't say write the byte values to a text box)
If it were a MP3 or WMA file I would play it using a MediaElement but I don't want to have to make it into a file as this would put a crimp on applying dynamic effects to the audio.
I've seen a few posts from people saying low level audio support is poor/non-existant in Silverlight so I'm open to any suggestions/ideas people may have.
The simple answer is that there is no support for PCM playback from Silverlight in version 2. So unless you want to write a fully managed PCM to MP3 converter you are stuck. Even then I'm not sure you could get the MediaElement to play from isolated storage.
Is there any chance you could use a web service to perform the conversion?
See also this question:
Where's the sound API in Silverlight? Or, how do I write a music app to run in the browser?
Update: Silverlight 3 supports your custom audio sources. However, it won't let you intercept samples to perform effects on WMA or MP3, presumably for DRM reasons, so you would still potentially need to write your own decoder.
Short answer is use a MediaElement + a MediaStreamSource
Check out these:
http://blogs.msdn.com/gillesk/archive/2009/03/23/playing-back-wave-files-in-silverlight.aspx
http://code.msdn.microsoft.com/wavmss/Release/ProjectReleases.aspx?ReleaseId=2417
Basically, write a decoder in managed code to convert G.711 a-Law to PCM, then do whatever modifications you want to the raw values, then pass those into a MediaStreamSource.
Looks like Silverlight 3 supports direct PCM output now, or will when released. I don't see anything in the docs about the raw AV pipeline yet.
Mark Heath's answer is correct - only certain formats are supported - mp3 and certain flavours of WMA (unfortunately not WMA lossless which would be 'closer' to PCM).
To play PCM data in Silverlight, you could do the following:
* Convert the PCM into mp3 data, and store it in memory.
* Play the mp3 data using the technique presented at ManagedMediaHelpers. The idea here involves a class called Mp3MediaStreamSource (derived from System.Windows.Media.MediaStreamSource) that provides mp3 chunks to a MediaElement to play. The chunks will need to be in a stream, but of course a memory stream will do.
I initially thought you might be able to provide PCM chunks via MediaStreamSource, but this does not work. It's a real shame as it would solve your problem (and the one I was facing - making a Speex audio file player) really easily!

Categories