Finally working
parent
4705db80c9
commit
e0430c41a6
34
audio.go
34
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,
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
16
keymap.go
16
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
|
||||
|
|
|
@ -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)
|
||||
}
|
20
main.go
20
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
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
4
port.go
4
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)
|
||||
|
|
Loading…
Reference in New Issue