2019-06-05 19:39:55 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
audio_test.go
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This file 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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
package audio
|
2019-05-08 10:51:21 +03:00
|
|
|
|
|
|
|
import (
|
2019-05-22 08:26:58 +03:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2019-06-04 05:58:40 +03:00
|
|
|
"os"
|
2019-05-08 10:51:21 +03:00
|
|
|
"testing"
|
2019-05-22 08:26:58 +03:00
|
|
|
"time"
|
2019-05-08 10:51:21 +03:00
|
|
|
|
2019-06-03 12:05:28 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/codecutil"
|
2019-06-05 19:39:55 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2019-05-21 06:09:10 +03:00
|
|
|
"github.com/yobert/alsa"
|
2019-05-09 05:41:02 +03:00
|
|
|
)
|
2019-05-08 10:51:21 +03:00
|
|
|
|
2019-05-22 08:26:58 +03:00
|
|
|
// Check that a device exists with the given config parameters.
|
2019-06-05 19:39:55 +03:00
|
|
|
func checkDevice(ac *Config) error {
|
2019-05-21 06:09:10 +03:00
|
|
|
cards, err := alsa.OpenCards()
|
|
|
|
if err != nil {
|
2019-05-22 08:26:58 +03:00
|
|
|
return errors.New("no audio cards found")
|
2019-05-21 06:09:10 +03:00
|
|
|
}
|
|
|
|
defer alsa.CloseCards(cards)
|
|
|
|
var testDev *alsa.Device
|
|
|
|
for _, card := range cards {
|
|
|
|
devices, err := card.Devices()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, dev := range devices {
|
|
|
|
if dev.Type != alsa.PCM || !dev.Record {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
testDev = dev
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if testDev == nil {
|
2019-05-22 08:26:58 +03:00
|
|
|
return errors.New("no suitable device found")
|
|
|
|
}
|
|
|
|
err = testDev.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-21 06:09:10 +03:00
|
|
|
}
|
|
|
|
_, err = testDev.NegotiateChannels(2)
|
2019-05-09 05:41:02 +03:00
|
|
|
if err != nil {
|
2019-05-22 08:26:58 +03:00
|
|
|
return err
|
2019-05-21 06:09:10 +03:00
|
|
|
}
|
|
|
|
foundRate := false
|
2019-05-22 17:49:44 +03:00
|
|
|
for i := 0; i < len(Rates) && !foundRate; i++ {
|
|
|
|
if Rates[i] < ac.SampleRate {
|
2019-05-21 06:09:10 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-05-22 17:49:44 +03:00
|
|
|
if Rates[i]%ac.SampleRate == 0 {
|
|
|
|
_, err = testDev.NegotiateRate(Rates[i])
|
2019-05-21 06:09:10 +03:00
|
|
|
if err == nil {
|
|
|
|
foundRate = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundRate {
|
|
|
|
_, err = testDev.NegotiateRate(defaultSampleRate)
|
|
|
|
if err != nil {
|
2019-05-22 08:26:58 +03:00
|
|
|
return err
|
2019-05-21 06:09:10 +03:00
|
|
|
}
|
2019-05-09 05:41:02 +03:00
|
|
|
}
|
2019-05-22 08:26:58 +03:00
|
|
|
var aFmt alsa.FormatType
|
|
|
|
switch ac.BitDepth {
|
|
|
|
case 16:
|
|
|
|
aFmt = alsa.S16_LE
|
|
|
|
case 32:
|
|
|
|
aFmt = alsa.S32_LE
|
|
|
|
default:
|
|
|
|
return errors.New("unsupported bitdepth")
|
|
|
|
}
|
|
|
|
_, err = testDev.NegotiateFormat(aFmt)
|
2019-05-21 06:09:10 +03:00
|
|
|
if err != nil {
|
2019-05-22 08:26:58 +03:00
|
|
|
return err
|
2019-05-21 06:09:10 +03:00
|
|
|
}
|
|
|
|
_, err = testDev.NegotiateBufferSize(8192, 16384)
|
|
|
|
if err != nil {
|
2019-05-22 08:26:58 +03:00
|
|
|
return err
|
2019-05-21 06:09:10 +03:00
|
|
|
}
|
|
|
|
if err = testDev.Prepare(); err != nil {
|
2019-05-22 08:26:58 +03:00
|
|
|
return err
|
2019-05-21 06:09:10 +03:00
|
|
|
}
|
2019-05-22 08:26:58 +03:00
|
|
|
if testDev != nil {
|
|
|
|
testDev.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-21 06:09:10 +03:00
|
|
|
|
2019-06-05 19:39:55 +03:00
|
|
|
func TestDevice(t *testing.T) {
|
2019-05-22 08:26:58 +03:00
|
|
|
// We want to open a device with a standard configuration.
|
2019-06-05 19:39:55 +03:00
|
|
|
ac := &Config{
|
2019-05-22 08:26:58 +03:00
|
|
|
SampleRate: 8000,
|
|
|
|
Channels: 1,
|
2019-06-05 19:39:55 +03:00
|
|
|
RecPeriod: 0.3,
|
2019-05-22 08:26:58 +03:00
|
|
|
BitDepth: 16,
|
2019-06-05 19:39:55 +03:00
|
|
|
Codec: codecutil.ADPCM,
|
2019-05-22 08:26:58 +03:00
|
|
|
}
|
2019-06-03 20:01:35 +03:00
|
|
|
n := 2 // Number of periods to wait while recording.
|
2019-05-21 06:09:10 +03:00
|
|
|
|
2019-05-22 08:26:58 +03:00
|
|
|
// Skip if there are no suitable devices to test with.
|
|
|
|
err := checkDevice(ac)
|
|
|
|
if err != nil {
|
2019-06-04 05:58:40 +03:00
|
|
|
t.Skip(err)
|
2019-05-22 08:26:58 +03:00
|
|
|
}
|
2019-05-08 11:37:33 +03:00
|
|
|
|
2019-06-05 19:39:55 +03:00
|
|
|
// Create a new audio Device, start, read/lex, and then stop it.
|
|
|
|
l := logger.New(logger.Debug, os.Stderr)
|
|
|
|
ai, err := NewDevice(ac, l)
|
2019-05-28 19:36:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-05-22 17:49:44 +03:00
|
|
|
dst := bytes.NewBuffer(make([]byte, 0))
|
2019-05-28 19:36:58 +03:00
|
|
|
err = ai.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-06-03 12:05:28 +03:00
|
|
|
go codecutil.LexBytes(dst, ai, time.Duration(ac.RecPeriod*float64(time.Second)), ai.ChunkSize())
|
2019-06-05 19:39:55 +03:00
|
|
|
time.Sleep(time.Duration(ac.RecPeriod*float64(time.Second)) * time.Duration(n))
|
2019-05-21 06:09:10 +03:00
|
|
|
ai.Stop()
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|