VB版本MIDI钢琴简谱播放器源代码QZQ-2024-8-30

Option Explicit

Const INVALID_NOTE = -1 ’ Code for keyboard keys that we don’t handle

Dim numDevices As Long ’ number of midi output devices
Dim curDevice As Long ’ current midi device
Dim hmidi As Long ’ midi output handle
Dim rc As Long ’ return code
Dim midimsg As Long ’ midi output message buffer
Dim channel As Integer ’ midi output channel
Dim volume As Integer ’ midi volume
Dim baseNote As Integer ’ the first note on our “piano”

Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

'This is a complete piano application u can contact me at haisrini@email.com

Private Const MAXPNAMELEN = 32 ’ Maximum product name length

’ Error values for functions used in this sample. See the function for more information
Private Const MMSYSERR_BASE = 0
Private Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2) ’ device ID out of range
Private Const MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11) ’ invalid parameter passed
Private Const MMSYSERR_NODRIVER = (MMSYSERR_BASE + 6) ’ no device driver present
Private Const MMSYSERR_NOMEM = (MMSYSERR_BASE + 7) ’ memory allocation error

Private Const MMSYSERR_INVALHANDLE = (MMSYSERR_BASE + 5) ’ device handle is invalid
Private Const MIDIERR_BASE = 64
Private Const MIDIERR_STILLPLAYING = (MIDIERR_BASE + 1) ’ still something playing
Private Const MIDIERR_NOTREADY = (MIDIERR_BASE + 3) ’ hardware is still busy
Private Const MIDIERR_BADOPENMODE = (MIDIERR_BASE + 6) ’ operation unsupported w/ open mode

'User-defined variable the stores information about the MIDI output device.
Private Type MIDIOUTCAPS
wMid As Integer ’ Manufacturer identifier of the device driver for the MIDI output device
’ For a list of identifiers, see the Manufacturer Indentifier topic in the
’ Multimedia Reference of the Platform SDK.

wPid As Integer ’ Product Identifier Product of the MIDI output device. For a list of
’ product identifiers, see the Product Identifiers topic in the Multimedia
’ Reference of the Platform SDK.

vDriverVersion As Long ’ Version number of the device driver for the MIDI output device.
’ The high-order byte is the major version number, and the low-order byte is
’ the minor version number.

szPname As String * MAXPNAMELEN ’ Product name in a null-terminated string.

wTechnology As Integer ’ One of the following that describes the MIDI output device:
’ MOD_FMSYNTH-The device is an FM synthesizer.
’ MOD_MAPPER-The device is the Microsoft MIDI mapper.
’ MOD_MIDIPORT-The device is a MIDI hardware port.
’ MOD_SQSYNTH-The device is a square wave synthesizer.
’ MOD_SYNTH-The device is a synthesizer.

wVoices As Integer ’ Number of voices supported by an internal synthesizer device. If the
’ device is a port, this member is not meaningful and is set to 0.

wNotes As Integer ’ Maximum number of simultaneous notes that can be played by an internal
’ synthesizer device. If the device is a port, this member is not meaningful
’ and is set to 0.

wChannelMask As Integer ’ Channels that an internal synthesizer device responds to, where the least
’ significant bit refers to channel 0 and the most significant bit to channel
’ 15. Port devices that transmit on all channels set this member to 0xFFFF.

dwSupport As Long ’ One of the following describes the optional functionality supported by
’ the device:
’ MIDICAPS_CACHE-Supports patch caching.
’ MIDICAPS_LRVOLUME-Supports separate left and right volume control.
’ MIDICAPS_STREAM-Provides direct support for the midiStreamOut function.
’ MIDICAPS_VOLUME-Supports volume control.

’ If a device supports volume changes, the MIDICAPS_VOLUME flag will be set
’ for the dwSupport member. If a device supports separate volume changes on
’ the left and right channels, both the MIDICAPS_VOLUME and the
’ MIDICAPS_LRVOLUME flags will be set for this member.
End Type

Private Declare Function midiOutGetNumDevs Lib “winmm” () As Integer
’ This function retrieves the number of MIDI output devices present in the system.
’ The function returns the number of MIDI output devices. A zero return value means
’ there are no MIDI devices in the system.

