summaryrefslogtreecommitdiff
path: root/cmd/playsmf/main.go
diff options
context:
space:
mode:
authorLibravatar Terin Stock <terinjokes@gmail.com>2018-07-29 19:37:27 +0000
committerLibravatar Terin Stock <terinjokes@gmail.com>2019-01-27 17:55:12 -0800
commitca933d746ba16ef326d9b4ba85d51940ab9e8add (patch)
tree203e060468f27bb05095aaa1cb755d7c961bd530 /cmd/playsmf/main.go
parentchore: initial commit (diff)
downloadmidifi-ca933d746ba16ef326d9b4ba85d51940ab9e8add.tar.xz
feat: initial version of midifi software
This CL introduces the initial version of the midifi software, via a simplified CLI interface "playsmf". Though it is far from complete, this version can be manually invoked with the locations of the MIDI communications port (the `-com` flag), and MIDI file. It will playback the MIDI file with the correct time. Software is currently tested with an integration test, that invokes the program with a known MIDI file and compares the output of the serial port, standard out, and standard error. Golden files can be updated by providing the `-test.update-golden` flag. Change-Id: I312bc721736e2edf385ece5141133ffa6bd20a72 Signed-off-by: Terin Stock <terinjokes@gmail.com>
Diffstat (limited to 'cmd/playsmf/main.go')
-rw-r--r--cmd/playsmf/main.go114
1 files changed, 114 insertions, 0 deletions
diff --git a/cmd/playsmf/main.go b/cmd/playsmf/main.go
new file mode 100644
index 0000000..7b55ff3
--- /dev/null
+++ b/cmd/playsmf/main.go
@@ -0,0 +1,114 @@
+package main
+
+import (
+ "container/list"
+ "flag"
+ "fmt"
+ "os"
+ "time"
+
+ "gitlab.com/gomidi/midi"
+ "gitlab.com/gomidi/midi/mid"
+ "gitlab.com/gomidi/midi/midimessage"
+ "gitlab.com/gomidi/midi/smf"
+ "go.terinstock.com/midifi/pkg/ttymididrv"
+)
+
+var (
+ drv mid.Driver
+ midiSerial string
+)
+
+func init() {
+ flag.StringVar(&midiSerial, "com", "", "The serial port of the MIDI synth")
+}
+
+func main() {
+ flag.Parse()
+ drv = ttymididrv.New(midiSerial, 38400)
+ defer drv.Close()
+
+ outs, err := drv.Outs()
+ if err != nil {
+ fmt.Printf("err: %v\n", err)
+ os.Exit(-1)
+ }
+ out := outs[0]
+
+ err = out.Open()
+ if err != nil {
+ fmt.Printf("err: %v\n", err)
+ os.Exit(-1)
+ }
+ defer out.Close()
+
+ if flag.NArg() == 0 {
+ flag.Usage()
+ os.Exit(-1)
+ }
+
+ f, err := os.Open(flag.Arg(0))
+ if err != nil {
+ fmt.Println("unable to load SMF file")
+ os.Exit(-2)
+ }
+ defer f.Close()
+
+ fmt.Printf("file: %s\n", flag.Arg(0))
+
+ mf := mid.NewReader(mid.NoLogger())
+ mf.SMFHeader = func(header smf.Header) {
+ fmt.Printf("file format: %s\n", header.Format.String())
+ fmt.Printf("number of tracks: %d\n", header.NumTracks)
+ fmt.Printf("time format: %s\n", header.TimeFormat.String())
+ fmt.Println("===========")
+ }
+ events := &list.List{}
+ mf.Msg.Each = func(pos *mid.Position, msg midi.Message) {
+ addEvent(events, Event{track: pos.Track, abs: pos.AbsoluteTicks, msg: msg})
+ }
+ mf.ReadSMF(f)
+
+ w := mid.WriteTo(out)
+
+ var lastDur time.Duration
+ for event := events.Front(); event != nil; event = event.Next() {
+ dur := *mf.TimeAt(event.Value.(Event).abs)
+ if lastDur != dur {
+ <-time.After(dur - lastDur)
+ lastDur = dur
+ }
+
+ msg := event.Value.(Event).msg
+ if midimessage.IsLive(msg) {
+ fmt.Printf("%s\n", msg.String())
+ w.Write(msg)
+ }
+ }
+}
+
+type Event struct {
+ track int16
+ abs uint64
+ msg midi.Message
+}
+
+func addEvent(l *list.List, evt Event) {
+ if l.Front() == nil {
+ l.PushBack(evt)
+ return
+ }
+
+ for cur := l.Front(); cur != nil; cur = cur.Next() {
+ peek := cur.Next()
+ if peek == nil {
+ l.InsertAfter(evt, cur)
+ return
+ }
+
+ if peek.Value.(Event).abs >= evt.abs {
+ l.InsertAfter(evt, cur)
+ return
+ }
+ }
+}