X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/8a42be0580469b8fb15fc3369724332f063dca4a..f737b8a74d0f94182eaaf5d106505643be40927d:/radio/main.go diff --git a/radio/main.go b/radio/main.go index eab957a..5edd54e 100644 --- a/radio/main.go +++ b/radio/main.go @@ -1,15 +1,9 @@ package main import ( - "code.octet-stream.net/broadcaster/internal/protocol" "encoding/json" "flag" "fmt" - "github.com/gopxl/beep/v2" - "github.com/gopxl/beep/v2/mp3" - "github.com/gopxl/beep/v2/speaker" - "github.com/gopxl/beep/v2/wav" - "golang.org/x/net/websocket" "log" "os" "os/signal" @@ -17,9 +11,16 @@ import ( "strings" "syscall" "time" + + "code.octet-stream.net/broadcaster/internal/protocol" + "github.com/gopxl/beep/v2" + "github.com/gopxl/beep/v2/mp3" + "github.com/gopxl/beep/v2/speaker" + "github.com/gopxl/beep/v2/wav" + "golang.org/x/net/websocket" ) -const version = "v1.0.0" +const version = "v1.1.0" const sampleRate = 44100 var config RadioConfig = NewRadioConfig() @@ -130,6 +131,7 @@ func runWebsocket(fileSpecChan chan []protocol.FileSpec, playlistSpecChan chan [ } if t == protocol.StopType { + log.Println("Received stop transmission message from server") stop <- true } } @@ -225,7 +227,10 @@ func playlistWorker(ch <-chan []protocol.PlaylistSpec, stop <-chan bool) { } var soonestTime time.Time for _, v := range specs { - t, err := time.ParseInLocation(protocol.StartTimeFormat, v.StartTime, loc) + t, err := time.ParseInLocation(protocol.StartTimeFormatSecs, v.StartTime, loc) + if err != nil { + t, err = time.ParseInLocation(protocol.StartTimeFormat, v.StartTime, loc) + } if err != nil { log.Println("Error parsing start time", err) continue @@ -240,7 +245,7 @@ func playlistWorker(ch <-chan []protocol.PlaylistSpec, stop <-chan bool) { } } if found { - duration := soonestTime.Sub(time.Now()) + duration := time.Until(soonestTime) log.Println("Next playlist will be id", nextId, "in", duration.Seconds(), "seconds") timer = time.NewTimer(duration) } else { @@ -270,6 +275,7 @@ entries: select { case <-time.After(duration): case <-cancel: + log.Println("Cancelling pre-play delay") break entries } @@ -284,7 +290,6 @@ entries: Playlist: playlist.Name, Filename: p.Filename, } - ptt.EngagePTT() f, err := os.Open(filepath.Join(config.CachePath, p.Filename)) if err != nil { log.Println("Couldn't open file for playlist", p.Filename) @@ -308,25 +313,37 @@ entries: defer streamer.Close() done := make(chan bool) + log.Println("PTT on for playback") + ptt.EngagePTT() if format.SampleRate != sampleRate { + log.Println("Configuring resampler for audio provided at sample rate", format.SampleRate) resampled := beep.Resample(4, format.SampleRate, sampleRate, streamer) + log.Println("Playing resampled audio") speaker.Play(beep.Seq(resampled, beep.Callback(func() { done <- true }))) } else { + log.Println("Playing audio at native sample rate") speaker.Play(beep.Seq(streamer, beep.Callback(func() { done <- true }))) } + aborting := false select { case <-done: + log.Println("Audio playback complete") case <-cancel: - ptt.DisengagePTT() - break entries + log.Println("Playlist aborting as requested") + aborting = true } + speaker.Clear() + log.Println("PTT off") ptt.DisengagePTT() + if aborting { + break entries + } } log.Println("Playlist finished", playlist.Name) statusCollector.PlaylistBeginIdle <- true