DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: generating sound

  1. #1
    Domenico Guest

    generating sound


    I need to generate sound from a math equation of a wave. HELP ME PLEASE!

  2. #2
    Mike D Sutton Guest

    Re: generating sound

    > I need to generate sound from a math equation of a wave. HELP ME PLEASE!

    This was posted by someone a while ago, unfortunately I don't still have
    their name but hopefully the code should still be useful:

    First add a module to the project then paste this code into it:
    '***
    Public Declare Function waveOutOpen Lib "winmm.dll" _
    (ByRef lphWaveOut As Long, ByVal uDeviceID As Long, _
    ByRef lpFormat As WAVEFORMATEX, ByVal dwCallback As Long, _
    ByVal dwInstance As Long, ByVal dwFlags As Long) As Long

    Public Declare Function waveOutClose Lib "winmm.dll" _
    (ByVal hWaveOut As Long) As Long

    Public Declare Function waveOutPrepareHeader Lib "winmm.dll" _
    (ByVal hWaveOut As Long, ByRef lpWaveOutHdr As WAVEHDR, _
    ByVal uSize As Long) As Long

    Public Declare Function waveOutUnprepareHeader Lib "winmm.dll" _
    (ByVal hWaveOut As Long, ByRef lpWaveOutHdr As WAVEHDR, _
    ByVal uSize As Long) As Long

    Public Declare Function waveOutWrite Lib "winmm.dll" _
    (ByVal hWaveOut As Long, ByRef lpWaveOutHdr As WAVEHDR, _
    ByVal uSize As Long) As Long

    Public Type WAVEHDR
    lpData As Long
    dwBufferLength As Long
    dwBytesRecorded As Long
    dwUser As Long
    dwFlags As Long
    dwLoops As Long
    lpNext As Long
    Reserved As Long
    End Type

    Public Type WAVEFORMATEX
    wFormatTag As Integer
    nChannels As Integer
    nSamplesPerSec As Long
    nAvgBytesPerSec As Long
    nBlockAlign As Integer
    wBitsPerSample As Integer
    cbSize As Integer
    End Type

    Public Const WAVE_MAPPER = -1&
    Public Const WAVE_FORMAT_PCM = 1
    Public Const CALLBACK_FUNCTION = &H30000
    Public Const MM_WOM_DONE = &H3BD

    Public FinishedPlaying As Boolean

    Public Sub waveOutProc(ByVal hWaveOut As Long, ByVal uMsg As Long, _
    ByVal dwInstance As Long, ByVal dwParam1 As Long, ByVal dwParam2 As
    Long)
    FinishedPlaying = uMsg = MM_WOM_DONE
    End Sub
    '***

    Now on the form (Form1), add two text box's (Text1 & Text2), a command
    button (Command1) and a horizontal scrollbar (HScroll1) then paste this code
    in and run:
    '***
    Private Const PI As Double = 3.141592654

    Private Sub Form_Load()
    Text1.Text = 500
    Text2.Text = 5
    Command1.Caption = "Generate noise"
    HScroll1.Value = HScroll1.Max \ 2
    End Sub

    Private Sub Command1_Click()
    Dim wavFormat As WAVEFORMATEX
    Dim wavHead As WAVEHDR
    Dim wavBuffer() As Integer
    Dim hWaveOut As Long
    Dim i As Long
    Dim Frequency As Double
    Dim FreqConst As Double
    Dim BufferLen As Long
    Dim MultAmt As Single

    If ((Val(Text1.Text) > 0) Or (Val(Text2.Text) > 0)) Then
    BufferLen = 44100 * Val(Text2.Text) 'Create the buffer
    ReDim wavBuffer(0 To BufferLen - 1)

    'Setup the nessasary info for outputting CD quality sound:
    With wavFormat
    .wFormatTag = WAVE_FORMAT_PCM
    .nChannels = 2
    .wBitsPerSample = 16
    .nSamplesPerSec = 44100
    .nBlockAlign = 2
    .nAvgBytesPerSec = 176400
    End With

    With wavHead
    .lpData = VarPtr(wavBuffer(0))
    .dwBufferLength = LenB(wavBuffer(0)) * BufferLen
    End With

    FinishedPlaying = False
    Command1.Enabled = False
    MultAmt = HScroll1.Value / HScroll1.Max

    'Increasing frequency:
    ' For i = 0 To BufferLen - 1
    ' wavBuffer(i) = (32767 * Sin(i * Frequency)) * MultAmt
    ' Frequency = Frequency + 0.000001
    ' Next i

    'White noise:
    ' Randomize 'Create a random seed
    ' For i = 0 To BufferLen - 1
    ' wavBuffer(i) = (32767 * Rnd) * MultAmt
    ' Next i

    'Specific frequency:
    FreqConst = 44100 / (PI * 2) / Val(Text1.Text)
    For i = 0 To BufferLen - 1
    wavBuffer(i) = HScroll1.Value * Sin((i Mod 44100) / FreqConst)
    Next i

    waveOutOpen hWaveOut, WAVE_MAPPER, wavFormat, _
    AddressOf waveOutProc, 0, CALLBACK_FUNCTION
    waveOutPrepareHeader hWaveOut, wavHead, LenB(wavHead)
    waveOutWrite hWaveOut, wavHead, LenB(wavHead)

    Do While (Not FinishedPlaying)
    DoEvents
    Loop

    Call waveOutUnprepareHeader(hWaveOut, wavHead, LenB(wavHead))
    Call waveOutClose(hWaveOut)

    Command1.Enabled = True
    End If
    End Sub
    '***

    There are three different types of waves this generates, you'll probably be
    most interested in the increacing frequency one, but it also shows how to
    create noise and a fixed fequency tone (Note; I edited the original to make
    it a big more readable and allow setting the volume of the other waves)
    Text box 1 is the frequency of the fixed frequency wave only, text box 2 is
    the length of any of the three wave types and the scroll bar is the volume.
    Hope this helps,

    Mike


    -- EDais --

    - Microsoft Visual Basic MVP -
    WWW: Http://EDais.earlsoft.co.uk/
    Work E-Mail: EDais@btclick.com
    Other E-Mail: Mike.Sutton@btclick.com



  3. #3
    Mike D Sutton Guest

    Re: generating sound

    > Thank you for your posts... but... Can I also add more waves together in
    order
    > to change the timbre?


    Whatever is in the buffer will get players, just generate the sound you want
    in the buffer and use this code to play it. To combine existing WAV files
    then you'll have to do some research and experimentation I'm afraid, it's a
    complicated process and something I've had little experience with..
    Hope this helps,

    Mike


    -- EDais --

    - Microsoft Visual Basic MVP -
    WWW: Http://EDais.earlsoft.co.uk/
    Work E-Mail: EDais@btclick.com
    Other E-Mail: Mike.Sutton@btclick.com



  4. #4
    Domenico Guest

    Re: generating sound


    Thank you for your posts... but... Can I also add more waves together in order
    to change the timbre?

    "Mike D Sutton" <Mike.Sutton@btclick.com> wrote:
    >> I need to generate sound from a math equation of a wave. HELP ME PLEASE!

    >
    >This was posted by someone a while ago, unfortunately I don't still have
    >their name but hopefully the code should still be useful:
    >
    >First add a module to the project then paste this code into it:
    >'***
    >Public Declare Function waveOutOpen Lib "winmm.dll" _
    > (ByRef lphWaveOut As Long, ByVal uDeviceID As Long, _
    > ByRef lpFormat As WAVEFORMATEX, ByVal dwCallback As Long, _
    > ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
    >
    >Public Declare Function waveOutClose Lib "winmm.dll" _
    > (ByVal hWaveOut As Long) As Long
    >
    >Public Declare Function waveOutPrepareHeader Lib "winmm.dll" _
    > (ByVal hWaveOut As Long, ByRef lpWaveOutHdr As WAVEHDR, _
    > ByVal uSize As Long) As Long
    >
    >Public Declare Function waveOutUnprepareHeader Lib "winmm.dll" _
    > (ByVal hWaveOut As Long, ByRef lpWaveOutHdr As WAVEHDR, _
    > ByVal uSize As Long) As Long
    >
    >Public Declare Function waveOutWrite Lib "winmm.dll" _
    > (ByVal hWaveOut As Long, ByRef lpWaveOutHdr As WAVEHDR, _
    > ByVal uSize As Long) As Long
    >
    >Public Type WAVEHDR
    > lpData As Long
    > dwBufferLength As Long
    > dwBytesRecorded As Long
    > dwUser As Long
    > dwFlags As Long
    > dwLoops As Long
    > lpNext As Long
    > Reserved As Long
    >End Type
    >
    >Public Type WAVEFORMATEX
    > wFormatTag As Integer
    > nChannels As Integer
    > nSamplesPerSec As Long
    > nAvgBytesPerSec As Long
    > nBlockAlign As Integer
    > wBitsPerSample As Integer
    > cbSize As Integer
    >End Type
    >
    >Public Const WAVE_MAPPER = -1&
    >Public Const WAVE_FORMAT_PCM = 1
    >Public Const CALLBACK_FUNCTION = &H30000
    >Public Const MM_WOM_DONE = &H3BD
    >
    >Public FinishedPlaying As Boolean
    >
    >Public Sub waveOutProc(ByVal hWaveOut As Long, ByVal uMsg As Long, _
    > ByVal dwInstance As Long, ByVal dwParam1 As Long, ByVal dwParam2 As
    >Long)
    > FinishedPlaying = uMsg = MM_WOM_DONE
    >End Sub
    >'***
    >
    >Now on the form (Form1), add two text box's (Text1 & Text2), a command
    >button (Command1) and a horizontal scrollbar (HScroll1) then paste this

    code
    >in and run:
    >'***
    >Private Const PI As Double = 3.141592654
    >
    >Private Sub Form_Load()
    > Text1.Text = 500
    > Text2.Text = 5
    > Command1.Caption = "Generate noise"
    > HScroll1.Value = HScroll1.Max \ 2
    >End Sub
    >
    >Private Sub Command1_Click()
    > Dim wavFormat As WAVEFORMATEX
    > Dim wavHead As WAVEHDR
    > Dim wavBuffer() As Integer
    > Dim hWaveOut As Long
    > Dim i As Long
    > Dim Frequency As Double
    > Dim FreqConst As Double
    > Dim BufferLen As Long
    > Dim MultAmt As Single
    >
    > If ((Val(Text1.Text) > 0) Or (Val(Text2.Text) > 0)) Then
    > BufferLen = 44100 * Val(Text2.Text) 'Create the buffer
    > ReDim wavBuffer(0 To BufferLen - 1)
    >
    > 'Setup the nessasary info for outputting CD quality sound:
    > With wavFormat
    > .wFormatTag = WAVE_FORMAT_PCM
    > .nChannels = 2
    > .wBitsPerSample = 16
    > .nSamplesPerSec = 44100
    > .nBlockAlign = 2
    > .nAvgBytesPerSec = 176400
    > End With
    >
    > With wavHead
    > .lpData = VarPtr(wavBuffer(0))
    > .dwBufferLength = LenB(wavBuffer(0)) * BufferLen
    > End With
    >
    > FinishedPlaying = False
    > Command1.Enabled = False
    > MultAmt = HScroll1.Value / HScroll1.Max
    >
    > 'Increasing frequency:
    >' For i = 0 To BufferLen - 1
    >' wavBuffer(i) = (32767 * Sin(i * Frequency)) * MultAmt
    >' Frequency = Frequency + 0.000001
    >' Next i
    >
    > 'White noise:
    >' Randomize 'Create a random seed
    >' For i = 0 To BufferLen - 1
    >' wavBuffer(i) = (32767 * Rnd) * MultAmt
    >' Next i
    >
    > 'Specific frequency:
    > FreqConst = 44100 / (PI * 2) / Val(Text1.Text)
    > For i = 0 To BufferLen - 1
    > wavBuffer(i) = HScroll1.Value * Sin((i Mod 44100) / FreqConst)
    > Next i
    >
    > waveOutOpen hWaveOut, WAVE_MAPPER, wavFormat, _
    > AddressOf waveOutProc, 0, CALLBACK_FUNCTION
    > waveOutPrepareHeader hWaveOut, wavHead, LenB(wavHead)
    > waveOutWrite hWaveOut, wavHead, LenB(wavHead)
    >
    > Do While (Not FinishedPlaying)
    > DoEvents
    > Loop
    >
    > Call waveOutUnprepareHeader(hWaveOut, wavHead, LenB(wavHead))
    > Call waveOutClose(hWaveOut)
    >
    > Command1.Enabled = True
    > End If
    >End Sub
    >'***
    >
    >There are three different types of waves this generates, you'll probably

    be
    >most interested in the increacing frequency one, but it also shows how to
    >create noise and a fixed fequency tone (Note; I edited the original to make
    >it a big more readable and allow setting the volume of the other waves)
    >Text box 1 is the frequency of the fixed frequency wave only, text box 2

    is
    >the length of any of the three wave types and the scroll bar is the volume.
    >Hope this helps,
    >
    > Mike
    >
    >
    > -- EDais --
    >
    > - Microsoft Visual Basic MVP -
    >WWW: Http://EDais.earlsoft.co.uk/
    >Work E-Mail: EDais@btclick.com
    >Other E-Mail: Mike.Sutton@btclick.com
    >
    >



  5. #5
    Ray Mercer Guest

    Re: generating sound


    "Domenico" <cryptorchid@freemail.it> wrote in message
    news:3cd2bf51$1@10.1.10.29...
    >
    > Thank you for your posts... but... Can I also add more waves together in

    order
    > to change the timbre?


    Yes. In order to combine wave audio you just sum the two and clip the
    result if you go over the max for the format you are using. In other words,
    to combine two wave samples you just add them together.

    HTH,
    Ray Mercer



  6. #6
    Domenico Guest

    Re: generating sound


    Ok, but I'm not very good with VB, and I don't know where to start.... I have
    a Y = a cos (w + g) and with your saples we can edit the "a" term (volume)
    and the "w" term (depends on the frequence)... in order to change the timbre
    we have to add another Y = a cos (w + g) - the "g" term is where the wave
    begin, in fact we can have two waves with the same equation and mix them
    together... the risult will depend on the "g" -. Just tell me where should
    I start from...
    PS excuse me for my very poor english!


    "Ray Mercer" <raymer@mvps.org> wrote:
    >
    >"Domenico" <cryptorchid@freemail.it> wrote in message
    >news:3cd2bf51$1@10.1.10.29...
    >>
    >> Thank you for your posts... but... Can I also add more waves together

    in
    >order
    >> to change the timbre?

    >
    >Yes. In order to combine wave audio you just sum the two and clip the
    >result if you go over the max for the format you are using. In other words,
    >to combine two wave samples you just add them together.
    >
    >HTH,
    >Ray Mercer
    >
    >



  7. #7
    Mike D Sutton Guest

    Re: generating sound

    > Ok, but I'm not very good with VB, and I don't know where to start.... I
    have
    > a Y = a cos (w + g) and with your saples we can edit the "a" term (volume)
    > and the "w" term (depends on the frequence)... in order to change the

    timbre
    > we have to add another Y = a cos (w + g) - the "g" term is where the wave
    > begin, in fact we can have two waves with the same equation and mix them
    > together... the risult will depend on the "g" -. Just tell me where should
    > I start from...


    Use Linear interpolation:

    '***
    Private Function LinearI(inA As Integer, _
    inB As Integer, inPos As Byte) As Integer
    LinearI = ((inB - inA) * (inPos / 255)) + inA
    End Function
    '***

    Now to combine the two samples, just use:

    New = LinearI(A, B, 128)

    Where A and B are the source values. The good thing about this method is
    you can mix the two values at any amount, for instance, if you wanted to
    bias the 'A' sample then you'd use something like this instead:

    New = LinearI(A, B, 25)

    Which would result in a value ~90% of A but only ~10% of B.
    The inPos parameter of the LinearI() function determines the mix amount, the
    nearer it is to 0 the more the final sample will resemble A, where as the
    nearer it is to 255 the more the final sample will resemble B. You could
    even achieve cross-fades by simply incrementing the position for each
    sample.
    Hope this helps,

    Mike


    -- EDais --

    - Microsoft Visual Basic MVP -
    WWW: Http://EDais.earlsoft.co.uk/
    Work E-Mail: EDais@btclick.com
    Other E-Mail: Mike.Sutton@btclick.com



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center