Calvin
Calvin

Reputation: 710

Set sample rate in windows 7

I'm currently struggling to find a solution to programmatically set the sample rate and bit rate of a USB sound card. I'm working on switching over our test program from Windows XP to Windows 7, and we have a product that acts like a USB soundcard. Some of our tests are sending out 48k signals though the soundcard, and the measuring the signal after routing it though a DAC. I think the measurement hardwares firmware/software isn't putting its signal generator in exclusive mode, and Windows is getting confused and putting the device in shared mode, which defaults the sample rate to 44.1k/16bit, I would like to change this value when we start the unit up to 48k/24bit.

I'm hoping someone could push me in the right direction, because everything I'm seeing is telling me that this isn't possible... (also, I'd prefer .NET solutions, or anything I can call/execute from .NET would be fine).

Here is one thing I tried, but this ended up only setting up a object to use to play back audio, it doesn't set the sample/bit depth for good.

Imports NAudio.Wave

Module ConfigureDevice

    Private Const SAMPLE_RATE As Integer = 48000
    Private Const CHANNELS As Integer = 2

    Sub Main(ByVal args() As String)

        ConfigureDirectSound(args(0))

    End Sub


    Private Sub ConfigureDirectSound(ByVal name As String)

        Dim out As New DirectSoundOut(GetWaveOutDeviceNumber(name))
        Dim waveFormat = New WaveFormat(SAMPLE_RATE, CHANNELS)
        Dim waveProvider = New BufferedWaveProvider(waveFormat)

        out.Init(waveProvider)

    End Sub

    Private Function GetWaveOutDeviceNumber(ByVal name As String) As System.Guid

        Dim devices = DirectSoundOut.Devices

        For Each d In devices
            If d.Description = name Then
                Return d.Guid
            End If
        Next

        Return Nothing

    End Function

End Module

Upvotes: 1

Views: 2008

Answers (1)

Forest Kunecke
Forest Kunecke

Reputation: 2160

It looks like you'll need to explore the WASAPI. From what I'm reading, that's the only way to programmatically force the output sample rate, besides changing the audio file itself.

http://msdn.microsoft.com/en-us/library/windows/desktop/dd371455(v=vs.85).aspx

You may have to search around for a C++/CLI wrapper for it (or write it yourself) to get any use out of it in .NET.

Upvotes: 1

Related Questions