macro to create CSV from Wave File

G

Guest

Anyone know how to decode a wave file into it's two tables of data? I am
interested in coverting wave files to text. I think I have a macro somewhere
that writes a wave file, but I don't know how to reverse engineer it.
 
G

Guest

this is what I have for writing wave files. these command buttons say PLAY,
REC, and STOP.

' Add Winsock component using Excel View/Toolbars/Control Toolbox
' From Control Toolbox select More Controls icon, then Microsoft Winsock
Control

Public TCPvolts As Winsock
Public EndNow As Boolean

Private Sub CommandButton1_Click()
Dim unConverted, inData As String
Dim n0 As String, limit As Long, n As Long
Dim max As Single, min As Single, range As Single

EndNow = False
' 1. TCP/IP connection
Set TCPvolts = CreateObject("MSWinsock.Winsock.1")
TCPvolts.RemotePort = 888
TCPvolts.RemoteHost = Sheet1.Cells(13, 4)
Sheet1.Cells(22, 3).Value = "Connecting"
TCPvolts.Connect
While TCPvolts.State <> sckConnected And Not EndNow
DoEvents
Wend

If EndNow Then
Sheet1.Cells(22, 3).Value = "Done"
Exit Sub
End If
Sheet1.Cells(22, 3).Value = "Playing"

' 2. Convert column A to voltage 0-255 and send

limit = Sheet1.Cells(17, 4).Value
max = Sheet1.Cells(1, 1).Value
min = Sheet1.Cells(1, 1).Value

For i = 1 To limit
If Sheet1.Cells(i, 1).Value > max Then
max = Sheet1.Cells(i, 1).Value
End If
If Sheet1.Cells(i, 1).Value < min Then
min = Sheet1.Cells(i, 1).Value
End If
Next i

range = max - min

unConverted = ""
n = 0
data = ""
While n < limit And Not EndNow
n = n + 1
data = data + Chr(Round((Sheet1.Cells(n, 1) - min) / range * 255, 0))
DoEvents
Wend
If Not EndNow Then
TCPvolts.SendData data
End If
DoEvents
TCPvolts.Close
Sheet1.Cells(22, 3).Value = "Done"
End Sub
Private Sub CommandButton2_Click()
Dim unConverted, inData As String
Dim n0 As String, limit As Long, n As Long

EndNow = False

' 1. TCP/IP connection
Set TCPvolts = CreateObject("MSWinsock.Winsock.1")
TCPvolts.RemotePort = 888
Sheet1.Cells(22, 3).Value = "Connecting"
TCPvolts.RemoteHost = Sheet1.Cells(13, 4)
TCPvolts.Connect
While TCPvolts.State <> sckConnected And Not EndNow
DoEvents
Wend

If EndNow Then
Sheet1.Cells(22, 3).Value = "Done"
Exit Sub
End If
Sheet1.Cells(22, 3).Value = "Recording"

' 2. Collect real-time raw data, convert, and display as voltage
unConverted = ""
n = 0
limit = Sheet1.Cells(17, 4).Value
While n < limit And Not EndNow
If TCPvolts.BytesReceived >= 2 Or Len(unConverted) >= 2 Then
n = n + 1
If Len(unConverted) + n < limit Then
TCPvolts.GetData inData ' Read data
unConverted = unConverted & inData
End If
n0 = Mid(unConverted, 1, 1)
Sheet1.Cells(n, 1).Value = Asc(n0) / 255
unConverted = Mid(unConverted, 2)
End If
DoEvents
Wend
TCPvolts.Close
Sheet1.Cells(22, 3).Value = "Done"
End Sub

Private Sub CommandButton3_Click()
EndNow = True
End Sub

Private Sub CommandButton4_Click() ' Write Wav file
Dim fs, f, filelen
Dim limit As Long, n As Long
Dim max As Single, min As Single, range As Single
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateTextFile(ActiveWorkbook.Path + "\Excel.wav", True)
MsgBox "Sound written to " & ActiveWorkbook.Path + "\Excel.wav"

