VB icon

A PIECE OF CODE FOR READING WAVE FILES

Email
Submitted on: 1/5/2015 3:34:00 PM
By: Altis (from psc cd)  
Level: Advanced
User Rating: By 16 Users
Compatibility: VB 5.0, VB 6.0
Views: 7288
 
     (Updates is on http://www.planet-source-code.com/xq/ASP/txtCodeId.26377/lngWId.1/qx/vb/scripts/ShowCode.htm) Fellow programmers. Ever wonder how one company's software reads a wave file and show it as a wave sample?

This code demonstrated how you can read a wave file and DISPLAY it as a wave sample without using a single API! It opens a wave file (FOR RANDOM) to reads the so-call BINARY (ASCII) and draws the lines in a picture box according to the data in the file.
Please note that the wave samples produces by this code are not as detail as those you found in some WAVE EDITOR.

I'm still figuring out the wave format and try to play the sound. If you know a function that can play sections of a wave file, generate sounds at different volume and frequency or directly make the speakers (connected to the sound card) vibrate at different frequency and volume, please send me an E-mail...Thanks.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: APIECE OF CODE FOR READING WAVE FILES
' Description:(Updates is on <a href="http://www.planet-source-code.com/xq/ASP/txtCodeId.26377/lngWId.1/qx/vb/scripts/ShowCode.htm">http://www.planet-source-code.com/xq/ASP/txtCodeId.26377/lngWId.1/qx/vb/scripts/ShowCode.htm</a>)
Fellow programmers. Ever wonder how one company's software reads a wave file and show it as a wave sample?<br><br>
This code demonstrated how you can read a wave file and DISPLAY it as a wave sample without using a single API! It opens a wave file (FOR RANDOM) to reads the so-call BINARY (ASCII) and draws the lines in a picture box according to the data in the file.<br>
Please note that the wave samples produces by this code are not as detail as those you found in some WAVE EDITOR.<br><br>
I'm still figuring out the wave format and try to play the sound. If you know a function that can play sections of a wave file, generate sounds at different volume and frequency or directly make the speakers (connected to the sound card) vibrate at different frequency and volume, please send me an E-mail...Thanks.
' By: Altis (from psc cd)
'
' Returns:WAVE SAMPLES
'
' Assumes:Insert a command button, picture box and a common dialog box before running this code.
'**************************************

Option Explicit
Private Sub Command1_Click()
 Dim FileNumber As Integer
 Dim I As Single
 Dim Min As Single
 Dim Max As Single
 Dim Temp As Integer
 Dim XZoomrate As Single
 Dim YZoomrate As Single
 Dim LastX As Single
 Dim LastY As Single
 On Error GoTo ErrorHandler
 ' Enable Cancel error
 With Picture1
 CommonDialog1.CancelError = True
 CommonDialog1.Filter = "Wave files (*.wav)|*.wav"
 CommonDialog1.ShowOpen
 
 ' Change the caption of the form
 Me.Caption = CommonDialog1.filename
 
 I = 44 ' Set I To 44, since the wave sample is begin at Byte 44.
 ' Open file to get the length of the wav
 ' e file.
 FileNumber = FreeFile
 Open CommonDialog1.filename For Random As #FileNumber
 Do
Get #FileNumber, I, Temp
I = I + 1
' Get the smallest and largest number. T
' hey will be use for the adjustment
' of the vertical size.
If Temp < Min Then Min = Temp
If Temp > Max Then Max = Temp
 Loop Until EOF(FileNumber)
 Close #FileNumber
 ' Adjust values and reset values
 XZoomrate = (.Width / I)
 YZoomrate = (Max - Min) / (.Height / 2)
 .CurrentX = 100
 .CurrentY = .Height / 2
 LastX = 100
 LastY = .Height / 2
 .AutoRedraw = True
 I = 44
 ' Reopen file using a different FileNumb
 ' er
 FileNumber = FileNumber + 1
 .Cls
 Open CommonDialog1.filename For Random As #FileNumber
 Do
Get #FileNumber, I, Temp
' Set CurrentX and CurrentY
.CurrentX = .CurrentX + XZoomrate
.CurrentY = (Temp / YZoomrate) + .Height / 2
' Plot graph
Picture1.Line (LastX, LastY)-(.CurrentX, .CurrentY), vbBlack
' Reset values
LastX = .CurrentX
LastY = .CurrentY
I = I + 1
If .CurrentX > .Width Then Exit Do
 Loop Until EOF(FileNumber)
 Close #FileNumber
 End With
 
ErrorHandler:
 ' Do nothing
End Sub
Private Sub Form_Resize()
 On Error Resume Next
 ' Resize control
 With Picture1
 .BackColor = vbWhite
 .ForeColor = vbBlack
 .Move 50, 500, Width - 200, Height - 800
 End With
End Sub


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.