Finally working
parent
4705db80c9
commit
e0430c41a6
34
audio.go
34
audio.go
|
@ -2,8 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/faiface/beep"
|
"github.com/faiface/beep"
|
||||||
|
@ -15,19 +18,19 @@ type audio struct {
|
||||||
play chan int
|
play chan int
|
||||||
packDir string
|
packDir string
|
||||||
sampleRate beep.SampleRate
|
sampleRate beep.SampleRate
|
||||||
pack []beep.Streamer
|
pack []beep.StreamSeeker
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a audio) load() {
|
func (a *audio) load() {
|
||||||
a.loadStreamers()
|
a.loadStreamers()
|
||||||
a.initSpeaker()
|
a.initSpeaker()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a audio) initSpeaker() {
|
func (a *audio) initSpeaker() {
|
||||||
speaker.Init(a.sampleRate, a.sampleRate.N(time.Second/60))
|
speaker.Init(a.sampleRate, a.sampleRate.N(time.Second/10))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a audio) loadStreamers() {
|
func (a *audio) loadStreamers() {
|
||||||
files, err := ioutil.ReadDir(a.packDir)
|
files, err := ioutil.ReadDir(a.packDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -36,14 +39,16 @@ func (a audio) loadStreamers() {
|
||||||
sort.Sort(byFileInfoName(files))
|
sort.Sort(byFileInfoName(files))
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.Name()[0] == '.' {
|
if !strings.HasSuffix(file.Name(), ".wav") {
|
||||||
continue
|
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)
|
f, err := os.Open(fn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -56,18 +61,21 @@ func (a audio) loadStreamer(fn string) {
|
||||||
a.sampleRate = format.SampleRate
|
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])
|
speaker.Play(a.pack[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a audio) watch() {
|
func (a *audio) watch() {
|
||||||
for play, ready := <-a.play; ready; {
|
log.Println("Awaiting for audio...")
|
||||||
|
for play := range a.play {
|
||||||
a.playSample(play)
|
a.playSample(play)
|
||||||
}
|
}
|
||||||
|
log.Println("No more incoming audio")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAudio(play chan int, packDir string) audio {
|
func newAudio(play chan int, packDir string) *audio {
|
||||||
a := audio{
|
a := &audio{
|
||||||
play: play,
|
play: play,
|
||||||
packDir: packDir,
|
packDir: packDir,
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPlay(t *testing.T) {
|
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()
|
go audio.watch()
|
||||||
|
|
||||||
m <- 1
|
p <- 1
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 1)
|
||||||
m <- 2
|
p <- 2
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 1)
|
||||||
m <- 2
|
p <- 1
|
||||||
time.Sleep(time.Second * 2)
|
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
|
play chan int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k keymap) load(file string) {
|
func (k *keymap) load(file string) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -25,6 +25,7 @@ func (k keymap) load(file string) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
k.mapped = make(map[int]int)
|
||||||
for _, m := range mapping {
|
for _, m := range mapping {
|
||||||
key, err := strconv.Atoi(m[0])
|
key, err := strconv.Atoi(m[0])
|
||||||
if err != nil {
|
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]
|
value, has := k.mapped[key.i]
|
||||||
if !has {
|
if !has {
|
||||||
log.Printf("key %d not found", key.i)
|
log.Printf("key %d not found", key.i)
|
||||||
|
@ -47,18 +48,23 @@ func (k keymap) lookup(key stateFlip) int {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k keymap) watch() {
|
func (k *keymap) watch() {
|
||||||
|
log.Println("Awaiting stateFlips...")
|
||||||
for key := range k.changes {
|
for key := range k.changes {
|
||||||
play := k.lookup(key)
|
play := k.lookup(key)
|
||||||
if play >= 0 {
|
if play >= 0 {
|
||||||
|
log.Println(play)
|
||||||
k.play <- play
|
k.play <- play
|
||||||
|
log.Println("change read")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Println("No more stateFlips")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newKeymap(changes chan stateFlip, mapFile string) keymap {
|
func newKeymap(changes chan stateFlip, mapFile string) *keymap {
|
||||||
k := keymap{
|
k := &keymap{
|
||||||
changes: changes,
|
changes: changes,
|
||||||
|
play: make(chan int),
|
||||||
}
|
}
|
||||||
k.load(mapFile)
|
k.load(mapFile)
|
||||||
return k
|
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)
|
p := newPort(sp)
|
||||||
ma := newMoving(p.samp)
|
ma := newMoving(p.samp)
|
||||||
ch := chanLogger(ma.changes)
|
ch := chanLogger(ma.changes)
|
||||||
go func() {
|
mapped := newKeymap(ch, mapFile)
|
||||||
for {
|
audio := newAudio(mapped.play, pack)
|
||||||
<-ch
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
/*
|
|
||||||
mapped := newKeymap(ch, mapFile)
|
|
||||||
audio := newAudio(mapped.play, pack)
|
|
||||||
|
|
||||||
go audio.watch()
|
go audio.watch()
|
||||||
go mapped.watch()
|
go mapped.watch()
|
||||||
*/
|
|
||||||
go ma.watch()
|
go ma.watch()
|
||||||
p.watch()
|
p.watch()
|
||||||
}
|
}
|
||||||
|
@ -37,11 +30,12 @@ func main() {
|
||||||
func chanLogger(in chan stateFlip) chan stateFlip {
|
func chanLogger(in chan stateFlip) chan stateFlip {
|
||||||
out := make(chan stateFlip)
|
out := make(chan stateFlip)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
log.Println("Logging stateFlips...")
|
||||||
sf := <-in
|
for sf := range in {
|
||||||
log.Print(sf)
|
log.Print(sf)
|
||||||
out <- sf
|
out <- sf
|
||||||
}
|
}
|
||||||
|
log.Println("Not logging stateFlips")
|
||||||
}()
|
}()
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,8 @@ type stateFlip struct {
|
||||||
fast float64
|
fast float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m moving) watch() {
|
func (m *moving) watch() {
|
||||||
|
log.Println("Watching for samples...")
|
||||||
for samp := range m.samp {
|
for samp := range m.samp {
|
||||||
for len(m.slowAvg) < len(samp) {
|
for len(m.slowAvg) < len(samp) {
|
||||||
i := len(m.slowAvg)
|
i := len(m.slowAvg)
|
||||||
|
@ -50,10 +51,12 @@ func (m moving) watch() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Println("No more samples")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMoving(samp chan []byte) *moving {
|
func newMoving(samp chan []byte) *moving {
|
||||||
return &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 (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/goburrow/serial"
|
"github.com/goburrow/serial"
|
||||||
)
|
)
|
||||||
|
@ -12,7 +13,8 @@ type port struct {
|
||||||
samp chan []byte
|
samp chan []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p port) watch() {
|
func (p *port) watch() {
|
||||||
|
log.Println("Opening serial port...")
|
||||||
port, err := serial.Open(p.Config)
|
port, err := serial.Open(p.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
Loading…
Reference in New Issue