Results 1 to 5 of 5

Thread: Create a sound by code

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved Create a sound by code

    Hi, I need to create a sound by code, whow could I do that.
    Would the WAV format be useable?

    I need to generate an echo-ranging type of sound, that is one tone with the echo of that tone after varying times, and the the frequency of that echo can be different from the starting tone. During the run of that echo-ranging a random noise should be heared.

    Any help would be appriciated.
    Last edited by opus; Sep 2nd, 2005 at 07:42 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

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

    Re: Create a sound by code

    Take a look at this thread: create wav file

    I also know how to add echo to it, but I can't do it right now cuz I'm at work. I don't know if I'll have time even when I get home. Anyways, I'll do it when I have time.

  3. #3
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Create a sound by code

    You use to be able to declare functions to use the "Sound.drv" in vb5 and I have some code that you can have.. but I'm not sure if it will work..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

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

    Re: Create a sound by code

    Here it is, with echo too...
    It will create some sample files in C drive
    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 * 5) As Integer ' 5 Seconds of sound
    38.    
    39.     GenerateTone 440, Buff, 1, , , 44100 * 0.2
    40.     AddEcho Buff, 44100 * 0.35, , 1.5
    41.     SaveWaveFile "C:\test_Wave_1.wav", Buff
    42.    
    43.     Erase Buff
    44.     GenerateTone 440, Buff, 1, , , 44100 * 0.3
    45.     AddEcho Buff, 44100 * 0.6, , 1.5
    46.     SaveWaveFile "C:\test_Wave_2.wav", Buff
    47.    
    48.     Erase Buff
    49.     GenerateDualTone 440, 438.6, Buff, 0.5, 0.5, , , 44100 * 0.35
    50.     AddEcho Buff, 44100 * 0.45, , 1.1
    51.     SaveWaveFile "C:\test_Wave_3.wav", Buff
    52.    
    53.     Erase Buff
    54.     GenerateDualTone 440, 1000, Buff, 0.5, 0.5, , , 44100 * 0.35
    55.     AddEcho Buff, 44100 * 0.1, , 1.1
    56.     AddEcho Buff, 44100 * 0.55, , 1.4
    57.     SaveWaveFile "C:\test_Wave_4.wav", Buff
    58.    
    59.     Erase Buff
    60.     GenerateDualTone 400, 700, Buff, 0.5, 0.5, , , 44100 * 0.4
    61.     AddEcho Buff, 44100 * 0.5, , 1.1
    62.     GenerateDualTone 500, 800, Buff, 0.5, 0.5, , 10000, 44100 * 0.4
    63.     AddEcho Buff, 44100 * 0.45, , 1.3
    64.     SaveWaveFile "C:\test_Wave_5.wav", Buff
    65. End Sub
    66.  
    67. 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)
    68.     Dim K As Long, V1 As Double
    69.     Const PI As Double = 3.14159265358979
    70.  
    71.     V1 = SamplesPerSec / (PI * 2 * Frequency)
    72.    
    73.     If Length = -1 Then Length = UBound(IntBuff) - Startpos
    74.    
    75.     For K = Startpos To Startpos + Length
    76.         IntBuff(K) = CInt(Fix(Sin(K / V1) * (32766.5 * Amplitude)))
    77.     Next K
    78. End Sub
    79.  
    80. Private Sub GenerateDualTone(ByVal Frequency1 As Single, ByVal Frequency2 As Single, IntBuff() As Integer, Optional Amplitude1 As Single = 1, Optional Amplitude2 As Single = 1, Optional SamplesPerSec As Long = 44100, Optional Startpos As Long = 0, Optional Length As Long = -1)
    81.     Dim K As Long, V1 As Double, V2 As Double, Sample As Long
    82.     Const PI As Double = 3.14159265358979
    83.  
    84.     V1 = SamplesPerSec / (PI * 2 * Frequency1)
    85.     V2 = SamplesPerSec / (PI * 2 * Frequency2)
    86.    
    87.     If Length = -1 Then Length = UBound(IntBuff) - Startpos
    88.    
    89.     For K = Startpos To Startpos + Length
    90.         Sample = Sin(K / V1) * (32766.5 * Amplitude1) + Sin(K / V2) * (32766.5 * Amplitude2)
    91.        
    92.         If Sample < -32767 Then Sample = -32767
    93.         If Sample > 32767 Then Sample = 32767
    94.        
    95.         IntBuff(K) = Sample
    96.     Next K
    97. End Sub
    98.  
    99. Private Sub AddEcho(IntBuff() As Integer, ByVal Delay As Long, Optional Startpos As Long = 0, Optional ByVal Strength As Single = 1)
    100.     Dim K As Long, NewSample As Long
    101.    
    102.     For K = Startpos + Delay To UBound(IntBuff)
    103.         NewSample = Fix((IntBuff(K - Delay) / 2 * Strength) + IntBuff(K) / 2)
    104.        
    105.         If NewSample < -32767 Then NewSample = -32767
    106.         If NewSample > 32767 Then NewSample = 32767
    107.        
    108.         IntBuff(K) = NewSample
    109.     Next K
    110. End Sub
    111.  
    112. Private Sub SaveWaveFile(ByVal WaveFileName As String, ByRef Buffer() As Integer, Optional SamplesPerSec As Long = 44100)
    113.     Dim WF As tWAVEFORMATEX
    114.    
    115.     WF.wFormatTag = 1 'WAVE_FORMAT_PCM
    116.     WF.nChannels = 1
    117.     WF.wBitsPerSample = 16
    118.     WF.nSamplesPerSec = SamplesPerSec
    119.    
    120.     WF.nBlockAlign = (WF.wBitsPerSample * WF.nChannels) \ 8
    121.     WF.nAvgBytesPerSec = WF.nSamplesPerSec * WF.nBlockAlign
    122.    
    123.     Open WaveFileName For Binary Access Write Lock Write As #1
    124.         WaveWriteHeader 1, WF
    125.         Put #1, , Buffer
    126.         WaveWriteHeaderEnd 1
    127.     Close #1
    128. End Sub
    129.  
    130. Private Sub WaveWriteHeader(ByVal OutFileNum As Integer, WaveFmt As tWAVEFORMATEX)
    131.     Dim header As FileHeader
    132.     Dim HdrFormat As WaveFormat
    133.     Dim chunk As ChunkHeader
    134.    
    135.     With header
    136.         .lRiff = &H46464952 ' "RIFF"
    137.         .lFileSize = 0
    138.         .lWave = &H45564157 ' "WAVE"
    139.         .lFormat = &H20746D66 ' "fmt "
    140.         .lFormatLength = Len(HdrFormat)
    141.     End With
    142.    
    143.     With HdrFormat
    144.         .wFormatTag = WaveFmt.wFormatTag
    145.         .nChannels = WaveFmt.nChannels
    146.         .nSamplesPerSec = WaveFmt.nSamplesPerSec
    147.         .nAvgBytesPerSec = WaveFmt.nAvgBytesPerSec
    148.         .nBlockAlign = WaveFmt.nBlockAlign
    149.         .wBitsPerSample = WaveFmt.wBitsPerSample
    150.     End With
    151.    
    152.     chunk.lType = &H61746164 ' "data"
    153.     chunk.lLen = 0
    154.    
    155.     Put #OutFileNum, 1, header
    156.     Put #OutFileNum, , HdrFormat
    157.     Put #OutFileNum, , chunk
    158. End Sub
    159.  
    160. Private Sub WaveWriteHeaderEnd(ByVal OutFileNum As Integer)
    161.     Dim header As FileHeader
    162.     Dim HdrFormat As WaveFormat
    163.     Dim chunk As ChunkHeader
    164.     Dim Lng As Long
    165.    
    166.     Lng = LOF(OutFileNum)
    167.     Put #OutFileNum, 5, Lng
    168.    
    169.     Lng = LOF(OutFileNum) - (Len(header) + Len(HdrFormat) + Len(chunk))
    170.     Put #OutFileNum, Len(header) + Len(HdrFormat) + 5, Lng
    171. End Sub

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Create a sound by code

    Thanks CVMichael, that will get me starting. I will look at the code after the weekend. (There is another reality besides this one ;-) )
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

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