Skip to content

Instantly share code, notes, and snippets.

@marinat
Last active June 25, 2019 15:08
Show Gist options
  • Save marinat/585d03eeb6dfd785076223e23fc8de82 to your computer and use it in GitHub Desktop.
Save marinat/585d03eeb6dfd785076223e23fc8de82 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/faiface/beep/effects"
"log"
"os"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
)
var mainCtrl *beep.Ctrl
var s beep.StreamSeekCloser
var volume = &effects.Volume{
Base: 2,
}
var songPos int
var songLen int
func main() {
tracks := []string{
//"./ukulele.mp3",
"./bg.mp3",
"./classic.mp3",
}
i := 0
f, err := os.Open("./bg.mp3")
if err != nil {
log.Fatal(err)
}
_, format, err := mp3.Decode(f)
if err != nil {
log.Fatal(err)
}
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
next := make(chan bool)
go func() {
for {
select {
case <-next:
fmt.Println(mainCtrl)
songLen = play(tracks[i])
i += 1
fmt.Println(songLen)
case <-time.After(time.Second):
fmt.Println(time.Second)
songPos += 1
if songPos > songLen && mainCtrl != nil {
fmt.Println(mainCtrl)
fmt.Println("end track")
songPos = 0
i += 1
songLen = play(tracks[i])
}
}
}
}()
for {
fmt.Print("Press [ENTER] to next. ")
fmt.Scanln()
next <- true
}
}
func play(path string) int{
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
s, format, err := mp3.Decode(f)
if err != nil {
log.Fatal(err)
}
volume.Streamer = s
mainCtrl = &beep.Ctrl{Streamer: volume}
speaker.Play(mainCtrl)
return int(float32(s.Len()) / float32(format.SampleRate))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment