Results 1 to 10 of 10

Thread: VB6.0 – Sound and DirectXSound Tutorial

Threaded View

  1. #10

    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