Advertisement
Robomatics

Reading audio

Apr 24th, 2013
3,508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.21 KB | None | 0 0
  1. Public Class Form1
  2.  
  3.     'Define the pic and graphics to draw with
  4.     Dim pic As New Bitmap(Me.Width, Me.height)
  5.     Dim gfx As Graphics = Graphics.FromImage(pic)
  6.  
  7.     'Data storage
  8.     Dim samplez As New List(Of Short)
  9.     Dim maxamount As Short
  10.  
  11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12.  
  13.         gfx.DrawString("CLICK HERE!", New Font("Arial", 20), Brushes.Black, 0, 0)
  14.  
  15.         PictureBox1.Image = pic
  16.  
  17.     End Sub
  18.  
  19.     Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
  20.         OpenFileDialog1.ShowDialog()
  21.     End Sub
  22.  
  23.     Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
  24.  
  25.         GC.Collect()
  26.         Dim wavefile() As Byte = IO.File.ReadAllBytes(OpenFileDialog1.FileName)
  27.         GC.Collect()
  28.  
  29.         Dim memstream As New IO.MemoryStream(wavefile)
  30.         Dim binreader As New IO.BinaryReader(memstream)
  31.  
  32.         Dim ChunkID As Integer = binreader.ReadInt32()
  33.         Dim filesize As Integer = binreader.ReadInt32()
  34.         Dim rifftype As Integer = binreader.ReadInt32()
  35.         Dim fmtID As Integer = binreader.ReadInt32()
  36.         Dim fmtsize As Integer = binreader.ReadInt32()
  37.         Dim fmtcode As Integer = binreader.ReadInt16()
  38.         Dim channels As Integer = binreader.ReadInt16()
  39.         Dim samplerate As Integer = binreader.ReadInt32()
  40.         Dim fmtAvgBPS As Integer = binreader.ReadInt32()
  41.         Dim fmtblockalign As Integer = binreader.ReadInt16()
  42.         Dim bitdepth As Integer = binreader.ReadInt16()
  43.  
  44.         If fmtsize = 18 Then
  45.             Dim fmtextrasize As Integer = binreader.ReadInt16()
  46.             binreader.ReadBytes(fmtextrasize)
  47.         End If
  48.  
  49.         Dim DataID As Integer = binreader.ReadInt32()
  50.         Dim DataSize As Integer = binreader.ReadInt32()
  51.  
  52.         'Grabbing the data into 16bit words known as samples
  53.         samplez.Clear()
  54.         For i = 0 To (DataSize - 1) / 2
  55.  
  56.             samplez.Add(binreader.ReadInt16())
  57.  
  58.             If samplez(samplez.Count - 1) > maxamount Then 'Using this for the pic
  59.                 maxamount = samplez(samplez.Count - 1)
  60.             End If
  61.  
  62.         Next
  63.  
  64.         DrawAudio()
  65.  
  66.     End Sub
  67.  
  68.     Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
  69.  
  70.         If OpenFileDialog1.FileName <> "" Then
  71.             DrawAudio()
  72.         End If
  73.  
  74.     End Sub
  75.  
  76.     Private Sub DrawAudio()
  77.  
  78.         'Redefine since size changed
  79.         pic = New Bitmap(PictureBox1.Width, PictureBox1.Height)
  80.         gfx = Graphics.FromImage(pic)
  81.  
  82.         'Clear picturebox
  83.         gfx.FillRectangle(Brushes.Black, 0, 0, pic.Width, pic.Height)
  84.  
  85.  
  86.         Dim ratio As Integer = (samplez.Count - 1) / (pic.Width - 1) 'If there are 10000 samples and 200 pixels, this would be every 50th sample is shown
  87.         Dim halfpic As Integer = (pic.Height / 2) 'Simply half the height of the picturebox
  88.  
  89.         For i = 1 To pic.Width - 10 Step 2 'Steping 2 because in one go, we do 2 samples
  90.             Dim leftdata As Integer = Math.Abs(samplez(i * ratio)) 'Grabbing that N-th sample to display. Using Absolute to show them one direction
  91.             Dim leftpercent As Single = leftdata / (maxamount * 2) 'This breaks it down to something like 0.0 to 1.0. Multiplying by 2 to make it half.
  92.             Dim leftpicheight As Integer = leftpercent * pic.Height 'So when the percent is tied to the height, its only a percent of the height
  93.             gfx.DrawLine(Pens.LimeGreen, i, halfpic, i, leftpicheight + halfpic) 'Draw dat! The half pic puts it in the center
  94.  
  95.             Dim rightdata As Integer = Math.Abs(samplez((i + 1) * ratio)) 'Same thing except we're grabbing i + 1 because we'd skip it because of the 'step 2' on the for statement
  96.             Dim rightpercent As Single = -rightdata / (maxamount * 2) 'put a negative infront of data so it goes down.
  97.             Dim rightpicheight As Integer = rightpercent * pic.Height
  98.             gfx.DrawLine(Pens.Blue, i, halfpic, i, rightpicheight + halfpic)
  99.  
  100.         Next
  101.  
  102.         PictureBox1.Image = pic
  103.  
  104.     End Sub
  105.  
  106. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement