收藏 分销(赏)

vb简谱播放代码.pdf

上传人:曲**** 文档编号:230487 上传时间:2023-03-20 格式:PDF 页数:16 大小:360.10KB
下载 相关 举报
vb简谱播放代码.pdf_第1页
第1页 / 共16页
vb简谱播放代码.pdf_第2页
第2页 / 共16页
vb简谱播放代码.pdf_第3页
第3页 / 共16页
vb简谱播放代码.pdf_第4页
第4页 / 共16页
vb简谱播放代码.pdf_第5页
第5页 / 共16页
点击查看更多>>
资源描述

1、Option ExplicitConst INVALID NOTE=-1 Code for keyboard keys that we don,thandleDim numDevices As Long number of midi output devicesDim curDevice As Long current midi deviceDim hmidi As Long midi output handleDim rc As Long return codeDim midimsg As Long midi output message bufferDim channel As Integ

2、er midi output channelDim volume As Integer midi volumeDim baseNote As Integer the first note on our piano”Private Declare Sub Sleep Lib kernel32(ByVai dwMilliseconds As Long)Set the value for the starting note of the pianoPrivate Sub base_Click()Dim s As StringDim i As Integers=InputBox(Enter the n

3、ew base note for the keyboard(0-111)”,“Base note”,CStr(baseNote)If IsNumeric(s)Theni=CInt(s)If(i=0 And i 112)Then baseNote=iEnd IfEnd IfEnd SubSelect the midi output channelPrivate Sub chan_Click(Index As Integer)chan(channel).Checked=Falsechannel=Indexchan(channel).Checked=TrueEnd SubPrivate Sub Co

4、mmand1 Click()Dim s As StringDim i As Integers=Textl.TextFor i=1 To Len(s)Select Case Mid(s,i,1)Case LLabel2.Caption=ZCall StartNote(0)Sleep 300Call StopNote(0)c cCase SLabel2.Caption=SCall StartNote(1)Sleep 200Call StopNote(1)Case X”Label2.Caption=XCall StartNote(2)Sleep 300Call StopNote(2)Case DLa

5、bel2.Caption=DCall StartNote(3)Sleep 300Call StopNote(3)c 小Case CLabel2.Caption=CCall StartNote(4)Sleep 300 Call StopNote(4)Case VLabel2.Caption=VCall StartNote(5)Sleep 300Call StopNote(5)c 八Case GLabel2.Caption=GCall StartNote(6)Sleep 300Call StopNote(6)Case B”Labe12.Caption=BCall StartNote(7)Sleep

6、 300Call StopNote(7)Case H”Label2.Caption=HCall StartNote(8)Sleep 300Call StopNote(8)Case N”Label2.Caption=NCall StartNote(9)Sleep 300Call StopNote(9)Case J”Label2.Caption=JCall StartNote(10)Sleep 300Call StopNote(10)Case MLabel2.Caption=MCall StartNote(11)Sleep 300Call StopNote(11)CaseLabel2.Captio

7、n=铸?Call StartNote(12)Sleep 300Call StopNote(12)Case L”Label2.Caption=LCall StartNote(13)Sleep 300Call StopNote(13)Case.Label2.Caption=Call StartNote(14)Sleep 300Call StopNote(14)Case;Label2.Caption=铐?Call StartNote(15)Sleep 300Call StopNote(15)Case VLabel2.Caption=/Call StartNote(16)Sleep 300Call S

8、topNote(16)Case-Label2.Caption=一Call StopNote(0)Sleep 300Call StopNote(0)End SelectNextEnd Sub Open the midi device selected in the menu.The menu index equals the midi device number+1.Private Sub device_Click(Index As Integer)device(curDevice+1).Checked=False device(Index).Checked=True curDevice=Ind

9、ex-1 rc=midiOutClose(hmidi)rc=midiOutOpen(hmidi,curDevice,0,0,0)If(rc 0)ThenMsgBox“Couldnt open midi out,rc=&rc End If End SubIf user presses a keyboard key,start the corresponding midi note Private Sub FormKeyDown(KeyCode As Integer,Shift As Integer)StartNote NoteFromKey(KeyCode)End SubIf user lift

10、s a keyboard key,stop the corresponding midi note Private Sub Form_KeyUp(KeyCode As Integer,Shift As Integer)StopNote NoteFromKey(KeyCode)End SubPrivate Sub Form Load()Dim i As LongDim caps As MIDIOUTCAPSSet the first device as midi mapper device(0).Caption=MIDI Mapper”device(0).Visible=True device(

11、0).Enabled=True Get the rest of the midi devices numDevices=midiOutGetNumDevs()For i=0 To(numDevices-1)midiOutGetDevCaps i,caps,Len(caps)device(i+1).Caption=caps.szPname device(i+1).Visible=True device(i+1).Enabled=TrueNextSelect the MIDI Mapper as the default devicedevice_Click(0)Set the default ch

12、annel channel=0chan(channel).Checked=True Set the base note baseNote=60Set volume range volume=127 vol.Min=127 vol.Max=0 vol.Value=volumeEnd SubPrivate Sub Form_Unload(Cancel As Integer)Close current midi device rc=midiOutClose(hmidi)End SubStart a note when user click on itPrivate Sub key_MouseDown

13、(Index As Integer,Button As Integer,Shift As Integer,X As Single,Y As Single)StartNote(Index)End SubStop the note when user lifts the mouse buttonPrivate Sub key_MouseUp(Index As Integer,Button As Integer,Shift As Integer,X As Single,Y As Single)StopNote(Index)End SubPress the button and send midi s

14、tart event Private Sub StartNote(Index As Integer)If(Index=INVALID_NOTE)Then Exit SubEnd IfIf(key(Index).Value=1)Then Exit SubEnd Ifkey(Index).Value=1midimsg=&H90+(baseNote+Index)*&H100)+(volume*&H10000)+channel midiOutShortMsg hmidi,midimsg End SubRaise the button and send midi stop eventPrivate Su

