|
-
Aug 22nd, 2005, 03:59 PM
#1
Thread Starter
Fanatic Member
create wav file
dear all,
do you know where to find good code to create a wav file from VB? i would like to generate a simple sine wav, however i can't seem to find any good code of it, the ones i've tried are quite imprecise and add weird 'clicks' to it.
any suggestion welcomed 
cheers,
wc.
When your car breaks down,
close all windows and retry 
=> please rate all users posts! <=
-
Aug 22nd, 2005, 04:12 PM
#2
Re: create wav file
This generates a wave file (no clicks or ticks... just clear sound ):
(It makes a 1 second tone of 440 Hz)
VB Code:
Option Explicit
Private Type tWAVEFORMATEX
wFormatTag As Integer
nChannels As Integer
nSamplesPerSec As Long
nAvgBytesPerSec As Long
nBlockAlign As Integer
wBitsPerSample As Integer
cbSize As Integer
ExtraData(1 To 32) As Byte ' makes the structure 50 bytes long
End Type
Private Type FileHeader
lRiff As Long
lFileSize As Long
lWave As Long
lFormat As Long
lFormatLength As Long
End Type
Private Type WaveFormat
wFormatTag As Integer
nChannels As Integer
nSamplesPerSec As Long
nAvgBytesPerSec As Long
nBlockAlign As Integer
wBitsPerSample As Integer
End Type
Private Type ChunkHeader
lType As Long
lLen As Long
End Type
Private Sub Form_Load()
Dim Buff(0 To 44100) As Integer
GenerateTone 440, Buff, 1
SaveWaveFile "C:\test_Wave.wav", Buff
End Sub
Private Sub GenerateTone(ByVal Frequency As Single, IntBuff() As Integer, Optional Amplitude As Single = 1, Optional SamplesPerSec As Long = 44100, Optional Startpos As Long = 0, Optional Length As Long = -1)
Dim K As Long, V1 As Double
Const PI As Double = 3.14159265358979
V1 = SamplesPerSec / (PI * 2 * Frequency)
If Length = -1 Then Length = UBound(IntBuff) - Startpos
For K = Startpos To Startpos + Length
IntBuff(K) = CInt(Fix(Sin(K / V1) * (32766.5 * Amplitude)))
Next K
End Sub
Private Sub SaveWaveFile(ByVal WaveFileName As String, ByRef Buffer() As Integer, Optional SamplesPerSec As Long = 44100)
Dim WF As tWAVEFORMATEX
WF.wFormatTag = 1 'WAVE_FORMAT_PCM
WF.nChannels = 1
WF.wBitsPerSample = 16
WF.nSamplesPerSec = SamplesPerSec
WF.nBlockAlign = (WF.wBitsPerSample * WF.nChannels) \ 8
WF.nAvgBytesPerSec = WF.nSamplesPerSec * WF.nBlockAlign
Open WaveFileName For Binary Access Write Lock Write As #1
WaveWriteHeader 1, WF
Put #1, , Buffer
WaveWriteHeaderEnd 1
Close #1
End Sub
Private Sub WaveWriteHeader(ByVal OutFileNum As Integer, WaveFmt As tWAVEFORMATEX)
Dim header As FileHeader
Dim HdrFormat As WaveFormat
Dim chunk As ChunkHeader
With header
.lRiff = &H46464952 ' "RIFF"
.lFileSize = 0
.lWave = &H45564157 ' "WAVE"
.lFormat = &H20746D66 ' "fmt "
.lFormatLength = Len(HdrFormat)
End With
With HdrFormat
.wFormatTag = WaveFmt.wFormatTag
.nChannels = WaveFmt.nChannels
.nSamplesPerSec = WaveFmt.nSamplesPerSec
.nAvgBytesPerSec = WaveFmt.nAvgBytesPerSec
.nBlockAlign = WaveFmt.nBlockAlign
.wBitsPerSample = WaveFmt.wBitsPerSample
End With
chunk.lType = &H61746164 ' "data"
chunk.lLen = 0
Put #OutFileNum, 1, header
Put #OutFileNum, , HdrFormat
Put #OutFileNum, , chunk
End Sub
Private Sub WaveWriteHeaderEnd(ByVal OutFileNum As Integer)
Dim header As FileHeader
Dim HdrFormat As WaveFormat
Dim chunk As ChunkHeader
Dim Lng As Long
Lng = LOF(OutFileNum)
Put #OutFileNum, 5, Lng
Lng = LOF(OutFileNum) - (Len(header) + Len(HdrFormat) + Len(chunk))
Put #OutFileNum, Len(header) + Len(HdrFormat) + 5, Lng
End Sub
-
Aug 22nd, 2005, 04:34 PM
#3
Thread Starter
Fanatic Member
Re: create wav file
thank you very much CVm, i will test this tomorrow morning as i don't have vb installed here.
i am going to perform many audio processing operations, you seem to be the guru expert in here. any suggestions on reading for this?
i am very much interested in psycoaudio processing, mp3 algoirithms and such...
When your car breaks down,
close all windows and retry 
=> please rate all users posts! <=
-
Aug 22nd, 2005, 05:21 PM
#4
Re: create wav file
 Originally Posted by wildcat_2000
any suggestions on reading for this?
Sorry, everything I know is from trial & error type of thing, I never did any reading on anything I know about sound processing...
 Originally Posted by wildcat_2000
i am very much interested in psycoaudio processing, mp3 algoirithms and such... 
I have no idea what is psycoaudio...
And I don't know any mp3 algorithms also, I only used DLL's wich I found on the net to compress to MP3, but no clue on how they work...
-
Aug 22nd, 2005, 05:32 PM
#5
Thread Starter
Fanatic Member
-
May 15th, 2006, 05:06 AM
#6
Addicted Member
Re: create wav file
Hello
What parameters/values should i change in CVMichael's code to make it work for 8KHz/8-bit/Mono ?
10x
-
May 15th, 2006, 07:19 AM
#7
Re: create wav file
 Originally Posted by Mimo
Hello
What parameters/values should i change in CVMichael's code to make it work for 8KHz/8-bit/Mono ?
10x
Ow, common, that's something so simple, you just have to look through the code...
Anyways, if you still did not figure it out yet:
In the SaveWaveFile
VB Code:
WF.wFormatTag = 1 'WAVE_FORMAT_PCM
[b]WF.nChannels = 1[/b]
[b]WF.wBitsPerSample = 8[/b]
WF.nSamplesPerSec = SamplesPerSec
And in the Form_Load:
SaveWaveFile "C:\test_Wave.wav", Buff, 8000
[Edit]
Actually, this code was not made for 8 bit.
You have to work with Byte array instead of Integer Array for 8 Bit
-
May 16th, 2006, 01:32 AM
#8
Addicted Member
Re: create wav file
Sorry I have not looked @ the code at first.
Anyway it works, the changes are (if someone esle is interested):
VB Code:
GenerateTone 440, Buff, 1, 8000
SaveWaveFile "C:\test_Wave.wav", Buff, 8000
'.....
IntBuff(K) = CByte(Fix((Sin(K / V1) + 1) * (127.5 * Amplitude)))
' ...
... ByRef Buffer() As Byte ' (twice)
10x ...
-
May 16th, 2006, 03:12 AM
#9
Hyperactive Member
Re: create wav file
You should also check this code out, Shows how to make ya own waves also but with a lot of features
http://www.pscode.com/vb/scripts/Sho...xtCodeId=65169
When your dreams come true.
On error resume pulling hair out.
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
|