Results 1 to 9 of 9

Thread: create wav file

  1. #1

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    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! <=

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Option Explicit
    2.  
    3. Private Type tWAVEFORMATEX
    4.     wFormatTag As Integer
    5.     nChannels As Integer
    6.     nSamplesPerSec As Long
    7.     nAvgBytesPerSec As Long
    8.     nBlockAlign As Integer
    9.     wBitsPerSample As Integer
    10.     cbSize As Integer
    11.     ExtraData(1 To 32) As Byte ' makes the structure 50 bytes long
    12. End Type
    13.  
    14. Private Type FileHeader
    15.     lRiff As Long
    16.     lFileSize As Long
    17.     lWave As Long
    18.     lFormat As Long
    19.     lFormatLength As Long
    20. End Type
    21.  
    22. Private Type WaveFormat
    23.     wFormatTag As Integer
    24.     nChannels As Integer
    25.     nSamplesPerSec As Long
    26.     nAvgBytesPerSec As Long
    27.     nBlockAlign As Integer
    28.     wBitsPerSample As Integer
    29. End Type
    30.  
    31. Private Type ChunkHeader
    32.     lType As Long
    33.     lLen As Long
    34. End Type
    35.  
    36. Private Sub Form_Load()
    37.     Dim Buff(0 To 44100) As Integer
    38.    
    39.     GenerateTone 440, Buff, 1
    40.    
    41.     SaveWaveFile "C:\test_Wave.wav", Buff
    42. End Sub
    43.  
    44. 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)
    45.     Dim K As Long, V1 As Double
    46.     Const PI As Double = 3.14159265358979
    47.  
    48.     V1 = SamplesPerSec / (PI * 2 * Frequency)
    49.    
    50.     If Length = -1 Then Length = UBound(IntBuff) - Startpos
    51.    
    52.     For K = Startpos To Startpos + Length
    53.         IntBuff(K) = CInt(Fix(Sin(K / V1) * (32766.5 * Amplitude)))
    54.     Next K
    55. End Sub
    56.  
    57. Private Sub SaveWaveFile(ByVal WaveFileName As String, ByRef Buffer() As Integer, Optional SamplesPerSec As Long = 44100)
    58.     Dim WF As tWAVEFORMATEX
    59.    
    60.     WF.wFormatTag = 1 'WAVE_FORMAT_PCM
    61.     WF.nChannels = 1
    62.     WF.wBitsPerSample = 16
    63.     WF.nSamplesPerSec = SamplesPerSec
    64.    
    65.     WF.nBlockAlign = (WF.wBitsPerSample * WF.nChannels) \ 8
    66.     WF.nAvgBytesPerSec = WF.nSamplesPerSec * WF.nBlockAlign
    67.    
    68.     Open WaveFileName For Binary Access Write Lock Write As #1
    69.         WaveWriteHeader 1, WF
    70.         Put #1, , Buffer
    71.         WaveWriteHeaderEnd 1
    72.     Close #1
    73. End Sub
    74.  
    75. Private Sub WaveWriteHeader(ByVal OutFileNum As Integer, WaveFmt As tWAVEFORMATEX)
    76.     Dim header As FileHeader
    77.     Dim HdrFormat As WaveFormat
    78.     Dim chunk As ChunkHeader
    79.    
    80.     With header
    81.         .lRiff = &H46464952 ' "RIFF"
    82.         .lFileSize = 0
    83.         .lWave = &H45564157 ' "WAVE"
    84.         .lFormat = &H20746D66 ' "fmt "
    85.         .lFormatLength = Len(HdrFormat)
    86.     End With
    87.    
    88.     With HdrFormat
    89.         .wFormatTag = WaveFmt.wFormatTag
    90.         .nChannels = WaveFmt.nChannels
    91.         .nSamplesPerSec = WaveFmt.nSamplesPerSec
    92.         .nAvgBytesPerSec = WaveFmt.nAvgBytesPerSec
    93.         .nBlockAlign = WaveFmt.nBlockAlign
    94.         .wBitsPerSample = WaveFmt.wBitsPerSample
    95.     End With
    96.    
    97.     chunk.lType = &H61746164 ' "data"
    98.     chunk.lLen = 0
    99.    
    100.     Put #OutFileNum, 1, header
    101.     Put #OutFileNum, , HdrFormat
    102.     Put #OutFileNum, , chunk
    103. End Sub
    104.  
    105. Private Sub WaveWriteHeaderEnd(ByVal OutFileNum As Integer)
    106.     Dim header As FileHeader
    107.     Dim HdrFormat As WaveFormat
    108.     Dim chunk As ChunkHeader
    109.     Dim Lng As Long
    110.    
    111.     Lng = LOF(OutFileNum)
    112.     Put #OutFileNum, 5, Lng
    113.    
    114.     Lng = LOF(OutFileNum) - (Len(header) + Len(HdrFormat) + Len(chunk))
    115.     Put #OutFileNum, Len(header) + Len(HdrFormat) + 5, Lng
    116. End Sub

  3. #3

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    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! <=

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: create wav file

    Quote 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...
    Quote 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...

  5. #5

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    Re: create wav file

    thank you anyway

    cheers,

    wc.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  6. #6
    Addicted Member
    Join Date
    Nov 1999
    Location
    Lost
    Posts
    216

    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

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: create wav file

    Quote 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:
    1. WF.wFormatTag = 1 'WAVE_FORMAT_PCM
    2.     [b]WF.nChannels = 1[/b]
    3.     [b]WF.wBitsPerSample = 8[/b]
    4.     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

  8. #8
    Addicted Member
    Join Date
    Nov 1999
    Location
    Lost
    Posts
    216

    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:
    1. GenerateTone 440, Buff, 1, 8000
    2. SaveWaveFile "C:\test_Wave.wav", Buff, 8000
    3. '.....
    4.  
    5. IntBuff(K) = CByte(Fix((Sin(K / V1) + 1) * (127.5 * Amplitude)))
    6.  
    7. ' ...
    8. ... ByRef Buffer() As Byte  ' (twice)


    10x ...

  9. #9
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    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
  •  



Click Here to Expand Forum to Full Width