diff --git a/input/audio/audio.go b/input/audio/audio.go index 5d97d7c8..745b71fe 100644 --- a/input/audio/audio.go +++ b/input/audio/audio.go @@ -252,17 +252,19 @@ func (d *Device) open() error { // Eg. the audioinjector sound card is supposed to record at 8000Hz and 16000Hz but it can't due to a firmware issue, // a fix for this is to remove 8000 and 16000 from the rates slice. var rates = [8]int{8000, 16000, 32000, 44100, 48000, 88200, 96000, 192000} - foundRate := false + var rate int - for i := 0; i < len(rates) && !foundRate; i++ { - if rates[i] < d.SampleRate { + foundRate := false + for r := range rates { + if r < d.SampleRate { continue } - if rates[i]%d.SampleRate == 0 { - rate, err = d.dev.NegotiateRate(rates[i]) + if r%d.SampleRate == 0 { + rate, err = d.dev.NegotiateRate(r) if err == nil { foundRate = true d.l.Log(logger.Debug, pkg+"alsa device sample rate set", "rate", rate) + break } } } @@ -385,11 +387,7 @@ func (d *Device) Read(p []byte) (int, error) { } // Read from ring buffer. - n, err := d.rb.Read(p) - if err != nil { - return 0, err - } - return n, nil + return d.rb.Read(p) } // formatBuffer returns audio that has been converted to the desired format. @@ -440,6 +438,7 @@ func (d *Device) formatBuffer() alsa.Buffer { // nearestPowerOfTwo finds and returns the nearest power of two to the given integer. // If the lower and higher power of two are the same distance, it returns the higher power. // For negative values, 1 is returned. +// Source: https://stackoverflow.com/a/45859570 func nearestPowerOfTwo(n int) int { if n <= 0 { return 1 diff --git a/revid/revid.go b/revid/revid.go index f64f13fb..410db7a2 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -626,6 +626,7 @@ func (r *Revid) setupInputForFile() (func() error, error) { } // startAudioDevice is used to start capturing audio from an audio device and processing it. +// It returns a function that can be used to stop the device and any errors that occur. func (r *Revid) startAudioDevice() (func() error, error) { // Create audio device. ac := &audio.Config{ diff --git a/revid/revid_test.go b/revid/revid_test.go index 8622dbd9..8ab2e62f 100644 --- a/revid/revid_test.go +++ b/revid/revid_test.go @@ -41,7 +41,7 @@ import ( const raspividPath = "/usr/local/bin/raspivid" // Suppress all test logging, except for t.Errorf output. -var silent bool = true +var silent = true // TestRaspivid tests that raspivid starts correctly. // It is intended to be run on a Raspberry Pi.