Compare commits

...

6 Commits

54
main.go
View File

@ -47,7 +47,7 @@ func xPrintIdle() (idle uint64) {
}
func speakToday() {
cmdSetVolume := exec.Command("amixer", "-D", "pulse", "set", "Master", fmt.Sprintf("%d%%", 50+(ntick/int64(MaxRunTime.Seconds()/3))*50))
cmdSetVolume := exec.Command("amixer", "-D", "pulse", "set", "Master", fmt.Sprintf("%d%%", 50+50/(int64(MaxRunTime.Seconds()/3)/ntick+1)))
if err := cmdSetVolume.Run(); err != nil {
log.Println(err)
}
@ -101,7 +101,7 @@ func speakWeather() {
log.Println(err)
}
cmdSetVolume := exec.Command("amixer", "-D", "pulse", "set", "Master", fmt.Sprintf("%d%%", 50+(ntick/int64(MaxRunTime.Seconds()/3))*50))
cmdSetVolume := exec.Command("amixer", "-D", "pulse", "set", "Master", fmt.Sprintf("%d%%", 50+50/(int64(MaxRunTime.Seconds()/3)/ntick+1)))
if err := cmdSetVolume.Run(); err != nil {
log.Println(err)
}
@ -121,6 +121,11 @@ func speakWeather() {
log.Println(err)
}
cmd3 := exec.Command("/home/nemunaire/scripts/wakeup/ratp-traffic.sh", "rers", "B")
if err := cmd3.Run(); err != nil {
log.Println(err)
}
if cmdAmbiant.Process != nil {
(*cmdAmbiant.Process).Kill()
}
@ -132,11 +137,13 @@ func speakWeather() {
}
}
func loadFile(path string) (s beep.StreamSeekCloser, format beep.Format, err error) {
func loadFile(filepath string) (name string, s beep.StreamSeekCloser, format beep.Format, err error) {
for _, decoder := range []func(io.ReadCloser) (beep.StreamSeekCloser, beep.Format, error){flac.Decode, mp3.Decode, wav.Decode} {
var fd *os.File
fd, err = os.Open(path)
name = path.Base(filepath)
fd, err = os.Open(filepath)
if err != nil {
return
}
@ -159,6 +166,7 @@ func loadFile(path string) (s beep.StreamSeekCloser, format beep.Format, err err
func main() {
var weatherTime = flag.Duration("weather", -1, "Speak weather?")
var noshuffle = flag.Bool("noshuffle", false, "Don't shuffle music order")
var ignoreidle = flag.Bool("ignoreidle", false, "Don't stop the reveil on idle detection change")
var sr = flag.Int("samplerate", 44100, "Samplerate for unifying output stream")
var claironTime = flag.Duration("clairon", -1, "Time before running the wake up clairon song")
flag.DurationVar(&MaxRunTime, "maxruntime", MaxRunTime, "Maximum duration before auto exit")
@ -169,21 +177,27 @@ func main() {
return
}
rand.Seed(time.Now().UnixNano())
seed := time.Now().Unix() % 172800 * 64
log.Println("Starting reveil with seed:", seed)
rand.Seed(seed)
sampleRate := beep.SampleRate(*sr)
paths := []string{}
playlist := []beep.Streamer{}
formats := []beep.Format{}
// Load playlist
log.Println("Loading playlist...")
for _, arg := range flag.Args() {
s, f, err := loadFile(arg)
p, s, f, err := loadFile(arg)
if err != nil {
log.Printf("Unable to load %s: %s", arg, err)
continue
}
paths = append(paths, p)
playlist = append(playlist, s)
formats = append(formats, f)
}
@ -192,6 +206,7 @@ func main() {
log.Println("Shuffling playlist...")
// Shuffle the playlist
rand.Shuffle(len(playlist), func(i, j int) {
paths[i], paths[j] = paths[j], paths[i]
playlist[i], playlist[j] = playlist[j], playlist[i]
formats[i], formats[j] = formats[j], formats[i]
})
@ -203,14 +218,15 @@ func main() {
dontUpdateVolume := false
hasClaironed := claironTime == nil || *claironTime == -1
hasSpokeWeather := weatherTime == nil || *weatherTime == -1
reverseOrder := time.Now().Unix()%86400%2 == 0
playedItem := -1
// Create infinite stream
playedItem := -1
stream := beep.Iterate(func() beep.Streamer {
if !hasClaironed && time.Since(launched) >= *claironTime {
log.Println("clairon time!")
hasClaironed = true
sample, format, err := loadFile("/home/nemunaire/www/audio/miracle-morning/clairon-reveil.mp3")
*claironTime += *claironTime / 2
_, sample, format, err := loadFile("/home/nemunaire/www/audio/miracle-morning/clairon-reveil.mp3")
if err == nil {
volume.Volume = 1
dontUpdateVolume = true
@ -219,6 +235,8 @@ func main() {
} else {
return sample
}
} else {
log.Println("Error loading clairon:", err)
}
}
@ -230,9 +248,17 @@ func main() {
dontUpdateVolume = false
volume.Volume = -2 - math.Log(5/float64(ntick))/3
playedItem += 1
if reverseOrder {
playedItem -= 1
} else {
playedItem += 1
}
if playedItem >= len(playlist) {
playedItem = 0
} else if playedItem < 0 {
playedItem = len(playlist) - 1
}
if i, ok := playlist[playedItem].(beep.StreamSeekCloser); ok {
@ -278,8 +304,12 @@ loop:
volume.Volume = -2 - math.Log(5/float64(ntick))/3
}
if xPrintIdle() < idle {
break loop
if ignoreidle == nil || !*ignoreidle {
if idle < 60000 {
idle = xPrintIdle()
} else if xPrintIdle() < idle {
break loop
}
}
case <-interrupt:
break loop