From e851ea228cc315cb59a8a09c6bb8eb0322c25bb1 Mon Sep 17 00:00:00 2001 From: Trek H Date: Wed, 29 May 2019 02:20:19 +0930 Subject: [PATCH] revid: added switch for codec conversion after recording --- revid/audio-input.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/revid/audio-input.go b/revid/audio-input.go index af3c6197..c86ee336 100644 --- a/revid/audio-input.go +++ b/revid/audio-input.go @@ -353,21 +353,19 @@ func (a *AudioDevice) Read(p []byte) (n int, err error) { // in the desired format specified by the ac's parameters. func (a *AudioDevice) formatBuffer() alsa.Buffer { var err error - wantChannels := a.Channels - wantRate := a.SampleRate // If nothing needs to be changed, return the original. - if a.ab.Format.Channels == wantChannels && a.ab.Format.Rate == wantRate { + if a.ab.Format.Channels == a.Channels && a.ab.Format.Rate == a.SampleRate { return a.ab } formatted := alsa.Buffer{Format: a.ab.Format} bufCopied := false - if a.ab.Format.Channels != wantChannels { + if a.ab.Format.Channels != a.Channels { // Convert channels. // TODO(Trek): Make this work for conversions other than stereo to mono. - if a.ab.Format.Channels == 2 && wantChannels == 1 { + if a.ab.Format.Channels == 2 && a.Channels == 1 { formatted.Data, err = pcm.StereoToMono(a.ab) if err != nil { a.l.Log(logger.Warning, "channel conversion failed, audio has remained stereo", "error", err.Error()) @@ -378,19 +376,28 @@ func (a *AudioDevice) formatBuffer() alsa.Buffer { } } - if a.ab.Format.Rate != wantRate { + if a.ab.Format.Rate != a.SampleRate { // Convert rate. if bufCopied { - formatted.Data, err = pcm.Resample(formatted, wantRate) + formatted.Data, err = pcm.Resample(formatted, a.SampleRate) } else { - formatted.Data, err = pcm.Resample(a.ab, wantRate) + formatted.Data, err = pcm.Resample(a.ab, a.SampleRate) } if err != nil { a.l.Log(logger.Warning, "rate conversion failed, audio has remained original rate", "error", err.Error()) } else { - formatted.Format.Rate = wantRate + formatted.Format.Rate = a.SampleRate } } + + switch a.Codec { + case PCM: + case ADPCM: + // TODO(Trek):Add ADPCM conversion. + default: + a.l.Log(logger.Error, "codec conversion failed, audio has remained original codec", "error", err.Error()) + } + return formatted }