15、b StopNote(Index As Integer)If(Index=INVALID_NOTE)Then Exit SubEnd Ifkey(Index).Value=0midimsg=&H80+(baseNote+Index)*&H100)+channel midiOutShortMsg hmidi,midimsgEnd SubGet the note corresponding to a keyboard keyPrivate Function NoteFromKey(key As Integer)NoteFromKey=INVALID_NOTE Select Case key Cas

16、e vbKeyZNoteFromKey=0Case vbKeySNoteFromKey=1Case vbKeyXNoteFromKey=2Case vbKeyDNoteFromKey=3Case vbKeyCNoteFromKey=4Case vbKeyVNoteFromKey=5Case vbKeyGNoteFromKey=6Case vbKeyBNoteFromKey=7Case vbKeyHNoteFromKey=8Case vbKeyNNoteFromKey=9Case vbKeyJNoteFromKey=10Case vbKeyMNoteFromKey=11 Case 188 com

17、maNoteFromKey=12Case vbKeyLNoteFromKey=13Case 190 periodNoteFromKey=14Case 186 semicolonNoteFromKey=15Case 191 forward slash NoteFromKey=16End SelectEnd FunctionSet the volumePrivate Sub vol_Change()volume=vol.ValueEnd SubPrivate Sub TimerITimer()Dim i As IntegerRandomizei=Rnd*12Select Case iCase 0L

18、abel2.Caption=ZCall StartNote(0)Sleep 500Call StopNote(0)Case 1Label2.Caption=SCall StartNote(1)Sleep 500Call StopNote(1)Case 2Label2.Caption=XCall StartNote(2)Sleep 500Call StopNote(2)Case 3Label2.Caption=DCall StartNote(3)Sleep 500Call StopNote(3)Case 4Label2.Caption=CCall StartNote(4)Sleep 500Cal

19、l StopNote(4)Case 5Label2.Caption=VCall StartNote(5)Sleep 500Call StopNote(5)Case 6Label2.Caption=BCall StartNote(6)Sleep 500Call StopNote(6)Case 7Label2.Caption=GCall StartNote(7)Sleep 500Call StopNote(7)Case 8Labe 12.Caption=Call StartNote(8)Sleep 500Call StopNote(8)Case 9Label2.Caption=NCall Star

20、tNote(9)Sleep 500Call StopNote(9)Case 10Label2.Caption=JCall StartNote(10)Sleep 500Call StopNote(10)Case 11Label2.Caption=MCall StartNote(11)Sleep 500Call StopNote(11)End SelectEnd SubThis is a complete piano application u can contact me at Option ExplicitPublic Const MAXPNAMELEN=32 Maximum product

21、name length Error values for functions used in this sample.See the function for more informationPublic Const MMSYSERR_BASE=0Public Const MMSYSERR_BADDEVICEID=(MMSYSERR_BASE+2)device IDout of rangePublic Const MMSYSERR_INVALPARAM=(MMSYSERR_BASE+11)invalidparameter passedPublic Const MMSYSERR_NODRIVER

22、=(MMSYSERR_BASE+6)no devicedriver presentPublic Const MMSYSERR_NOMEM=(MMSYSERR_BASE+7)memoryallocation errorPublic Const MMSYSERR_INVALHANDLE=(MMSYSERR_BASE+5)devicehandle is invalidPublic Const MIDIERR_BASE=64Public Const MIDIERR_STILLPLAYING=(MIDIERR_BASE+1)still something playingPublic Const MIDI

23、ERR_NOTREADY=(MIDIERR_BASE+3)hardwareis still busyPublic Const MIDIERR_BADOPENMODE=(MIDIERR_BASE+6)operationunsupported w/open mode 5 User-defined variable the stores information about the MIDI output device.Type MIDIOUTCAPSwMid As Integer Manufacturer identifier of thedevice driver for the MIDI out

