首页 > 其他分享 >VB.NET钢琴MIDI简谱播放器代码QZQ2024-8-7

VB.NET钢琴MIDI简谱播放器代码QZQ2024-8-7

时间:2024-08-08 16:28:10浏览次数:17  
标签:Case VB Return MIDI ByVal Private End Integer QZQ2024

Imports System.Runtime.InteropServices

Public Class Form1
'义 Windows API 函数
<DllImport(“winmm.dll”)>
Private Shared Function midiOutGetNumDevs() As Integer
End Function

<DllImport("winmm.dll")>
Private Shared Function midiOutGetDevCaps(ByVal uDeviceID As Integer, ByRef lpCaps As MIDIOUTCAPS, ByVal uSize As Integer) As Integer
End Function

<DllImport("winmm.dll")>
Private Shared Function midiOutClose(ByVal hMidiOut As IntPtr) As Integer
End Function

<DllImport("winmm.dll")>
Private Shared Function midiOutOpen(ByRef lphMidiOut As IntPtr, ByVal uDeviceID As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal dwFlags As Integer) As Integer
End Function

<DllImport("winmm.dll")>
Private Shared Function midiOutShortMsg(ByVal hMidiOut As IntPtr, ByVal dwMsg As Integer) As Integer
End Function

'IDI 输出设备的结构体
Private Structure MIDIOUTCAPS
    Public wMid As Short
    Public wPid As Short
    Public vDriverVersion As Integer
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> Public szPname As String
    Public wTechnology As Short
    Public wVoices As Short
    Public wNotes As Short
    Public wChannelMask As Short
    Public dwSupport As Integer
End Structure

Private hmidi As IntPtr 'IDI 句柄
Private curDevice As Integer '前 MIDI 设备索引
Private channel As Integer = 0 'IDI 通道
Private volume As Integer = 127 '量
Private selectedPatch As Integer '择的音色编号

Private Const baseNote As Integer = 60 '础音符

Private isPlaying As Boolean = False '放状态标志
Private pausedAt As Integer '停时的位置
Private timer As New System.Windows.Forms.Timer() '于控制播放节奏的计时器
Private noteDurations As New List(Of Tuple(Of Integer, Integer)) '明在类级别,使整个类都能访问
Private firstPlay As Boolean = True

Private currentIndex As Integer = 0 '一个音轨的当前索引
Dim p As Integer = 200 '定义播放速度




