Imports "DX7.BAS"
Dim synth As New DX7(1)
Rem @TTFORM BEGIN 1
Rem Layout generated by the form designer (Ctrl+D).
Rem @TTFORM W Panel 520 260 17 StartDemo EndDemo
Rem @TTFORM T 0 "Yamaha DX7 MIDI demo"
Rem @TTFORM C 3 heading 16 12 480 16 1 1 0 -
Rem @TTFORM T 1 "Channel 1-16 / Voice 1-32 / Enable actual MIDI output"
Rem @TTFORM C 4 channelBox 16 36 48 24 1 1 0 -
Rem @TTFORM T 2 "1"
Rem @TTFORM C 4 voiceBox 80 36 48 24 1 1 0 -
Rem @TTFORM T 3 "1"
Rem @TTFORM C 2 voiceButton 144 36 144 28 1 1 0 SelectVoice
Rem @TTFORM T 4 "Select voice"
Rem @TTFORM C 5 enableMidi 304 36 192 24 1 1 0 ToggleOutput
Rem @TTFORM T 5 "Send MIDI OUT"
Rem @TTFORM C 5 arturiaMode 16 76 480 24 1 1 1 ToggleMode
Rem @TTFORM T 6 "Arturia CC18: algorithm only (off = Yamaha SysEx)"
Rem @TTFORM C 4 algorithmBox 16 104 56 24 1 1 0 -
Rem @TTFORM T 7 "1"
Rem @TTFORM C 4 feedbackBox 88 104 56 24 1 1 0 -
Rem @TTFORM T 8 "0"
Rem @TTFORM C 4 operatorBox 160 104 56 24 1 1 0 -
Rem @TTFORM T 9 "1"
Rem @TTFORM C 4 levelBox 232 104 56 24 1 1 0 -
Rem @TTFORM T 10 "90"
Rem @TTFORM C 2 soundButton 304 104 192 28 1 1 0 ApplySound
Rem @TTFORM T 11 "Apply sound"
Rem @TTFORM C 2 noteOnButton 16 156 112 28 1 1 0 StartNote
Rem @TTFORM T 12 "Note 60 on"
Rem @TTFORM C 2 noteOffButton 144 156 112 28 1 1 0 StopNote
Rem @TTFORM T 13 "Note off"
Rem @TTFORM C 2 panicButton 272 156 112 28 1 1 0 PanicNotes
Rem @TTFORM T 14 "Panic"
Rem @TTFORM C 3 status 16 208 480 24 1 1 0 -
Rem @TTFORM T 15 "DRY RUN - MIDI OUT is off"
Rem @TTFORM C 2 closeButton 400 156 96 28 1 1 0 CloseDemo
Rem @TTFORM T 16 "Close"
Dim Panel As New Window("", 520, 260)
Panel.Text = "Yamaha DX7 MIDI demo"
Dim heading As New Label(Panel, "", 16, 12, 480, 16)
heading.Text = "Channel 1-16 / Voice 1-32 / Enable actual MIDI output"
Dim channelBox As New TextBox(Panel, "", 16, 36, 48, 24)
channelBox.Text = "1"
Dim voiceBox As New TextBox(Panel, "", 80, 36, 48, 24)
voiceBox.Text = "1"
Dim voiceButton As New Button(Panel, "", 144, 36, 144, 28)
voiceButton.Text = "Select voice"
AddHandler voiceButton.Click, AddressOf SelectVoice
Dim enableMidi As New CheckBox(Panel, "", 304, 36, 192, 24)
enableMidi.Text = "Send MIDI OUT"
AddHandler enableMidi.Click, AddressOf ToggleOutput
Dim arturiaMode As New CheckBox(Panel, "", 16, 76, 480, 24)
arturiaMode.Text = "Arturia CC18: algorithm only (off = Yamaha SysEx)"
arturiaMode.Checked = True
AddHandler arturiaMode.Click, AddressOf ToggleMode
Dim algorithmBox As New TextBox(Panel, "", 16, 104, 56, 24)
algorithmBox.Text = "1"
Dim feedbackBox As New TextBox(Panel, "", 88, 104, 56, 24)
feedbackBox.Text = "0"
Dim operatorBox As New TextBox(Panel, "", 160, 104, 56, 24)
operatorBox.Text = "1"
Dim levelBox As New TextBox(Panel, "", 232, 104, 56, 24)
levelBox.Text = "90"
Dim soundButton As New Button(Panel, "", 304, 104, 192, 28)
soundButton.Text = "Apply sound"
AddHandler soundButton.Click, AddressOf ApplySound
Dim noteOnButton As New Button(Panel, "", 16, 156, 112, 28)
noteOnButton.Text = "Note 60 on"
AddHandler noteOnButton.Click, AddressOf StartNote
Dim noteOffButton As New Button(Panel, "", 144, 156, 112, 28)
noteOffButton.Text = "Note off"
AddHandler noteOffButton.Click, AddressOf StopNote
Dim panicButton As New Button(Panel, "", 272, 156, 112, 28)
panicButton.Text = "Panic"
AddHandler panicButton.Click, AddressOf PanicNotes
Dim status As New Label(Panel, "", 16, 208, 480, 24)
status.Text = "DRY RUN - MIDI OUT is off"
Dim closeButton As New Button(Panel, "", 400, 156, 96, 28)
closeButton.Text = "Close"
AddHandler closeButton.Click, AddressOf CloseDemo
AddHandler Panel.Load, AddressOf StartDemo
AddHandler Panel.Closed, AddressOf EndDemo
Panel.Show()
Application.Run()
End
Rem @TTFORM END

