Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Custom GDI+ class
Splash Screen for the VFP application
Subclassing CommandButton control to create BackColor property
Obtaining names and positions for shortcuts located on the Windows Desktop
How to put a vertical text scrolling on the form (a movie cast)
How to make a VFP form fading out when released (GDI version)
Displaying dimmed window behind VFP top-level form
Enumerating devices installed on the local machine
Printing text with the Escape function
Moving shortcut to a specified position on the Windows Desktop
GDI+: Implementing image scrolling with inertia
System Image List Viewer
How to put a horizontal text scrolling on the form (a news line, marquee)
Storing content of the Clipboard to a bitmap file
Vertical Label control
How to play AVI file on the _screen
Mapping and disconnecting network drives
Creating a console window for Visual FoxPro application
Custom FTP Class for Visual FoxPro application
Testing Clipboard functions: emptying the clipboard
Detecting changes in connections to removable drives (VFP9)
Monitoring clipboard content changes (VFP9)
How to detect if additional monitor is connected and active
How to enable the SE_SHUTDOWN_NAME privilege for the application
How to play MIDI notes

User rating: 10/10 (1 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Before you begin:
Set the note in 0-127 range and the velocity (how hard the key is pressed) in 20-200+ range and press Enter. Also you can select a program -- an emultaion of a musical instrument the note to be played with.

The "Demo Sound" button shows how simple custom MIDI sounds can be used in VFP application.

See also:
  • Using Multimedia Command Strings to play MIDI files
  • How to play a waveform sound
  • Playing WAV sounds simultaneously
  • WAV file player
  • Controlling master audio volume by sending WM_APPCOMMAND messages
  • oForm = CREATEOBJECT("Tform")
    * end of main
    DEFINE CLASS Tform AS form
    PROTECTED midi
        Caption="Play MIDI Notes"
        ADD OBJECT Label1 AS Label WITH AutoSize=.T.,;
            BackStyle=0, Caption="Sound:", Height=17,;
            Left=14, Top=20, Width=31
        ADD OBJECT cmbProgram As ComboBox WITH Left=62,;
            Top=15, Width=120, Height=24, Style=2
        ADD OBJECT Label2 AS Label WITH AutoSize=.T.,;
            BackStyle=0, Caption="Note (0 to 127)", Height=17,;
            Left=14, Top=62, Width=31
        ADD OBJECT txtNote AS Spinner WITH Height=24,;
            KeyboardHighValue=127, KeyboardLowValue=0, Left=12,;
            SpinnerHighValue=127, SpinnerLowValue=0,;
            Top=86, Width=85, Value=48
        ADD OBJECT Label3 AS Label WITH AutoSize=.T.,;
            BackStyle=0, Caption="Velocity:", Height=17,;
            Left=120, Top=62, Width=46
        ADD OBJECT txtVelocity AS Spinner WITH Height=24,;
            KeyboardHighValue=200, KeyboardLowValue=20, Left=108,;
            SpinnerHighValue=200, SpinnerLowValue=20, Top=86,;
            Width=85, Value=100
        ADD OBJECT cmdPlay AS CommandButton WITH Top=86,;
            Left=200, Height=27, Width=48,;
            Caption="Play", Default=.T.
        ADD OBJECT cmdStop AS CommandButton WITH Top=86,;
            Left=250, Height=27, Width=48,;
            Caption="Stop", Default=.T.
        ADD OBJECT cmdDemo AS CommandButton WITH Top=140,;
            Left=12, Height=27, Width=100,;
            Caption="Demo Sound", Default=.T.
        THIS.midi = CREATEOBJECT("TMidiNote")
    PROCEDURE cmbProgram.Init
        LOCAL nIndex
        FOR nIndex=0 TO 127
            THIS.AddItem("Program " + LTRIM(STR(nIndex)))
    PROCEDURE cmbProgram.InteractiveChange
    PROCEDURE cmdDemo.Click
    PROCEDURE cmdPlay.Click
    PROCEDURE cmdStop.Click
    PROCEDURE PlayDemoSound
        THIS.midi.PlayNote(54, 100)
        THIS.midi.PlayNote(50, 100)
        THIS.midi.PlayNote(48, 100)
        THIS.midi.PlayNote(44, 100)
    PROCEDURE ChangePatch
        LOCAL nProgram
        nProgram = THIS.cmbProgram.ListIndex-1
    PROCEDURE StopNote
        LOCAL nNote
        nNote = THIS.txtNote.Value
    PROCEDURE PlayNote
        LOCAL nNote, nVelocity
        nNote = THIS.txtNote.Value
        nVelocity = THIS.txtVelocity.Value
        THIS.midi.PlayNote(nNote, nVelocity)
    DEFINE CLASS TMidiNote As Session
    PROTECTED hDevice, channel
    PROCEDURE Init(lDeclare)
        IF NOT THIS.OpenDevice()
            RETURN .F.
    PROCEDURE Destroy
    PROTECTED FUNCTION OpenDevice() As Boolean
        IF midiOutGetNumDevs() = 0
            MESSAGEBOX("No Midi Devices found.",;
                48, "MIDI Error")
            RETURN .F.
        LOCAL hDevice, nResult
        nResult = midiOutOpen(@hDevice, 0,0,0,0)
        THIS.hDevice = m.hDevice
        IF nResult <> 0
            MESSAGEBOX("Call to midiOutOpen failed: " +;
                TRANSFORM(nResult), 48, "MIDI Error")
    RETURN (m.nResult=0)
        IF THIS.hDevice <> 0
            = midiOutReset(THIS.hDevice)
            = midiOutClose(THIS.hDevice)
            THIS.hDevice = 0
    PROCEDURE SendMIDICommand(b1, b2, b3, b4)
        IF THIS.hDevice <> 0
            LOCAL nMsg, nResult
            nMsg = + BITLSHIFT(m.b1,4) +;
                BITLSHIFT(m.b2,8) +;
                BITLSHIFT(m.b3,16) + BITLSHIFT(m.b4,24)
            nResult = midiOutShortMsg(THIS.hDevice, m.nMsg)
            IF nResult <> 0
                MESSAGEBOX("Call to midiOutShortMsg failed: " +;
                    TRANSFORM(nResult), 48, "MIDI Error")
    PROCEDURE PlayNote(nNote, nVelocity)
            nNote, nVelocity, 0)
    PROCEDURE StopNote(nNote)
            m.nNote, 0, 0)
    PROCEDURE ChangePatch(nPatchNo)
            nPatchNo, 0, 0)
    PROCEDURE declare
        DECLARE INTEGER midiOutGetNumDevs IN Winmm
        DECLARE INTEGER midiOutClose IN Winmm INTEGER hmo  
        DECLARE INTEGER midiOutReset IN Winmm INTEGER hmo  
        DECLARE Sleep IN kernel32 INTEGER dwMilliseconds
        DECLARE INTEGER midiOutOpen IN Winmm;
            INTEGER @lphmo, INTEGER uDeviceID, INTEGER dwCallback,;
            INTEGER dwCallbackInstance, INTEGER dwFlags
        DECLARE INTEGER midiOutShortMsg IN Winmm;
            INTEGER hmo, LONG dwMsg    

    User rating: 10/10 (1 votes)
    Rate this code sample:
    • ~
    4497 bytes  
    Created: 2008-04-09 19:38:20  
    Modified: 2013-11-09 11:37:35  
    Visits in 7 days: 97  
    Listed functions:
    Printer friendly API declarations
    My comment:
    This is a list of MIDI instruments (a.k.a. voices, timbres, programs and patches):
    0. AcousticGrandPiano
    1. BrightAcousticPiano
    2. ElectricGrandPiano
    3. HonkyTonkPiano
    4. ElectricPiano1
    5. ElectricPiano2
    6. Harpsichord
    7. Clavi
    8. Celesta
    9. Glockenspiel
    10. MusicBox
    11. Vibraphone
    12. Marimba
    13. Xylophone
    14. TubularBells
    15. Dulcimer
    16. DrawbarOrgan
    17. PercussiveOrgan
    18. RockOrgan
    19. ChurchOrgan
    20. ReedOrgan
    21. Accordion
    22. Harmonica
    23. TangoAccordion
    24. AcousticGuitarNylon
    25. AcousticGuitarSteel
    26. ElectricGuitarJazz
    27. ElectricGuitarClean
    28. ElectricGuitarMuted
    29. OverdrivenGuitar
    30. DistortionGuitar
    31. GuitarHarmonics
    32. AcousticBass
    33. ElectricBassFinger
    34. ElectricBassPick
    35. FretlessBass
    36. SlapBass1
    37. SlapBass2
    38. SynthBass1
    39. SynthBass2
    40. Violin
    41. Viola
    42. Cello
    43. Contrabass
    44. TremoloStrings
    45. PizzicatoStrings
    46. OrchestralHarp
    47. Timpani
    48. StringEnsemble1
    49. StringEnsemble2
    50. SynthStrings1
    51. SynthStrings2
    52. ChoirAahs
    53. VoiceOohs
    54. SynthVoice
    55. OrchestraHit
    56. Trumpet
    57. Trombone
    58. Tuba
    59. MutedTrumpet
    60. FrenchHorn
    61. BrassSection
    62. SynthBrass1
    63. SynthBrass2
    64. SopranoSax
    65. AltoSax
    66. TenorSax
    67. BaritoneSax
    68. Oboe
    69. EnglishHorn
    70. Bassoon
    71. Clarinet
    72. Piccolo
    73. Flute
    74. Recorder
    75. PanFlute
    76. BlownBottle
    77. Shakuhachi
    78. Whistle
    79. Ocarina
    80. Lead1Square
    81. Lead2Sawtooth
    82. Lead3Calliope
    83. Lead4Chiff
    84. Lead5Charang
    85. Lead6Voice
    86. Lead7Fifths
    87. Lead8BassPluslead
    88. Pad1NewAge
    89. Pad2Warm
    90. Pad3Polysynth
    91. Pad4Choir
    92. Pad5Bowed
    93. Pad6Metallic
    94. Pad7Halo
    95. Pad8Sweep
    96. FX1Rain
    97. FX2Soundtrack
    98. FX3Crystal
    99. FX4Atmosphere
    100. FX5Brightness
    101. FX6Goblins
    102. FX7Echoes
    103. FX8SciFi
    104. Sitar
    105. Banjo
    106. Shamisen
    107. Koto
    108. Kalimba
    109. Bagpipe
    110. Fiddle
    111. Shanai
    112. TinkleBell
    113. Agogo
    114. SteelDrums
    115. Woodblock
    116. TaikoDrum
    117. MelodicTom
    118. SynthDrum
    119. ReverseCymbal
    120. GuitarFretNoise
    121. BreathNoise
    122. Seashore
    123. BirdTweet
    124. TelephoneRing
    125. Helicopter
    126. Applause
    127. Gunshot
    Word Index links for this example:
    Translate this page:
      Spanish    Portuguese    German    French    Italian offers instant, free translations of text or web pages.
    User Contributed Notes:
    There are no notes on this subject.

    Copyright 2001-2018 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

    Privacy policy
    Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.6.39), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
    Last Topics Visited (
    24 sec.Function: 'FillConsoleOutputAttribute'
    Function group: 'Console'
    43 sec.Function: 'getservbyport'
    Function group: 'Windows Sockets 2 (Winsock)'
    1.1 min.Project
    1.4 min.
    1.78 min.All Functions
    2.08 min.Function: 'GetFileSize'
    Function group: 'File System'
    2.43 min.Gallery
    2.75 min.Function: 'FtpRenameFile'
    Function group: 'Internet Functions (WinInet)'
    3.13 min.Function: 'SetVolumeLabel'
    Function group: 'File System'
    3.88 min.All Functions
    Advertise here!