如何在VB.NET中为winmm.dll使用不同的乐器和音量 [英] How do I use different instruments and volume for winmm.dll in VB.NET
本文介绍了如何在VB.NET中为winmm.dll使用不同的乐器和音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在vb.net中编程从代码中播放音符。它使用winmm.dll,但我希望能够选择不同的乐器并改变音量。
这是vb.net代码:
I have programmed in vb.net to play musical notes from code. It uses winmm.dll but I want to be able to select different instruments and to change the volume.
Here is the vb.net code:
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports VB = Microsoft.VisualBasic
Public Class Form1
Protected Declare Auto Function midiOutOpen Lib "winmm.dll" (ByRef lphMidiOut As IntPtr, udeviceId As Integer, dwCallback As Integer, dwInstance As Integer, dwFlags As Integer) As Integer
Protected Declare Auto Function midiOutClose Lib "winmm.dll" (hMidiOut As IntPtr) As Integer
Protected Declare Auto Function midiOutShortMsg Lib "winmm.dll" (hMidiOut As IntPtr, dwMsg As Integer) As Integer
Protected Const MMSYSERR_NOERROR As Integer = 0
Public Const CALLBACK_FUNCTION As Integer = &H30000
Public Const NOTES_ON = &H90
Public Const MIDI_MAPPER = (-1)
Dim y As Integer, x As Integer
Dim highint As Integer, MidiMessage As Integer, lowint As Integer
Dim Note1Play(200) As Integer, Length1_Note(200) As Single
Dim Note2Play(200) As Integer, Length2_Note(200) As Single
Dim Note3Play(200) As Integer, Length3_Note(200) As Single
Dim Note4Play(200) As Integer, Length4_Note(200) As Single
Dim midiOpenError As Long
Dim midiMessageOut As Long
Dim midiData2 As Integer
Dim hMidiOut As IntPtr
Dim NUMBEROFNOTES As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Static start As Single
midiOpenError = midiOutOpen(hMidiOut, MIDI_MAPPER, 0, 0, 0)
midiMessageOut = NOTES_ON
midiData2 = 127
Happy_Birthday()
'Silent_Night
'Star_Wars
For y = 1 To NUMBEROFNOTES
lowint = (Note1Play(y) * 256) + &H90
PlayVoice1()
lowint = (Note2Play(y) * 256) + &H94
PlayVoice1()
lowint = (Note3Play(y) * 256) + &H96
PlayVoice1()
lowint = (Note4Play(y) * 256) + &H98
PlayVoice1()
start = VB.Timer()
Do While VB.Timer() < start + Length1_Note(y) - 0.2
System.Windows.Forms.Application.DoEvents()
Loop
Next y
Application.Exit()
End Sub
Private Sub PlayVoice1()
highint = (midiData2 * 256) * 256
MidiMessage = lowint + highint
x = midiOutShortMsg(hMidiOut, MidiMessage)
End Sub
Private Sub Happy_Birthday()
NUMBEROFNOTES = 26
'DDEDGF# DDEDAF# DDD^ BGFECCBGAG
'B4 = 71: A4s = 70: A4 = 69: G4s = 68: G4 = 67:
'F4s = 66: F4 = 65: E4 = 64: D4s = 63: D4 = 62
' C4s = 61: C4 = 60
'******************** Data for Happy Birthday VOICE ONE **************************************
For y = 1 To 12 'G G A G >C <B G G A G D C
Note1Play(y) = Choose(y, 55, 55, 57, 55, 60, 59, 55, 55, 57, 55, 62, 60)
Length1_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82)
Next y
For y = 13 To 24 'G G >G E C C B A F F E C
Note1Play(y) = Choose(y - 12, 55, 55, 67, 64, 60, 60, 59, 57, 65, 65, 64, 60)
Length1_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
Next y
For y = 25 To 26 ' D C
Note1Play(y) = Choose(y - 24, 62, 60)
Length1_Note(y) = Choose(y - 24, 0.91, 1.82)
Next y
'******************** Data for Happy Birthday VOICE Two **************************************
For y = 1 To 12 'F F E E E F F F F F B G
Note2Play(y) = Choose(y, 53, 53, 52, 52, 52, 53, 53, 53, 53, 53, 59, 55)
Length2_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82)
Next y
For y = 13 To 24 'E E >C C <G <G F F >C C C <G
Note2Play(y) = Choose(y - 12, 52, 52, 60, 60, 55, 55, 53, 53, 60, 60, 60, 55)
Length2_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
Next y
For y = 25 To 26 ' B G
Note2Play(y) = Choose(y - 24, 59, 55)
Length2_Note(y) = Choose(y - 24, 0.91, 1.82)
Next y
' '******************** Data for Happy Birthday VOICE Three ************************************
For y = 1 To 12 'B B >C C E D <B B B B F E
Note3Play(y) = Choose(y, 59, 59, 60, 60, 64, 62, 59, 59, 59, 59, 65, 64)
Length3_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82)
Next y
For y = 13 To 24 'C C E G E E C C A A G E
Note3Play(y) = Choose(y - 12, 60, 60, 64, 67, 64, 64, 60, 60, 69, 69, 67, 64)
Length3_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
Next y
For y = 25 To 26
Note3Play(y) = Choose(y - 24, 65, 64)
Length3_Note(y) = Choose(y - 24, 0.91, 1.82)
Next y
'******************** Data for Happy Birthday VOICE Four *************************************
For y = 1 To 12 'G G G G G G G G G G G C
Note4Play(y) = Choose(y, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 60)
Length4_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 0.182, 0.455, 0.455, 0.91, 0.91, 0.91, 0.182)
Next y
For y = 13 To 24 'G G G C C C F F F F G G
Note4Play(y) = Choose(y - 12, 55, 55, 55, 60, 60, 60, 53, 53, 53, 53, 55, 55)
Length4_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
Next y
For y = 25 To 26 ' G C
Note4Play(y) = Choose(y - 24, 55, 60)
Length4_Note(y) = Choose(y - 24, 0.91, 1.82)
Next y
End Sub
我尝试了什么:
我尝试使用其他程序员的代码来使用不同的乐器,但它们似乎从不工作所以我正在尝试这个网站。
What I have tried:
I tried using other programmer's code to use different instruments but they never seem to work so I am trying this site.
推荐答案
嗯... Windows MultiMedia中没有任何乐器。 MIDI适用于带有MIDI接口的REAL乐器。这适用于在真实乐器上播放MIDI文件。 (MIDI)乐器数字接口。
Windows和.NET Framework都没有任何可以提供各种乐器声音的东西。
您将不得不为此找到第三方库。
Ummm... Windows MultiMedia doesn't have any instruments in it. MIDI is for use with REAL instruments with MIDI interfaces in them. This is for stuff like playing MIDI files on real instruments. (MIDI) Musical Instrument Digital Interface.
Neither Windows, nor the .NET Framework has anything that will supply the sounds of various instruments.
You're going to have to find a 3rd party library for that.
我找到了答案,因此可以在vb6中使用许多不同的工具
I found the answer so it is possible to use many different instruments in vb6
Private Declare Function midiOutShortMsg Lib "winmm.dll" (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long
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
Private Declare Function midiOutClose Lib "winmm.dll" (ByVal hMidiOut As Long) As Long
Dim Note1Play(200) As Long, Length1_Note(200) As Single
Dim NUMBEROFNOTES As Integer
Dim ThisTime
Dim y As Integer
Dim handleMidiOut As Long
Dim ret As Integer
Dim Instrument As Integer
Private Sub Form_Load()
OpenMidiOut (-1) 'device id For midi mapper
Instrument = 14
ret = midiOutShortMsg(handleMidiOut, (256 * Instrument) + 192)
Happy_Birthday
For y = 1 To NUMBEROFNOTES
Call PlayNote(0, Note1Play(y), 127)
ThisTime = Timer
Do
Loop While ThisTime + Length2_Note(y) * 0.7 > Timer
Call StopNote(0, Note1Play(y), 127)
Next y
Call CloseMidiOut
End
End Sub
Private Function OpenMidiOut(ByVal dev_id As Integer) As Integer
Call CloseMidiOut
ret = midiOutOpen(handleMidiOut, dev_id, 0, 0, 0)
OpenMidiOut = (handleMidiOut <> 0)
End Function
Private Sub CloseMidiOut() ' close any opened midi device
If handleMidiOut <> 0 Then
ret = midiOutClose(handleMidiOut)
handleMidiOut = 0
End If
End Sub
Private Sub PlayNote(Ch As Integer, ByVal nn As Integer, vel As Integer)
Call ExecuteMidiOutShortMsg(&H90 + Ch, nn, vel)
End Sub
Private Sub StopNote(Ch As Integer, ByVal nn As Integer, vel As Integer)
Call ExecuteMidiOutShortMsg(&H80 + Ch, nn, vel)
End Sub
Private Sub ExecuteMidiOutShortMsg(b1 As Integer, b2 As Integer, b3 As Integer)
ret = midiOutShortMsg(handleMidiOut, PackDword(0, b3, b2, b1))
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call CloseMidiOut 'unloads the opened Midi Mapper
End Sub
Private Function PackDword(i1 As Integer, i2 As Integer, i3 As Integer, i4 As Integer) As Long
PackDword = i2 * &H10000 + i3 * &H100 + i4
End Function
Private Sub Happy_Birthday()
NUMBEROFNOTES = 26
'function "Choose" can only handle up to 12 variables at a time
For y = 1 To 12 'G G A G >C <B G G A G D C
Note1Play(y) = Choose(y, 55, 55, 57, 55, 60, 59, 55, 55, 57, 55, 62, 60)
Length1_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82)
Next y
For y = 13 To 24 'G G >G E C C B A F F E C
Note1Play(y) = Choose(y - 12, 55, 55, 67, 64, 60, 60, 59, 57, 65, 65, 64, 60)
Length1_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
Next y
For y = 25 To 26 ' D C
Note1Play(y) = Choose(y - 24, 62, 60)
Length1_Note(y) = Choose(y - 24, 0.91, 1.82)
Next y
End Sub
这篇关于如何在VB.NET中为winmm.dll使用不同的乐器和音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文