Sub SelectVoice(sender As Button)
    Dim program As Integer = Midi.CheckRange(CInt(voiceBox.Text), 1, 32)
    synth.SetChannel(GetChannel())
    synth.Program(program)
    ShowLast()
End Sub

Sub ToggleOutput(sender As CheckBox)
    Midi.EnableOutput(sender.Checked)
    If sender.Checked Then
        status.Text = "MIDI OUT enabled - no connection detection"
    Else
        status.Text = "DRY RUN - MIDI OUT is off"
    End If
End Sub

Sub ApplySound(sender As Button)
    Dim algo As Integer = Midi.CheckRange(CInt(algorithmBox.Text), 1, 32)
    If arturiaMode.Checked Then
        Dim ccValue As Integer = ((algo - 1) * 127 + 15) \ 31
        synth.SetChannel(GetChannel())
        Midi.ControlChange(GetChannel(), 18, ccValue)
    Else
        Dim fb As Integer = Midi.CheckRange(CInt(feedbackBox.Text), 0, 7)
        Dim op As Integer = Midi.CheckRange(CInt(operatorBox.Text), 1, 6)
        Dim level As Integer = Midi.CheckRange(CInt(levelBox.Text), 0, 99)
        synth.SetChannel(GetChannel())
        synth.Algorithm(algo)
        synth.Feedback(fb)
        synth.OperatorLevel(op, level)
    End If
    ShowLast()
End Sub

Sub StartNote(sender As Button)
    synth.SetChannel(GetChannel())
    synth.NoteOn(60, 96)
    ShowLast()
End Sub

Sub StopNote(sender As Button)
    synth.SetChannel(GetChannel())
    synth.NoteOff(60)
    ShowLast()
End Sub

Sub PanicNotes(sender As Button)
    synth.Panic()
    ShowLast()
End Sub

Sub CloseDemo(sender As Button)
    Panel.Close()
End Sub

Sub StartDemo(sender As Window)
    Midi.EnableOutput(False)
    status.Text = "DRY RUN - MIDI OUT is off"
    UpdateMode()
    Print "DX7 demo ready. MIDI output starts disabled."
End Sub

Sub EndDemo(sender As Window)
    Midi.EnableOutput(False)
End Sub

Function GetChannel() As Integer
    Return Midi.CheckRange(CInt(channelBox.Text), 1, 16)
End Function

Sub ShowLast()
    status.Text = Left(Midi.LastMessage(), 60)
    Print Midi.LastMessage()
End Sub

Sub ToggleMode(sender As CheckBox)
    UpdateMode()
End Sub

Sub UpdateMode()
    voiceBox.Enabled = Not arturiaMode.Checked
    voiceButton.Enabled = Not arturiaMode.Checked
    feedbackBox.Enabled = Not arturiaMode.Checked
    operatorBox.Enabled = Not arturiaMode.Checked
    levelBox.Enabled = Not arturiaMode.Checked
    If arturiaMode.Checked Then
        status.Text = "Arturia: Algorithm 1-32 via CC18; MIDI Learn required"
    Else
        status.Text = "Yamaha: Algo 1-32 | FB 0-7 | Op 1-6 | Level 0-99"
    End If
End Sub