'义乐器和对应的 MIDI 音色值的字典
Private instrumentMidiMap As New Dictionary(Of String, Integer) From {
    {"大钢琴(声学钢琴)", 0},
    {"明亮的钢琴", 1},
    {"电钢琴", 2},
    {"酒吧钢琴", 3},
    {"柔和的电钢琴", 4},
    {"加合唱效果的电钢琴", 5},
    {"羽管键琴(拨弦古钢琴)", 6},
    {"科拉维科特琴(击弦古钢琴)", 7},
    {"钢片琴", 8},
    {"钟琴", 9},
    {"八音盒", 10},
    {"颤音琴", 11},
    {"马林巴", 12},
    {"木琴", 13},
    {"管钟", 14},
    {"大扬琴", 15},
    {"击杆风琴", 16},
    {"打击式风琴", 17},
    {"摇滚风琴", 18},
    {"教堂风琴", 19},
    {"簧管风琴", 20},
    {"手风琴", 21},
    {"口琴", 22},
    {"探戈手风琴", 23},
    {"尼龙弦吉他", 24},
    {"钢弦吉他", 25},
    {"爵士电吉他", 26},
    {"清音电吉他", 27},
    {"闷音电吉他", 28},
    {"加驱动效果的电吉他", 29},
    {"加失真效果的电吉他", 30},
    {"吉他和音", 31},
    {"大贝司(声学贝司)", 32},
    {"电贝司(指弹)", 33},
    {"电贝司(拨片)", 34},
    {"无品贝司", 35},
    {"掌击贝司1", 36},
    {"掌击贝司2", 37},
    {"电子合成贝司1", 38},
    {"电子合成贝司2", 39},
    {"小提琴", 40},
    {"中提琴", 41},
    {"大提琴", 42},
    {"低音大提琴", 43},
    {"弦乐群颤音音色", 44},
    {"弦乐群拨弦音色", 45},
    {"竖琴", 46},
    {"定音鼓", 47},
    {"弦乐合奏音色1", 48},
    {"弦乐合奏音色2", 49},
    {"合成弦乐合奏音色1", 50},
    {"合成弦乐合奏音色2", 51},
    {"人声合唱啊", 52},
    {"人声嘟", 53},
    {"合成人声", 54},
    {"管弦乐敲击齐奏", 55},
    {"小号", 56},
    {"长号", 57},
    {"大号", 58},
    {"加弱音器小号", 59},
    {"法国号(圆号)", 60},
    {"铜管组(铜管乐器合奏音色)", 61},
    {"合成铜管音色1", 62},
    {"合成铜管音色2", 63},
    {"高音萨克斯风", 64},
    {"次中音萨克斯风", 65},
    {"中音萨克斯风", 66},
    {"低音萨克斯风", 67},
    {"双簧管", 68},
    {"英国管", 69},
    {"巴松(大管)", 70},
    {"单簧管(黑管)", 71},
    {"短笛", 72},
    {"长笛", 73},
    {"竖笛", 74},
    {"排箫", 75},
    {"吹瓶声", 76},
    {"日本尺八", 77},
    {"口哨声", 78},
    {"奥卡雷那", 79},
    {"合成主音1(方波)", 80},
    {"合成主音2(锯齿波)", 81},
    {"合成主音3", 82},
    {"合成主音4", 83},
    {"合成主音5", 84},
    {"合成主音6(人声)", 85},
    {"合成主音7(平行五度)", 86},
    {"合成主音8(贝司加主音)", 87},
    {"合成音色1(新世纪)", 88},
    {"合成音色2(温暖)", 89},
    {"合成音色3", 90},
    {"合成音色4(合唱)", 91},
    {"合成音色5", 92},
    {"合成音色6(金属声)", 93},
    {"合成音色7(光环)", 94},
    {"合成音色8", 95},
    {"合成效果1雨声", 96},
    {"合成效果2音轨", 97},
    {"合成效果3水晶", 98},
    {"合成效果4大气", 99},
    {"合成效果5明亮", 100},
    {"合成效果6鬼怪", 101},
    {"合成效果7回声", 102},
    {"合成效果8科幻", 103},
    {"西塔尔(印度)", 104},
    {"班卓琴(美洲)", 105},
    {"三昧线(日本)", 106},
    {"十三弦筝(日本)", 107},
    {"卡林巴", 108},
    {"风笛", 109},
    {"民族提琴", 110},
    {"唢呐", 111},
    {"叮当铃", 112},
    {"阿哥哥鼓", 113},
    {"钢鼓", 114},
    {"木鱼", 115},
    {"太鼓", 116},
    {"古高音鼓", 117},
    {"合成鼓", 118},
    {"铜钹", 119},
    {"磨弦", 120},
    {"呼吸声", 121},
    {"海浪声", 122},
    {"鸟鸣", 123},
    {"电话铃", 124},
    {"直升机", 125},
    {"鼓掌声", 126},
    {"枪声", 127}
}




Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    curDevice = 0 '为第一个设备
    OpenMidiDevice() '开 MIDI 设备

    '器名称添加到多选框
    For Each instrument In instrumentMidiMap.Keys
        CheckedListBox1.Items.Add(instrument)
        CheckedListBox2.Items.Add(instrument)
        CheckedListBox3.Items.Add(instrument)
    Next
    CheckedListBox1.SetSelected(0, True) '始选中第一项
    CheckedListBox2.SetSelected(96, True) '始选中第96项
    CheckedListBox3.SetSelected(114, True) '始选中第114项



    '始化计时器
    timer.Interval = p '00 毫秒的间隔,可根据需要调整
    AddHandler timer.Tick, AddressOf Timer_Tick

    TextBox1.Text = "1,1,5,5,6,6,5,-,-,-,4,4,3,3,2,2,1,-,-,-,-,5,5,4,4,3,3,2,-,-,-,-,-,5,5,4,4,3,3,2,-,-,-,-,-,1,1,5,5,6,6,5,-,-,-,-,4,4,3,3,2,2,1,-,-,-,-"
    TextBox2.Text = "-,-,-,-,-,1,2,3,-,-,-,-,-,3,3,2,2,2,2,-,-,-,-,-,-,5,5,5,5,4,4,-,-,-,-,-,-,-,5,5,4,4,4,4,-,-,-,-,-,-,-,1,2,3,3,2,1,-,-,-,-,-,-,1,1,2,2,2,1,-,-,-,-,-,-"
    TextBox3.Text = "-,-,-,-,-,1,2,3,-,-,-,-,-,3,3,2,2,2,2,-,-,-,-,-,-,5,5,5,5,4,4,-,-,-,-,-,-,-,5,5,4,4,4,4,-,-,-,-,-,-,-,1,2,3,3,2,1,-,-,-,-,-,-,1,1,2,2,2,1,-,-,-,-,-,-"


