Results 1 to 10 of 10

Thread: VB6.0 – Sound and DirectXSound Tutorial

Hybrid View

  1. #1

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

    Re: Tutorial under construction

    Splitting a Stereo buffer to 2 Mono buffers

    In a Stereo buffer, the left channel is stored first, then the right channel.
    So, at position 1, you will find the sample of the left channel, position 2 is right channel, position 3 is left channel again, and so on.

    Here is code to split Stereo buffer to 2 Mono buffers, for 8 and 16 Bit:
    vb Code:
    1. Public Sub SoundStereoToMono16(InStereo() As Integer, OutLeftChannel() As Integer, OutRightChannel() As Integer)
    2.     Dim InPos As Long
    3.     Dim OutPos As Long
    4.    
    5.     ReDim OutLeftChannel((UBound(InStereo) + 1) \ 2 - 1)
    6.     ReDim OutRightChannel(UBound(OutLeftChannel))
    7.    
    8.     For InPos = 0 To UBound(InStereo) Step 2
    9.         OutLeftChannel(OutPos) = InStereo(InPos)
    10.         OutRightChannel(OutPos) = InStereo(InPos + 1)
    11.        
    12.         OutPos = OutPos + 1
    13.     Next InPos
    14. End Sub
    15.  
    16. Public Sub SoundStereoToMono8(InStereo() As Byte, OutLeftChannel() As Byte, OutRightChannel() As Byte)
    17.     Dim InPos As Long
    18.     Dim OutPos As Long
    19.    
    20.     ReDim OutLeftChannel((UBound(InStereo) + 1) \ 2 - 1)
    21.     ReDim OutRightChannel(UBound(OutLeftChannel))
    22.    
    23.     For InPos = 0 To UBound(InStereo) Step 2
    24.         OutLeftChannel(OutPos) = InStereo(InPos)
    25.         OutRightChannel(OutPos) = InStereo(InPos + 1)
    26.        
    27.         OutPos = OutPos + 1
    28.     Next InPos
    29. End Sub

  2. #2

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

    Re: Tutorial under construction

    Mixing buffers

    Mixing sound is actually very easy, you simply add the value from the first buffer to the value from the second buffer (and so on...).

    When mixing sounds, since you add 2 (or more) sounds into one, the volume might get too loud, therefore I also added a parameter to the function to lower the volume.

    I posted 2 functions, one mix 2 sounds, and another to mix 3 sounds. As you can see the only difference is where it's adding the buffers together, so it's easy to make a function to mix 4 sounds (and more), because all you do is to add all of them together.

    The last parameter is the "Divisor" (Div), if you mix 2 sounds you might want to put the divisor to 1/2 (0.5), or if you mix 3 sounds, 1/3 (0.33), and so on. I put the Div value equal to 1, because this is optional.
    vb Code:
    1. Public Function WaveMIX2(Buffer1() As Integer, Buffer2() As Integer, Optional Div As Single = 1) As Integer()
    2.     Dim K As Long, Sample As Single
    3.     Dim Ret() As Integer
    4.    
    5.     ReDim Ret(UBound(Buffer1))
    6.    
    7.     For K = 0 To UBound(Buffer1)
    8.         Sample = (CSng(Buffer1(K)) + Buffer2(K)) * Div
    9.        
    10.         If Sample < -32768 Then Sample = -32768
    11.         If Sample > 32767 Then Sample = 32767
    12.        
    13.         Ret(K) = Sample
    14.     Next K
    15.    
    16.     WaveMIX2 = Ret
    17. End Function
    18.  
    19. Public Function WaveMIX3(Buffer1() As Integer, Buffer2() As Integer, Buffer3() As Integer, Optional Div As Single = 1) As Integer()
    20.     Dim K As Long, Sample As Single
    21.     Dim Ret() As Integer
    22.    
    23.     ReDim Ret(UBound(Buffer1))
    24.    
    25.     For K = 0 To UBound(Buffer1)
    26.         Sample = (CSng(Buffer1(K)) + Buffer2(K) + Buffer3(K)) * Div
    27.        
    28.         If Sample < -32768 Then Sample = -32768
    29.         If Sample > 32767 Then Sample = 32767
    30.        
    31.         Ret(K) = Sample
    32.     Next K
    33.    
    34.     WaveMIX3 = Ret
    35. End Function
    Last edited by CVMichael; Dec 23rd, 2008 at 09:34 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