tags:

views:

1266

answers:

3

I have a server that sends data via a socket, the data is a wav 'file'. I can easily write the data to disk and then play it in WMP, but I have no idea how I can play it as I read it from the socket. Is it possible?

Bonus question: how would I do it if the stream was in mp3 or other format?

This is for windows in native C++.

+1  A: 

Because you've said WMP, I'm assuming the question applies to trying to play a wav file on a windows machine. If not, this answer isn't relevant.

What you want to do isn't trivial. There is a good article here on code project that describes the windows audio model. It describes how to set up the audio device and how to stream data into the device for playback. You "simply" need to supply data coming in from your socket as data for the playback buffers. But that's where all of the tricky work is. You have to be sure that

  • You have enough data to begin a playback
  • Handle the case when your socket is starved for data and you have nothing to send to the playback buffer
  • You are able to read data off of the socket with enough speed to keep the playback buffers full

It's an interesting exercise. But tricky.

Mark
I said "this is for windows in native C++.", so I thought I was being explicit enough :-)
gbjbaanb
my reading comprehension has stunk these days :-) Yes, you are correct
Mark
A: 

Mark is right about this being a tricky problem. The waveOutXXXX API is ancient (it predates Windows 95) and requires more error-prone coding than you would think. You will have an easier time interacting with the API in C++ than with C#. Just make sure this is something you really want to do.

If your stream is some format other than WAV file data (like MP3 or WMA), you will have to perform the additional step of decoding the data into WAV format and playing it with the waveOutXXXX API. Finding a good component to do MP3 decoding is trickier than you would expect - I think this is related to the Fraunhofer licensing situation (you're supposed to pay them if you use MP3 code in any way).

I'd find an off-the-shelf product to do this, unless you want the learning experience.

MusiGenesis
A: 

Mark is right about this being a tricky problem. It may be less tricky if you use DirectSound instead of waveOut. http://www.gamedev.net/reference/articles/article710.asp">Here's an article on streaming wave files from disk: streaming from the network is essentially the same process. Make sure you collect enough data from the network before you start - you'll want more than the 2 buffers the article mentions.

Even less tricky would be FMOD. From the FAQ:

Enhanced Internet features

  • Internet audio streaming. Custom internet streaming code is included, which allows for seamless SHOUTcast, Icecast and http streaming support.
  • Download capability. A side effect of FMOD’s modular file system which supports network files, even static samples can be loaded off the internet.

File format support: FMOD currently supports a wide range of audio file formats. partial list:

  • MP3 - (MPEG I/II Layer 3, including VBR support)
  • OGG - (Ogg Vorbis format)
  • WAV - (Microsoft Wave files, inlcluding compressed wavs. PCM, MP3 and IMA ADPCM compressed wav
AShelly