End Sub

'开 MIDI 设备的方法
Private Sub OpenMidiDevice()
    Dim numDevices As Integer = midiOutGetNumDevs() '取设备数量
    Dim caps As New MIDIOUTCAPS '备信息结构体

    '历设备获取信息(这里可以根据需要进行处理,如显示设备名称等)
    For i As Integer = 0 To numDevices - 1
        midiOutGetDevCaps(i, caps, Marshal.SizeOf(caps))
    Next

    Dim rc As Integer = midiOutOpen(hmidi, curDevice, 0, 0, 0) '开设备
    If rc <> 0 Then
        MessageBox.Show("无法打开 MIDI 输出设备!")
    End If
End Sub

Private Sub Button11_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button11.Click

    If TextBox1.Text.Trim() = "" Or TextBox2.Text.Trim() = "" Or TextBox3.Text.Trim() = "" Then
        MessageBox.Show("请在3个文本框中输入数字简谱!")
        Return
    End If


    If Not isPlaying Then
        noteDurations.Clear()


        ParseAndPlayDigitalScore(TextBox1.Text, noteDurations)


        isPlaying = True
        firstPlay = True ' 设置首次播放标志
        timer.Start()
    End If
End Sub

'停按钮点击事件
Private Sub Button12_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button12.Click
    If isPlaying Then
        isPlaying = False
        timer.Stop()
        pausedAt = timer.Tag '录暂停时的位置
    End If
End Sub

'止按钮点击事件
Private Sub Button14_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button14.Click
    isPlaying = False
    timer.Stop()
    pausedAt = 0 '置暂停位置
End Sub

'续按钮点击事件
Private Sub Button13_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button13.Click
    If Not isPlaying Then
        isPlaying = True
        timer.Start()
    End If
End Sub

' 解析并播放数字简谱
Private Sub ParseAndPlayDigitalScore(ByVal digitalScore As String, ByRef noteDurations As List(Of Tuple(Of Integer, Integer)))
    Dim scoreParts As String() = digitalScore.Split(",")

    noteDurations.Clear() ' 清空之前的内容

    For Each part In scoreParts
        Dim noteValue As Integer
        Dim duration As Integer = 1 ' 默认时长为 1
        ' 处理音符
        If part.Contains(".") Then
            noteValue = ConvertNoteValue(part.Replace(".", "")) - 12
        ElseIf part.Contains("'") Then
            noteValue = ConvertNoteValue(part.Replace("'", "")) + 12
        Else
            noteValue = ConvertNoteValue(part)
        End If

        ' 处理时长
        If part.Contains("-") Then
            Dim dashCount As Integer = part.Count(Function(c) c = "-")
            duration = dashCount + 1
        End If

        noteDurations.Add(Tuple.Create(noteValue, duration))
    Next
End Sub

