views:

1476

answers:

5

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.

+3  A: 

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: http://stackoverflow.com/questions/347288/wheres-the-sound-api-in-silverlight-or-how-do-i-write-a-music-app-to-run-in-t

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.

Mark Heath
Mark, Thanks for your response. Silverlight does not look like it will allow me to do what I want (yet - maybe sometime it will) so for now I'm turning my focus towards WPF/XBAP. Just need to sort out deploying a fully trusted application, but again if I'm struggling I'll make another question.
A: 

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!

Andy Wyatt
A: 

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.

+2  A: 

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.

loarabia
A: 

Please see http://alvas.net/alvas.audio,articles.aspx#how-to-save-audio-to-mp3-on-silverlight article and code sample

ava
Alvas.Audio is not cross-platform compatible because it utilizes Pinvoke which in turn requires elevated permissions and out-of-browser execution. In my opinion this renders it worthless for many if not most Silverlight Projects.
Oliver Weichhold