Skip to content

Commit

Permalink
Merge branch 'master' into audio-file-size-checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mjs authored Sep 18, 2019
2 parents c374b64 + 46c0b0a commit 40c25cb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
62 changes: 38 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ import (
"github.com/TheCacophonyProject/audiobait/playlist"
)

const maxRetries = 4
const retryInterval = 30 * time.Second
const (
maxRetries = 4
retryInterval = 30 * time.Second
updateScheduleInterval = time.Minute * 100
)

var errTryLater = errors.New("error getting schedule, try again later")

// version is populated at link time via goreleaser
var version = "No version provided"
Expand Down Expand Up @@ -76,8 +81,11 @@ func runMain() error {

soundsDownloaded := false
for i := 1; i <= maxRetries; i++ {
err = DownloadAndPlaySounds(conf.AudioDir, soundCard)
if err != nil {
if err := DownloadAndPlaySounds(conf.AudioDir, soundCard); err == errTryLater {
log.Println(err)
log.Printf("waiting %s until updateing schedule", updateScheduleInterval)
time.Sleep(updateScheduleInterval)
} else if err != nil {
log.Println("Error dowloading sounds and schedule:", err)
if i < maxRetries {
log.Println("Trying again in", retryInterval)
Expand All @@ -91,35 +99,41 @@ func runMain() error {
}

if !soundsDownloaded {
log.Println("Could not download sounds and schedule.")
log.Println("Could not download sounds and schedule. Will try again tomorrow")
}

// Wait until tomorrow.
playlist.WaitUntilNextDay()

}

}

func DownloadAndPlaySounds(audioDir string, soundCard playlist.AudioDevice) error {
downloader, err := NewDownloader(audioDir)
if err != nil {
return err
}
for {
downloader, err := NewDownloader(audioDir)
if err != nil {
return err
}

schedule := downloader.GetTodaysSchedule()
if len(schedule.Combos) == 0 {
return errors.New("No audio schedule for device, or no sounds to play in schedule.")
}
schedule := downloader.GetTodaysSchedule()
if len(schedule.Combos) == 0 {
return errTryLater
}

files, err := downloader.GetFilesForSchedule(schedule)
if err != nil {
return err
}
files, err := downloader.GetFilesForSchedule(schedule)
if err != nil {
return err
}

player := playlist.NewPlayer(soundCard, files, audioDir)
player.SetRecorder(AudioBaitEventRecorder{})
waitTime := player.TimeUntilNextCombo(schedule.Combos)

log.Printf("Playing todays audiobait schedule...")
player := playlist.NewPlayer(soundCard, files, audioDir)
player.SetRecorder(AudioBaitEventRecorder{})
player.PlayTodaysSchedule(schedule)
return nil
if waitTime > updateScheduleInterval {
return errTryLater
} else {
log.Printf("Playing todays audiobait schedule...")
player.PlayTodaysSchedule(schedule)
return nil
}
}
}
5 changes: 5 additions & 0 deletions playlist/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ func (sp SchedulePlayer) createWindow(combo Combo) *window.Window {
return win
}

func (sp SchedulePlayer) TimeUntilNextCombo(combos []Combo) time.Duration {
count := sp.findNextCombo(combos)
return sp.createWindow(combos[count]).Until()
}

// playSounds plays the sounds for a combo.
func (sp SchedulePlayer) playSounds(combo Combo, chooser *SoundChooser) {
log.Print("Starting sound burst")
Expand Down

0 comments on commit 40c25cb

Please sign in to comment.