Results 1 to 14 of 14

Thread: [RESOLVED] Change The Frequency Of The Sound

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Resolved [RESOLVED] Change The Frequency Of The Sound

    Dear Friend,
    I need your help to make small application that will change the frequency of the specific sound.This is used to create some musical program in which you can play different notes based on the fundamental frequency.when the frequency is changed the base note will be converted into another note and by this way you can play musical notes.
    I don't want to use the Beep function but i want to use the custom sound.
    Can anybody help me?

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Change The Frequency Of The Sound

    this code speedup, speeddown wav file, it's not my own code, i just got the speedup/down code from somewher in internet, then used it as i need, add 2 command button, change the wav file path as u need
    Code:
    Private Type RIFF
    riffId As String * 4
    riffLen As Long
    riffType As String * 4
    End Type
    
    Private Type RIFF_CHUNK
    chunkId As String * 4
    chunkLen 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 Declare Function sndPlaySound Lib "WINMM.DLL" Alias _
          "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As _
          Long) As Long
       Const SND_SYNC = &H0
       Const SND_ASYNC = &H1
       Const SND_NODEFAULT = &H2
       Const SND_LOOP = &H8
       Const SND_NOSTOP = &H10
       
    Dim WaveFile As String, WaveFast As String, WaveSlow As String
    
    Private Sub Command1_Click() 'fast
    FileCopy WaveFile, WaveFast
    soundname$ = WaveFast
    
    SpeedUp soundname$
    
       wFlags% = SND_ASYNC Or SND_NODEFAULT
       x% = sndPlaySound(soundname$, wFlags%)
    End Sub
    
    Private Sub Command2_Click() 'slow
    FileCopy WaveFile, WaveSlow
    soundname$ = WaveSlow
    
    SpeedDown soundname$
    
       wFlags% = SND_ASYNC Or SND_NODEFAULT
       x% = sndPlaySound(soundname$, wFlags%)
    
    End Sub
    
    Sub SpeedUp(WaveFile As String)
    Dim nFile As Integer
    nFile = FreeFile
    Dim r As RIFF, fmt As RIFF_CHUNK, wf As WAVEFORMAT
    Open WaveFile For Binary As #nFile
    Get #nFile, , r     'the 'RIFF' header
    Get #nFile, , fmt   'the 'fmt ' chunk
    Get #nFile, , wf    'the WAVEFORMAT header
    'increase the sample rate
    wf.nSamplesPerSec = wf.nSamplesPerSec * 2
    wf.nAvgBytesPerSec = wf.nAvgBytesPerSec * 2
    'write back the WAVEFORMAT structure to the file
    Put #nFile, Seek(nFile) - Len(wf), wf
    'close the file
    Close nFile
    End Sub
    
    Sub SpeedDown(WaveFile As String)
    Dim nFile As Integer
    nFile = FreeFile
    Dim r As RIFF, fmt As RIFF_CHUNK, wf As WAVEFORMAT
    Open WaveFile For Binary As #nFile
    Get #nFile, , r     'the 'RIFF' header
    Get #nFile, , fmt   'the 'fmt ' chunk
    Get #nFile, , wf    'the WAVEFORMAT header
    'decrease the sample rate
    wf.nSamplesPerSec = wf.nSamplesPerSec / 2
    wf.nAvgBytesPerSec = wf.nAvgBytesPerSec / 2
    'write back the WAVEFORMAT structure to the file
    Put #nFile, Seek(nFile) - Len(wf), wf
    'close the file
    Close nFile
    End Sub
     
    Private Sub Form_Load()
    WaveFile = App.Path & "\somewav.wav" 'original file
    WaveFast = App.Path & "\somewavfast.wav" 'to be modified as fast file
    WaveSlow = App.Path & "\somewavslow.wav" 'to be modified as slow file
    End Sub
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Re: Change The Frequency Of The Sound

    Will speed up and speed down change the frequency of the sound?

  4. #4
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Change The Frequency Of The Sound

    have a try atleast.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Re: Change The Frequency Of The Sound

    It Worked !!!!!
    Great Thanks for your Help. I just need your small help that i dont want to convert that in new file called as wavefast or wave slow but instead of that i want to play fast or slow only with that file. Is it possible?

  6. #6
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Change The Frequency Of The Sound

    then no need to use FileCopy, just change the line like this in both fast and slow
    Code:
    soundname$ = WaveFile
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Re: Change The Frequency Of The Sound

    I have Removed File Copy line and soundname$ = WaveFile is changed.
    However this creates the problem that original file changes as per the new frequency and sample rate so that when we press the button second time we get the doubled frequency file as original file and frequency becomes 4 times as we execute the command button.Is it possible that program only plays the sound and not changing the original file?
    I think i made mistake in writing in the previous post.

  8. #8
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Change The Frequency Of The Sound

    thats why we copy and change that, if u dont want to show the changed file change the location to any temp folder.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Re: Change The Frequency Of The Sound

    I got your point. We are creating a modified file and then we are playing it with the sndplaysound32, however can we put small code that will delete the changed file after we play it. In this way we will not be collecting all the modified files. Is it wavefast.delete?

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Re: Change The Frequency Of The Sound

    We have to first create the file from original wave file because every time you need fresh copy of the original file which needs to modified with the base frequency, then we have to modify that with the help of your code and then we need to delete it.

  11. #11
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Change The Frequency Of The Sound

    each time it wil not create the additional file, just overwrite it, if u dont want to keep that file finaly u can delete it.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Re: Change The Frequency Of The Sound

    Got That....

    Many many Thanks for your Help.This helped me a lot. I am marking this as resolved but my last question is that can we increase or decrease the duration of the sound playing? because when we increase the frequency the duration becomes half and when we decrease that the frequency duration becomes double, for a musical program, all note should play for equal duration

  13. #13
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: [RESOLVED] Change The Frequency Of The Sound

    sorry i dont hav much knwledge in that, may be any other person wil help u.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Re: [RESOLVED] Change The Frequency Of The Sound

    One more problem comes while running this code,

    When i put this code under For Loop , to play 5 or 6 notes in sequence, after playing first note error comes that permission denied, Run time error 70
    when i debuged that it took me to the file copy line. I believe that at second time the file wav cannot be copied in wavfast file as it is used by the previous process. I mean to say that For i=1 to 5, it plays for i=1, but it does not play for i=2 as the above error comes.

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