Skip to content

Commit

Permalink
Improve FeelVR file loading
Browse files Browse the repository at this point in the history
Support kiiroo files that have the `text` field stored in a `subs`
object.

Sort the events in the Kiiroo scripts on load.
  • Loading branch information
funjack committed Oct 22, 2017
1 parent 85106c5 commit db1bde4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
10 changes: 9 additions & 1 deletion protocol/kiiroo/loader.go
Expand Up @@ -49,11 +49,19 @@ func LoadText(r io.Reader) (protocol.Player, error) {
func LoadJSON(r io.Reader) (protocol.Player, error) {
var format struct {
Text string `json:"text"`
Subs struct {
Text string `json:"text"`
} `json:"subs"`
}
d := json.NewDecoder(r)
err := d.Decode(&format)
if err != nil {
return nil, err
}
return Load(bytes.NewBufferString(format.Text))
if format.Text != "" {
return Load(bytes.NewBufferString(format.Text))
} else if format.Subs.Text != "" {
return Load(bytes.NewBufferString(format.Subs.Text))
}
return nil, ErrNoEvents
}
1 change: 1 addition & 0 deletions protocol/kiiroo/loader_test.go
Expand Up @@ -46,6 +46,7 @@ func TestLoadJSON(t *testing.T) {
`{
"text": " {1.00:4,2.50:1}"
}`,
`{"subs":{"text":" {1.00:4,2.50:1}"}}`,
}
for i, c := range inputJson {
p, err := LoadJSON(bytes.NewBufferString(c))
Expand Down
2 changes: 2 additions & 0 deletions protocol/kiiroo/player.go
Expand Up @@ -3,6 +3,7 @@ package kiiroo
import (
"bytes"
"io"
"sort"

"github.com/funjack/launchcontrol/protocol"
)
Expand Down Expand Up @@ -32,6 +33,7 @@ func (k *ScriptPlayer) Load(r io.Reader) error {
if err != nil {
return err
}
sort.Sort(es)

k.Script = k.alg.Actions(es)
return nil
Expand Down
18 changes: 18 additions & 0 deletions protocol/kiiroo/types.go
Expand Up @@ -13,6 +13,8 @@ import (
var (
// ErrEventFormat is the error returned the event could not be parsed.
ErrEventFormat = errors.New("invalid event format")
// ErrNoEvents is the error returned when no events could be detected.
ErrNoEvents = errors.New("no events found")
)

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

// Len is the number of elements in the collection.
func (es Events) Len() int {
return len(es)
}

// Less reports whether the element with
// index i should sort before the element with index j.
func (es Events) Less(i, j int) bool {
return es[i].Time < es[j].Time
}

// Swap swaps the elements with indexes i and j.
func (es Events) Swap(i, j int) {
es[i], es[j] = es[j], es[i]
}

// MarshalText implements the encoding.TextMarshaler interface.
func (es Events) MarshalText() (text []byte, err error) {
var values = make([]string, len(es))
Expand Down
37 changes: 37 additions & 0 deletions protocol/kiiroo/types_test.go
@@ -1,6 +1,7 @@
package kiiroo

import (
"sort"
"testing"
"time"
)
Expand Down Expand Up @@ -103,3 +104,39 @@ func TestEventsUnmarshalText(t *testing.T) {
}
}
}

func TestEventsSeort(t *testing.T) {
want := Events{
{
Time: time.Millisecond * 1230,
Value: 2,
},
{
Time: time.Millisecond * 1500,
Value: 4,
},
{
Time: time.Millisecond * 3000,
Value: 0,
},
}

var es Events
err := es.UnmarshalText([]byte("{1.50:4,1.23:2,3.00:0}"))
if err != nil {
t.Error(err)
}
sort.Sort(es)

if len(want) != len(es) {
t.Errorf("length does not match, want: %d, got %d", len(want), len(es))
}
for i := range want {
if want[i].Time != es[i].Time {
t.Errorf("time does not match, want: %s, got: %s", want[i].Time, es[i].Time)
}
if want[i].Value != es[i].Value {
t.Errorf("value does not match, want: %d, got: %d", want[i].Value, es[i].Value)
}
}
}

0 comments on commit db1bde4

Please sign in to comment.