' 转换简谱音符为 MIDI 音符值
Private Function ConvertNoteValue(ByVal noteStr As String) As Integer
    Select Case noteStr


        Case "1...."
            Return -28


        Case "2...."
            Return -27

        Case "3...."
            Return -26

        Case "4...."
            Return -25

        Case "5...."
            Return -24


        Case "6...."
            Return -23

        Case "7...."
            Return -22


        Case "1..."
            Return -21

        Case "2..."
            Return -20

        Case "3..."
            Return -19

        Case "4..."
            Return -18

        Case "5..."
            Return -17


        Case "6..."
            Return -16

        Case "7..."
            Return -15

        Case "1.."
            Return -14

        Case "2.."
            Return -13

        Case "3.."
            Return -12

        Case "4.."
            Return -11


        Case "5.."
            Return -10


        Case "6.."
            Return -9

        Case "7.."
            Return -8

        Case "1."
            Return -7

        Case "2."
            Return -6

        Case "3."
            Return -5


        Case "4."
            Return -4


        Case "5."
            Return -3


        Case "6."
            Return -2

        Case "7."
            Return -1


        Case "1"
            Return 0

        Case "2"
            Return 1

        Case "3"
            Return 2

        Case "4"
            Return 3

        Case "5"
            Return 4


        Case "6"
            Return 5

        Case "7"
            Return 6

        Case "1'"
            Return 7

        Case "2'"
            Return 8

        Case "3'"
            Return 9

        Case "4'"
            Return 10


        Case "5'"
            Return 11

        Case "6'"
            Return 12


        Case "7'"
            Return 13

        Case "1''"
            Return 14

        Case "2''"
            Return 15


        Case "3''"
            Return 16

        Case "4''"
            Return 17


        Case "5''"
            Return 18


        Case "6''"
            Return 19


        Case "7''"
            Return 20



        Case "1'''"
            Return 21

        Case "2'''"
            Return 22

        Case "3'''"
            Return 23

        Case "4'''"
            Return 24


        Case "5'''"
            Return 25


        Case "6'''"
            Return 26

        Case "7'''"
            Return 27



        Case "1''''"
            Return 28

        Case "2''''"
            Return 29

        Case "3''''"
            Return 30

        Case "4''''"
            Return 31


        Case "5''''"
            Return 32


        Case "6''''"
            Return 33

        Case "7''''"
            Return 34

        Case "-"
            Return 90

        Case Else
            MessageBox.Show("不支持的音符: " & noteStr)
            Return -90 ' 这里返回一个默认的错误值
    End Select
End Function





'时器的 Tick 事件,用于控制播放节奏
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    If firstPlay And (currentIndex >= noteDurations.Count) Then
        timer.Stop()
        isPlaying = False
        firstPlay = False ' 播放完成后重置标志
        Return
    End If

    If currentIndex < noteDurations.Count Then
        Dim noteValue1 As Integer = noteDurations(currentIndex).Item1




        selectedPatch = instrumentMidiMap(CheckedListBox1.SelectedItem.ToString)

        PlayNote1(noteValue1)
        PlayNote2(noteValue1)
        PlayNote3(noteValue1)


        ' 根据较长的时长暂停计时器
        Dim maxDuration As Integer = Math.Max(noteValue1, noteValue1)

        currentIndex += 1
    Else
        If currentIndex >= noteDurations.Count Then
            currentIndex = 0
        End If
        If currentIndex >= noteDurations.Count Then
            timer.Stop()
            isPlaying = False
        End If
    End If
End Sub


'放音符的方法
Private Sub PlayNote1(ByVal noteIndex As Integer)
    selectedPatch = instrumentMidiMap(CheckedListBox1.SelectedItem.ToString)

    ' 发送第一个音轨的音色选择和音符消息
    Dim patchChangeMessage1 As Integer = &HC0 Or (selectedPatch << 8)
    midiOutShortMsg(hmidi, patchChangeMessage1)

    Dim midiOutValue1 As Integer = &H90 Or ((baseNote + noteIndex) << 8) Or (volume << 16) Or (channel << 24)
    midiOutShortMsg(hmidi, midiOutValue1)



End Sub

'放音符的方法
Private Sub PlayNote2(ByVal noteIndex As Integer)
    selectedPatch = instrumentMidiMap(CheckedListBox2.SelectedItem.ToString)

    ' 发送第一个音轨的音色选择和音符消息
    Dim patchChangeMessage1 As Integer = &HC0 Or (selectedPatch << 8)
    midiOutShortMsg(hmidi, patchChangeMessage1)

    Dim midiOutValue1 As Integer = &H90 Or ((baseNote + noteIndex) << 8) Or (volume << 16) Or (channel << 24)
    midiOutShortMsg(hmidi, midiOutValue1)


