From 06c6fd04959975644e308b23138313bfcfde4765 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Sun, 24 Jul 2022 11:54:55 -0400 Subject: [PATCH] fix bug, test --- src/PortAudio.jl | 15 +++++++++++++-- test/runtests.jl | 8 ++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/PortAudio.jl b/src/PortAudio.jl index 7e9e93f..e48f00e 100644 --- a/src/PortAudio.jl +++ b/src/PortAudio.jl @@ -48,6 +48,7 @@ using .LibPortAudio: Pa_Initialize, paInputOverflowed, Pa_IsStreamStopped, + paNoDevice, paNoFlag, Pa_OpenStream, paOutputUnderflowed, @@ -195,12 +196,22 @@ function show(io::IO, device::PortAudioDevice) print(io, device.output_bounds.max_channels) end +function check_device_exists(device_index, device_type) + if device_index == paNoDevice + throw(ArgumentError("No $device_type device available")) + end +end + function get_default_input_index() - handle_status(Pa_GetDefaultInputDevice()) + device_index = Pa_GetDefaultInputDevice() + check_device_exists(device_index, "input") + device_index end function get_default_output_index() - handle_status(Pa_GetDefaultOutputDevice()) + device_index = Pa_GetDefaultOutputDevice() + check_device_exists(device_index, "output") + device_index end # we can look up devices by index or name diff --git a/test/runtests.jl b/test/runtests.jl index a597519..9933c68 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,6 +11,7 @@ using PortAudio: get_output_type, handle_status, initialize, + name, PortAudioException, PortAudio, PortAudioDevice, @@ -18,7 +19,7 @@ using PortAudio: safe_load, seek_alsa_conf, terminate, - name + write_buffer using PortAudio.LibPortAudio: Pa_AbortStream, PaError, @@ -109,7 +110,9 @@ using Test: @test, @test_logs, @test_nowarn, @testset, @test_throws initialize() end -if !isempty(devices()) +if isempty(devices()) + @test_throws ArgumentError("No input device available") get_default_input_index() +else @testset "Tests with sound" begin # these default values are specific to local machines input_name = get_device(get_default_input_index()).name @@ -130,6 +133,7 @@ if !isempty(devices()) sleep(1) println("Testing pass-through") stream = PortAudioStream(input_name, output_name, 2, 2; adjust_channels = true) + write_buffer(stream.sink_messenger.buffer, acquire_lock = false) sink = stream.sink source = stream.source @test sprint(show, stream) == """