av/revid/audio_linux.go

81 lines
2.0 KiB
Go
Raw Permalink Normal View History

2019-11-22 05:52:23 +03:00
/*
NAME
audio_linux.go
AUTHORS
Alan Noble <alan@ausocean.org>
2020-09-03 10:14:22 +03:00
Trek Hopton <trek@ausocean.org>
2019-11-22 05:52:23 +03:00
LICENSE
revid is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package revid
import (
"fmt"
"strconv"
"bitbucket.org/ausocean/av/codec/codecutil"
"bitbucket.org/ausocean/av/container/mts"
"bitbucket.org/ausocean/av/device"
2019-11-22 05:52:23 +03:00
"bitbucket.org/ausocean/av/device/alsa"
)
// Used to reliably read, write, and test audio metadata entry keys.
const (
SampleRateKey = "sampleRate"
ChannelsKey = "channels"
PeriodKey = "period"
BitDepthKey = "bitDepth"
CodecKey = "codec"
)
2019-11-22 05:52:23 +03:00
func (r *Revid) setupAudio() error {
2020-08-11 13:47:52 +03:00
// Create new ALSA device.
d := alsa.New(r.cfg.Logger)
r.input = d
// Configure ALSA device.
r.cfg.Logger.Debug("configuring input device")
err := d.Setup(r.cfg)
switch err := err.(type) {
case nil:
// Do nothing.
case device.MultiError:
r.cfg.Logger.Warning("errors from configuring input device", "errors", err)
default:
return err
2020-08-11 13:47:52 +03:00
}
r.cfg.Logger.Info("input device configured")
2020-08-11 13:47:52 +03:00
// Set revid's lexer.
l, err := codecutil.NewByteLexer(d.DataSize())
if err != nil {
return err
}
r.lexTo = l.Lex
// Add metadata.
mts.Meta.Add(SampleRateKey, strconv.Itoa(int(r.cfg.SampleRate)))
mts.Meta.Add(ChannelsKey, strconv.Itoa(int(r.cfg.Channels)))
mts.Meta.Add(PeriodKey, fmt.Sprintf("%.6f", r.cfg.RecPeriod))
mts.Meta.Add(BitDepthKey, strconv.Itoa(int(r.cfg.BitDepth)))
mts.Meta.Add(CodecKey, r.cfg.InputCodec)
2019-11-22 05:52:23 +03:00
return nil
}