Private Declare Function midiOutGetDevCaps Lib “winmm.dll” Alias “midiOutGetDevCapsA” (ByVal uDeviceID As Long, lpCaps As MIDIOUTCAPS, ByVal uSize As Long) As Long
’ This function queries a specified MIDI output device to determine its capabilities.
’ The function requires the following parameters;
’ uDeviceID- unsigned integer variable identifying of the MIDI output device. The
’ device identifier specified by this parameter varies from zero to one
’ less than the number of devices present. This parameter can also be a
’ properly cast device handle.
’ lpMidiOutCaps- address of a MIDIOUTCAPS structure. This structure is filled with
’ information about the capabilities of the device.
’ cbMidiOutCaps- the size, in bytes, of the MIDIOUTCAPS structure. Use the Len
’ function with the MIDIOUTCAPS variable as the argument to get
’ this value.

’ The function returns MMSYSERR_NOERROR if successful or one of the following error values:
’ MMSYSERR_BADDEVICEID The specified device identifier is out of range.
’ MMSYSERR_INVALPARAM The specified pointer or structure is invalid.
’ MMSYSERR_NODRIVER The driver is not installed.
’ MMSYSERR_NOMEM The system is unable to load mapper string description.

Private Declare Function midiOutClose Lib “winmm.dll” (ByVal hMidiOut As Long) As Long
’ The function closes the specified MIDI output device. The function requires a
’ handle to the MIDI output device. If the function is successful, the handle is no
’ longer valid after the call to this function. A successful function call returns
’ MMSYSERR_NOERROR.

’ A failure returns one of the following:
’ MIDIERR_STILLPLAYING Buffers are still in the queue.
’ MMSYSERR_INVALHANDLE The specified device handle is invalid.
’ MMSYSERR_NOMEM The system is unable to load mapper string description.

