home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Programmer'…arterly (Limited Edition) / Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso / code / ch28code / baswave.bas next >
Encoding:
BASIC Source File  |  1995-08-02  |  9.6 KB  |  181 lines

  1. Attribute VB_Name = "basWave"
  2. '*********************************************************************
  3. ' BASWAVE.BAS - Plays a wave file from a resource.
  4. '*********************************************************************
  5. Option Explicit
  6. #If Win32 Then
  7. Private Declare Function PlaySound Lib "winmm.dll" Alias _
  8.     "PlaySoundA" (lpszName As Any, ByVal hModule&, _
  9.     ByVal dwFlags As Long) As Long
  10. Private Declare Function sndPlaySound& Lib "winmm" Alias _
  11.     "sndPlaySoundA" (lpszSoundName As Any, ByVal uFlags As Long)
  12. '*********************************************************************
  13. ' Flag values for uFlags parameter.
  14. '*********************************************************************
  15. Public Const SND_SYNC = &H0            ' Play synchronously (default)
  16. Public Const SND_ASYNC = &H1           ' Play asynchronously
  17.                                        ' **SEE NOTE IN PlayWavRes!!!!
  18. Public Const SND_NODEFAULT = &H2       ' No default sound event is used
  19. Public Const SND_MEMORY = &H4          ' lpszSoundName points to a
  20.                                        ' memory file.
  21. Public Const SND_ALIAS = &H10000       ' Name is a WIN.INI [sounds] entry
  22. Public Const SND_FILENAME = &H20000    ' Name is a file name
  23. Public Const SND_RESOURCE = &H40004    ' Name is a resource name or atom
  24. Public Const SND_ALIAS_ID = &H110000   ' Name is a WIN.INI [sounds]
  25.                                        ' entry identifier.
  26. Public Const SND_ALIAS_START = 0       ' Must be > 4096 to keep strings
  27.                                        ' in same section of resource file.
  28. Public Const SND_LOOP = &H8            ' Loop the sound until next sndPlaySound
  29. Public Const SND_NOSTOP = &H10         ' Don't stop any currently playing sound
  30. Public Const SND_VALID = &H1F          ' Valid flags
  31. Public Const SND_NOWAIT = &H2000       ' Don't wait if the driver is busy
  32. Public Const SND_VALIDFLAGS = &H17201F ' Set of valid flag bits. Anything outside
  33.                                        ' this range will raise an error.
  34. Public Const SND_RESERVED = &HFF000000 ' In particular these flags are reserved
  35. Public Const SND_TYPE_MASK = &H170007
  36. Public Const SND_PURGE = &H40          ' Purge non-static events for task
  37. Public Const SND_APPLICATION = &H80    ' Look for application specific association
  38. #Else
  39. Private Declare Function sndPlaySound Lib "MMSYSTEM" ( _
  40.     lpszSoundName As Any, ByVal wFlags%) As Integer
  41. '*********************************************************************
  42. '  Flag values for wFlags parameter.
  43. '*********************************************************************
  44. Public Const SND_SYNC = &H0        ' Play synchronously (default)
  45. Public Const SND_ASYNC = &H1       ' Play asynchronously
  46.                                    ' **SEE NOTE IN PlayWavRes!!!!
  47. Public Const SND_NODEFAULT = &H2   ' Don't use default sound
  48. Public Const SND_MEMORY = &H4      ' lpszSoundName points to a memory file
  49. Public Const SND_LOOP = &H8        ' Loop the sound until next sndPlaySound
  50. Public Const SND_NOSTOP = &H10     ' Don't stop any currently playing sound
  51. #End If
  52. '*********************************************************************
  53. ' Plays a wave file from a resource.
  54. '*********************************************************************
  55. Public Sub PlayWaveRes(vntResourceID As Variant, Optional vntFlags)
  56.     Dim bytSound() As Byte ' Always store binary data in byte arrays!
  57.     bytSound = LoadResData(vntResourceID, "WAVE")
  58.     '*****************************************************************
  59.     ' If no flags were provided, then set the defaults.
  60.     '*****************************************************************
  61.     If IsMissing(vntFlags) Then
  62.         vntFlags = SND_NODEFAULT Or SND_SYNC Or SND_MEMORY
  63.     End If
  64.     '*****************************************************************
  65.     ' Make sure the SND_MEMORY bit is set.
  66.     '*****************************************************************
  67.     If (vntFlags And SND_MEMORY) = 0 Then
  68.         vntFlags = vntFlags Or SND_MEMORY
  69.     End If
  70.     '*****************************************************************
  71.     ' WARNING:  If you want to play sound files asynchronously in
  72.     '           Win32, then you MUST change bytSound() from a local
  73.     '           variable to a module-level or static variable. Doing
  74.     '           this prevents your array from being destroyed before
  75.     '           sndPlaySound is complete. If you fail to do this, you
  76.     '           will pass an invalid memory pointer, which will cause
  77.     '           a GPF in MCI.
  78.     '*****************************************************************
  79.     If (vntFlags And SND_ASYNC) Then   ' Turn off SND_ASYNC if present
  80.         vntFlags = vntFlags Xor SND_ASYNC
  81.     End If
  82.     '*****************************************************************
  83.     ' Pass the address of the first element in the byte array to play
  84.     ' the wave file.
  85.     '*****************************************************************
  86.     If sndPlaySound(bytSound(0), vntFlags) = False Then
  87.         MsgBox "PlayWaveRes failed!", vbCritical
  88.     End If
  89. End Sub
  90.  
  91. Public Sub PlayWaveFile(strFileName As String, Optional vntFlags)
  92.     '*****************************************************************
  93.     ' If no flags were provided, then set the defaults.
  94.     '*****************************************************************
  95.     If IsMissing(vntFlags) Then
  96.         vntFlags = SND_NODEFAULT Or SND_SYNC
  97.     End If
  98.     '*****************************************************************
  99.     ' Turn off SND_MEMORY if present.
  100.     '*****************************************************************
  101.     If (vntFlags And SND_MEMORY) Then
  102.         vntFlags = vntFlags Xor SND_MEMORY
  103.     End If
  104.     '*****************************************************************
  105.     ' Play the wave (BE SURE TO USE ByVal!!!!).
  106.     '*****************************************************************
  107.     If sndPlaySound(ByVal strFileName, vntFlags) = False Then
  108.         MsgBox "PlayWaveFile failed!", vbCritical
  109.     End If
  110. End Sub
  111. #If Win32 Then
  112. '*********************************************************************
  113. ' The following procedures were provided for educational purposes only.
  114. ' All Win32 apps should start using PlaySound because sndPlaySound
  115. ' is now obsolete. I chose to use sndPlaySound in this module
  116. ' because PlaySound is not available on Win16.
  117. '*********************************************************************
  118. Public Sub PlayWaveRes32(vntResourceID As Variant, Optional vntFlags)
  119.     Dim bytSound() As Byte ' Always store binary data in byte arrays!
  120.     bytSound = LoadResData(vntResourceID, "WAVE")
  121.     '*****************************************************************
  122.     ' If no flags were provided, then set the defaults.
  123.     '*****************************************************************
  124.     If IsMissing(vntFlags) Then
  125.         vntFlags = SND_NODEFAULT Or SND_SYNC Or SND_MEMORY
  126.     End If
  127.     '*****************************************************************
  128.     ' WARNING:  If you want to play sound files asynchronously in
  129.     '           Win32, then you MUST change bytSound() from a local
  130.     '           variable to a module-level or static variable. Doing
  131.     '           this prevents your array from being destroyed before
  132.     '           sndPlaySound is complete. If you fail to do this, you
  133.     '           will pass an invalid memory pointer, which will cause
  134.     '           a GPF in MCI.
  135.     '*****************************************************************
  136.     If (vntFlags And SND_ASYNC) Then   ' Turn off SND_ASYNC if present
  137.         vntFlags = vntFlags Xor SND_ASYNC
  138.     End If
  139.     '*****************************************************************
  140.     ' Make sure these bits are set properly.
  141.     '*****************************************************************
  142.     vntFlags = IIf((vntFlags And SND_ALIAS) = 0, vntFlags, _
  143.                     vntFlags Xor SND_ALIAS)
  144.     vntFlags = IIf((vntFlags And SND_MEMORY) = 0, vntFlags _
  145.                     Or SND_MEMORY, vntFlags)
  146.     vntFlags = IIf((vntFlags And SND_FILENAME) = 0, vntFlags, _
  147.                     vntFlags Xor SND_FILENAME)
  148.     '*****************************************************************
  149.     ' Call PlaySound to play the wave file.
  150.     '*****************************************************************
  151.     If PlaySound(bytSound(0), 0&, vntFlags) = False Then
  152.         MsgBox "PlayWaveFile32 failed!", vbCritical
  153.     End If
  154. End Sub
  155.  
  156. Public Sub PlayWaveFile32(strFileName As String, Optional vntFlags)
  157.     '*****************************************************************
  158.     ' If no flags were provided, then set the defaults.
  159.     '*****************************************************************
  160.     If IsMissing(vntFlags) Then
  161.         vntFlags = SND_NODEFAULT Or SND_SYNC Or SND_FILENAME
  162.     End If
  163.     '*****************************************************************
  164.     ' Make sure these bits are set properly.
  165.     '*****************************************************************
  166.     vntFlags = IIf((vntFlags And SND_ALIAS) = 0, vntFlags, _
  167.                     vntFlags Xor SND_ALIAS)
  168.     vntFlags = IIf((vntFlags And SND_MEMORY) = 0, vntFlags, _
  169.                     vntFlags Xor SND_MEMORY)
  170.     vntFlags = IIf((vntFlags And SND_FILENAME) = 0, vntFlags _
  171.                     Or SND_FILENAME, vntFlags)
  172.     '*****************************************************************
  173.     ' Call PlaySound to play the wave file.
  174.     '*****************************************************************
  175.     If PlaySound(ByVal strFileName, 0&, vntFlags) = False Then
  176.         MsgBox "PlayWaveFile32 failed!", vbCritical
  177.     End If
  178. End Sub
  179. #End If
  180.  
  181.