|
-
Apr 3rd, 2008, 10:07 PM
#8
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:
' Change volume for 8 Bit Mono sound Public Sub ChangeVolume8Mono(Buffer8() As Byte, Percent As Single) Dim K As Long Dim Sample As Long For K = LBound(Buffer8) To UBound(Buffer8) Sample = ((CSng(Buffer8(K)) - 127) * Percent) + 127 If Sample < 0 Then Sample = 0 If Sample > 255 Then Sample = 255 Buffer8(K) = Sample Next K End Sub ' Change volume for 8 Bit Stereo sound Public Sub ChangeVolume8Stereo(Buffer8() As Byte, LPercent As Single, RPercent As Single) Dim K As Long Dim LSample As Long Dim RSample As Long For K = LBound(Buffer8) To UBound(Buffer8) Step 2 LSample = ((CSng(Buffer8(K)) - 127) * LPercent) + 127 RSample = ((CSng(Buffer8(K + 1)) - 127) * RPercent) + 127 If LSample < 0 Then LSample = 0 If LSample > 255 Then LSample = 255 If RSample < 0 Then RSample = 0 If RSample > 255 Then RSample = 255 Buffer8(K) = LSample Buffer8(K + 1) = RSample Next K 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:
' Change volume for 16 Bit Mono sound Public Sub ChangeVolume16Mono(Buffer16() As Integer, Percent As Single) Dim K As Long Dim Sample As Long For K = LBound(Buffer16) To UBound(Buffer16) Sample = Buffer16(K) * Percent If Sample < -32768 Then Sample = -32768 If Sample > 32767 Then Sample = 32767 Buffer16(K) = Sample Next K End Sub ' Change volume for 16 Bit Stereo sound Public Sub ChangeVolume16Stereo(Buffer16() As Integer, LPercent As Single, RPercent As Single) Dim K As Long Dim LSample As Long Dim RSample As Long For K = LBound(Buffer16) To UBound(Buffer16) Step 2 LSample = Buffer16(K) * LPercent RSample = Buffer16(K + 1) * RPercent If LSample < -32768 Then LSample = -32768 If LSample > 32767 Then LSample = 32767 If RSample < -32768 Then RSample = -32768 If RSample > 32767 Then RSample = 32767 Buffer16(K) = LSample Buffer16(K + 1) = RSample Next K 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|