2019-05-08 13:01:25 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
audio-input.go
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
audio-input.go 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 [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
2019-05-08 10:51:21 +03:00
|
|
|
package revid
|
|
|
|
|
|
|
|
import (
|
2019-06-03 20:01:35 +03:00
|
|
|
"bytes"
|
2019-05-08 10:51:21 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/yobert/alsa"
|
|
|
|
|
2019-06-03 20:01:35 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/adpcm"
|
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 (
|
|
|
|
rbTimeout = 100 * time.Millisecond
|
|
|
|
rbNextTimeout = 100 * time.Millisecond
|
2019-05-22 08:26:58 +03:00
|
|
|
rbLen = 200
|
2019-05-08 10:51:21 +03:00
|
|
|
)
|
|
|
|
|
2019-05-20 18:15:54 +03:00
|
|
|
const (
|
|
|
|
running = iota
|
|
|
|
paused
|
|
|
|
stopped
|
|
|
|
)
|
|
|
|
|
2019-05-22 17:49:44 +03:00
|
|
|
// Rates contains the audio sample rates used by revid.
|
|
|
|
var Rates = [8]int{8000, 16000, 32000, 44100, 48000, 88200, 96000, 192000}
|
2019-05-21 06:09:10 +03:00
|
|
|
|
2019-05-22 17:49:44 +03:00
|
|
|
// AudioDevice holds everything we need to know about the audio input stream.
|
|
|
|
type AudioDevice struct {
|
2019-05-22 18:53:51 +03:00
|
|
|
l Logger
|
2019-05-15 09:27:49 +03:00
|
|
|
mu sync.Mutex
|
|
|
|
source string // Name of audio source, or empty for the default source.
|
2019-05-28 19:36:58 +03:00
|
|
|
// Operating mode, either running, paused, or stopped.
|
|
|
|
// "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.
|
|
|
|
mode uint8
|
2019-05-08 10:51:21 +03:00
|
|
|
|
2019-05-15 09:27:49 +03:00
|
|
|
dev *alsa.Device // Audio input device.
|
|
|
|
ab alsa.Buffer // ALSA's buffer.
|
|
|
|
rb *ring.Buffer // Our buffer.
|
|
|
|
chunkSize int // This is the number of bytes that will be stored at a time.
|
2019-05-08 10:51:21 +03:00
|
|
|
|
|
|
|
*AudioConfig
|
|
|
|
}
|
|
|
|
|
2019-05-22 17:49:44 +03:00
|
|
|
// AudioConfig provides parameters used by AudioDevice.
|
2019-05-08 10:51:21 +03:00
|
|
|
type AudioConfig struct {
|
|
|
|
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-04 05:58:40 +03:00
|
|
|
// NewAudioDevice initializes and returns an AudioDevice which can be started, read from, and stopped.
|
|
|
|
func NewAudioDevice(cfg *AudioConfig, l Logger) (*AudioDevice, error) {
|
2019-05-22 18:53:51 +03:00
|
|
|
a := &AudioDevice{}
|
|
|
|
a.AudioConfig = cfg
|
2019-06-04 05:58:40 +03:00
|
|
|
a.l = l
|
2019-05-08 10:51:21 +03:00
|
|
|
|
|
|
|
// Open the requested audio device.
|
|
|
|
err := a.open()
|
|
|
|
if err != nil {
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Error, "failed to open audio device", "error", err.Error())
|
|
|
|
return nil, errors.New("failed to open audio device")
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Setup ring buffer to capture audio in periods of a.RecPeriod seconds and buffer rbDuration seconds in total.
|
2019-05-22 17:49:44 +03:00
|
|
|
a.ab = a.dev.NewBufferDuration(time.Duration(a.RecPeriod * float64(time.Second)))
|
|
|
|
cs := (float64((len(a.ab.Data)/a.dev.BufferFormat().Channels)*a.Channels) / float64(a.dev.BufferFormat().Rate)) * float64(a.SampleRate)
|
|
|
|
if cs < 1 {
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Error, "given AudioConfig parameters are too small", "error", err.Error())
|
|
|
|
return nil, errors.New("given AudioConfig parameters are too small")
|
2019-05-22 17:49:44 +03:00
|
|
|
}
|
2019-06-03 20:01:35 +03:00
|
|
|
if a.Codec == ADPCM {
|
|
|
|
a.chunkSize = adpcm.EncBytes(int(cs))
|
|
|
|
} else {
|
|
|
|
a.chunkSize = int(cs)
|
|
|
|
}
|
2019-05-08 10:51:21 +03:00
|
|
|
a.rb = ring.NewBuffer(rbLen, a.chunkSize, rbTimeout)
|
2019-05-28 19:36:58 +03:00
|
|
|
|
2019-05-20 18:15:54 +03:00
|
|
|
a.mode = paused
|
2019-05-28 19:36:58 +03:00
|
|
|
go a.input()
|
2019-05-08 10:51:21 +03:00
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
return a, 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.
|
|
|
|
func (a *AudioDevice) Start() error {
|
2019-05-15 09:27:49 +03:00
|
|
|
a.mu.Lock()
|
2019-05-28 19:36:58 +03:00
|
|
|
mode := a.mode
|
|
|
|
a.mu.Unlock()
|
|
|
|
switch mode {
|
2019-05-20 18:15:54 +03:00
|
|
|
case paused:
|
2019-05-28 19:36:58 +03:00
|
|
|
a.mu.Lock()
|
2019-05-22 08:26:58 +03:00
|
|
|
a.mode = running
|
2019-05-28 19:36:58 +03:00
|
|
|
a.mu.Unlock()
|
|
|
|
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:
|
|
|
|
return errors.New("invalid mode")
|
2019-05-15 09:27:49 +03:00
|
|
|
}
|
2019-05-28 19:36:58 +03:00
|
|
|
return nil
|
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-05-22 17:49:44 +03:00
|
|
|
func (a *AudioDevice) Stop() {
|
2019-05-20 18:15:54 +03:00
|
|
|
a.mu.Lock()
|
2019-05-22 08:26:58 +03:00
|
|
|
a.mode = stopped
|
|
|
|
a.mu.Unlock()
|
2019-05-15 09:27:49 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// ChunkSize returns the number of bytes written to the ringbuffer per a.RecPeriod.
|
2019-05-22 17:49:44 +03:00
|
|
|
func (a *AudioDevice) ChunkSize() int {
|
2019-05-08 10:51:21 +03:00
|
|
|
return a.chunkSize
|
|
|
|
}
|
|
|
|
|
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-05-22 17:49:44 +03:00
|
|
|
func (a *AudioDevice) open() error {
|
2019-05-28 19:36:58 +03:00
|
|
|
// Close any existing device.
|
2019-05-08 10:51:21 +03:00
|
|
|
if a.dev != nil {
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Debug, "closing device", "source", a.source)
|
2019-05-08 10:51:21 +03:00
|
|
|
a.dev.Close()
|
|
|
|
a.dev = nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Open sound card and open recording device.
|
|
|
|
a.l.Log(logger.Debug, "opening sound card")
|
2019-05-08 10:51:21 +03:00
|
|
|
cards, err := alsa.OpenCards()
|
|
|
|
if err != nil {
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Debug, "failed to open sound card")
|
2019-05-08 10:51:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer alsa.CloseCards(cards)
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Debug, "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
|
|
|
|
}
|
|
|
|
if dev.Title == a.source || a.source == "" {
|
|
|
|
a.dev = dev
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.dev == nil {
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Debug, "failed to find audio device")
|
|
|
|
return errors.New("no audio device found")
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Debug, "opening audio device", "source", a.dev.Title)
|
2019-05-08 10:51:21 +03:00
|
|
|
err = a.dev.Open()
|
|
|
|
if err != nil {
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Debug, "failed to open audio device")
|
2019-05-08 10:51:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2 channels is what most devices need to record in. If mono is requested,
|
|
|
|
// the recording will be converted in formatBuffer().
|
|
|
|
_, err = a.dev.NegotiateChannels(2)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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,
|
|
|
|
// a fix for this is to remove 8000 and 16000 from the Rates slice.
|
2019-05-08 10:51:21 +03:00
|
|
|
foundRate := false
|
2019-05-22 17:49:44 +03:00
|
|
|
for i := 0; i < len(Rates) && !foundRate; i++ {
|
|
|
|
if Rates[i] < a.SampleRate {
|
2019-05-08 10:51:21 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-05-22 17:49:44 +03:00
|
|
|
if Rates[i]%a.SampleRate == 0 {
|
|
|
|
_, err = a.dev.NegotiateRate(Rates[i])
|
2019-05-08 10:51:21 +03:00
|
|
|
if err == nil {
|
|
|
|
foundRate = true
|
2019-05-22 18:53:51 +03:00
|
|
|
a.l.Log(logger.Debug, "Sample rate set", "rate", Rates[i])
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no easily divisible rate is found, then use the default rate.
|
|
|
|
if !foundRate {
|
2019-05-22 18:53:51 +03:00
|
|
|
a.l.Log(logger.Warning, "Unable to sample at requested rate, default used.", "rateRequested", a.SampleRate)
|
2019-05-08 10:51:21 +03:00
|
|
|
_, err = a.dev.NegotiateRate(defaultSampleRate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-22 18:53:51 +03:00
|
|
|
a.l.Log(logger.Debug, "Sample rate set", "rate", defaultSampleRate)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:41:02 +03:00
|
|
|
var aFmt alsa.FormatType
|
2019-05-08 10:51:21 +03:00
|
|
|
switch a.BitDepth {
|
|
|
|
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-05-15 09:27:49 +03:00
|
|
|
return fmt.Errorf("unsupported sample bits %v", a.BitDepth)
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-05-09 05:41:02 +03:00
|
|
|
_, err = a.dev.NegotiateFormat(aFmt)
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either 8192 or 16384 bytes is a reasonable ALSA buffer size.
|
|
|
|
_, err = a.dev.NegotiateBufferSize(8192, 16384)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = a.dev.Prepare(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-22 18:53:51 +03:00
|
|
|
a.l.Log(logger.Debug, "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-05-22 17:49:44 +03:00
|
|
|
func (a *AudioDevice) input() {
|
2019-05-08 10:51:21 +03:00
|
|
|
for {
|
2019-05-28 19:36:58 +03:00
|
|
|
// Check mode.
|
2019-05-08 10:51:21 +03:00
|
|
|
a.mu.Lock()
|
2019-05-28 19:36:58 +03:00
|
|
|
mode := a.mode
|
|
|
|
a.mu.Unlock()
|
|
|
|
switch mode {
|
2019-05-20 18:15:54 +03:00
|
|
|
case paused:
|
2019-05-08 10:51:21 +03:00
|
|
|
time.Sleep(time.Duration(a.RecPeriod) * time.Second)
|
|
|
|
continue
|
2019-05-20 18:15:54 +03:00
|
|
|
case stopped:
|
2019-05-28 19:36:58 +03:00
|
|
|
if a.dev != nil {
|
|
|
|
a.l.Log(logger.Debug, "closing audio device", "source", a.source)
|
|
|
|
a.dev.Close()
|
|
|
|
a.dev = nil
|
|
|
|
}
|
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.
|
|
|
|
a.l.Log(logger.Debug, "recording audio for period", "seconds", a.RecPeriod)
|
2019-05-08 10:51:21 +03:00
|
|
|
err := a.dev.Read(a.ab.Data)
|
|
|
|
if err != nil {
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Debug, "read failed", "error", err.Error())
|
2019-05-08 10:51:21 +03:00
|
|
|
err = a.open() // re-open
|
|
|
|
if err != nil {
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Fatal, "reopening device failed", "error", err.Error())
|
|
|
|
return
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Process audio.
|
|
|
|
a.l.Log(logger.Debug, "processing audio")
|
2019-05-08 10:51:21 +03:00
|
|
|
toWrite := a.formatBuffer()
|
|
|
|
|
2019-05-28 19:36:58 +03:00
|
|
|
// Write audio to ringbuffer.
|
|
|
|
n, err := a.rb.Write(toWrite.Data)
|
2019-05-08 10:51:21 +03:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Debug, "wrote audio to ringbuffer", "length", n)
|
2019-05-08 10:51:21 +03:00
|
|
|
case ring.ErrDropped:
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Warning, "old audio data overwritten")
|
2019-05-08 10:51:21 +03:00
|
|
|
default:
|
2019-05-28 19:36:58 +03:00
|
|
|
a.l.Log(logger.Error, "unexpected ringbuffer error", "error", err.Error())
|
2019-05-08 10:51:21 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:26:02 +03:00
|
|
|
// Read reads a full PCM chunk from the ringbuffer, returning the number of bytes read upon success.
|
2019-05-08 10:51:21 +03:00
|
|
|
// Any errors returned are unexpected and should be considered fatal.
|
2019-05-22 17:49:44 +03:00
|
|
|
func (a *AudioDevice) Read(p []byte) (n int, err error) {
|
2019-05-28 19:36:58 +03:00
|
|
|
// Ready ringbuffer for read.
|
|
|
|
_, err = a.rb.Next(rbNextTimeout)
|
2019-05-08 10:51:21 +03:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case ring.ErrTimeout:
|
|
|
|
return 0, nil
|
|
|
|
default:
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-05-28 19:36:58 +03:00
|
|
|
|
|
|
|
// Read from ring buffer.
|
|
|
|
n, err = a.rb.Read(p)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case io.EOF:
|
|
|
|
return 0, nil
|
|
|
|
default:
|
|
|
|
return 0, err
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
2019-05-28 19:36:58 +03:00
|
|
|
|
2019-05-08 10:51:21 +03:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:01:35 +03:00
|
|
|
// formatBuffer returns audio that has been converted to the desired format.
|
2019-05-22 17:49:44 +03:00
|
|
|
func (a *AudioDevice) formatBuffer() alsa.Buffer {
|
2019-05-08 10:51:21 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// If nothing needs to be changed, return the original.
|
2019-05-28 19:50:19 +03:00
|
|
|
if a.ab.Format.Channels == a.Channels && a.ab.Format.Rate == a.SampleRate {
|
2019-05-08 10:51:21 +03:00
|
|
|
return a.ab
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:01:35 +03:00
|
|
|
formatted := alsa.Buffer{Format: a.ab.Format, Data: a.ab.Data}
|
2019-05-28 19:50:19 +03:00
|
|
|
if a.ab.Format.Channels != a.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-05-28 19:50:19 +03:00
|
|
|
if a.ab.Format.Channels == 2 && a.Channels == 1 {
|
2019-05-28 19:36:58 +03:00
|
|
|
formatted.Data, err = pcm.StereoToMono(a.ab)
|
|
|
|
if err != nil {
|
2019-06-03 20:01:35 +03:00
|
|
|
a.l.Log(logger.Fatal, "channel conversion failed", "error", err.Error())
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:50:19 +03:00
|
|
|
if a.ab.Format.Rate != a.SampleRate {
|
2019-05-08 10:51:21 +03:00
|
|
|
// Convert rate.
|
2019-06-03 20:01:35 +03:00
|
|
|
formatted.Data, err = pcm.Resample(formatted, a.SampleRate)
|
2019-05-08 10:51:21 +03:00
|
|
|
if err != nil {
|
2019-06-03 20:01:35 +03:00
|
|
|
a.l.Log(logger.Fatal, "rate conversion failed", "error", err.Error())
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:50:19 +03:00
|
|
|
|
|
|
|
switch a.Codec {
|
|
|
|
case PCM:
|
|
|
|
case 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 {
|
|
|
|
a.l.Log(logger.Fatal, "unable to encode", "error", err.Error())
|
|
|
|
}
|
|
|
|
formatted.Data = b.Bytes()
|
2019-05-28 19:50:19 +03:00
|
|
|
default:
|
|
|
|
a.l.Log(logger.Error, "codec conversion failed, audio has remained original codec", "error", err.Error())
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:51:21 +03:00
|
|
|
return formatted
|
|
|
|
}
|