revid: added basic PCM lexer and started adding startMic() func to revid setup.

This commit is contained in:
Trek H 2019-04-17 23:11:23 +09:30
parent 6ee286e988
commit e0ec6a8dc9
2 changed files with 36 additions and 0 deletions

View File

@ -36,6 +36,10 @@ import (
"time" "time"
) )
const (
audioChunkSize = 16000
)
var noDelay = make(chan time.Time) var noDelay = make(chan time.Time)
func init() { func init() {
@ -245,3 +249,28 @@ func MJPEG(dst io.Writer, src io.Reader, delay time.Duration) error {
} }
} }
} }
func PCM(dst io.Writer, src io.Reader, delay time.Duration) error {
var tick <-chan time.Time
if delay == 0 {
tick = noDelay
} else {
ticker := time.NewTicker(delay)
defer ticker.Stop()
tick = ticker.C
}
r := bufio.NewReader(src)
for {
buf := make([]byte, 0, audioChunkSize)
_, err := r.Read(buf)
if err != nil {
return err
}
<-tick
_, err = dst.Write(buf)
if err != nil {
return err
}
}
}

View File

@ -668,6 +668,13 @@ func (r *Revid) setupInputForFile() error {
return nil return nil
} }
// startMic is used to start capturing audio from an audio device and processing it.
func startMic() {
go processFrom(stream, r.config.Rate)
return nil
}
func (r *Revid) processFrom(read io.Reader, delay time.Duration) { func (r *Revid) processFrom(read io.Reader, delay time.Duration) {
r.config.Logger.Log(logger.Info, pkg+"reading input data") r.config.Logger.Log(logger.Info, pkg+"reading input data")
r.err <- r.lexTo(r.buffer, read, delay) r.err <- r.lexTo(r.buffer, read, delay)