From e0430c41a62e447e18b1444bac1f770838f351b5 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sat, 7 Apr 2018 06:36:25 +0800 Subject: [PATCH] Finally working --- audio.go | 34 +++++++++++++++++++++------------- audio_test.go | 19 +++++++++++-------- keymap.go | 16 +++++++++++----- keymap_test.go | 27 +++++++++++++++++++++++++++ main.go | 20 +++++++------------- moving.go | 7 +++++-- moving_test.go | 27 +++++++++++++++++++++++++++ port.go | 4 +++- 8 files changed, 112 insertions(+), 42 deletions(-) create mode 100644 keymap_test.go create mode 100644 moving_test.go diff --git a/audio.go b/audio.go index 2e9bdfd..329b781 100644 --- a/audio.go +++ b/audio.go @@ -2,8 +2,11 @@ package main import ( "io/ioutil" + "log" "os" + "path" "sort" + "strings" "time" "github.com/faiface/beep" @@ -15,19 +18,19 @@ type audio struct { play chan int packDir string sampleRate beep.SampleRate - pack []beep.Streamer + pack []beep.StreamSeeker } -func (a audio) load() { +func (a *audio) load() { a.loadStreamers() a.initSpeaker() } -func (a audio) initSpeaker() { - speaker.Init(a.sampleRate, a.sampleRate.N(time.Second/60)) +func (a *audio) initSpeaker() { + speaker.Init(a.sampleRate, a.sampleRate.N(time.Second/10)) } -func (a audio) loadStreamers() { +func (a *audio) loadStreamers() { files, err := ioutil.ReadDir(a.packDir) if err != nil { panic(err) @@ -36,14 +39,16 @@ func (a audio) loadStreamers() { sort.Sort(byFileInfoName(files)) for _, file := range files { - if file.Name()[0] == '.' { + if !strings.HasSuffix(file.Name(), ".wav") { continue } - a.loadStreamer(file.Name()) + + a.loadStreamer(path.Join(a.packDir, file.Name())) } } -func (a audio) loadStreamer(fn string) { +func (a *audio) loadStreamer(fn string) { + log.Printf("Loading file %d: %s", len(a.pack), fn) f, err := os.Open(fn) if err != nil { panic(err) @@ -56,18 +61,21 @@ func (a audio) loadStreamer(fn string) { a.sampleRate = format.SampleRate } -func (a audio) playSample(i int) { +func (a *audio) playSample(i int) { + a.pack[i].Seek(0) speaker.Play(a.pack[i]) } -func (a audio) watch() { - for play, ready := <-a.play; ready; { +func (a *audio) watch() { + log.Println("Awaiting for audio...") + for play := range a.play { a.playSample(play) } + log.Println("No more incoming audio") } -func newAudio(play chan int, packDir string) audio { - a := audio{ +func newAudio(play chan int, packDir string) *audio { + a := &audio{ play: play, packDir: packDir, } diff --git a/audio_test.go b/audio_test.go index 9bdfa2a..a2246a0 100644 --- a/audio_test.go +++ b/audio_test.go @@ -6,15 +6,18 @@ import ( ) func TestPlay(t *testing.T) { - m := make(chan int) + p := make(chan int) - audio := newAudio(m, "2489__jobro__piano-ff") + audio := newAudio(p, "2489__jobro__piano-ff") go audio.watch() - m <- 1 - time.Sleep(time.Second * 2) - m <- 2 - time.Sleep(time.Second * 2) - m <- 2 - time.Sleep(time.Second * 2) + p <- 1 + time.Sleep(time.Second * 1) + p <- 2 + time.Sleep(time.Second * 1) + p <- 1 + p <- 2 + time.Sleep(time.Second * 1) + + close(p) } diff --git a/keymap.go b/keymap.go index 62bf064..31e9880 100644 --- a/keymap.go +++ b/keymap.go @@ -13,7 +13,7 @@ type keymap struct { play chan int } -func (k keymap) load(file string) { +func (k *keymap) load(file string) { f, err := os.Open(file) if err != nil { panic(err) @@ -25,6 +25,7 @@ func (k keymap) load(file string) { panic(err) } + k.mapped = make(map[int]int) for _, m := range mapping { key, err := strconv.Atoi(m[0]) if err != nil { @@ -38,7 +39,7 @@ func (k keymap) load(file string) { } } -func (k keymap) lookup(key stateFlip) int { +func (k *keymap) lookup(key stateFlip) int { value, has := k.mapped[key.i] if !has { log.Printf("key %d not found", key.i) @@ -47,18 +48,23 @@ func (k keymap) lookup(key stateFlip) int { return value } -func (k keymap) watch() { +func (k *keymap) watch() { + log.Println("Awaiting stateFlips...") for key := range k.changes { play := k.lookup(key) if play >= 0 { + log.Println(play) k.play <- play + log.Println("change read") } } + log.Println("No more stateFlips") } -func newKeymap(changes chan stateFlip, mapFile string) keymap { - k := keymap{ +func newKeymap(changes chan stateFlip, mapFile string) *keymap { + k := &keymap{ changes: changes, + play: make(chan int), } k.load(mapFile) return k diff --git a/keymap_test.go b/keymap_test.go new file mode 100644 index 0000000..be12776 --- /dev/null +++ b/keymap_test.go @@ -0,0 +1,27 @@ +package main + +import ( + "log" + "testing" +) + +func TestLookup(t *testing.T) { + sf := make(chan stateFlip) + + k := newKeymap(sf, "2489__jobro__piano-ff/map.csv") + go k.watch() + go func() { + for play := range k.play { + log.Println("new change!") + log.Println(play) + } + }() + + sf <- stateFlip{0, true, 0, 0} + sf <- stateFlip{1, true, 0, 0} + sf <- stateFlip{2, true, 0, 0} + sf <- stateFlip{3, true, 0, 0} + sf <- stateFlip{4, true, 0, 0} + + close(sf) +} diff --git a/main.go b/main.go index 9ede98f..8d0cb43 100644 --- a/main.go +++ b/main.go @@ -18,18 +18,11 @@ func main() { p := newPort(sp) ma := newMoving(p.samp) ch := chanLogger(ma.changes) - go func() { - for { - <-ch - } - }() - /* - mapped := newKeymap(ch, mapFile) - audio := newAudio(mapped.play, pack) + mapped := newKeymap(ch, mapFile) + audio := newAudio(mapped.play, pack) - go audio.watch() - go mapped.watch() - */ + go audio.watch() + go mapped.watch() go ma.watch() p.watch() } @@ -37,11 +30,12 @@ func main() { func chanLogger(in chan stateFlip) chan stateFlip { out := make(chan stateFlip) go func() { - for { - sf := <-in + log.Println("Logging stateFlips...") + for sf := range in { log.Print(sf) out <- sf } + log.Println("Not logging stateFlips") }() return out } diff --git a/moving.go b/moving.go index 69cb487..96eaf61 100644 --- a/moving.go +++ b/moving.go @@ -17,7 +17,8 @@ type stateFlip struct { fast float64 } -func (m moving) watch() { +func (m *moving) watch() { + log.Println("Watching for samples...") for samp := range m.samp { for len(m.slowAvg) < len(samp) { i := len(m.slowAvg) @@ -50,10 +51,12 @@ func (m moving) watch() { } } } + log.Println("No more samples") } func newMoving(samp chan []byte) *moving { return &moving{ - samp: samp, + samp: samp, + changes: make(chan stateFlip), } } diff --git a/moving_test.go b/moving_test.go new file mode 100644 index 0000000..c370c7f --- /dev/null +++ b/moving_test.go @@ -0,0 +1,27 @@ +package main + +import ( + "testing" + "time" +) + +func TestChann(t *testing.T) { + samp := make(chan []byte) + + m := newMoving(samp) + go m.watch() + go func() { + for { + <-m.changes + } + }() + + samp <- []byte{0, 0, 0, 0} + time.Sleep(time.Second * 2) + samp <- []byte{2, 2, 2, 2} + time.Sleep(time.Second * 2) + samp <- []byte{0, 0, 0, 0} + time.Sleep(time.Second * 2) + + close(samp) +} diff --git a/port.go b/port.go index 491597f..d45520d 100644 --- a/port.go +++ b/port.go @@ -3,6 +3,7 @@ package main import ( "bufio" "fmt" + "log" "github.com/goburrow/serial" ) @@ -12,7 +13,8 @@ type port struct { samp chan []byte } -func (p port) watch() { +func (p *port) watch() { + log.Println("Opening serial port...") port, err := serial.Open(p.Config) if err != nil { panic(err)