Skip to content

Commit db1bde4

Browse files
committed
Improve FeelVR file loading
Support kiiroo files that have the `text` field stored in a `subs` object. Sort the events in the Kiiroo scripts on load.
1 parent 85106c5 commit db1bde4

5 files changed

Lines changed: 67 additions & 1 deletion

File tree

protocol/kiiroo/loader.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ func LoadText(r io.Reader) (protocol.Player, error) {
4949
func LoadJSON(r io.Reader) (protocol.Player, error) {
5050
var format struct {
5151
Text string `json:"text"`
52+
Subs struct {
53+
Text string `json:"text"`
54+
} `json:"subs"`
5255
}
5356
d := json.NewDecoder(r)
5457
err := d.Decode(&format)
5558
if err != nil {
5659
return nil, err
5760
}
58-
return Load(bytes.NewBufferString(format.Text))
61+
if format.Text != "" {
62+
return Load(bytes.NewBufferString(format.Text))
63+
} else if format.Subs.Text != "" {
64+
return Load(bytes.NewBufferString(format.Subs.Text))
65+
}
66+
return nil, ErrNoEvents
5967
}

protocol/kiiroo/loader_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestLoadJSON(t *testing.T) {
4646
`{
4747
"text": " {1.00:4,2.50:1}"
4848
}`,
49+
`{"subs":{"text":" {1.00:4,2.50:1}"}}`,
4950
}
5051
for i, c := range inputJson {
5152
p, err := LoadJSON(bytes.NewBufferString(c))

protocol/kiiroo/player.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kiiroo
33
import (
44
"bytes"
55
"io"
6+
"sort"
67

78
"github.com/funjack/launchcontrol/protocol"
89
)
@@ -32,6 +33,7 @@ func (k *ScriptPlayer) Load(r io.Reader) error {
3233
if err != nil {
3334
return err
3435
}
36+
sort.Sort(es)
3537

3638
k.Script = k.alg.Actions(es)
3739
return nil

protocol/kiiroo/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
var (
1414
// ErrEventFormat is the error returned the event could not be parsed.
1515
ErrEventFormat = errors.New("invalid event format")
16+
// ErrNoEvents is the error returned when no events could be detected.
17+
ErrNoEvents = errors.New("no events found")
1618
)
1719

1820
// Algorithm interface converts Kiiroo events into TimedActions.
@@ -57,6 +59,22 @@ func (e *Event) UnmarshalText(text []byte) error {
5759
// Events is an ordered series of Event objects.
5860
type Events []Event
5961

62+
// Len is the number of elements in the collection.
63+
func (es Events) Len() int {
64+
return len(es)
65+
}
66+
67+
// Less reports whether the element with
68+
// index i should sort before the element with index j.
69+
func (es Events) Less(i, j int) bool {
70+
return es[i].Time < es[j].Time
71+
}
72+
73+
// Swap swaps the elements with indexes i and j.
74+
func (es Events) Swap(i, j int) {
75+
es[i], es[j] = es[j], es[i]
76+
}
77+
6078
// MarshalText implements the encoding.TextMarshaler interface.
6179
func (es Events) MarshalText() (text []byte, err error) {
6280
var values = make([]string, len(es))

protocol/kiiroo/types_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kiiroo
22

33
import (
4+
"sort"
45
"testing"
56
"time"
67
)
@@ -103,3 +104,39 @@ func TestEventsUnmarshalText(t *testing.T) {
103104
}
104105
}
105106
}
107+
108+
func TestEventsSeort(t *testing.T) {
109+
want := Events{
110+
{
111+
Time: time.Millisecond * 1230,
112+
Value: 2,
113+
},
114+
{
115+
Time: time.Millisecond * 1500,
116+
Value: 4,
117+
},
118+
{
119+
Time: time.Millisecond * 3000,
120+
Value: 0,
121+
},
122+
}
123+
124+
var es Events
125+
err := es.UnmarshalText([]byte("{1.50:4,1.23:2,3.00:0}"))
126+
if err != nil {
127+
t.Error(err)
128+
}
129+
sort.Sort(es)
130+
131+
if len(want) != len(es) {
132+
t.Errorf("length does not match, want: %d, got %d", len(want), len(es))
133+
}
134+
for i := range want {
135+
if want[i].Time != es[i].Time {
136+
t.Errorf("time does not match, want: %s, got: %s", want[i].Time, es[i].Time)
137+
}
138+
if want[i].Value != es[i].Value {
139+
t.Errorf("value does not match, want: %d, got: %d", want[i].Value, es[i].Value)
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)