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-06-14 13:19:49 +03:00
|
|
|
"io/ioutil"
|
2019-06-04 05:58:40 +03:00
|
|
|
"os"
|
2019-06-14 13:19:49 +03:00
|
|
|
"strconv"
|
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-09 05:41:02 +03:00
|
|
|
)
|
2019-05-08 10:51:21 +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-06-05 19:39:55 +03:00
|
|
|
// Create a new audio Device, start, read/lex, and then stop it.
|
2019-10-23 05:29:12 +03:00
|
|
|
l := logger.New(logger.Debug, os.Stderr, true)
|
2019-06-05 19:39:55 +03:00
|
|
|
ai, err := NewDevice(ac, l)
|
2019-06-17 18:43:42 +03:00
|
|
|
// If there was an error opening the device, skip this test.
|
|
|
|
if _, ok := err.(OpenError); ok {
|
|
|
|
t.Skip(err)
|
|
|
|
}
|
|
|
|
// For any other error, report it.
|
2019-05-28 19:36:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = ai.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-06-18 10:54:32 +03:00
|
|
|
chunkSize := ai.ChunkSize()
|
|
|
|
lexer := codecutil.NewByteLexer(&chunkSize)
|
|
|
|
go lexer.Lex(ioutil.Discard, ai, time.Duration(ac.RecPeriod*float64(time.Second)))
|
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
|
|
|
}
|
2019-06-13 17:05:52 +03:00
|
|
|
|
2019-06-14 13:19:49 +03:00
|
|
|
var powerTests = []struct {
|
|
|
|
in int
|
|
|
|
out int
|
|
|
|
}{
|
|
|
|
{36, 32},
|
|
|
|
{47, 32},
|
|
|
|
{3, 4},
|
|
|
|
{46, 32},
|
|
|
|
{7, 8},
|
|
|
|
{2, 2},
|
|
|
|
{36, 32},
|
|
|
|
{757, 512},
|
|
|
|
{2464, 2048},
|
|
|
|
{18980, 16384},
|
|
|
|
{70000, 65536},
|
|
|
|
{8192, 8192},
|
|
|
|
{2048, 2048},
|
|
|
|
{65536, 65536},
|
|
|
|
{-2048, 1},
|
|
|
|
{-127, 1},
|
|
|
|
{-1, 1},
|
|
|
|
{0, 1},
|
|
|
|
{1, 2},
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:05:52 +03:00
|
|
|
func TestNearestPowerOfTwo(t *testing.T) {
|
2019-06-14 13:19:49 +03:00
|
|
|
for _, tt := range powerTests {
|
|
|
|
t.Run(strconv.Itoa(tt.in), func(t *testing.T) {
|
|
|
|
v := nearestPowerOfTwo(tt.in)
|
|
|
|
if v != tt.out {
|
|
|
|
t.Errorf("got %v, want %v", v, tt.out)
|
|
|
|
}
|
|
|
|
})
|
2019-06-13 17:05:52 +03:00
|
|
|
}
|
|
|
|
}
|