Private Declare Function midiOutOpen Lib “winmm.dll” (lphMidiOut As Long, ByVal uDeviceID As Long, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
’ The function opens a MIDI output device for playback. The function requires the
’ following parameters
’ lphmo- Address of an HMIDIOUT handle. This location is filled with a
’ handle identifying the opened MIDI output device. The handle
’ is used to identify the device in calls to other MIDI output
’ functions.
’ uDeviceID- Identifier of the MIDI output device that is to be opened.
’ dwCallback- Address of a callback function, an event handle, a thread
’ identifier, or a handle of a window or thread called during
’ MIDI playback to process messages related to the progress of
’ the playback. If no callback is desired, set this value to 0.
’ dwCallbackInstance- User instance data passed to the callback. Set this value to 0.
’ dwFlags-Callback flag for opening the device. Set this value to 0.

’ The function returns MMSYSERR_NOERROR if successful or one of the following error values:
’ MIDIERR_NODEVICE- No MIDI port was found. This error occurs only when the mapper is opened.
’ MMSYSERR_ALLOCATED- The specified resource is already allocated.
’ MMSYSERR_BADDEVICEID- The specified device identifier is out of range.
’ MMSYSERR_INVALPARAM- The specified pointer or structure is invalid.
’ MMSYSERR_NOMEM- The system is unable to allocate or lock memory.

Private Declare Function midiOutShortMsg Lib “winmm.dll” (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long
’ This function sends a short MIDI message to the specified MIDI output device. The function
’ requires the handle to the MIDI output device and a message is packed into a doubleword
’ value with the first byte of the message in the low-order byte. See the code sample for
’ how to create this value.

’ The function returns MMSYSERR_NOERROR if successful or one of the following error values:
’ MIDIERR_BADOPENMODE- The application sent a message without a status byte to a stream handle.
’ MIDIERR_NOTREADY- The hardware is busy with other data.
’ MMSYSERR_INVALHANDLE- The specified device handle is invalid.

Private mvarsType As Integer

Private Property Let sType(ByVal New_Data As Integer)
If Not IsNumeric(New_Data) Then New_Data = 0
If New_Data < 0 Then New_Data = 0
If New_Data > 127 Then New_Data = 127
mvarsType = New_Data
midiOutShortMsg hmidi, mvarsType * &H100 + &HC0
End Property

Private Property Get sType() As Integer
sType = mvarsType
End Property
’ Set the value for the starting note of the piano

Private Sub base_Click()
Dim s As String
Dim i As Integer
s = 60
If IsNumeric(s) Then
i = CInt(s)
If (i >= 0 And i < 112) Then
baseNote = i
End If
End If
End Sub

’ Select the midi output channel
Private Sub chan_Click(Index As Integer)
chan(channel).Checked = False
channel = Index
chan(channel).Checked = True
End Sub

Private Sub Command1_Click()
Dim s1 As String
Dim s2 As String
Dim i As Integer
Dim midiNote1 As Integer
Dim midiNote2 As Integer

Dim soundIndex1 As Integer
Dim soundIndex2 As Integer
soundIndex1 = Combo1.ListIndex
soundIndex2 = Combo2.ListIndex

s1 = Text1.Text
s2 = Text2.Text

For i = 1 To Len(s1) Or Len(s2) '取两个字符串长度中的较大值作为循环次数
Dim note1 As String
Dim note2 As String

  If i <= Len(s1) Then
     note1 = Mid(s1, i, 1)
  Else
     note1 = "" '如果第一个字符串结束了,设置为空字符串
  End If
  
  If i <= Len(s2) Then
     note2 = Mid(s2, i, 1)
  Else
     note2 = "" '如果第二个字符串结束了,设置为空字符串
  End If
  
   
    Select Case note1
          Case "-"
        midiNote1 = -1
 
     Case "1...."
        midiNote1 = 1
     Case "2...."
        midiNote1 = 2
     Case "3...."
        midiNote1 = 3
     Case "4...."
        midiNote1 = 4
     Case "5...."
        midiNote1 = 5
     Case "6...."
        midiNote1 = 6
     Case "7...."
        midiNote1 = 7
     Case "1..."
        midiNote1 = 8
     Case "2..."
        midiNote1 = 9
     Case "3..."
        midiNote1 = 10
     Case "4..."
        midiNote1 = 11

     Case "5..."
        midiNote1 = 12


     Case "6..."
        midiNote1 = 13

     Case "7..."
        midiNote1 = 14

    Case "1.."
        midiNote1 = 15


     Case "2.."
        midiNote1 = 16

     Case "3.."
        midiNote1 = 17

    Case "4.."
        midiNote1 = 18


     Case "5.."
        midiNote1 = 19

     Case "6.."
        midiNote1 = 20

 Case "7.."
        midiNote1 = 21


    Case "1."
        midiNote1 = 22


     Case "2."
        midiNote1 = 23

     Case "3."
        midiNote1 = 24

    Case "4."
        midiNote1 = 25


     Case "5."
        midiNote1 = 26

     Case "6."
        midiNote1 = 27

 Case "7."
        midiNote1 = 28




    Case "1"
        midiNote1 = 29


     Case "2"
        midiNote1 = 30

     Case "3"
        midiNote1 = 31

    Case "4"
        midiNote1 = 32


     Case "5"
        midiNote1 = 33

     Case "6"
        midiNote1 = 34

 Case "7"
        midiNote1 = 35




    Case "1'"
        midiNote1 = 36


     Case "2'"
        midiNote1 = 37

     Case "3'"
        midiNote1 = 38

    Case "4'"
        midiNote1 = 39


     Case "5'"
        midiNote1 = 40

     Case "6'"
        midiNote1 = 41

 Case "7'"
        midiNote1 = 42




    Case "1''"
        midiNote1 = 43


     Case "2''"
        midiNote1 = 44

     Case "3''"
        midiNote1 = 45

    Case "4''"
        midiNote1 = 46


     Case "5''"
        midiNote1 = 47

     Case "6''"
        midiNote1 = 48

 Case "7''"
        midiNote1 = 49


    Case "1'''"
        midiNote1 = 50


     Case "2'''"
        midiNote1 = 51

     Case "3'''"
        midiNote1 = 52

    Case "4'''"
        midiNote1 = 53


     Case "5'''"
        midiNote1 = 54

     Case "6'''"
        midiNote1 = 55

 Case "7'''"
        midiNote1 = 56


    Case "1''''"
        midiNote1 = 57


     Case "2''''"
        midiNote1 = 58

     Case "3''''"
        midiNote1 = 59

    Case "4''''"
        midiNote1 = 60


     Case "5''''"
        midiNote1 = 61

     Case "6''''"
        midiNote1 = 62

 Case "7''''"
        midiNote1 = 63
        
       End Select
       
       Select Case note2
            Case "-"
        midiNote2 = -1
     Case "1...."
        midiNote2 = 1
     Case "2...."
        midiNote2 = 2
     Case "3...."
        midiNote2 = 3
     Case "4...."
        midiNote2 = 4
     Case "5...."
        midiNote2 = 5
     Case "6...."
        midiNote2 = 6
     Case "7...."
        midiNote2 = 7
     Case "1..."
        midiNote2 = 8
     Case "2..."
        midiNote2 = 9
     Case "3..."
        midiNote2 = 10
     Case "4..."
        midiNote2 = 11

     Case "5..."
        midiNote2 = 12


     Case "6..."
        midiNote2 = 13

     Case "7..."
        midiNote2 = 14

    Case "1.."
        midiNote2 = 15


     Case "2.."
        midiNote2 = 16

     Case "3.."
        midiNote2 = 17

    Case "4.."
        midiNote2 = 18


     Case "5.."
        midiNote2 = 19

     Case "6.."
        midiNote2 = 20

 Case "7.."
        midiNote2 = 21


    Case "1."
        midiNote2 = 22


     Case "2."
        midiNote2 = 23

     Case "3."
        midiNote2 = 24

    Case "4."
        midiNote2 = 25


     Case "5."
        midiNote2 = 26

     Case "6."
        midiNote2 = 27

 Case "7."
        midiNote2 = 28




    Case "1"
        midiNote2 = 29


     Case "2"
        midiNote2 = 30

     Case "3"
        midiNote2 = 31

    Case "4"
        midiNote2 = 32


     Case "5"
        midiNote2 = 33

     Case "6"
        midiNote2 = 34

 Case "7"
        midiNote2 = 35




    Case "1'"
        midiNote2 = 36


     Case "2'"
        midiNote2 = 37

     Case "3'"
        midiNote2 = 38

    Case "4'"
        midiNote2 = 39


     Case "5'"
        midiNote2 = 40

     Case "6'"
        midiNote2 = 41

 Case "7'"
        midiNote2 = 42




    Case "1''"
        midiNote2 = 43


     Case "2''"
        midiNote2 = 44

     Case "3''"
        midiNote2 = 45

    Case "4''"
        midiNote2 = 46


     Case "5''"
        midiNote2 = 47

     Case "6''"
        midiNote2 = 48

 Case "7''"
        midiNote2 = 49


    Case "1'''"
        midiNote2 = 50


     Case "2'''"
        midiNote2 = 51

     Case "3'''"
        midiNote2 = 52

    Case "4'''"
        midiNote2 = 53


     Case "5'''"
        midiNote2 = 54

     Case "6'''"
        midiNote2 = 55

 Case "7'''"
        midiNote2 = 56


    Case "1''''"
        midiNote2 = 57


     Case "2''''"
        midiNote2 = 58

     Case "3''''"
        midiNote2 = 59

    Case "4''''"
        midiNote2 = 60


     Case "5''''"
        midiNote2 = 61

     Case "6''''"
        midiNote2 = 62

 Case "7''''"
        midiNote2 = 63



       End Select

本软件支持双音轨(2个文本框简谱)播放,支持自由选择音色。可以单音轨播放简谱也可以双音轨播放。就像歌曲的主旋律和伴奏同时演奏的效果。



简谱播放器

🎉精彩不容错过!新文章震撼登场,却因代码不全让人意犹未尽?别担心!我们为你准备了软件截图,让你先睹为快,感受其强大魅力。

✨如果你渴望解锁全部精彩内容,那么强烈建议你下载完整源代码。它将为你打开一扇通往编程新世界的大门,让你深入探索其中的奥秘,挖掘无限潜力。

🔗源代码下载网址:[https://download.csdn.net/download/qq_32257509/89695612]。赶紧行动起来,下载完整源代码,开启你的编程之旅吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易软科技(河源)有限公司

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值