package revid import ( "testing" "github.com/yobert/alsa" ) func TestAudioDevice(t *testing.T) { // We want to open a device with a standard configuration. ac := &AudioConfig{ SampleRate: 8000, Channels: 1, RecPeriod: 1, BitDepth: 16, Codec: ADPCM, } // Check that a device exists with the desired parameters. cards, err := alsa.OpenCards() if err != nil { t.Skip("skipping, no audio card found") } 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 { t.Skip("skipping, no suitable audio device found") } _, err = testDev.NegotiateChannels(2) if err != nil { t.Skip("skipping, no suitable audio device found") } foundRate := false for i := 0; i < len(rates) && !foundRate; i++ { if rates[i] < ac.SampleRate { continue } if rates[i]%ac.SampleRate == 0 { _, err = testDev.NegotiateRate(rates[i]) if err == nil { foundRate = true } } } if !foundRate { _, err = testDev.NegotiateRate(defaultSampleRate) if err != nil { t.Skip("skipping, no suitable audio device found") } } _, err = testDev.NegotiateFormat(alsa.S16_LE) if err != nil { t.Skip("skipping, no suitable audio device found") } _, err = testDev.NegotiateBufferSize(8192, 16384) if err != nil { t.Skip("skipping, no suitable audio device found") } if err = testDev.Prepare(); err != nil { t.Skip("skipping, no suitable audio device found") } testDev.Close() ai := NewAudioDevice(ac) ai.Start() ai.Stop() }