refactor(tracker): move NoteStr and NoteAsValue to gioui package

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2023-10-20 01:40:14 +03:00
parent a60814bab7
commit ff8e662857
3 changed files with 42 additions and 48 deletions

View File

@ -183,7 +183,7 @@ func (t *Tracker) NumberPressed(iv byte) {
func (t *Tracker) JammingPressed(e key.Event) byte {
if val, ok := noteMap[e.Name]; ok {
if _, ok := t.KeyPlaying[e.Name]; !ok {
n := tracker.NoteAsValue(t.OctaveNumberInput.Value, val)
n := noteAsValue(t.OctaveNumberInput.Value, val)
instr := t.InstrIndex()
noteID := tracker.NoteIDInstr(instr, n)
t.NoteOn(noteID)

View File

@ -160,7 +160,7 @@ func (te *TrackEditor) Layout(gtx layout.Context, t *Tracker) layout.Dimensions
} else {
if val, ok := noteMap[e.Name]; ok {
if _, ok := t.KeyPlaying[e.Name]; !ok {
n := tracker.NoteAsValue(t.OctaveNumberInput.Value, val)
n := noteAsValue(t.OctaveNumberInput.Value, val)
t.SetNote(n)
step = true
trk := t.Cursor().Track
@ -289,6 +289,45 @@ func (te *TrackEditor) Layout(gtx layout.Context, t *Tracker) layout.Dimensions
return dims
}
const baseNote = 24
var notes = []string{
"C-",
"C#",
"D-",
"D#",
"E-",
"F-",
"F#",
"G-",
"G#",
"A-",
"A#",
"B-",
}
func noteStr(val byte) string {
if val == 1 {
return "..." // hold
}
if val == 0 {
return "---" // release
}
oNote := mod(int(val-baseNote), 12)
octave := (int(val) - oNote - baseNote) / 12
if octave < 0 {
return fmt.Sprintf("%s%s", notes[oNote], string(byte('Z'+1+octave)))
}
if octave >= 10 {
return fmt.Sprintf("%s%s", notes[oNote], string(byte('A'+octave-10)))
}
return fmt.Sprintf("%s%d", notes[oNote], octave)
}
func noteAsValue(octave, note int) byte {
return byte(baseNote + (octave * 12) + note)
}
func (te *TrackEditor) layoutTracks(gtx C, t *Tracker) D {
defer op.Offset(image.Point{}).Push(gtx.Ops).Pop()
defer clip.Rect{Max: gtx.Constraints.Max}.Push(gtx.Ops).Pop()
@ -450,7 +489,7 @@ func (te *TrackEditor) layoutTracks(gtx C, t *Tracker) D {
}
widget.Label{}.Layout(gtx, textShaper, trackerFont, trackerFontSize, strings.ToUpper(text), op.CallOp{})
} else {
widget.Label{}.Layout(gtx, textShaper, trackerFont, trackerFontSize, tracker.NoteStr(c), op.CallOp{})
widget.Label{}.Layout(gtx, textShaper, trackerFont, trackerFontSize, noteStr(c), op.CallOp{})
}
op.Offset(image.Point{-patmarkWidth, trackRowHeight}).Add(gtx.Ops)
}

View File

@ -1,45 +0,0 @@
package tracker
import "fmt"
// note 81 = A4
// note 72 = C4
// note 24 = C0
const baseNote = 24
var notes = []string{
"C-",
"C#",
"D-",
"D#",
"E-",
"F-",
"F#",
"G-",
"G#",
"A-",
"A#",
"B-",
}
func NoteStr(val byte) string {
if val == 1 {
return "..." // hold
}
if val == 0 {
return "---" // release
}
oNote := mod(int(val-baseNote), 12)
octave := (int(val) - oNote - baseNote) / 12
if octave < 0 {
return fmt.Sprintf("%s%s", notes[oNote], string(byte('Z'+1+octave)))
}
if octave >= 10 {
return fmt.Sprintf("%s%s", notes[oNote], string(byte('A'+octave-10)))
}
return fmt.Sprintf("%s%d", notes[oNote], octave)
}
func NoteAsValue(octave, note int) byte {
return byte(baseNote + (octave * 12) + note)
}