feat: save MIDI tracks to generation area; playing tracks and audio preview are still under development

This commit is contained in:
josc146
2023-11-29 19:04:41 +08:00
parent 34112c79c7
commit a2062ae9cc
11 changed files with 172 additions and 44 deletions

View File

@@ -23,6 +23,7 @@ type MIDIMessage struct {
var ports []Port
var input rtmidi.MIDIIn
var out rtmidi.MIDIOut
var activeIndex int = -1
var lastNoteTime time.Time
@@ -33,6 +34,14 @@ func (a *App) midiLoop() {
runtime.EventsEmit(a.ctx, "midiError", err.Error())
return
}
out, err = rtmidi.NewMIDIOutDefault()
if err != nil {
runtime.EventsEmit(a.ctx, "midiError", err.Error())
}
err = out.OpenPort(0, "")
if err != nil {
runtime.EventsEmit(a.ctx, "midiError", err.Error())
}
ticker := time.NewTicker(500 * time.Millisecond)
go func() {
for {
@@ -55,7 +64,7 @@ func (a *App) midiLoop() {
func (a *App) OpenMidiPort(index int) error {
if input == nil {
return errors.New("failed to initialize MIDI")
return errors.New("failed to initialize MIDI input")
}
if activeIndex == index {
return nil
@@ -126,7 +135,7 @@ func (a *App) OpenMidiPort(index int) error {
func (a *App) CloseMidiPort() error {
if input == nil {
return errors.New("failed to initialize MIDI")
return errors.New("failed to initialize MIDI input")
}
if activeIndex == -1 {
return nil
@@ -140,3 +149,16 @@ func (a *App) CloseMidiPort() error {
}
return nil
}
func (a *App) PlayNote(msg MIDIMessage) error {
if out == nil {
return errors.New("failed to initialize MIDI output")
}
channelByte := byte(msg.Channel)
if msg.MessageType == "NoteOn" {
out.SendMessage([]byte{0x90 | channelByte, byte(msg.Note), byte(msg.Velocity)})
} else if msg.MessageType == "NoteOff" {
out.SendMessage([]byte{0x80 | channelByte, byte(msg.Note), byte(msg.Velocity)})
}
return nil
}