End Sub

'放音符的方法
Private Sub PlayNote3(ByVal noteIndex As Integer)
    selectedPatch = instrumentMidiMap(CheckedListBox3.SelectedItem.ToString)

    ' 发送第一个音轨的音色选择和音符消息
    Dim patchChangeMessage1 As Integer = &HC0 Or (selectedPatch << 8)
    midiOutShortMsg(hmidi, patchChangeMessage1)

    Dim midiOutValue1 As Integer = &H90 Or ((baseNote + noteIndex) << 8) Or (volume << 16) Or (channel << 24)
    midiOutShortMsg(hmidi, midiOutValue1)


End Sub
'闭 MIDI 设备
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
    midiOutClose(hmidi)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    '获取文本框中的字符串
    Dim str As String = TextBox1.Text
    '遍历字符串中的每个字符
    For i As Integer = 0 To str.Length - 1
        '获取当前字符
        Dim ch As Char = str(i)
        '根据当前字符进行相应的操作
        Select Case ch

            Case "1...."
                PlayNote1(0)


                PlayNote1(1)
            Case "2...."

            Case "3...."
                PlayNote1(2)

            Case "4...."
                PlayNote1(3)

            Case "5...."
                PlayNote1(4)


            Case "6...."
                PlayNote1(5)

            Case "7...."
                PlayNote1(6)


            Case "1..."
                PlayNote1(7)

            Case "2..."
                PlayNote1(8)

            Case "3..."
                PlayNote1(9)

            Case "4..."
                PlayNote1(10)

            Case "5..."
                PlayNote1(11)


            Case "6..."
                PlayNote1(12)

            Case "7..."
                PlayNote1(13)

            Case "1.."
                PlayNote1(14)

            Case "2.."
                PlayNote1(15)

            Case "3.."
                PlayNote1(16)

            Case "4.."
                PlayNote1(17)


            Case "5.."
                PlayNote1(18)


            Case "6.."
                PlayNote1(19)

            Case "7.."
                PlayNote1(20)

            Case "1."
                PlayNote1(21)

            Case "2."
                PlayNote1(22)

            Case "3."
                PlayNote1(23)


            Case "4."
                PlayNote1(24)


            Case "5."
                PlayNote1(25)


            Case "6."
                PlayNote1(26)

            Case "7."
                PlayNote1(27)


            Case "1"
                PlayNote1(28)

            Case "2"
                PlayNote1(29)

            Case "3"
                PlayNote1(30)

            Case "4"
                PlayNote1(31)

            Case "5"
                PlayNote1(32)


            Case "6"
                PlayNote1(33)

            Case "7"
                PlayNote1(34)

            Case "1'"
                PlayNote1(35)

            Case "2'"
                PlayNote1(36)

            Case "3'"
                PlayNote1(37)

            Case "4'"
                PlayNote1(38)


            Case "5'"
                PlayNote1(39)

            Case "6'"
                PlayNote1(40)


            Case "7'"
                PlayNote1(41)

            Case "1''"
                PlayNote1(42)

            Case "2''"
                PlayNote1(43)


            Case "3''"
                PlayNote1(44)

            Case "4''"
                PlayNote1(45)


            Case "5''"
                PlayNote1(46)


            Case "6''"
                PlayNote1(47)


            Case "7''"
                PlayNote1(48)



            Case "1'''"
                PlayNote1(49)
            Case "2'''"
                PlayNote1(1)

            Case "3'''"
                PlayNote1(50)

            Case "4'''"
                PlayNote1(51)


            Case "5'''"
                PlayNote1(52)


            Case "6'''"
                PlayNote1(53)

            Case "7'''"
                PlayNote1(54)



            Case "1''''"
                PlayNote1(55)

            Case "2''''"
                PlayNote1(56)

            Case "3''''"
                PlayNote1(57)

            Case "4''''"
                PlayNote1(58)


            Case "5''''"
                PlayNote1(59)


            Case "6''''"
                PlayNote1(60)

            Case "7''''"
                PlayNote1(61)


                '根据需要添加更多的 Case 语句,每个 Case 语句对应不同的数字字符串和对应的 音频 文件
        End Select

        System.Threading.Thread.Sleep(p) '暂停 200 毫秒,以控制播放节奏
    Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    '获取文本框中的字符串
    Dim str As String = TextBox2.Text
    '遍历字符串中的每个字符
    For i As Integer = 0 To str.Length - 1
        '获取当前字符
        Dim ch As Char = str(i)
        '根据当前字符进行相应的操作
        Select Case ch

            Case "1...."
                PlayNote2(0)


            Case "2...."
                PlayNote2(1)

            Case "3...."
                PlayNote2(2)

            Case "4...."
                PlayNote2(3)

            Case "5...."
                PlayNote2(4)


            Case "6...."
                PlayNote2(5)

            Case "7...."
                PlayNote2(6)


            Case "1..."
                PlayNote2(7)

            Case "2..."
                PlayNote2(8)

            Case "3..."
                PlayNote2(9)

            Case "4..."
                PlayNote2(10)

            Case "5..."
                PlayNote2(11)


            Case "6..."
                PlayNote2(12)

            Case "7..."
                PlayNote2(13)

            Case "1.."
                PlayNote2(14)

            Case "2.."
                PlayNote2(15)

            Case "3.."
                PlayNote2(16)

            Case "4.."
                PlayNote2(17)


            Case "5.."
                PlayNote2(18)


            Case "6.."
                PlayNote2(19)

            Case "7.."
                PlayNote2(20)

            Case "1."
                PlayNote2(21)

            Case "2."
                PlayNote2(22)

            Case "3."
                PlayNote2(23)


            Case "4."
                PlayNote2(24)


            Case "5."
                PlayNote2(25)


            Case "6."
                PlayNote2(26)

            Case "7."
                PlayNote2(27)


            Case "1"
                PlayNote2(28)

            Case "2"
                PlayNote2(29)

            Case "3"
                PlayNote2(30)

            Case "4"
                PlayNote2(31)

            Case "5"
                PlayNote2(32)


            Case "6"
                PlayNote2(33)

            Case "7"
                PlayNote2(34)

            Case "1'"
                PlayNote2(35)

            Case "2'"
                PlayNote2(36)

            Case "3'"
                PlayNote2(37)

            Case "4'"
                PlayNote2(38)


            Case "5'"
                PlayNote2(39)

            Case "6'"
                PlayNote2(40)


            Case "7'"
                PlayNote2(41)

            Case "1''"
                PlayNote2(42)

            Case "2''"
                PlayNote2(43)


            Case "3''"
                PlayNote2(44)

            Case "4''"
                PlayNote2(45)


            Case "5''"
                PlayNote2(46)


            Case "6''"
                PlayNote2(47)


            Case "7''"
                PlayNote2(48)



            Case "1'''"
                PlayNote2(49)
            Case "2'''"
                PlayNote2(1)

            Case "3'''"
                PlayNote2(50)

            Case "4'''"
                PlayNote2(51)


            Case "5'''"
                PlayNote2(52)


            Case "6'''"
                PlayNote2(53)

            Case "7'''"
                PlayNote2(54)



            Case "1''''"
                PlayNote2(55)

            Case "2''''"
                PlayNote2(56)

            Case "3''''"
                PlayNote2(57)

            Case "4''''"
                PlayNote2(58)


            Case "5''''"
                PlayNote2(59)


            Case "6''''"
                PlayNote2(60)

            Case "7''''"
                PlayNote2(61)


                '根据需要添加更多的 Case 语句,每个 Case 语句对应不同的数字字符串和对应的 音频 文件
        End Select

        System.Threading.Thread.Sleep(p) '暂停 200 毫秒,以控制播放节奏
    Next
