2

I have initialized the device using:

static  IWavePlayer     waveOut;
static  WaveFormat      waveFormat;
static  BufferedWaveProvider    waveProvider;

private static int AudioDeviceInit()
{
            waveOut = new DirectSoundOut();
            waveFormat = new WaveFormat(44100, 2);
            waveProvider = new BufferedWaveProvider(waveFormat);

            waveOut.Init(waveProvider);
            waveOut.Play();

            return 0;
}

I am adding pcm stream to it using:

waveProvider.AddSamples(samples, 0, size);

The above is working fine as long as the stream data is of the same configuration.

I have another function that receives sample rate and number of channels and I want to reconfigure the waveprovider to use the newly provided configuration. Here is the code that I am using:

private static void AudioConfigCallback(int rate, int channel)
{
    waveFormat = new WaveFormat(rate, channel);
    waveProvider = new BufferedWaveProvider(waveFormat);
    waveOut.Init(waveProvider);
    return;
}

This is not working and I believe that this is not the correct way of doing it as well. Any idea how I can reconfigure the device to use new sample_rate and num_channels

Thanks.

John Smith
  • 1,351
  • 3
  • 16
  • 21

1 Answers1

2

This is not possible. When you open an output device, whether WaveOut, Direct Sound, WASAPI or ASIO, at that point you must specify the format at which you will work. You must close the output device and re-open it with the new WaveFormat.

An alternative approach would be to always convert to a fixed WaveFormat, and use WaveFormatConversionStream to convert to the correct format whenever the incoming format changes. This would allow you to avoid opening and closing the output device.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Thanks Mark. In terms of performance, which is better, closing/opening the device or format conversion? – John Smith Jul 21 '11 at 13:37
  • closing/opening the device will perform better since any necessary SRC will be done by the driver itself (at least with WaveOut/DirectSoundOut). – Mark Heath Jul 21 '11 at 14:23
  • Yes was thinking the same. Its working now after closing and opening the device again. Thanks a lot for your help :) – John Smith Jul 21 '11 at 14:57