f.write "RIFF"
filelen = Sheet1.Cells(17, 4).Value + 36
'f.write Chr(&H64) ' file length - 8
'f.write Chr(&H1F)
'f.write Chr(&H0)
'f.write Chr(&H0)
f.write Chr(filelen Mod 256)
f.write Chr((filelen / 256) Mod 256)
f.write Chr((filelen / 256 / 256) Mod 256)
f.write Chr((filelen / 256 / 256 / 256) Mod 256)
f.write "WAVE"
f.write "fmt "
f.write Chr(16) ' Length of the fmt data (16 bytes)
f.write Chr(0)
f.write Chr(0)
f.write Chr(0)
f.write Chr(1) ' Format tag: 1 = PCM
f.write Chr(0)
f.write Chr(1) ' Channels: 1 = mono, 2 = stereo
f.write Chr(0)
f.write Chr(&H40) ' Samples per second: e.g., 8000
f.write Chr(&H1F)
f.write Chr(&H0)
f.write Chr(&H0)
f.write Chr(&H40) ' Sample rate * block align
f.write Chr(&H1F)
f.write Chr(&H0)
f.write Chr(&H0)
f.write Chr(1) ' channels * bits/sample / 8
f.write Chr(0)
f.write Chr(8) ' 8 or 16
f.write Chr(0)
f.write "data"
f.write Chr(Sheet1.Cells(17, 4).Value Mod 256)
f.write Chr((Sheet1.Cells(17, 4).Value / 256) Mod 256)
f.write Chr((Sheet1.Cells(17, 4).Value / 256 / 256) Mod 256)
f.write Chr((Sheet1.Cells(17, 4).Value / 256 / 256 / 256) Mod 256)

' Convert column A to voltage 0-255 and send
limit = Sheet1.Cells(17, 4).Value
max = Sheet1.Cells(1, 1).Value
min = Sheet1.Cells(1, 1).Value

For i = 1 To limit
If Sheet1.Cells(i, 1).Value > max Then
max = Sheet1.Cells(i, 1).Value
End If
If Sheet1.Cells(i, 1).Value < min Then
min = Sheet1.Cells(i, 1).Value
End If
Next i

range = max - min

For i = 1 To Sheet1.Cells(17, 4).Value
f.write Chr(Round((Sheet1.Cells(i, 1) - min) / range * 255, 0))
Next i
f.Close
End Sub
 
G

Guest

bump

fugazi48 said:
this is what I have for writing wave files. these command buttons say PLAY,
REC, and STOP.

' Add Winsock component using Excel View/Toolbars/Control Toolbox
' From Control Toolbox select More Controls icon, then Microsoft Winsock
Control

Public TCPvolts As Winsock
Public EndNow As Boolean

Private Sub CommandButton1_Click()
Dim unConverted, inData As String
Dim n0 As String, limit As Long, n As Long
Dim max As Single, min As Single, range As Single

EndNow = False
' 1. TCP/IP connection
Set TCPvolts = CreateObject("MSWinsock.Winsock.1")
TCPvolts.RemotePort = 888
TCPvolts.RemoteHost = Sheet1.Cells(13, 4)
Sheet1.Cells(22, 3).Value = "Connecting"
TCPvolts.Connect
While TCPvolts.State <> sckConnected And Not EndNow
DoEvents
Wend

If EndNow Then
Sheet1.Cells(22, 3).Value = "Done"
Exit Sub
End If
Sheet1.Cells(22, 3).Value = "Playing"

' 2. Convert column A to voltage 0-255 and send

limit = Sheet1.Cells(17, 4).Value
max = Sheet1.Cells(1, 1).Value
min = Sheet1.Cells(1, 1).Value

For i = 1 To limit
If Sheet1.Cells(i, 1).Value > max Then
max = Sheet1.Cells(i, 1).Value
End If
If Sheet1.Cells(i, 1).Value < min Then
min = Sheet1.Cells(i, 1).Value
End If
Next i

range = max - min

unConverted = ""
n = 0
data = ""
While n < limit And Not EndNow
n = n + 1
data = data + Chr(Round((Sheet1.Cells(n, 1) - min) / range * 255, 0))
DoEvents
Wend
If Not EndNow Then
TCPvolts.SendData data
End If
DoEvents
TCPvolts.Close
Sheet1.Cells(22, 3).Value = "Done"
End Sub
Private Sub CommandButton2_Click()
Dim unConverted, inData As String
Dim n0 As String, limit As Long, n As Long