End Sub




Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

    '获取文本框中的字符串
    Dim str As String = TextBox3.Text
    '遍历字符串中的每个字符
    For i As Integer = 0 To str.Length - 1
        '获取当前字符
        Dim ch As Char = str(i)
        '根据当前字符进行相应的操作
        Select Case ch

            Case "1...."
                PlayNote3(0)


            Case "2...."
                PlayNote3(1)

            Case "3...."
                PlayNote3(2)

            Case "4...."
                PlayNote3(3)

            Case "5...."
                PlayNote3(4)


            Case "6...."
                PlayNote3(5)

            Case "7...."
                PlayNote3(6)


            Case "1..."
                PlayNote3(7)

            Case "2..."
                PlayNote3(8)

            Case "3..."
                PlayNote3(9)

            Case "4..."
                PlayNote3(10)

            Case "5..."
                PlayNote3(11)


            Case "6..."
                PlayNote3(12)

            Case "7..."
                PlayNote3(13)

            Case "1.."
                PlayNote3(14)

            Case "2.."
                PlayNote3(15)

            Case "3.."
                PlayNote3(16)

            Case "4.."
                PlayNote3(17)


            Case "5.."
                PlayNote3(18)


            Case "6.."
                PlayNote3(19)

            Case "7.."
                PlayNote3(20)

            Case "1."
                PlayNote3(21)

            Case "2."
                PlayNote3(22)

            Case "3."
                PlayNote3(23)


            Case "4."
                PlayNote3(24)


            Case "5."
                PlayNote3(25)


            Case "6."
                PlayNote3(26)

            Case "7."
                PlayNote3(27)


            Case "1"
                PlayNote3(28)

            Case "2"
                PlayNote3(29)

            Case "3"
                PlayNote3(30)

            Case "4"
                PlayNote3(31)

            Case "5"
                PlayNote3(32)


            Case "6"
                PlayNote3(33)

            Case "7"
                PlayNote3(34)

            Case "1'"
                PlayNote3(35)

            Case "2'"
                PlayNote3(36)

            Case "3'"
                PlayNote3(37)

            Case "4'"
                PlayNote3(38)


            Case "5'"
                PlayNote3(39)

            Case "6'"
                PlayNote3(40)


            Case "7'"
                PlayNote3(41)

            Case "1''"
                PlayNote3(42)

            Case "2''"
                PlayNote3(43)


            Case "3''"
                PlayNote3(44)

            Case "4''"
                PlayNote3(45)


            Case "5''"
                PlayNote3(46)


            Case "6''"
                PlayNote3(47)


            Case "7''"
                PlayNote3(48)



            Case "1'''"
                PlayNote3(49)
            Case "2'''"
                PlayNote3(1)

            Case "3'''"
                PlayNote3(50)

            Case "4'''"
                PlayNote3(51)


            Case "5'''"
                PlayNote3(52)


            Case "6'''"
                PlayNote3(53)

            Case "7'''"
                PlayNote3(54)



            Case "1''''"
                PlayNote3(55)

            Case "2''''"
                PlayNote3(56)

            Case "3''''"
                PlayNote3(57)

            Case "4''''"
                PlayNote3(58)


            Case "5''''"
                PlayNote3(59)


            Case "6''''"
                PlayNote3(60)

            Case "7''''"
                PlayNote3(61)


                '根据需要添加更多的 Case 语句,每个 Case 语句对应不同的数字字符串和对应的 音频 文件
        End Select

        System.Threading.Thread.Sleep(p) '暂停 200 毫秒,以控制播放节奏
    Next
