Skip to content

Commit

Permalink
add string param edit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergodart committed Nov 7, 2024
1 parent 025fbaa commit 2ec3c2f
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 6 deletions.
44 changes: 44 additions & 0 deletions core/music/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package music

import (
"errors"
"regexp"
"strconv"
)

var noteMap = map[string]int{
"C": 0,
"Db": 1,
"D": 2,
"Eb": 3,
"E": 4,
"F": 5,
"Gb": 6,
"G": 7,
"Ab": 8,
"A": 9,
"Bb": 10,
"B": 11,
}

// ConvertNoteToMIDI converts a musical note (ex A6 or Db3) to a midi note number
func ConvertNoteToMIDI(note string) (int, error) {
re := regexp.MustCompile(`^([A-G][b]?)(-?\d+)$`)
matches := re.FindStringSubmatch(note)
if matches == nil || len(matches) < 3 {
return 0, errors.New("invalid note format")
}

noteName := matches[1]
octaveStr := matches[2]

octave, err := strconv.Atoi(octaveStr)
if err != nil {
return 0, errors.New("invalid octave")
}

if base, ok := noteMap[noteName]; ok {
return base + octave*12, nil
}
return 0, errors.New("unknown note name")
}
9 changes: 9 additions & 0 deletions ui/param/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"fmt"
"strconv"

"signls/core/common"
"signls/core/music"
Expand Down Expand Up @@ -72,3 +73,11 @@ func (c Channel) SetAlt(value int) {
n.(music.Audible).Note().Channel.SetRandomAmount(value)
}
}

func (c Channel) SetEditValue(input string) {
value, err := strconv.Atoi(input)
if err != nil {
return
}
c.Set(value - 1)
}
17 changes: 17 additions & 0 deletions ui/param/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package param

import (
"fmt"
"strconv"
"strings"

"signls/core/common"
"signls/core/node"
Expand Down Expand Up @@ -95,3 +97,18 @@ func (d Destination) SetDestinationAmount(dAmountX, dAmountY int) {
n.(*node.HoleEmitter).SetDestinationAmount(amountX+dAmountX, amountY+dAmountY)
}
}

func (d Destination) SetEditValue(input string) {
coordinates := strings.Split(input, ",")
if len(coordinates) != 2 {
return
}
x, errX := strconv.Atoi(coordinates[0])
y, errY := strconv.Atoi(coordinates[1])
if errX != nil || errY != nil {
return
}
for _, n := range d.nodes {
n.(*node.HoleEmitter).SetDestination(x, y)
}
}
2 changes: 2 additions & 0 deletions ui/param/direction.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ func (d Direction) SetFromKeyString(key string) {
node.SetDirection(dir)
}
}

func (d Direction) SetEditValue(value string) {}
8 changes: 8 additions & 0 deletions ui/param/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,11 @@ func (k *Key) keyIndex() int {
}
return 0
}

func (k *Key) SetEditValue(input string) {
key, err := music.ConvertNoteToMIDI(input)
if err != nil {
return
}
k.Set(key)
}
9 changes: 9 additions & 0 deletions ui/param/length.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"fmt"
"strconv"

"signls/core/common"
"signls/core/music"
Expand Down Expand Up @@ -97,3 +98,11 @@ func (l Length) SetAlt(value int) {
n.(music.Audible).Note().Length.SetRandomAmount(value)
}
}

func (l Length) SetEditValue(input string) {
value, err := strconv.Atoi(input)
if err != nil {
return
}
l.Set(value)
}
9 changes: 9 additions & 0 deletions ui/param/offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"fmt"
"strconv"

"signls/core/common"
"signls/core/node"
Expand Down Expand Up @@ -79,3 +80,11 @@ func (o Offset) SetAlt(value int) {
n.(*node.EuclidEmitter).Offset.SetRandomAmount(value)
}
}

func (o Offset) SetEditValue(input string) {
value, err := strconv.Atoi(input)
if err != nil {
return
}
o.Set(value)
}
1 change: 1 addition & 0 deletions ui/param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Param interface {
Display() string
Set(value int)
SetAlt(value int)
SetEditValue(input string)

Up()
Down()
Expand Down
9 changes: 9 additions & 0 deletions ui/param/probability.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"fmt"
"strconv"

"signls/core/common"
"signls/core/music"
Expand Down Expand Up @@ -61,3 +62,11 @@ func (p Probability) Set(value int) {
}

func (p Probability) SetAlt(value int) {}

func (p Probability) SetEditValue(input string) {
value, err := strconv.Atoi(input)
if err != nil {
return
}
p.Set(value)
}
2 changes: 2 additions & 0 deletions ui/param/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ func (r Root) Set(value int) {
}

func (r Root) SetAlt(value int) {}

func (r Root) SetEditValue(input string) {}
2 changes: 2 additions & 0 deletions ui/param/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ func (s Scale) scaleIndex() int {
}
return 0
}

func (s Scale) SetEditValue(input string) {}
9 changes: 9 additions & 0 deletions ui/param/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"fmt"
"strconv"

"signls/core/common"
"signls/core/node"
Expand Down Expand Up @@ -80,3 +81,11 @@ func (s Steps) SetAlt(value int) {
n.(*node.EuclidEmitter).Steps.SetRandomAmount(value)
}
}

func (s Steps) SetEditValue(input string) {
value, err := strconv.Atoi(input)
if err != nil {
return
}
s.Set(value)
}
9 changes: 9 additions & 0 deletions ui/param/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package param
import (
"fmt"
"math"
"strconv"

"signls/core/common"
"signls/core/node"
Expand Down Expand Up @@ -81,3 +82,11 @@ func (t Threshold) SetAlt(value int) {
n.(*node.Emitter).Behavior().(*node.TollEmitter).Threshold.SetRandomAmount(value)
}
}

func (t Threshold) SetEditValue(input string) {
value, err := strconv.Atoi(input)
if err != nil {
return
}
t.Set(value)
}
9 changes: 9 additions & 0 deletions ui/param/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"fmt"
"strconv"

"signls/core/common"
"signls/core/node"
Expand Down Expand Up @@ -82,3 +83,11 @@ func (t Triggers) SetAlt(value int) {
n.(*node.EuclidEmitter).Triggers.SetRandomAmount(value)
}
}

func (t Triggers) SetEditValue(input string) {
value, err := strconv.Atoi(input)
if err != nil {
return
}
t.Set(value)
}
9 changes: 9 additions & 0 deletions ui/param/velocity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"fmt"
"strconv"

"signls/core/common"
"signls/core/music"
Expand Down Expand Up @@ -72,3 +73,11 @@ func (v Velocity) SetAlt(value int) {
n.(music.Audible).Note().Velocity.SetRandomAmount(value)
}
}

func (v Velocity) SetEditValue(input string) {
value, err := strconv.Atoi(input)
if err != nil {
return
}
v.Set(value)
}
7 changes: 1 addition & 6 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ui

import (
"fmt"
"strconv"
"time"

"signls/core/common"
Expand Down Expand Up @@ -130,11 +129,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, m.keymap.EditNode):
m.input.Blur()
val, err := strconv.Atoi(m.input.Value())
if err != nil {
val = m.params[m.param].Value()
}
m.params[m.param].Set(val)
m.params[m.param].SetEditValue(m.input.Value())
return m, nil
case key.Matches(msg, m.keymap.Cancel, m.keymap.EditInput):
m.input.Blur()
Expand Down

0 comments on commit 2ec3c2f

Please sign in to comment.