Results 1 to 6 of 6

Thread: Play tone while key is held down?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Play tone while key is held down?

    I want to play a tone while the key is held down and immediately stop when key is up. I have messed with the Beep API and it plays it through the PC internal speaker. I need a tone to play through the soundcard speakers.

    The only way I know through the soundcard is a wav file but I need to adjust the length of how long is it played. It could be 1/2 a second, could be 1 full second for the tone to play.

    Any suggestions?

    Thanks!

  2. #2

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Re: Play tone while key is held down?

    Thanks. I tried it and didn't work like I needed. I'm wanting to produce tones for morse code. But I can't play a wav file cause you are creating how long each tone is. All I can do is use Beep api but thats not through the soundcard speaker.

    Anyone have any advice?

    Thanks!

  4. #4
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Play tone while key is held down?

    Perhaps these would help ?
    http://www.a1vbcode.com/app-2995.asp
    http://morsemadness.sourceforge.net/ - goto download link
    http://www.vbcodesource.com/exampleEx.html - there is tone generator at the bottom of the list. here is the download link - http://www.vbcodesource.org/download...eneratorEx.zip

    http://www.phon.ucl.ac.uk/resource/audio/vbasic.html



    EDIT: this is Java, but could give you a head start.
    http://morsecode.scphillips.com/
    IIF(Post.Rate > 0 , , )

  5. #5
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Play tone while key is held down?

    Why not try playing a dot when you press down the left mouse button and a dash when you press the right mouse button?

  6. #6
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: Play tone while key is held down?

    Here try this, it works for me. But you have to have a device isntalled the plays back .wav files. If not just convert your Beep.wav file to an MP3 and it'll work just fine.

    Put this into a module or declare them private and put them in your form.
    Code:
    Public Declare Function mciSendString Lib "winmm.dll" Alias _
        "mciSendStringA" (ByVal lpstrCommand As String, ByVal _
        lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
        hwndCallback As Long) As Long
    Public Declare Function mciGetErrorString Lib "winmm.dll" Alias _
        "mciGetErrorStringA" (ByVal dwError As Long, ByVal _
        lpstrBuffer As String, ByVal uLength As Long) As Long 'Get the error message of the mcidevice if any
    Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
        (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) _
        As Long
    
    Public Function Short_Name(Long_Path As String) As String
        'Returns short pathname of the passed lo
        '     ng pathname
        Dim Short_Path As String
        Dim PathLength As Long
        Short_Path = Space(250)
        PathLength = GetShortPathName(Long_Path, Short_Path, Len(Short_Path))
    
        If PathLength Then
            Short_Name = Left$(Short_Path, PathLength)
        End If
    End Function
    You use the Short_Name() function if the path to file is a long one.

    Now put this into your form and replace the Form1 with your forms name.

    Code:
    Option Explicit
        Dim boolPlaying As Boolean
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim ret As Long
    Dim buf As String
    Dim pos As Integer
    
        If boolPlaying = False Then
            If KeyCode = vbKeyUp Then
                mciSendString "stop Sound", 0&, 0, 0
                mciSendString "close Sound", 0&, 0, 0
                ret = mciSendString("open PathToBeepFile type waveaudio alias Sound", 0&, 0, 0)
    
                If ret <> 0 Then
                    buf = Space$(1024)
                    mciGetErrorString ret, buf, Len(buf)
                    pos = InStr(buf, Chr$(0))
                    buf = Left$(buf, pos - 1)
                    MsgBox buf
                    Exit Sub
                End If
    
                mciSendString "play Sound", 0&, 0, 0
                boolPlaying = True
            End If
        End If
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        If boolPlaying = True Then
            If KeyCode = vbKeyUp Then
                mciSendString "stop Sound", 0&, 0, 0
                mciSendString "close Sound", 0&, 0, 0
                boolPlaying = False
            End If
        End If
    End Sub
    Hope it works, also if you do convert your wave file into and MP3 file remove the "type waveaudio" from "ret = mciSendString("open PathToBeepFile type waveaudio alias Sound", 0&, 0, 0)".


    -Jaquio

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