End Sub


Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    midiOutClose(hmidi)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Call OpenMidiDevice()
End Sub


'随机谱曲
Private Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
    Randomize()


    '定义数组来存储数据

    Dim a(7) As String

    a = {"1", "2", "3", "4", "5", "6", "7"}


    TextBox1.Text = TextBox1.Text & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6)



End Sub
'随机谱曲
Private Sub Button6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button6.Click
    Randomize()


    '定义数组来存储数据

    Dim a(7) As String

    a = {"1", "2", "3", "4", "5", "6", "7"}



    TextBox2.Text = TextBox2.Text & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6)


End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    Randomize()


    '定义数组来存储数据

    Dim a(7) As String

    a = {"1", "2", "3", "4", "5", "6", "7"}



    TextBox3.Text = TextBox3.Text & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6) & "," & a(Rnd() * 6)


End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
End Sub

End Class

标签:Case,VB,Return,MIDI,ByVal,Private,End,Integer,QZQ2024
From: https://blog.csdn.net/qq_32257509/article/details/140986069

相关文章

  • vb学习
    在VisualBasic(VB),数据类型用于定义变量可以存储的数据种类。以下是一些常用的VB数据类型:数值类型:Byte:无符号8位整数(0到255)。Integer:有符号16位整数(-32,768到32,767)。UInteger:无符号16位整数。Long:有符号32位整数(-2,147,483,648到2,147,483,647)。ULong:无符号32位整......
  • 基于CAT计算出的VBM(四)指标与临床量表相关的原理与技术
    前言  前面学习了神经科学中常见的两步分析法:ttest2识别差异:双样本T检验用于在全脑范围内识别疾病组和对照组存在灰质体积差异的脑区团块,定义为ROI。corrROI和临床量表相关分析:进一步,针对疾病组的被试在差异脑区上的灰质体积和临床量表进行相关性分析,针对性的探索该ROI的......
  • LearnOpenGL 笔记 -- VAO & VBO
    1前言VAO和VBO属于我们学习opengl最先接触的几个概念,最开始学习的时候有可能无法直观的理解这个概念的作用和使用方法。笔者也是opengl新手,在此记录学习的相关笔记,便于之后进行查看。本文主要参考learnopengl教程以及opengl官网中的用法和解释,文中的代码实例使用opengl3.3,过......
  • 人生第一个VBA
    用途:整理excel格式和展示方式SubKeyWordHighlight()Dimstr()AsStringDimstrLenAsIntegerWithColumns("P").ColumnWidth=.ColumnWidth*8.WrapText=TrueEndWithForx=2ToRange("P2").End(xlDown).Rowstr=Split(Cells(x,15......
  • VB中的变量声明和初始化过程以及如何在Visual Basic中声明变量
    在VisualBasic(VB)中,变量是用来存储信息的容器。变量声明是创建变量的过程,它告诉VB你想要使用的变量的名称和类型。初始化是为变量赋初始值的过程。下面详细解释这两个过程:变量声明在VB中,你可以使用Dim关键字来声明变量。声明变量时,你需要指定变量的名称和类型。例如:vb......
  • VB中如何定义和使用数组
    在VisualBasic(VB)中,数组是一种数据结构,用于存储一系列相同类型的数据项。你可以定义数组的大小(即它可以存储多少个元素),并在程序中使用数组来存储和访问这些数据项。以下是在VB中定义和使用数组的基本步骤:定义数组使用Dim关键字:与声明单个变量一样,你使用Dim关键字来定义数组......
  • vb6.0版本鼠标点击移动操作程序代码
    'sleep函数PrivateDeclareSubSleepLib“kernel32”(ByValdwMillisecondsAsLong)'获取鼠标坐标PrivateTypePOINTAPIXAsLongYAsLongEndTypePrivateDeclareFunctionGetCursorPosLib“user32”(lpPointAsPOINTAPI)AsLong'移动鼠标PrivateDe......
  • VB中的数据类型(包括基本数据类型和复合数据类型)的定义
    在VB(VisualBasic)中,数据类型是程序设计的基础,它们定义了变量或常量可以存储的数据的种类和范围。数据类型可以分为基本数据类型和复合数据类型两大类。一、基本数据类型基本数据类型是VB中最基本的数据类型,用于存储单个值。VB中的基本数据类型主要包括以下几种:数据类型类型......
  • VB中的条件语句(如If...Then)是如何工作的
    VB(VisualBasic)中的条件语句,特别是If...Then语句,是控制程序流程的重要手段之一。这些语句允许程序根据特定条件执行不同的代码块。以下是If...Then语句在VB中工作的详细解释:基本结构If...Then语句的基本结构分为单行格式和多行(块状)格式。单行格式vb复制代码If条件Then......
  • 基于CAT的VBM和SBM计算学习笔记(二)感兴趣区(ROI)&全脑体积(TIV)
    前言 回顾一下上文:之前学习了用CAT计算VBM灰质体积的预处理过程,主要分为三步:Preprocessing:从使用DPABI生成T1图像再校准T1原点。Segement:CAT软件自带的自动化分割。Smooth:最后用Spm进行平滑操作。基于CAT的VBM和SBM计算学习笔记(一)VBMhttps://mp.csdn.net/mp_blog/creat......