Results 1 to 10 of 10

Thread: VB6.0 – Sound and DirectXSound Tutorial

Threaded View

  1. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Tutorial under construction

    Changing the buffer volume.

    When you change the volume of the sound, you change the amplitude of the waves within the sound. If the amplitude is big, the sound is loud, and if the amplitude is small, the sound is low..

    To change the volume you basically have to multiply the values within the array sound, to make the amplitude bigger or smaller.

    When changing volume for 8 Bit sound, since the value is stored in a Byte is from 0 to 255, and silence is 127, you have to first make the value signed ie. from -128 to 127, then multiply, and then bring it back to range 0 to 255.

    Here is how it's done for 8 Bit Mono and Stereo sound:
    vb Code:
    1. ' Change volume for 8 Bit Mono sound
    2. Public Sub ChangeVolume8Mono(Buffer8() As Byte, Percent As Single)
    3.     Dim K As Long
    4.     Dim Sample As Long
    5.    
    6.     For K = LBound(Buffer8) To UBound(Buffer8)
    7.         Sample = ((CSng(Buffer8(K)) - 127) * Percent) + 127
    8.        
    9.         If Sample < 0 Then Sample = 0
    10.         If Sample > 255 Then Sample = 255
    11.        
    12.         Buffer8(K) = Sample
    13.     Next K
    14. End Sub
    15.  
    16. ' Change volume for 8 Bit Stereo sound
    17. Public Sub ChangeVolume8Stereo(Buffer8() As Byte, LPercent As Single, RPercent As Single)
    18.     Dim K As Long
    19.     Dim LSample As Long
    20.     Dim RSample As Long
    21.    
    22.     For K = LBound(Buffer8) To UBound(Buffer8) Step 2
    23.         LSample = ((CSng(Buffer8(K)) - 127) * LPercent) + 127
    24.         RSample = ((CSng(Buffer8(K + 1)) - 127) * RPercent) + 127
    25.        
    26.         If LSample < 0 Then LSample = 0
    27.         If LSample > 255 Then LSample = 255
    28.        
    29.         If RSample < 0 Then RSample = 0
    30.         If RSample > 255 Then RSample = 255
    31.        
    32.         Buffer8(K) = LSample
    33.         Buffer8(K + 1) = RSample
    34.     Next K
    35. End Sub
    For 16 bit sound, it's even easier because the sound is already signed, so all you have to do, is multiply it's value.
    Here's how it's done for 16 Bit Mono and Stereo sound:
    vb Code:
    1. ' Change volume for 16 Bit Mono sound
    2. Public Sub ChangeVolume16Mono(Buffer16() As Integer, Percent As Single)
    3.     Dim K As Long
    4.     Dim Sample As Long
    5.    
    6.     For K = LBound(Buffer16) To UBound(Buffer16)
    7.         Sample = Buffer16(K) * Percent
    8.        
    9.         If Sample < -32768 Then Sample = -32768
    10.         If Sample > 32767 Then Sample = 32767
    11.        
    12.         Buffer16(K) = Sample
    13.     Next K
    14. End Sub
    15.  
    16. ' Change volume for 16 Bit Stereo sound
    17. Public Sub ChangeVolume16Stereo(Buffer16() As Integer, LPercent As Single, RPercent As Single)
    18.     Dim K As Long
    19.     Dim LSample As Long
    20.     Dim RSample As Long
    21.    
    22.     For K = LBound(Buffer16) To UBound(Buffer16) Step 2
    23.         LSample = Buffer16(K) * LPercent
    24.         RSample = Buffer16(K + 1) * RPercent
    25.        
    26.         If LSample < -32768 Then LSample = -32768
    27.         If LSample > 32767 Then LSample = 32767
    28.        
    29.         If RSample < -32768 Then RSample = -32768
    30.         If RSample > 32767 Then RSample = 32767
    31.        
    32.         Buffer16(K) = LSample
    33.         Buffer16(K + 1) = RSample
    34.     Next K
    35. End Sub
    Last edited by CVMichael; Dec 23rd, 2008 at 09:20 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width