• 如何控制或取得WAVE音效的聲音大小

說明

    這必須修改WAVE OUT 的音量,表示音量的數值是Long型態,較高的2位元組是右聲道,較低的2位元組是左聲道,聲音由小到大是從&H0到&HFFFF,由於VB是以有號數的方式來表示2Bytes形態的整數,之間需要做點小轉換,其實從觀察VB的Integer型態的數就能發現一種規律了 由&H0到&HFFFF分別為0,1,2.....,32767,-32768,-32767....,-1
    如果是無號數 應該是從0到65535,我們只要將大於32767的無號數減去65536,這樣產生的數列就是有號數了,這樣以下程式應該就不會不知道為什麼要這樣算了

程式

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"  (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function waveOutGetVolume _
    Lib "winmm.dll" (ByVal uDeviceID As Long, _
    lpdwVolume As Long) As Long
    Private Declare Function waveOutSetVolume _
    Lib "winmm.dll" (ByVal uDeviceID As Long, _
    ByVal dwVolume As Long) As Long
    Const WAVE_MAPPER = -1&

    Public Function SetWaveVolume(ByVal lVolume As Long, ByVal rVolume As Long) As Long
    Dim iVolume(1) As Integer, sVolume As Long
    If lVolume > 32767 Then
        lVolume = lVolume - 65536
    End If
    If rVolume > 32767 Then
        rVolume = rVolume - 65536
    End If
    iVolume(0) = lVolume
    iVolume(1) = rVolume
    CopyMemory sVolume, iVolume(0), 4
    waveOutSetVolume WAVE_MAPPER, sVolume
    End Function

    Public Function GetWaveVolume(ByRef lVolume As Long, ByRef rVolume As Long) As Long
    Dim iVolume(1) As Integer, sVolume As Long
    waveOutGetVolume WAVE_MAPPER, sVolume
    CopyMemory iVolume(0), sVolume, 4
    lVolume = iVolume(0)
    rVolume = iVolume(1)

    If lVolume < 0 Then
        lVolume = lVolume + 65536
    End If

    If rVolume < 0 Then
        rVolume = rVolume + 65536
    End If

    End Function

    註:這兩個函數所傳入的分別是左聲道和右聲道的音量,值介於0~65535若要設定
         SetWaveVolume 32768,32768
    若要取得
        Dim lVol As Long ,rVol As Long
        GetWaveVolume lVol,rVol

範例下載

相關資訊

文件出處

      Honey

VB心得筆記歡迎各位的指教,如果您有任何文章或資料願意提供給我們的,請來信到VBNote

如果對本站有任何建議,歡迎來信給Honey,我們會盡快給您答覆