2019-05-08 13:01:25 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2019-06-05 19:39:55 +03:00
|
|
|
audio.go
|
2019-05-08 13:01:25 +03:00
|
|
|
|
|
|
|
AUTHOR
|
2019-06-05 19:39:55 +03:00
|
|
|
Alan Noble <alan@ausocean.org>
|
2019-05-08 13:01:25 +03:00
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2019-06-05 19:39:55 +03:00
|
|
|
This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
2019-05-08 13:01:25 +03:00
|
|
|
|
|
|
|
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 [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
2019-06-05 19:39:55 +03:00
|
|
|
// Package audio provides access to input from audio devices.
|
|
|
|
package audio
|
2019-05-08 10:51:21 +03:00
|
|
|
|
|
|
|
import (
|
2019-06-03 20:01:35 +03:00
|
|
|
"bytes"
|
2019-05-08 10:51:21 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/yobert/alsa"
|
|
|
|
|
2019-06-03 20:01:35 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/adpcm"
|
2019-06-05 19:39:55 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/codecutil"
|
2019-05-08 10:51:21 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/pcm"
|
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
|
|
|
"bitbucket.org/ausocean/utils/ring"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-06-17 06:59:01 +03:00
|
|
|
pkg = "audio: "
|
2019-06-05 19:39:55 +03:00
|
|
|
rbTimeout = 100 * time.Millisecond
|
|
|
|
rbNextTimeout = 100 * time.Millisecond
|
|
|
|
rbLen = 200
|
|
|
|
defaultSampleRate = 48000
|
2019-05-08 10:51:21 +03:00
|
|
|
)
|
|
|
|
|
2019-06-17 06:59:01 +03:00
|
|
|
// "running" means the input goroutine is reading from the ALSA device and writing to the ringbuffer.
|
|
|
|
// "paused" means the input routine is sleeping until unpaused or stopped.
|
|
|
|
// "stopped" means the input routine is stopped and the ALSA device is closed.
|
2019-05-20 18:15:54 +03:00
|
|
|
const (
|
2019-07-11 08:24:06 +03:00
|
|
|
running = iota + 1
|
2019-05-20 18:15:54 +03:00
|
|
|
paused
|
|
|
|
stopped
|
|
|
|
)
|
|
|
|
|
2019-06-17 06:59:01 +03:00
|
|
|
// Device holds everything we need to know about the audio input stream and implements io.Reader.
|
2019-06-05 19:39:55 +03:00
|
|
|
type Device struct {
|
2019-06-17 06:59:01 +03:00
|
|
|
l Logger // Logger for device's routines to log to.
|
|
|
|
mode uint8 // Operating mode, either running, paused, or stopped.
|
|
|
|
mu sync.Mutex // Provides synchronisation when changing modes concurrently.
|
2019-06-05 19:39:55 +03:00
|
|
|
title string // Name of audio title, or empty for the default title.
|
2019-06-17 06:59:01 +03:00
|
|
|
dev *alsa.Device // ALSA's Audio input device.
|
2019-05-15 09:27:49 +03:00
|
|
|
ab alsa.Buffer // ALSA's buffer.
|
|
|
|
rb *ring.Buffer // Our buffer.
|
2019-06-17 06:59:01 +03:00
|
|
|
chunkSize int // This is the number of bytes that will be stored in rb at a time.
|
|
|
|
*Config // Configuration parameters for this device.
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-06-05 19:39:55 +03:00
|
|
|
// Config provides parameters used by Device.
|
|
|
|
type Config struct {
|
2019-05-08 10:51:21 +03:00
|
|
|
SampleRate int
|
|
|
|
Channels int
|
|
|
|
BitDepth int
|
2019-05-22 08:26:58 +03:00
|
|
|
RecPeriod float64
|
2019-05-08 10:51:21 +03:00
|
|
|
Codec uint8
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:39:55 +03:00
|
|
|
// Logger enables any implementation of a logger to be used.
|
|
|
|
// TODO: Make this part of the logger package.
|
|
|
|
type Logger interface {
|
|
|
|
SetLevel(int8)
|
|
|
|
Log(level int8, message string, params ...interface{})
|
|
|
|
}
|
|
|
|
|
2019-06-17 18:43:42 +03:00
|
|
|
// OpenError is used to determine whether an error has originated from attempting to open a device.
|
|
|
|
type OpenError error
|
|
|
|
|
2019-06-05 19:39:55 +03:00
|
|
|
// NewDevice initializes and returns an Device which can be started, read from, and stopped.
|
|
|
|
func NewDevice(cfg *Config, l Logger) (*Device, error) {
|
2019-06-18 12:05:45 +03:00
|
|
|
|
2019-06-20 11:34:58 +03:00
|
|
|
err := validate(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-18 12:05:45 +03:00
|
|
|
|
2019-06-13 17:05:52 +03:00
|
|
|
d := &Device{
|
|
|
|
Config: cfg,
|
|
|
|
l: l,
|
|
|
|
}
|
2019-05-08 10:51:21 +03:00
|
|
|
|
|
|
|
// Open the requested audio device.
|
2019-06-20 11:34:58 +03:00
|
|
|
err = d.open()
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-13 17:05:52 +03:00
|
|
|
d.l.Log(logger.Error, pkg+"failed to open device")
|
|
|
|
return nil, err
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:05:52 +03:00
|
|
|
// Setup the device to record with desired period.
|
2019-06-05 20:11:17 +03:00
|
|
|
d.ab = d.dev.NewBufferDuration(time.Duration(d.RecPeriod * float64(time.Second)))
|
2019-06-13 17:05:52 +03:00
|
|
|
|
|
|
|
// Account for channel conversion.
|
|
|
|
chunkSize := float64(len(d.ab.Data) / d.dev.BufferFormat().Channels * d.Channels)
|
|
|
|
|
|
|
|
// Account for resampling.
|
|
|
|
chunkSize = (chunkSize / float64(d.dev.BufferFormat().Rate)) * float64(d.SampleRate)
|
|
|
|
if chunkSize < 1 {
|
2019-06-05 19:39:55 +03:00
|
|
|
return nil, errors.New("given Config parameters are too small")
|
2019-05-22 17:49:44 +03:00
|
|
|
}
|
2019-06-13 17:05:52 +03:00
|
|
|
|
|
|
|
// Account for codec conversion.
|
2019-06-05 20:11:17 +03:00
|
|
|
if d.Codec == codecutil.ADPCM {
|
2019-06-13 17:05:52 +03:00
|
|
|
d.chunkSize = adpcm.EncBytes(int(chunkSize))
|
2019-06-03 20:01:35 +03:00
|
|
|
} else {
|
2019-06-13 17:05:52 +03:00
|
|
|
d.chunkSize = int(chunkSize)
|
2019-06-03 20:01:35 +03:00
|
|
|
}
|
2019-06-13 17:05:52 +03:00
|
|
|
|
|
|
|
// Create ring buffer with appropriate chunk size.
|
2019-06-05 20:11:17 +03:00
|
|
|
d.rb = ring.NewBuffer(rbLen, d.chunkSize, rbTimeout)
|
2019-05-28 19:36:58 +03:00
|
|
|
|
2019-06-13 17:05:52 +03:00
|
|
|
// Start device in paused mode.
|
2019-06-05 20:11:17 +03:00
|
|
|
d.mode = paused
|
|
|
|
go d.input()
|
2019-05-08 10:51:21 +03:00
|
|
|
|
2019-06-05 20:11:17 +03:00
|
|
|
return d, nil
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Start will start recording audio and writing to the ringbuffer.
|
2019-06-17 18:43:42 +03:00
|
|
|
// Once a Device has been stopped it cannot be started again. This is likely to change in future.
|
2019-06-05 20:11:17 +03:00
|
|
|
func (d *Device) Start() error {
|
|
|
|
d.mu.Lock()
|
|
|
|
mode := d.mode
|
|
|
|
d.mu.Unlock()
|
2019-05-28 19:36:58 +03:00
|
|
|
switch mode {
|
2019-05-20 18:15:54 +03:00
|
|
|
case paused:
|
2019-06-05 20:11:17 +03:00
|
|
|
d.mu.Lock()
|
|
|
|
d.mode = running
|
|
|
|
d.mu.Unlock()
|
2019-05-28 19:36:58 +03:00
|
|
|
return nil
|
2019-05-20 18:15:54 +03:00
|
|
|
case stopped:
|
2019-05-28 19:36:58 +03:00
|
|
|
// TODO(Trek): Make this reopen device and start recording.
|
|
|
|
return errors.New("device is stopped")
|
2019-05-20 18:15:54 +03:00
|
|
|
case running:
|
2019-05-28 19:36:58 +03:00
|
|
|
return nil
|
|
|
|
default:
|
2019-06-17 06:59:01 +03:00
|
|
|
return fmt.Errorf("invalid mode: %d", mode)
|
2019-05-15 09:27:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Stop will stop recording audio and close the device.
|
2019-06-17 18:43:42 +03:00
|
|
|
// Once a Device has been stopped it cannot be started again. This is likely to change in future.
|
2019-06-05 20:11:17 +03:00
|
|
|
func (d *Device) Stop() {
|
|
|
|
d.mu.Lock()
|
|
|
|
d.mode = stopped
|
|
|
|
d.mu.Unlock()
|
2019-05-15 09:27:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-05 20:11:17 +03:00
|
|
|
// ChunkSize returns the number of bytes written to the ringbuffer per d.RecPeriod.
|
|
|
|
func (d *Device) ChunkSize() int {
|
|
|
|
return d.chunkSize
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-06-18 12:05:45 +03:00
|
|
|
// validate checks if Config parameters are valid and returns an error if they are not.
|
|
|
|
func validate(c *Config) error {
|
|
|
|
if c.SampleRate <= 0 {
|
|
|
|
return fmt.Errorf("invalid sample rate: %v", c.SampleRate)
|
|
|
|
}
|
|
|
|
if c.Channels <= 0 {
|
|
|
|
return fmt.Errorf("invalid number of channels: %v", c.Channels)
|
|
|
|
}
|
|
|
|
if c.BitDepth <= 0 {
|
|
|
|
return fmt.Errorf("invalid bitdepth: %v", c.BitDepth)
|
|
|
|
}
|
|
|
|
if c.RecPeriod <= 0 {
|
|
|
|
return fmt.Errorf("invalid recording period: %v", c.RecPeriod)
|
|
|
|
}
|
2019-06-20 11:59:08 +03:00
|
|
|
if !codecutil.IsValid(c.Codec) {
|
|
|
|
return errors.New("invalid codec")
|
2019-06-18 12:05:45 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// open the recording device with the given name and prepare it to record.
|
2019-05-08 10:51:21 +03:00
|
|
|
// If name is empty, the first recording device is used.
|
2019-06-05 20:11:17 +03:00
|
|
|
func (d *Device) open() error {
|
2019-05-28 19:36:58 +03:00
|
|
|
// Close any existing device.
|
2019-06-05 20:11:17 +03:00
|
|
|
if d.dev != nil {
|
|
|
|
d.l.Log(logger.Debug, pkg+"closing device", "title", d.title)
|
|
|
|
d.dev.Close()
|
|
|
|
d.dev = nil
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Open sound card and open recording device.
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"opening sound card")
|
2019-05-08 10:51:21 +03:00
|
|
|
cards, err := alsa.OpenCards()
|
|
|
|
if err != nil {
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(err)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
defer alsa.CloseCards(cards)
|
|
|
|
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"finding audio device")
|
2019-05-08 10:51:21 +03:00
|
|
|
for _, card := range cards {
|
|
|
|
devices, err := card.Devices()
|
|
|
|
if err != nil {
|
2019-05-21 06:09:10 +03:00
|
|
|
continue
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
for _, dev := range devices {
|
|
|
|
if dev.Type != alsa.PCM || !dev.Record {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-05 20:11:17 +03:00
|
|
|
if dev.Title == d.title || d.title == "" {
|
|
|
|
d.dev = dev
|
2019-05-08 10:51:21 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-05 20:11:17 +03:00
|
|
|
if d.dev == nil {
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(errors.New("no audio device found"))
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"opening audio device", "title", d.dev.Title)
|
|
|
|
err = d.dev.Open()
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(err)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2 channels is what most devices need to record in. If mono is requested,
|
|
|
|
// the recording will be converted in formatBuffer().
|
2019-06-17 18:43:42 +03:00
|
|
|
channels, err := d.dev.NegotiateChannels(2)
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(err)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-06-17 18:43:42 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"alsa device channels set", "channels", channels)
|
2019-05-08 10:51:21 +03:00
|
|
|
|
|
|
|
// Try to negotiate a rate to record in that is divisible by the wanted rate
|
|
|
|
// so that it can be easily downsampled to the wanted rate.
|
2019-06-17 18:43:42 +03:00
|
|
|
// rates is a slice of common sample rates including the standard for CD (44100Hz) and standard for professional audio recording (48000Hz).
|
2019-05-28 19:36:58 +03:00
|
|
|
// Note: if a card thinks it can record at a rate but can't actually, this can cause a failure.
|
|
|
|
// Eg. the audioinjector sound card is supposed to record at 8000Hz and 16000Hz but it can't due to a firmware issue,
|
2019-06-17 18:43:42 +03:00
|
|
|
// a fix for this is to remove 8000 and 16000 from the rates slice.
|
|
|
|
var rates = [8]int{8000, 16000, 32000, 44100, 48000, 88200, 96000, 192000}
|
2019-06-18 12:33:38 +03:00
|
|
|
|
2019-06-17 18:43:42 +03:00
|
|
|
var rate int
|
2019-06-18 12:33:38 +03:00
|
|
|
foundRate := false
|
|
|
|
for r := range rates {
|
|
|
|
if r < d.SampleRate {
|
2019-05-08 10:51:21 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-06-18 12:33:38 +03:00
|
|
|
if r%d.SampleRate == 0 {
|
|
|
|
rate, err = d.dev.NegotiateRate(r)
|
2019-05-08 10:51:21 +03:00
|
|
|
if err == nil {
|
|
|
|
foundRate = true
|
2019-06-17 18:43:42 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"alsa device sample rate set", "rate", rate)
|
2019-06-18 12:33:38 +03:00
|
|
|
break
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no easily divisible rate is found, then use the default rate.
|
|
|
|
if !foundRate {
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Warning, pkg+"Unable to sample at requested rate, default used.", "rateRequested", d.SampleRate)
|
2019-06-17 18:43:42 +03:00
|
|
|
rate, err = d.dev.NegotiateRate(defaultSampleRate)
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(err)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-06-17 18:43:42 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"alsa device sample rate set", "rate", rate)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:41:02 +03:00
|
|
|
var aFmt alsa.FormatType
|
2019-06-05 20:11:17 +03:00
|
|
|
switch d.BitDepth {
|
2019-05-08 10:51:21 +03:00
|
|
|
case 16:
|
2019-05-09 05:41:02 +03:00
|
|
|
aFmt = alsa.S16_LE
|
2019-05-08 10:51:21 +03:00
|
|
|
case 32:
|
2019-05-09 05:41:02 +03:00
|
|
|
aFmt = alsa.S32_LE
|
2019-05-08 10:51:21 +03:00
|
|
|
default:
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(fmt.Errorf("unsupported sample bits %v", d.BitDepth))
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-06-13 17:05:52 +03:00
|
|
|
devFmt, err := d.dev.NegotiateFormat(aFmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-17 18:43:42 +03:00
|
|
|
var bitdepth int
|
2019-06-13 17:05:52 +03:00
|
|
|
switch devFmt {
|
|
|
|
case alsa.S16_LE:
|
2019-06-17 18:43:42 +03:00
|
|
|
bitdepth = 16
|
2019-06-13 17:05:52 +03:00
|
|
|
case alsa.S32_LE:
|
2019-06-17 18:43:42 +03:00
|
|
|
bitdepth = 32
|
2019-06-13 17:05:52 +03:00
|
|
|
default:
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(fmt.Errorf("unsupported sample bits %v", d.BitDepth))
|
2019-06-13 17:05:52 +03:00
|
|
|
}
|
2019-06-17 18:43:42 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"alsa device bit depth set", "bitdepth", bitdepth)
|
2019-06-13 17:05:52 +03:00
|
|
|
|
|
|
|
// A 50ms period is a sensible value for low-ish latency. (this could be made configurable if needed)
|
|
|
|
// Some devices only accept even period sizes while others want powers of 2.
|
|
|
|
// So we will find the closest power of 2 to the desired period size.
|
|
|
|
const wantPeriod = 0.05 //seconds
|
2019-06-17 18:43:42 +03:00
|
|
|
bytesPerSecond := rate * channels * (bitdepth / 8)
|
2019-06-13 18:21:26 +03:00
|
|
|
wantPeriodSize := int(float64(bytesPerSecond) * wantPeriod)
|
2019-06-13 17:05:52 +03:00
|
|
|
nearWantPeriodSize := nearestPowerOfTwo(wantPeriodSize)
|
|
|
|
|
2019-06-13 18:21:26 +03:00
|
|
|
// At least two period sizes should fit within the buffer.
|
2019-06-17 18:43:42 +03:00
|
|
|
bufSize, err := d.dev.NegotiateBufferSize(nearWantPeriodSize * 2)
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(err)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-06-17 18:43:42 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"alsa device buffer size set", "buffersize", bufSize)
|
2019-05-08 10:51:21 +03:00
|
|
|
|
2019-06-05 20:11:17 +03:00
|
|
|
if err = d.dev.Prepare(); err != nil {
|
2019-06-17 18:43:42 +03:00
|
|
|
return OpenError(err)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-06-13 17:05:52 +03:00
|
|
|
|
|
|
|
d.l.Log(logger.Debug, pkg+"successfully negotiated ALSA params")
|
2019-05-08 10:51:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// input continously records audio and writes it to the ringbuffer.
|
|
|
|
// Re-opens the device and tries again if ASLA returns an error.
|
2019-06-05 20:11:17 +03:00
|
|
|
func (d *Device) input() {
|
2019-05-08 10:51:21 +03:00
|
|
|
for {
|
2019-05-28 19:36:58 +03:00
|
|
|
// Check mode.
|
2019-06-05 20:11:17 +03:00
|
|
|
d.mu.Lock()
|
|
|
|
mode := d.mode
|
|
|
|
d.mu.Unlock()
|
2019-05-28 19:36:58 +03:00
|
|
|
switch mode {
|
2019-05-20 18:15:54 +03:00
|
|
|
case paused:
|
2019-06-05 20:11:17 +03:00
|
|
|
time.Sleep(time.Duration(d.RecPeriod) * time.Second)
|
2019-05-08 10:51:21 +03:00
|
|
|
continue
|
2019-05-20 18:15:54 +03:00
|
|
|
case stopped:
|
2019-06-05 20:11:17 +03:00
|
|
|
if d.dev != nil {
|
|
|
|
d.l.Log(logger.Debug, pkg+"closing audio device", "title", d.title)
|
|
|
|
d.dev.Close()
|
|
|
|
d.dev = nil
|
2019-05-28 19:36:58 +03:00
|
|
|
}
|
2019-05-22 08:26:58 +03:00
|
|
|
return
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-05-28 19:36:58 +03:00
|
|
|
|
|
|
|
// Read from audio device.
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"recording audio for period", "seconds", d.RecPeriod)
|
|
|
|
err := d.dev.Read(d.ab.Data)
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"read failed", "error", err.Error())
|
|
|
|
err = d.open() // re-open
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Fatal, pkg+"reopening device failed", "error", err.Error())
|
2019-05-28 19:36:58 +03:00
|
|
|
return
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Process audio.
|
2019-06-13 17:05:52 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"processing audio")
|
2019-06-05 20:11:17 +03:00
|
|
|
toWrite := d.formatBuffer()
|
2019-05-08 10:51:21 +03:00
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Write audio to ringbuffer.
|
2019-06-05 20:11:17 +03:00
|
|
|
n, err := d.rb.Write(toWrite.Data)
|
2019-05-08 10:51:21 +03:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Debug, pkg+"wrote audio to ringbuffer", "length", n)
|
2019-05-08 10:51:21 +03:00
|
|
|
case ring.ErrDropped:
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Warning, pkg+"old audio data overwritten")
|
2019-05-08 10:51:21 +03:00
|
|
|
default:
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Error, pkg+"unexpected ringbuffer error", "error", err.Error())
|
2019-05-08 10:51:21 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:39:55 +03:00
|
|
|
// Read reads from the ringbuffer, returning the number of bytes read upon success.
|
2019-06-13 17:12:26 +03:00
|
|
|
func (d *Device) Read(p []byte) (int, error) {
|
2019-05-28 19:36:58 +03:00
|
|
|
// Ready ringbuffer for read.
|
2019-06-13 17:12:26 +03:00
|
|
|
_, err := d.rb.Next(rbNextTimeout)
|
2019-06-13 17:05:52 +03:00
|
|
|
if err != nil {
|
2019-05-08 10:51:21 +03:00
|
|
|
return 0, err
|
|
|
|
}
|
2019-05-28 19:36:58 +03:00
|
|
|
|
|
|
|
// Read from ring buffer.
|
2019-06-18 12:33:38 +03:00
|
|
|
return d.rb.Read(p)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:01:35 +03:00
|
|
|
// formatBuffer returns audio that has been converted to the desired format.
|
2019-06-05 20:11:17 +03:00
|
|
|
func (d *Device) formatBuffer() alsa.Buffer {
|
2019-05-08 10:51:21 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// If nothing needs to be changed, return the original.
|
2019-06-05 20:11:17 +03:00
|
|
|
if d.ab.Format.Channels == d.Channels && d.ab.Format.Rate == d.SampleRate {
|
|
|
|
return d.ab
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-06-13 17:05:52 +03:00
|
|
|
var formatted alsa.Buffer
|
2019-06-05 20:11:17 +03:00
|
|
|
if d.ab.Format.Channels != d.Channels {
|
2019-05-08 10:51:21 +03:00
|
|
|
// Convert channels.
|
2019-05-28 19:36:58 +03:00
|
|
|
// TODO(Trek): Make this work for conversions other than stereo to mono.
|
2019-06-05 20:11:17 +03:00
|
|
|
if d.ab.Format.Channels == 2 && d.Channels == 1 {
|
2019-06-13 17:05:52 +03:00
|
|
|
formatted, err = pcm.StereoToMono(d.ab)
|
2019-05-28 19:36:58 +03:00
|
|
|
if err != nil {
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Fatal, pkg+"channel conversion failed", "error", err.Error())
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 20:11:17 +03:00
|
|
|
if d.ab.Format.Rate != d.SampleRate {
|
2019-05-08 10:51:21 +03:00
|
|
|
// Convert rate.
|
2019-06-13 17:05:52 +03:00
|
|
|
formatted, err = pcm.Resample(formatted, d.SampleRate)
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Fatal, pkg+"rate conversion failed", "error", err.Error())
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:50:19 +03:00
|
|
|
|
2019-06-05 20:11:17 +03:00
|
|
|
switch d.Codec {
|
2019-06-05 19:39:55 +03:00
|
|
|
case codecutil.PCM:
|
|
|
|
case codecutil.ADPCM:
|
2019-06-03 20:01:35 +03:00
|
|
|
b := bytes.NewBuffer(make([]byte, 0, adpcm.EncBytes(len(formatted.Data))))
|
|
|
|
enc := adpcm.NewEncoder(b)
|
|
|
|
_, err = enc.Write(formatted.Data)
|
|
|
|
if err != nil {
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Fatal, pkg+"unable to encode", "error", err.Error())
|
2019-06-03 20:01:35 +03:00
|
|
|
}
|
|
|
|
formatted.Data = b.Bytes()
|
2019-05-28 19:50:19 +03:00
|
|
|
default:
|
2019-06-05 20:11:17 +03:00
|
|
|
d.l.Log(logger.Error, pkg+"unhandled audio codec")
|
2019-05-28 19:50:19 +03:00
|
|
|
}
|
|
|
|
|
2019-05-08 10:51:21 +03:00
|
|
|
return formatted
|
|
|
|
}
|
2019-06-13 17:05:52 +03:00
|
|
|
|
|
|
|
// nearestPowerOfTwo finds and returns the nearest power of two to the given integer.
|
|
|
|
// If the lower and higher power of two are the same distance, it returns the higher power.
|
|
|
|
// For negative values, 1 is returned.
|
2019-06-18 12:33:38 +03:00
|
|
|
// Source: https://stackoverflow.com/a/45859570
|
2019-06-13 17:05:52 +03:00
|
|
|
func nearestPowerOfTwo(n int) int {
|
|
|
|
if n <= 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if n == 1 {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
v := n
|
|
|
|
v--
|
|
|
|
v |= v >> 1
|
|
|
|
v |= v >> 2
|
|
|
|
v |= v >> 4
|
|
|
|
v |= v >> 8
|
|
|
|
v |= v >> 16
|
|
|
|
v++ // higher power of 2
|
|
|
|
x := v >> 1 // lower power of 2
|
|
|
|
if (v - n) > (n - x) {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|