Skip to content

Commit

Permalink
add threshold parameter and better defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfitz committed Jan 23, 2013
1 parent e2c488f commit 8fe0235
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions sonden.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ import (

// Flags
var (
ampAddrs = flag.String("amps", "", "Comma-separated list of ip:port of Denon amps")
idle = flag.Duration("idle", 5*time.Minute, "length of silence before turning off amps")
alsaDev = flag.String("alsadev", "", "If non-empty, arecord(1) is used instead of rec(1) with this ALSA device name. e.g. plughw:CARD=Audio,DEV=0 (see arecord -L)")
ampAddrs = flag.String("amps", "", "Comma-separated list of ip:port of Denon amps")
idle = flag.Duration("idle", 5*time.Minute, "length of silence before turning off amps")
alsaDev = flag.String("alsadev", "", "If non-empty, arecord(1) is used instead of rec(1) with this ALSA device name. e.g. plughw:CARD=Audio,DEV=0 (see arecord -L)")
threshold = flag.Float64("threshold", 0, "optional sound cut-off threshold to use")
)

const (
quietVarianceThreshold = 1000 // typically ~2. occasionally as high as 16.
sampleHz = 8 << 10
ringSize = sampleHz // 2 seconds of audio
quietVarianceThreshold = 1000 // typically ~2. occasionally as high as 16.
alsaQuietVarianceThreshold = 2000 // why different than previous line? dunno. wrong sample params?
sampleHz = 8 << 10
ringSize = sampleHz // 2 seconds of audio
)

type sampleRing struct {
Expand Down Expand Up @@ -128,6 +130,13 @@ func main() {
"-f", "S16_LE",
"-t", "raw")
}
if *threshold == 0 {
if *alsaDev != "" {
*threshold = alsaQuietVarianceThreshold
} else {
*threshold = quietVarianceThreshold
}
}
out, _ := cmd.StdoutPipe()
err := cmd.Start()
if err != nil {
Expand Down Expand Up @@ -172,15 +181,15 @@ func main() {
continue
}
v := ring.Variance()
audioPlaying := v > quietVarianceThreshold
audioPlaying := v > *threshold
log.Printf("variance = %v; playing = %v", v, audioPlaying)
if audioPlaying {
lastPlaying = time.Now()
setAmps(true)
} else if time.Since(lastPlaying) > *idle {
setAmps(false)
} else {
log.Printf("turning amps off in %v", *idle - time.Since(lastPlaying))
log.Printf("turning amps off in %v", *idle-time.Since(lastPlaying))
}
}
}

0 comments on commit 8fe0235

Please sign in to comment.