0

I have a simple c# console app that plays a wav file. I want to change the sound card settings in C# in my app to 24bit/48Khz. How do I do that programtically?

    static void Main(string[] args)
    {
        SoundPlayer simpleSound = new SoundPlayer(MyProg.Properties.Resources.BOOTLOAD48000);
        simpleSound.Play();

     }
8
  • what do you mean by "the audio" Nov 15, 2012 at 22:32
  • Working code is as below: static void Main(string[] args) { SoundPlayer simpleSound = new SoundPlayer(MyProg.Properties.Resources.SOUNDWAVFILE); simpleSound.Play(); Nov 15, 2012 at 22:33
  • you can edit your question. to put the code in Nov 15, 2012 at 22:34
  • I'm playing a .wav file using SoundPlayer(), that's what I meant by the audio. Sorry I wasn;t clear Nov 15, 2012 at 22:35
  • 1
    If I were you, I'd edit your question to say "How do I change my sound card settings using C#" Nov 15, 2012 at 22:48

3 Answers 3

1

The sample rate, bit depth, channels, and anything else, is embedded in the wav file's header.

The straight forward way to resample is by using a 3rd party S.A NAudio. search stackoverflow to find more detailed answers.

1
  • I think I asked the wrong question. Thanks, Sam for pointing that out. I don't want to resample my audio, just want to set the PC soundcard to 24bit/48Khz so that all my audio data comes across bit accurate. So, the question really is how do I change my sound card settings in C#. Sorry for the confusion. Nov 15, 2012 at 23:20
1

Audio playback subsystem is not as straightforward as you might thought of. At the very least, your playback does not go to sound card directly: it is queued somewhere into cozy place so that data is mixed behind the scenes with audio from other applications, if any, and then its forwarded to the device.

I have no faintest idea why you want, or even need, to have it bit accurate on hardware, however if it is what you really need then you need to leverage low level exclusive access APIs to get an intimate connection with audio rendering hardware and deliver the data right into playback buffer bypassing any mixing that typically takes place on the way. Most likely those APIs are native, and for sure you will need to delivier raw data after you already managed to take it out of the file. It is going to be not as easy as SoundPlayer.Play.

The good news however is that if the hardware is really capable of playing this format, you have good chances to do the mentioned.

0

To force Windows to use a specific sample rate you need to use WASAPI in exclusive mode. Otherwise, you are sharing the soundcard with other applications, and they may need it to be at a different sample rate. NAudio allows you to do this. You need to pass AudioClientShareMode.Exclusive into the constructor for WasapiOut.

Note that WASAPI can only be used on Windows Vista and above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.