EndNow = False

' 1. TCP/IP connection
Set TCPvolts = CreateObject("MSWinsock.Winsock.1")
TCPvolts.RemotePort = 888
Sheet1.Cells(22, 3).Value = "Connecting"
TCPvolts.RemoteHost = Sheet1.Cells(13, 4)
TCPvolts.Connect
While TCPvolts.State <> sckConnected And Not EndNow
DoEvents
Wend

If EndNow Then
Sheet1.Cells(22, 3).Value = "Done"
Exit Sub
End If
Sheet1.Cells(22, 3).Value = "Recording"

' 2. Collect real-time raw data, convert, and display as voltage
unConverted = ""
n = 0
limit = Sheet1.Cells(17, 4).Value
While n < limit And Not EndNow
If TCPvolts.BytesReceived >= 2 Or Len(unConverted) >= 2 Then
n = n + 1
If Len(unConverted) + n < limit Then
TCPvolts.GetData inData ' Read data
unConverted = unConverted & inData
End If
n0 = Mid(unConverted, 1, 1)
Sheet1.Cells(n, 1).Value = Asc(n0) / 255
unConverted = Mid(unConverted, 2)
End If
DoEvents
Wend
TCPvolts.Close
Sheet1.Cells(22, 3).Value = "Done"
End Sub

Private Sub CommandButton3_Click()
EndNow = True
End Sub

Private Sub CommandButton4_Click() ' Write Wav file
Dim fs, f, filelen
Dim limit As Long, n As Long
Dim max As Single, min As Single, range As Single
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateTextFile(ActiveWorkbook.Path + "\Excel.wav", True)
MsgBox "Sound written to " & ActiveWorkbook.Path + "\Excel.wav"

f.write "RIFF"
filelen = Sheet1.Cells(17, 4).Value + 36
'f.write Chr(&H64) ' file length - 8
'f.write Chr(&H1F)
'f.write Chr(&H0)
'f.write Chr(&H0)
f.write Chr(filelen Mod 256)
f.write Chr((filelen / 256) Mod 256)
f.write Chr((filelen / 256 / 256) Mod 256)
f.write Chr((filelen / 256 / 256 / 256) Mod 256)
f.write "WAVE"
f.write "fmt "
f.write Chr(16) ' Length of the fmt data (16 bytes)
f.write Chr(0)
f.write Chr(0)
f.write Chr(0)
f.write Chr(1) ' Format tag: 1 = PCM
f.write Chr(0)
f.write Chr(1) ' Channels: 1 = mono, 2 = stereo
f.write Chr(0)
f.write Chr(&H40) ' Samples per second: e.g., 8000
f.write Chr(&H1F)
f.write Chr(&H0)
f.write Chr(&H0)
f.write Chr(&H40) ' Sample rate * block align
f.write Chr(&H1F)
f.write Chr(&H0)
f.write Chr(&H0)
f.write Chr(1) ' channels * bits/sample / 8
f.write Chr(0)
f.write Chr(8) ' 8 or 16
f.write Chr(0)
f.write "data"
f.write Chr(Sheet1.Cells(17, 4).Value Mod 256)
f.write Chr((Sheet1.Cells(17, 4).Value / 256) Mod 256)
f.write Chr((Sheet1.Cells(17, 4).Value / 256 / 256) Mod 256)
f.write Chr((Sheet1.Cells(17, 4).Value / 256 / 256 / 256) Mod 256)

' Convert column A to voltage 0-255 and send
limit = Sheet1.Cells(17, 4).Value
max = Sheet1.Cells(1, 1).Value
min = Sheet1.Cells(1, 1).Value

For i = 1 To limit
If Sheet1.Cells(i, 1).Value > max Then
max = Sheet1.Cells(i, 1).Value
End If
If Sheet1.Cells(i, 1).Value < min Then
min = Sheet1.Cells(i, 1).Value
End If
Next i

range = max - min

For i = 1 To Sheet1.Cells(17, 4).Value
f.write Chr(Round((Sheet1.Cells(i, 1) - min) / range * 255, 0))
Next i
f.Close
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top