av/device/alsa/alsa_test.go

109 lines
2.4 KiB
Go

/*
NAME
alsa_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 http://www.gnu.org/licenses.
*/
package alsa
import (
"io/ioutil"
"os"
"strconv"
"testing"
"time"
"bitbucket.org/ausocean/av/codec/codecutil"
"bitbucket.org/ausocean/av/codec/pcm"
"bitbucket.org/ausocean/av/revid/config"
"bitbucket.org/ausocean/utils/logger"
)
func TestDevice(t *testing.T) {
// We want to open a device with a standard configuration.
c := config.Config{
SampleRate: 8000,
Channels: 1,
RecPeriod: 0.3,
BitDepth: 16,
InputCodec: codecutil.ADPCM,
}
n := 2 // Number of periods to wait while recording.
// Create a new ALSA device, start, read/lex, and then stop it.
l := logger.New(logger.Debug, os.Stderr, true)
ai := New(l)
err := ai.Set(c)
// 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.
if err != nil {
t.Error(err)
}
err = ai.Start()
if err != nil {
t.Error(err)
}
cs := pcm.DataSize(c.SampleRate, c.Channels, c.BitDepth, c.RecPeriod, c.InputCodec)
lexer, err := codecutil.NewByteLexer(cs)
go lexer.Lex(ioutil.Discard, ai, time.Duration(c.RecPeriod*float64(time.Second)))
time.Sleep(time.Duration(c.RecPeriod*float64(time.Second)) * time.Duration(n))
ai.Stop()
}
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},
}
func TestNearestPowerOfTwo(t *testing.T) {
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)
}
})
}
}