revid: added switch for codec conversion after recording

This commit is contained in:
Trek H 2019-05-29 02:20:19 +09:30
parent c2b5ee0574
commit e851ea228c
1 changed files with 16 additions and 9 deletions

View File

@ -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
}