Home Up Feedback Contents Search

Sound Utilities 

 

How to Make Your Program to Have Sound Utilities

Win API offers many functions that provides complex functionality to your applications. I have been especially interested for multimedia API functions, because I decided to embed some sound utilities in Talking Cash Register system.

If you are Visual Basic programmer, there are few ways to do that. One way is to use some multimedia Visual Basic ActiveX controls such as RealAudio control, or to use Win32 API PlaySound function. I have decided for more usual, compact and flexible multimedia Win API functions: sndPlaySound, waveOutGetNumDevs, waveOutSetVolume and waveOutGetVolume.

WaveOutGetNumDevs is a function that returns the number of devices in the system, that are capable of playing back wave audio data. WaveOutSetVolume and WaveOutGetVolume are functions for setting/getting sound card volume which, along with slide control, represents powerful utility that allows user to control sound card volume from your applications.

More interesting function is sndPlaySound function that reads standard WAV files on disk and play it. Please pay attention for its second parameter, uFlags, that can significantly change behavior of that function. There are brief descriptions for most important uFlags parameters:

SND_SYNC - sndPlaySound plays WAV file and then returns control to program.
SND_ASYNC - sndPlaySound starts WAV file and returns control to program immediately.
SND_NODEFAULT - sndPlaySound will not play default beep if the specified resource is missing.
SND_LOOP will make the sound to play continuously.
SND_ASYNC + SND_LOOP will make the sound continuously play and let the program continue on.

Now, you can place this code in the General Declarations section of a module where you intend to embed this sound utilities:

#If Win32 Then
    Private Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long
    Private Declare Function sndPlaySound Lib "winmm.dll" _
        Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
    Private Declare Function waveOutSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, _
        ByVal dwVolume As Long) As Long
    Private Declare Function waveOutGetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, _
        lpdwVolume As Long) As Long
    Private Const SND_SYNC = &H0
    Private Const SND_ASYNC = &H1
    Private Const SND_NODEFAULT = &H2
    Private SND_MEMORY = &H4
    Private Const SND_LOOP = &H8
    Private Const SND_NOSTOP = &H10
#ElseIf Win16 Then
    Private Declare Function waveOutGetNumDevs Lib "mmsystem.dll" () As Integer
    Private Declare Function sndPlaySound Lib "mmsystem.dll" _
        (ByVal lpszSoundName$, ByVal wFlags%) As Integer
    Private Declare Function waveOutSetVolume Lib "winmm.dll" (ByVal uDeviceID As Integer, _
        ByVal dwVolume As Integer) As Integer
    Private Declare Function waveOutGetVolume Lib "winmm.dll" (ByVal uDeviceID As Integer, _
        lpdwVolume As Integer) As Integer
    Private Const SND_SYNC = &H0
    Private Const SND_ASYNC = &H1
    Private Const SND_NODEFAULT = &H2
    Private SND_MEMORY = &H4
    Private Const SND_LOOP = &H8
    Private Const SND_NOSTOP = &H10
#End If

To get if system can play sound use following function:

Public Function CanPlaySound() as Boolean
Dim i As Integer
    i = waveOutGetNumDevs()
    If i > 0 Then
        MsgBox "Your system supports a sound card.", vbInformation, "Sound Test"
        CanPlaySound = True
    Else
        MsgBox "Your system cannot play Sound Blaster Files.", vbInformation, "Sound Test"
        CanPlaySound = False
    End If
End Function

To set and to get sound volume use following functions:

Public Sub ChangeVolume(Volume As Long)
Dim ret As Long
    '0 is ID number for default sound device
    ret = waveOutSetVolume(0, Volume)
End Sub

Public Function GetVolume() As Long
Dim ret As Long
Dim Volume As Long
    '0 is ID number for default sound device
    ret = waveOutGetVolume(0, Volume)
    'Tip: If you want to get sound card volume of right channel use high
    'two bytes from long result, or low two bytes for left channel sound card volume
    'Sound card volume for right channel
    Volume = ((Volume And &HFFFF0000) / &H10000) And &HFFFF&
    GetVolume = Volume
End Function

To start play sound and return control to program immediately, use following function:

Public Sub SpeakWord()
Dim ret As Long
Dim soundpath as String
    'soundpath variable depends on locations of Windows system directories and Windows Setup Multimedia Options
    soundpath = "C:\WINDOWS\MEDIA\CHIMES.WAV"
    ret = sndPlaySound(soundpath, SND_ASYNC)
End Sub

 
Send mail to gaja@cent.co.yu with questions or comments about this web site.
Last modified: April 02, 1999