From e0ec6a8dc980cfb8e01e80130509ff0f385ad5dd Mon Sep 17 00:00:00 2001 From: Trek H Date: Wed, 17 Apr 2019 23:11:23 +0930 Subject: [PATCH] revid: added basic PCM lexer and started adding startMic() func to revid setup. --- codec/lex/lex.go | 29 +++++++++++++++++++++++++++++ revid/revid.go | 7 +++++++ 2 files changed, 36 insertions(+) diff --git a/codec/lex/lex.go b/codec/lex/lex.go index da0dd1b6..a3d05df5 100644 --- a/codec/lex/lex.go +++ b/codec/lex/lex.go @@ -36,6 +36,10 @@ import ( "time" ) +const ( + audioChunkSize = 16000 +) + var noDelay = make(chan time.Time) 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 + } + } +} diff --git a/revid/revid.go b/revid/revid.go index d8068115..64fad64b 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -668,6 +668,13 @@ func (r *Revid) setupInputForFile() error { 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) { r.config.Logger.Log(logger.Info, pkg+"reading input data") r.err <- r.lexTo(r.buffer, read, delay)