Getting the Preferred Audio Device Number

Getting the Preferred Audio Device Number

Why You Need This

If you only program for people who have a single piece of audio hardware, then you don't. But many Windows users have more than one output—for example, the motherboard's built-in audio and an added PCI SoundBlaster card. They can select their "preferred device" (that is, the one with the speakers attached :-) using Windows Sounds and Multimedia control panel. (For Google's benefit, at this point we'll stop to note that the "preferred audio device" is the same thing as the "default audio device".)

Windows refers to audio input and output devices by device numbers. Windows API audio functions generally require a device number. Once you have the device number, you can get the device name, properties and so forth. Some functions will let you pass the constant WAVE_MAPPER (which is a signed long of value -1) in lieu of a device number, which will automatically route the sound to the preferred device.

Note that you can't just assume that device 0 is the default device, or even a reasonable device; on my laptop, device 0 is my wretched WinModem. It's not a great output device for playing MP3 files. Actually it's not really good for anything.

Sometimes you aren't given the opportunity to pass WAVE_MAPPER (say you are using someone else's audio tools) or you are doing something tricky like requesting the device name. Then there's no getting around determining what the device number is.

Getting The Preferred Device Number

There is a Windows API call to do this, but it is a general purpose messaging API and not trivial to use. (And note that this only works on Windows ME, Win2k or later.) Here's what Microsoft has in the way of documentation:

If you program with the Windows API all day long, then you are all set. But if you're just some pathetic Visual Basic hack like I am, this is not sufficient. Visual Basic doesn't have any more clue than I do about the value of DRVM_MAPPER_PREFERRED_GET and the API Manager is no help. This constant is defined in mmddk.h, which you can get by downloading and installing 90 MB of the soon-to-cost-$199 Win2k Driver Developer Kit or (because I'm annoyed at them about this) clicking here.

Strangely, the API Viewer thinks it knows about the WaveOutMessage message function. This is kind of unfortunate, because what it knows isn't right.

' This is WRONG WRONG WRONG WRONG
Public Const WAVE_MAPPER = -1&
Public Declare Function waveOutMessage Lib "winmm.dll" Alias "waveOutMessage" _
(ByVal hWaveOut As Long, ByVal msg As Long, ByVal dw1 As Long, ByVal dw2 As _
 Long) As Long
'
' NO STOP EVIL BAD
Now I'm just an uneducated foon when it comes to VB, but passing a parameter by value just can't be right when the function is expecting a pointer. Paste this in a module instead:
Option Explicit
Public Declare Function waveOutMessage Lib "winmm.dll" (ByVal hWaveOut _
As Long, ByVal msg As Long, dw1_ptr As Long, dw2_ptr As Long) As Long
Private Const WAVE_MAPPER As Long = -1&
Private Const DRVM_MAPPER As Long = &H2000&
Private Const DRVM_MAPPER_PREFERRED_GET As Long = DRVM_MAPPER + 21
Private Const MMSYSERR_NOERROR = 0  '  no error
'

Public Function GetPreferredWaveoutDevice() As Long
Dim errcode As Long
Dim DWord1Ptr As Long
    errcode = waveOutMessage(WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, DWord1Ptr, 0)
If errcode = MMSYSERR_NOERROR Then
    GetPreferredWaveoutDevice = DWord1Ptr
Else
    GetPreferredWaveoutDevice = -1
End If
End Function

One hopes that passing WAVE_MAPPER by value is equivalent to specifing the device handle as WAVE_MAPPER cast as HWAVEOUT, as we are instructed to do. The above seems to work correctly and returns MMSYSERR_NOERROR, but it might not be completely correct. Mail me if I goofed.

Since I write in Labview a lot more often than in VB, I went ahead and made a Labview VI to do the same thing. Take your pick:

You can figure out how to get the preferred audio input device or preferred MIDI device using the same technique; see Microsoft's developer note for the details.

Rob Calhoun
02 Oct 2002
rob at sign calhoun dot net. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值