4

The following code works with some wav files, but with others I get, "InvalidOperationException was unhandled. Message=Sound API only supports playing PCM wave files."

var webClient = new WebClient();
webClient.DownloadFile(url, fileName);
var fileSound = new SoundPlayer(fileName);
fileSound.PlaySync();

Is there a way to programmatically check if a wav file is "bad" (not a PCM wave file) and then convert it as necessary?

What is odd is that the code works in the legacy Delphi app - all of the wav files play just fine. Here's the Delphi code:

filename := GetEnvironmentVariable('TEMP')+'\archieAndDingbat.wav';
URLDownloadToFile(nil, PChar(url), PChar(filename), 0, nil);
PlaySound(filename);

I looked at the properties of the two files in Explorer, and I see that there is, indeed, a difference. For the file that does play, its audio format is PCM; the one that won't play is CCITT u-Law.

So...I either need a way to convert from CCITT u-Law to PCM on the fly after downloading these files (they are download from an url and then played locally) OR perhaps a different way of playing these files than PlaySync() ...

Arioch 'The
  • 15,799
  • 35
  • 62
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

3 Answers3

1

Look at audiolab library from mitov. It works great

Francis Lee
  • 975
  • 8
  • 23
  • 4
    Okay, I'll czech it out tomorrow. – B. Clay Shannon-B. Crow Raven Sep 12 '12 at 23:07
  • Well, now i know the missing verb for "I accidentally" meme :-) – Arioch 'The Sep 13 '12 at 08:00
  • Mitov seems to be for Embarcadero products only, which is "so 1990s" (at one time I was a Delphi diehard, but that was then...) – B. Clay Shannon-B. Crow Raven Sep 13 '12 at 15:16
  • @Clay, hey, Delphi isn't 1990s :-) Few days ago Embarcadero released [`Delphi XE3`](http://www.embarcadero.com/products/delphi). – TLama Sep 13 '12 at 20:29
  • @Clay WUT? *Mitov Software offers Delphi, C++ Builder, Visual C++ MFC/Win32 and . NET 2.0-4.0 (C#, Visual Basic, C++/CLI, J#)* Transparent bridge VCL<->MFC is alone not very borlandish. Neither is .Net novadays. – Arioch 'The Sep 14 '12 at 07:32
  • DirectX codecs (from http://free-codecs.com for example) used for playback in dot-net via http://mitov.com/products/basicaudio#downloads - what there about "Embarcadero-only" ??? – Arioch 'The Sep 14 '12 at 07:35
  • @TLama: some people still like Disco, too. Not to diss Delphi; I used to love it. It's just not in the same ballpark with the Microsoft tools anymore. In fact, if I was a "betting man," I'd invest (big time) in MS. Maybe not this year or next, but after that I think they're going to put on muscles that would make Clay Matthews look like a 98-lb. weakling. – B. Clay Shannon-B. Crow Raven Sep 14 '12 at 19:11
1

So, do you want to PLAY the file or CONVERT it ? What is the primary goal ? Do you play it as a prove you can convert it, or do you convert it because you don't know how to play not-converted file ? http://www.catb.org/esr/faqs/smart-questions.html#goal

Your question's title claims "convert" but the body claims "Play"

This answer is about playing files.


You also may try to use FFDShow codecs directly without DirectX intermediate. http://en.wikipedia.org/wiki/Libavcodec and http://libav.org/ and http://ffmpeg.org/ (they recently had a schism)

Googling for "FFDShow dotnet", "libav dotnet", "ffmpeg dotnet" shows a bunch of libraries to use it, such as


There is also BASS library. It is targeted as sound playback during gaming, so it probably has less range of formats and not much for re-coding. Still many music players are built on top of it. Some says it is the most simple API to use. So it worth considering. http://www.un4seen.com/


http://MediaInfo.sf.net is a library (native win32/win64 DLL) allowing to check most multimedia formats content. I don't know if using tis C or C++ APis is easy from C# side.

Community
  • 1
  • 1
Arioch 'The
  • 15,799
  • 35
  • 62
  • 1
    But OP wants to play wave files in C# not in Delphi. Anyway, you don't need to use DSPack wrappers, check out how simply you can [`play a file`](http://msdn.microsoft.com/en-us/library/windows/desktop/dd389098(v=vs.85).aspx), so in Delphi you can use just DirectShow headers ;-) – TLama Sep 13 '12 at 08:33
  • @TLama According to Henry, those headers are just his DSPack copypasted, and he is very upset for it. He expect EMB just cloned them and would never maintain. – Arioch 'The Sep 13 '12 at 09:16
  • @TLama Bass has .Net API as well – Arioch 'The Sep 13 '12 at 09:18
  • "do you want to PLAY the file or CONVERT it" - I would prefer to just play it, but .NET's Soundplayer class requires the .wav file to be a PCM .wav file, and refuses to play CCITT u-Law .wav files. So, if there's no way to directly play the CCITT u-Law .wav files, I will need to convert them on the fly from CCITT u-Law to PCM .wav file format. – B. Clay Shannon-B. Crow Raven Sep 13 '12 at 15:00
  • IOW, all I want to do is play the file; I don't care how, just as long as it works, the code/library is free or open source - the less muss and fuss the better. – B. Clay Shannon-B. Crow Raven Sep 13 '12 at 15:15
  • as to https://github.com/ermau/libav.net, I can't make heads or tails of what it is supposed to be/do. I've never head of FFmpeg before. All I want to do is play a .wav file of the CCITT u-Law dialect. I find it hard to believe there's not a rather straightforward way to either play such a file, or at least to convert the file to the PCM dialect (at which point I can play it with my existing code). – B. Clay Shannon-B. Crow Raven Sep 13 '12 at 15:21
  • Clay, try BASS. Dunno about .Net but in Delphi it is simple and reliable. ffmpeg is a complex combine, perhaps too complex for you. BASS is usually liked for simplicity, give it a try. Also you may try Mitov's BasicAudio, though it relies on DirectX which sometimes is fragile (BASS is self-contained). – Arioch 'The Sep 14 '12 at 07:37
  • ffmpeg/libav is a greatest independent library about video and audio formats, probably the only full-scale competitor to DirectX and GStreamer. But it probably can be too complex for your needs. However seeing how many .Net wrappers are in the wild for it - i guess many devels find it vlauable after all. PS. i rephrased your question title, ESR's essay will hint you why. Sorry to say, but original title was kind of misleading. – Arioch 'The Sep 14 '12 at 07:43
0

The way to do it is to use newkie's code at: http://www.codeproject.com/Articles/175030/PlaySound-A-Better-Way-to-Play-Wav-Files-in-C?msg=4366037#xx4366037xx

In my case, at least, I had to change all of the lowercase x's to uppercase x's, though, to get it to work.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862