|
-
Apr 3rd, 2008, 10:35 PM
#10
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:
Public Function WaveMIX2(Buffer1() As Integer, Buffer2() As Integer, Optional Div As Single = 1) As Integer() Dim K As Long, Sample As Single Dim Ret() As Integer ReDim Ret(UBound(Buffer1)) For K = 0 To UBound(Buffer1) Sample = (CSng(Buffer1(K)) + Buffer2(K)) * Div If Sample < -32768 Then Sample = -32768 If Sample > 32767 Then Sample = 32767 Ret(K) = Sample Next K WaveMIX2 = Ret End Function Public Function WaveMIX3(Buffer1() As Integer, Buffer2() As Integer, Buffer3() As Integer, Optional Div As Single = 1) As Integer() Dim K As Long, Sample As Single Dim Ret() As Integer ReDim Ret(UBound(Buffer1)) For K = 0 To UBound(Buffer1) Sample = (CSng(Buffer1(K)) + Buffer2(K) + Buffer3(K)) * Div If Sample < -32768 Then Sample = -32768 If Sample > 32767 Then Sample = 32767 Ret(K) = Sample Next K WaveMIX3 = Ret 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|