24、put device For a list of identifiers,seethe Manufacturer Indentifier topic in theMultimedia Reference of thePlatform SDK.wPid As Integer Product Identifier Product ofthe MIDI output device.For a list of product identifiers,see the Product Identifiers topic in the MultimediaReference of the Platform

25、SDK.vDriverVersion As Long Version number of the devicedriver for the MIDI output device.The high-order byte is the major version number,and the low-order byte isthe minor version number.szPname As String*MAXPNAMELEN Product name in a null-terminated string.wTechnology As Integer One of the followin

26、g thatdescribes the MIDI output device:MOD_FMSYNTH-The device is anFM synthesizer.MOD_MAPPER-The device is theMicrosoft MIDI mapper.MOD_MIDIPORT-The device is a MIDI hardware port.MOD_SQSYNTH-The device is asquare wave synthesizer.MOD_SYNTH-The device is a synthesizer.wVoices As Integer Number of vo

27、ices supported byan internal synthesizer device.If thedevice is a port,this member is not meaningful and is set to 0.wNotes As Integer Maximum number of simultaneousnotes that can be played by an internal synthesizer device.If the device is a port,this member is not meaningfuland is set to 0.wChanne

28、lMask As Integer Channels that an internalsynthesizer 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 OxFFFF.dwSupport As Long One of the following describesthe optional fu

29、nctionality supported bythe device:MIDICAPS_CACHE-Supportspatch caching.MIDICAPS_LRVOLUME-Supportsseparate left and right volume control.MIDICAPS_STREAM-Providesdirect support for the midiStreamOut function.MIDICAPS_VOLUME-Supportsvolume control.If a device supports volume changes,the MIDICAPS_VOLUM

30、E 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 theMIDICAPS_LRVOLUME flags will be set for this member.End TypeDeclare Function midiOutGetNumDevs Lib winmni()As IntegerThis function retrieves the num

31、ber of MIDI output devices present in the system.The function returns the number of MIDI output devices.A zero return value meansthere are no MIDI devices in the system.Declare Function midiOutGetDevCaps Lib winmm.dll Alias“midiOutGetDevCapsA”(ByVai uDevicelD As Long,IpCaps As MIDIOUTCAPS,ByVai uSiz

32、e As Long)As LongThis function queries a specified MIDI output device to determine its capabilities.The function requires the following parameters;uDevicelD-unsigned integer variable identifying of the MIDIoutput device.Thedevice identifier specified by this parametervaries from zero to one less tha

33、n the number of devices present.Thisparameter can also be a properly cast device handle.IpMidiOutCaps-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 Lenfunction wit

34、h the MIDIOUTCAPS variable as the argument to get this value.The function returns MMSYSERR_NOERROR if successful or one of thefollowing error values:MMSYSERR_BADDEVICEIDrange.MMSYSERR_INVALPARAM invalid.MMSYSERR_NODRIVER MMSYSERR_NOMEM string description.The specified device identifier is out ofThe

35、specified pointer or structure isThe driver is not installed.The system is unable to load mapperDeclare Function midiOutClose Lib“winmm.dll(ByVai hMidiOut As Long)As Long The function closes the specified MIDI output device.The function requires ahandle to the MIDI output device.If the function is s

36、uccessful,the handle is nolonger valid after the call to this function.A successful function call returnsMMSYSERR_NOERROR.A failure returns one of the following:MIDIERR_STILLPLAYING MMSYSERR_INVALHANDLE MMSYSERR_NOMEMBuffers are still in the queue.The specified device handle is invalid.The system is

37、 unable to load mapper string description.Declare Function midiOutOpen Lib winmm.dll”(IphMidiOut As Long,ByVai uDevicelD As Long,ByVai dwCallback As Long,ByVai dwlnstance As Long,ByVai dwFlags As Long)As Long The function opens a MIDI output device for playback.The function requires thefollowing par

38、ametersIphmo-is filled with adevice.The handleother MIDI output uDevicelD-is to be opened.dwCal1back-handle,a thread thread called during to the progress of set this value to 0.Address of an HMIDIOUT handle.This locationhandle identifying the opened MIDI outputis used to identify the device in calls

39、 tofunctions.Identifier of the MIDI output device thatAddress of a calIback function,an eventidentifier,or a handle of a window orMIDI playback to process messages related the playback.If no calIback is desired,dwCallbacklnstance-User instance data passed to the callback.Set this value to 0.dwFlags-

40、CalIback 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_N0DEVICE-No MIDI port was found.This error occursonly when the mapper is opened.MMSYSERR_ALLOCATED-The specified resource is already allocated.MMS

41、YSERR BADDEVICEID-The specified device identifier is out ofrange.MMSYSERR_INVALPARAM-The specified pointer or structure is invalid.MMSYSERR_NOMEM-The system is unable to allocate or lockmemory.Declare Function midiOutShortMsg Lib“winmm.dll”(ByVai hMidiOut As Long,ByVai dwMsg As Long)As LongThis func

42、tion sends a short MIDI message to the specified MIDI output device.The functionrequires 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 forhow 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.

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 应用文书 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服