Results 1 to 10 of 10

Thread: VB6.0 – Sound and DirectXSound Tutorial

Threaded View

  1. #5

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

    Re: Tutorial under construction

    How to display the wave data (oscilloscope):

    This is very easily done because you simply display the wave data "as is".

    The only problem is that you have to write separate code for 8 and 16 bit sound formats, and also for mono and stereo.

    In the following code, you will see 3 functions:
    DisplayWaveData8 is for displaying 8 bit data, stereo and mono.
    DisplayWaveData16_8 is for displaying 16 bit data, but the data is recorded in byte arrays (instead of integer arrays), therefore, the function simply copies the array to an integer array.
    DisplayWaveData16 is for displaying 16 bit data, stereo and mono.
    VB Code:
    1. Private Sub DisplayWaveData8(DataBuff() As Byte, Pic As PictureBox, Stereo As Boolean)
    2.     Dim Stp As Single, HBuffer As Long, Q As Single
    3.     Dim LX As Single, LY As Single, RX As Single, RY As Single
    4.     Dim LVal As Single, RVal As Single, K As Long
    5.    
    6.     If Not Stereo Then
    7.         HBuffer = UBound(DataBuff)
    8.         Stp = HBuffer / (Pic.Width / 15)
    9.        
    10.         Pic.Scale (0, 127)-(HBuffer, -127)
    11.         Pic.PSet (0, 0)
    12.        
    13.         Pic.Cls
    14.        
    15.         For Q = 0 To HBuffer - 2 Step Stp
    16.             Pic.Line -(Fix(Q), DataBuff(Fix(Q)) - 127)
    17.         Next Q
    18.     Else
    19.         HBuffer = UBound(DataBuff) \ 2
    20.         Stp = HBuffer / (Pic.Width / 15)
    21.        
    22.         Pic.Scale (0, 256)-(HBuffer, -256)
    23.         Pic.PSet (0, 0)
    24.        
    25.         Pic.Cls
    26.        
    27.         LX = 0
    28.         LY = -127
    29.         RX = 0
    30.         RY = 127
    31.        
    32.         For Q = 0 To HBuffer - 2 Step Stp
    33.             K = Q
    34.             K = K - (K Mod 2)
    35.            
    36.             LVal = DataBuff(K + 1) - 255
    37.             RVal = DataBuff(K)
    38.            
    39.             Pic.Line (LX, LY)-(K, LVal)
    40.             Pic.Line (RX, RY)-(K, RVal)
    41.            
    42.             LX = K
    43.             LY = LVal
    44.            
    45.             RX = K
    46.             RY = RVal
    47.         Next Q
    48.     End If
    49. End Sub
    50.  
    51. ' the sound is 16 bit, but it comes in Bytes, not Integer
    52. Private Sub DisplayWaveData16_8(DataBuff() As Byte, Pic As PictureBox, Stereo As Boolean)
    53.     Dim Buff() As Integer
    54.    
    55.     ReDim Buff(UBound(DataBuff) \ 2 - 1)
    56.    
    57.     CopyMemory Buff(0), DataBuff(0), UBound(DataBuff) + 1
    58.    
    59.     DisplayWaveData16 Buff, Pic, Stereo
    60. End Sub
    61.  
    62. Private Sub DisplayWaveData16(DataBuff() As Integer, Pic As PictureBox, Stereo As Boolean)
    63.     Dim Stp As Single, HBuffer As Long, Q As Single
    64.     Dim LX As Single, LY As Single, RX As Single, RY As Single
    65.     Dim LVal As Single, RVal As Single, K As Long
    66.    
    67.     If Not Stereo Then
    68.         HBuffer = UBound(DataBuff)
    69.         Stp = HBuffer / (Pic.Width / 15)
    70.        
    71.         Pic.Scale (0, 0.5)-(HBuffer, -0.5)
    72.         Pic.PSet (0, 0)
    73.        
    74.         Pic.Cls
    75.         For Q = 0 To HBuffer - 2 Step Stp
    76.             Pic.Line -(Fix(Q), DataBuff(Fix(Q)) / 65536#)
    77.         Next Q
    78.     Else
    79.         HBuffer = UBound(DataBuff) \ 2
    80.         Stp = HBuffer / (Pic.Width / 15)
    81.        
    82.         Pic.Scale (0, 1)-(HBuffer, -1)
    83.         Pic.PSet (0, 0)
    84.        
    85.         Pic.Cls
    86.        
    87.         LX = 0
    88.         LY = -0.5
    89.         RX = 0
    90.         RY = 0.5
    91.        
    92.         For Q = 0 To HBuffer - 2 Step Stp
    93.             K = Q
    94.             K = K - (K Mod 2)
    95.            
    96.             LVal = DataBuff(K + 1) / 65536# - 0.5
    97.             RVal = DataBuff(K) / 65536# + 0.5
    98.            
    99.             Pic.Line (LX, LY)-(K, LVal)
    100.             Pic.Line (RX, RY)-(K, RVal)
    101.            
    102.             LX = K
    103.             LY = LVal
    104.            
    105.             RX = K
    106.             RY = RVal
    107.         Next Q
    108.     End If
    109. End Sub
    To see the previous functions in use, see this project: Recording, and displaying the wave data

    Note: The functions are made assuming that the picture box is sitting on a form with ScaleMode = vbTwips (the default).
    Attached Files Attached Files
    Last edited by CVMichael; Mar 2nd, 2006 at 12:38 PM.

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