-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spectrul flux analysis to detect silence
- Loading branch information
Showing
3 changed files
with
229 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package alexa | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/evanphx/alexa/portaudio" | ||
) | ||
|
||
const DefaultQuietTime = time.Second | ||
|
||
func ListenIntoBuffer(opts ListenOpts) (*bytes.Buffer, error) { | ||
portaudio.Initialize() | ||
defer portaudio.Terminate() | ||
|
||
sig := make(chan os.Signal, 1) | ||
signal.Notify(sig, os.Interrupt, os.Kill) | ||
|
||
defer signal.Reset(os.Interrupt, os.Kill) | ||
|
||
in := make([]int16, 8196) | ||
stream, err := portaudio.OpenDefaultStream(1, 0, 16000, len(in), in) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer stream.Close() | ||
|
||
err = stream.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var ( | ||
buf bytes.Buffer | ||
heardSomething bool | ||
quiet bool | ||
quietTime = opts.QuietDuration | ||
quietStart time.Time | ||
lastFlux float64 | ||
) | ||
|
||
vad := NewVAD(len(in)) | ||
|
||
if quietTime == 0 { | ||
quietTime = DefaultQuietTime | ||
} | ||
|
||
if opts.State != nil { | ||
opts.State(Waiting) | ||
} | ||
|
||
reader: | ||
for { | ||
err = stream.Read() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = binary.Write(&buf, binary.LittleEndian, in) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
flux := vad.Flux(in) | ||
|
||
if lastFlux == 0 { | ||
lastFlux = flux | ||
continue | ||
} | ||
|
||
if heardSomething { | ||
if flux*1.75 <= lastFlux { | ||
if !quiet { | ||
quietStart = time.Now() | ||
} else { | ||
diff := time.Since(quietStart) | ||
|
||
if diff > quietTime { | ||
break reader | ||
} | ||
} | ||
|
||
quiet = true | ||
} else { | ||
quiet = false | ||
lastFlux = flux | ||
} | ||
} else { | ||
if flux >= lastFlux*1.75 { | ||
heardSomething = true | ||
if opts.State != nil { | ||
opts.State(Listening) | ||
} | ||
} | ||
|
||
lastFlux = flux | ||
} | ||
|
||
select { | ||
case <-sig: | ||
break reader | ||
default: | ||
} | ||
} | ||
|
||
err = stream.Stop() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &buf, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package alexa | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/mjibson/go-dsp/fft" | ||
) | ||
|
||
type VAD struct { | ||
samples []complex128 | ||
fft []complex128 | ||
spectrum []float64 | ||
lastSpectrum []float64 | ||
} | ||
|
||
func NewVAD(width int) *VAD { | ||
return &VAD{ | ||
samples: make([]complex128, width), | ||
spectrum: make([]float64, width/2+1), | ||
lastSpectrum: make([]float64, width/2+1), | ||
} | ||
} | ||
|
||
func (v *VAD) Flux(samples []int16) float64 { | ||
for i, s := range samples { | ||
v.samples[i] = complex(float64(s), 0) | ||
} | ||
|
||
v.fft = fft.FFT(v.samples) | ||
copy(v.spectrum, v.lastSpectrum) | ||
|
||
for i, _ := range v.spectrum { | ||
c := v.fft[i] | ||
v.spectrum[i] = math.Sqrt(real(c)*real(c) + imag(c)*imag(c)) | ||
} | ||
|
||
var flux float64 | ||
|
||
for i, s := range v.spectrum { | ||
flux += (s - v.lastSpectrum[i]) | ||
} | ||
|
||
return flux | ||
} |