Microsoft KB Archive/71102

= VB3 Emulating QuickBasic's SOUND Statement in Visual Basic =

Article ID: 71102

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q71102



SUMMARY
The SOUND statement found in Microsoft QuickBasic is not implemented within Microsoft Visual Basic. You can perform sound through a Windows API call that is equivalent to the QuickBasic SOUND statement.



MORE INFORMATION
The QuickBasic version of the SOUND statement can be executed by calling several Windows 3.0 API function calls. Within Windows, you must open up a VoiceQueue with the OpenSound call routine. Using the function SetVoiceSound, place all of the values corresponding to the desired frequencies and durations. Once the VoiceQueue has the desired frequencies and durations, you start the process by calling StartSound. After the sounds have been played, you must free up the VoiceQueue by calling CloseSound. If you plan on placing a large amount of information into the VoiceQueue, you may need to resize the VoiceQueue buffer by calling the SetVoiceQueueSize function.

After executing the StartSound function, you cannot place any more sound into the VoiceQueue until the VoiceQueue is depleted. Placing more sound into the queue will overwrite any information that was previously in the VoiceQueue. If you are going to place sound into the VoiceQueue after a StartSound statement, you will need to call WaitSoundState with an argument of one. When WaitSoundState returns NULL, the VoiceQueue is empty and processing can continue.

Below is an example of using the Windows API function calls, which will imitate the QuickBasic SOUND statement:

In the general section place the following: Declare Function OpenSound Lib &quot;sound.drv&quot; As Integer Declare Function VoiceQueueSize Lib &quot;sound.drv&quot; (ByVal nVoice%, ByVal nBytes%) As Integer Declare Function SetVoiceSound Lib &quot;sound.drv&quot; (ByVal nSource%, ByVal Freq&, ByVal nDuration%) As Integer Declare Function StartSound Lib &quot;sound.drv&quot; As Integer Declare Function CloseSound Lib &quot;sound.drv&quot; As Integer Declare Function WaitSoundState Lib &quot;sound.drv&quot; (ByVal State%) As Integer NOTE: All Declare statements above each must be placed on one line.

The SetVoiceSound takes two arguments. The first variable, Freq, is a two WORD parameter. The HIGH WORD will hold the actual frequency in hertz. The LOW WORD will hold the fractional frequency. The formula, X * 2 ^ 16, will shift the variable &quot;X&quot; into the HIGH WORD location. The second variable, Duration%, is the duration in clock ticks. There are 18.2 tick clicks per second on all Intel computers.

The following simplistic example shows how you can place several frequencies and durations into the VoiceQueue before starting the sound by calling the StartSound function: Sub Form_Click Suc% = OpenSound S% = SetVoiceSound(1, 100 * 2 ^ 16, 100)  ' Frequency = 100 hz   S% = SetVoiceSound(1, 90 * 2 ^ 16, 90)     ' Frequency = 90 hz   S% = SetVoiceSound(1, 80 * 2 ^ 16, 90)     ' Frequency = 80 hz   S% = StartSound While (WaitSoundState(1) <> 0): Wend      ' Wait for sound to play. Succ% = CloseSound End Sub The following is another simple example, which creates a siren sound:

  Within the general section, place the following Sound procedure: Sub Sound (ByVal Freq as Long, ByVal Duration%) Freq = Freq * 2 ^ 16                ' Shift frequency to high byte. S% = SetVoiceSound(1, Freq, Duration%) S% = StartSound While (WaitSoundState(1) <> 0): Wend End Sub   Place the code below into any event procedure. The example below uses the Form_Click event procedure. Clicking any position on the form will create a police siren. Sub Form_Click Suc% = OpenSound For j& = 440 To 1000 Step 5 Call Sound(j&, j& / 100) Next j&  For j& = 1000 To 440 Step -5 Call Sound(j&, j& / 100) Next j&  Succ% = CloseSound End Sub 

