[VB6]Set volume level of WAV files-VBForums
Results 1 to 3 of 3

Thread: [VB6]Set volume level of WAV files

  1. #1

    Thread Starter
    New Member Cereal Killer's Avatar
    Join Date
    Dec 2012
    Posts
    2

    [VB6]Set volume level of WAV files

    Hi men

    A week ago I asked for some help in MasterDrive.it community on how I could set volume level of WAV sounds before that my application played them using the PlaySound API function. In that thread, I didn't found anything helpful, so I decided to post here.

    Looking for a solution, I found this example on Microsoft's web site:
    Code:
    Option Explicit
    
     Private Declare Function sndPlaySound Lib "MMSYSTEM.DLL" (ByVal lpszSoundName As String, ByVal wFlags As Integer) As Integer
     Private Declare Function waveoutSetVolume Lib "MMSYSTEM.DLL" (ByVal wDeviceID As Integer, ByVal dwVolumeRight As Integer, ByVal dwVolumeLeft As Integer) As Integer
     Private Declare Function waveOutGetVolume Lib "MMSYSTEM.DLL" (ByVal wDeviceID As Integer, lpdwvolume As Long) As Integer
    
       Const SND_ASYNC = &H1
       Const SND_NODEFAULT = &H2
    
       Dim CurrentVolLeft As Long
       Dim CurrentVolRight As Long
    
    Sub Form_Load()
       Dim x As Integer
       Dim BothVolumes As Long
    
     ' Note that the waveid is 0 indicating the first wave output device.
     ' If you were to play multiple wavefiles on multiple wave output devices
     ' you would use 1 for the second wave output device, 2 for the third and
     ' so on.
     ' This code will retrieve the current volume setting
    
       x = waveOutGetVolume(0, BothVolumes)
    
       ' This code isolates the low-order word.
       ' Note that the value &HFFFF& is a Long Integer, which is the same
       ' as 0000FFFF, but because Visual Basic would automatically
       ' truncate this to FFFF, you must force the logical operation to use
       ' a four-byte Long Integer (0000FFFF) rather than a two-byte Integer
       ' (FFFF). This is accomplished by using the type casting
       ' character (&).
       CurrentVolLeft = BothVolumes And &HFFFF&
    
       ' This code isolates the high-order word.
       ' Enter the following two lines as one, single line:
       CurrentVolRight = ((BothVolumes And &HFFFF0000) / &H10000) And &HFFFF&
    
       LeftVol.Caption = Hex$(CurrentVolLeft)   ' Update the label with the
       RightVol.Caption = Hex$(CurrentVolRight) ' current volume settings.
    End Sub
    
    Sub PlaySound_Click()
       Dim x As Integer
       Dim wFlags As Integer
       Dim SoundName As String
       SoundName = "C:\WINDOWS\MSREMIND.WAV"    ' Pick any wave file.
       wFlags = SND_ASYNC Or SND_NODEFAULT
       x = sndPlaySound(SoundName$, wFlags%)    ' Play the wave file.
    End Sub
    
    Sub LeftUp_Click()
       ' Increase the left sound channel setting:
       Dim x As Integer
       CurrentVolLeft = CurrentVolLeft + &H1000&
       ' Prevent the channel setting from exceeding the maximum limit:
       If CurrentVolLeft > &HFFFF& Then CurrentVolLeft = &HFFFF&
       LeftVol.Caption = Format$(Hex$(CurrentVolLeft))
       ' Enter the following two lines as one, single line:
       x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)), CInt("&H" + Hex$(CurrentVolLeft)))
    End Sub
    
    Sub LeftDown_Click()
       ' Decrease the left sound channel setting:
       Dim x As Integer
       CurrentVolLeft = CurrentVolLeft - &H1000&
       ' Prevent the channel setting from dropping below the minimum limit:
       If CurrentVolLeft < &H0& Then CurrentVolLeft = &H0&
       LeftVol.Caption = Hex$(CurrentVolLeft)
       ' Enter the following two lines as one, single line:
       x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)), CInt("&H" + Hex$(CurrentVolLeft)))
    End Sub
    
    Sub RightUp_Click()
       ' Increase the right sound channel setting:
       Dim x As Integer
       CurrentVolRight = CurrentVolRight + &H1000&
       ' Prevent the channel setting from exceeding the maximum limit.
       If CurrentVolRight > &HFFFF& Then CurrentVolRight = &HFFFF&
       RightVol.Caption = Hex$(CurrentVolRight)
       ' Enter the following two lines as one, single line:
       x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)), CInt("&H" + Hex$(CurrentVolLeft)))
    End Sub
    
    Sub RightDown_Click()
       ' Decrease the right sound channel setting:
       Dim x As Integer
       CurrentVolRight = CurrentVolRight - &H1000&
       ' Prevent the channel setting from dropping below the minimum limit:
       If CurrentVolRight < 0 Then CurrentVolRight = 0
       RightVol.Caption = Hex$(CurrentVolRight)
       ' Enter the following two lines as one, single line:
       x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)), CInt("&H" + Hex$(CurrentVolLeft)))
    End Sub
    and it seems that it's what I was looking for, that is a way to change volume level only for the sounds played by my program, without changing the Master Mixer's settings.

    Trying to run the code above, VB returns me an error saying that it's impossibile to find the MMSYSTEM.dll So I looked into my System32 folder to see if I really miss this dll, but I discovered I have that file.
    So why VB can't access to that functions?

    Moreover, because that error doesn't let me try the code, I don't know if it works or not.

    So my question is: do you know a way to change volume level of a sound that is going to be played by PlaySound function called by a SINGLE application (mine), without modifing Master Volume settings?
    I tryed using DirectX and DirectSound, but they don't meet my needs.

    If only I could try the code above...Do you know how to fix the error that appears?

    Thank you very very much,
    bye

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,142

    Re: [VB6]Set volume level of WAV files

    Those APIs are now exported from winmm.dll. The "How to Control the Volume of Sound Files from Visual Basic" article you got that code from is pretty old - the page says it applies to Microsoft Visual Basic 3.0, which is 16-bit. The proper declare for them are:

    Code:
    Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSound As String, ByVal fuSound As Long) As Long
    Private Declare Function waveOutGetVolume Lib "winmm.dll" (ByVal hwo As Long, ByRef pdwVolume As Long) As Long
    Private Declare Function waveOutSetVolume Lib "winmm.dll" (ByVal hwo As Long, ByVal dwVolume As Long) As Long
    Click the function's names to view their documentation page. Your code for waveoutSetVolume will have to be modified to suit the declaration.
    Last edited by Bonnie West; Dec 8th, 2012 at 03:10 PM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    New Member Cereal Killer's Avatar
    Join Date
    Dec 2012
    Posts
    2

    Re: [VB6]Set volume level of WAV files

    Ok, now I've changed declares and I tested the code.

    It changes the WAV volume settings of Master volume, so ALL the programs running increase thier sounds' volumes when they play 'em.

    What I want to do is:

    I've a sound saved as WAV file on my PC, used by my application. Now, when I'm going to play it using PlaySound function, before I call this function, I want to set sound volume to a percentage defined by me, and only after volume is changed, playing the sound. All this without modifing Master volume settings.

    In other words I think that I must load the file in a buffer. Once sound is loaded in this buffer, I can change buffer volume level, and then play the sound contained into this buffer.

    I'm not sure if I was clear or if what I thought it's possible directly from VB or not. In any case I would apreciate another your answer.

    The only alternative I have is to:
    1. read and keep in memory Master volume settings;
    2. modify the values with mine
    3. reset old value
    but I don't think this is the better solution...

    Thanks,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.