Corrected go code

This commit is contained in:
starcalc 2022-10-16 18:47:51 +02:00
parent 2b3f45ea8c
commit 930bf41b5d
1 changed files with 38 additions and 14 deletions

View File

@ -11,23 +11,34 @@ import (
"os" "os"
"sync" "sync"
"time" "time"
"fmt"
) )
var sounds = []*beep.Buffer{ var sounds = []*beep.Buffer{
mustLoadStream("./mini_grand/mini_grand-001.wav"), mustLoadStream("./default/001.wav"),
mustLoadStream("./mini_grand/mini_grand-003.wav"), mustLoadStream("./default/002.wav"),
mustLoadStream("./mini_grand/mini_grand-005.wav"), mustLoadStream("./default/003.wav"),
mustLoadStream("./mini_grand/mini_grand-006.wav"), mustLoadStream("./default/004.wav"),
mustLoadStream("./mini_grand/mini_grand-008.wav"), mustLoadStream("./default/005.wav"),
mustLoadStream("./mini_grand/mini_grand-010.wav"), mustLoadStream("./default/006.wav"),
mustLoadStream("./mini_grand/mini_grand-012.wav"), mustLoadStream("./default/007.wav"),
mustLoadStream("./mini_grand/mini_grand-013.wav"), mustLoadStream("./default/008.wav"),
} }
var counter int
func mustLoadStream(p string) *beep.Buffer { func mustLoadStream(p string) *beep.Buffer {
f, err := os.Open(p) counter++
log.Println("Loading: /media/usb/00" + fmt.Sprintf("%d", counter) + ".wav")
f, err := os.Open("/media/usb/00" + fmt.Sprintf("%d", counter) + ".wav")
if err != nil { if err != nil {
log.Fatal(err) log.Println("Failed: /media/usb/00" + fmt.Sprintf("%d", counter) + ".wav")
log.Println("Loading " + p)
f, err = os.Open(p)
if err != nil {
// log.Fatal(err)
return nil
}
} }
defer f.Close() defer f.Close()
@ -38,7 +49,9 @@ func mustLoadStream(p string) *beep.Buffer {
defer streamer.Close() defer streamer.Close()
newFormat := format newFormat := format
newFormat.SampleRate = format.SampleRate / 4 // newFormat.SampleRate = format.SampleRate / 4
// newFormat.SampleRate = format.SampleRate / 2
newFormat.SampleRate = format.SampleRate
resample := beep.Resample(4, format.SampleRate, newFormat.SampleRate, streamer) resample := beep.Resample(4, format.SampleRate, newFormat.SampleRate, streamer)
buffer := beep.NewBuffer(newFormat) buffer := beep.NewBuffer(newFormat)
@ -86,7 +99,10 @@ func (w *soundWorker) Start() {
defer w.mtx.Unlock() defer w.mtx.Unlock()
w.isRunning = true w.isRunning = true
w.ctrl.Streamer.(beep.StreamSeeker).Seek(0) // w.ctrl.Streamer.(beep.StreamSeeker).Seek(0)
// log.Println("Buffer Start: " + fmt.Sprintf("%d", w.ctrl.Streamer))
w.ctrl.Streamer = beep.Seq(w.buffer.Streamer(0, w.buffer.Len()), beep.Silence(-1))
// w.ctrl.Streamer = w.buffer.Streamer(0, w.buffer.Len())
w.volume.Volume = 1 w.volume.Volume = 1
w.ctrl.Paused = false w.ctrl.Paused = false
} }
@ -104,6 +120,7 @@ func main() {
} }
format := sounds[0].Format() format := sounds[0].Format()
// if err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/100)); err != nil {
if err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/100)); err != nil { if err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/100)); err != nil {
return return
} }
@ -112,13 +129,18 @@ func main() {
mixer := beep.Mixer{} mixer := beep.Mixer{}
for _, buffer := range sounds { for _, buffer := range sounds {
if buffer == nil {
continue
}
c := &beep.Ctrl{ c := &beep.Ctrl{
Streamer: buffer.Streamer(0, buffer.Len()), // Streamer: buffer.Streamer(0, buffer.Len()),
Streamer: beep.Seq(buffer.Streamer(0, buffer.Len()), beep.Silence(-1)),
Paused: true, Paused: true,
} }
volume := &effects.Volume{ volume := &effects.Volume{
Streamer: c, Streamer: c,
// Streamer: beep.Seq(c, beep.Silence(-1)),
Base: 2, Base: 2,
Volume: 1, Volume: 1,
} }
@ -136,6 +158,7 @@ func main() {
c := make(chan byte) c := make(chan byte)
// List das byte von serial ein und prueft die bits
go func() { go func() {
for b := range c { for b := range c {
for i, w := range soundWorkers { for i, w := range soundWorkers {
@ -152,6 +175,7 @@ func main() {
} }
}() }()
// List das byte ein
for { for {
buf := make([]byte, 1) buf := make([]byte, 1)
_, err := stream.Read(buf) _, err := stream.Read(buf)