Add support for delaying before or after a triggered note.

This commit is contained in:
James Coleman 2024-11-30 10:24:28 -06:00
parent 1eb7440496
commit f154cb9e09
3 changed files with 17 additions and 1 deletions

View File

@ -243,13 +243,16 @@ midi_routers:
- channel: 0
match_all_notes: true
match_all_velocities: true
delay_before: 200ms
mqtt_topic: osc/behringer_wing/send/$ctl/user/2/2/bu/val
mqtt_payload:
- "1"
- channel: 0
match_all_notes: true
match_all_velocities: true
delay_before: 200ms
mqtt_topic: osc/behringer_wing/send/$ctl/user/2/2/bu/val
mqtt_payload:
- "0"
delay_after: 200ms
```

View File

@ -15,7 +15,7 @@ import (
const (
serviceName = "midi-request-trigger"
serviceDescription = "Takes trigger MIDI messages by HTTP or MQTT requests and trigger HTTP or MQTT requests by MIDI messages"
serviceVersion = "0.2.2"
serviceVersion = "0.3"
)
// App is the global application structure for communicating between servers and storing information.

View File

@ -85,6 +85,9 @@ type NoteTrigger struct {
Velocity uint8 `fig:"velocity"`
// If we should match all velocity values.
MatchAllVelocities bool `fig:"match_all_velocities"`
// Allow delaying the request.
DelayBefore time.Duration `fig:"delay_before"`
DelayAfter time.Duration `fig:"deplay_after"`
// Custom MQTT message. Do not set to ignore MQTT.
MqttTopic string `fig:"mqtt_topic"`
// Nil payload will generate a payload with midi info.
@ -187,7 +190,12 @@ func (r *MidiRouter) sendRequest(channel, note, velocity uint8) {
// For all logging, we want to print the message so setup a common string to print.
logInfo := fmt.Sprintf("note %s(%d) on channel %v with velocity %v", midi.Note(note), note, channel, velocity)
// Delay before.
time.Sleep(trig.DelayBefore)
// If MQTT trigger, send the MQTT request.
if trig.MqttTopic != "" && r.MqttClient != nil {
// If payload provided, send the defined payload.
if trig.MqttPayload != nil {
data, err := json.Marshal(trig.MqttPayload)
if err != nil {
@ -197,6 +205,7 @@ func (r *MidiRouter) sendRequest(channel, note, velocity uint8) {
r.Log(SendLog, "-> [MQTT] %s: %s", trig.MqttTopic, string(data))
}
} else {
// If no payload provided, send the note information as JSON.
payload := MQTTPayload{
Channel: channel,
Note: note,
@ -212,6 +221,7 @@ func (r *MidiRouter) sendRequest(channel, note, velocity uint8) {
}
}
// If URL trigger defined, perform a HTTP request.
if trig.URL != "" {
// Default method to GET if nothing is defined.
if trig.Method == "" {
@ -280,6 +290,9 @@ func (r *MidiRouter) sendRequest(channel, note, velocity uint8) {
r.Log(DebugLog, "Trigger response: %s\n%s", logInfo, string(body))
}
}
// Delay after.
time.Sleep(trig.DelayAfter)
}
}
}