distinguish error numbers from codes

This commit is contained in:
Brandon Taylor 2021-06-21 09:31:53 -04:00
parent 12e1c2cde8
commit c61aeb8365
2 changed files with 24 additions and 42 deletions

View file

@ -76,26 +76,22 @@ function is_xrun(error_code)
error_code == paOutputUnderflowed || error_code == paInputOverflowed error_code == paOutputUnderflowed || error_code == paInputOverflowed
end end
function is_xrun(number::Integer) function get_error_text(error_number)
is_xrun(PaErrorCode(number)) unsafe_string(@locked Pa_GetErrorText(error_number))
end
function get_error_text(error_code)
unsafe_string(@locked Pa_GetErrorText(error_code))
end end
# General utility function to handle the status from the Pa_* functions # General utility function to handle the status from the Pa_* functions
function handle_status(err; warn_xruns::Bool = true) function handle_status(error_number; warn_xruns::Bool = true)
if Int(err) < 0 if error_number < 0
if is_xrun(err) if is_xrun(PaErrorCode(error_number))
if warn_xruns if warn_xruns
@warn("libportaudio: " * get_error_text(err)) @warn("libportaudio: " * get_error_text(error_number))
end end
else else
throw(ErrorException("libportaudio: " * get_error_text(err))) throw(ErrorException("libportaudio: " * get_error_text(error_number)))
end end
end end
err error_number
end end
macro stderr_as_debug(expression) macro stderr_as_debug(expression)
@ -525,8 +521,8 @@ function interleave!(long, wide, n, already, offset, wide_to_long)
end end
end end
function handle_xrun(stream, error_code, recover_xruns) function handle_xrun(stream, error_number, recover_xruns)
if recover_xruns && is_xrun(error_code) if recover_xruns && is_xrun(PaErrorCode(error_number))
recover_xrun(stream) recover_xrun(stream)
end end
end end

View file

@ -20,6 +20,7 @@ using PortAudio:
terminate terminate
using PortAudio.LibPortAudio: using PortAudio.LibPortAudio:
Pa_AbortStream, Pa_AbortStream,
PaError,
PaErrorCode, PaErrorCode,
paFloat32, paFloat32,
Pa_GetDefaultHostApi, Pa_GetDefaultHostApi,
@ -30,6 +31,7 @@ using PortAudio.LibPortAudio:
Pa_GetStreamInfo, Pa_GetStreamInfo,
Pa_GetStreamTime, Pa_GetStreamTime,
Pa_HostApiDeviceIndexToDeviceIndex, Pa_HostApiDeviceIndexToDeviceIndex,
paHostApiNotFound,
Pa_HostApiTypeIdToHostApiIndex, Pa_HostApiTypeIdToHostApiIndex,
PaHostErrorInfo, PaHostErrorInfo,
paInDevelopment, paInDevelopment,
@ -80,40 +82,24 @@ if !isempty(devices())
initialize() initialize()
@testset "libportaudio" begin @testset "libportaudio" begin
@test handle_status(Pa_GetHostApiCount()) isa Integer @test handle_status(Pa_GetHostApiCount()) >= 0
@test handle_status(Pa_GetDefaultHostApi()) isa Integer @test handle_status(Pa_GetDefaultHostApi()) >= 0
@test_throws ErrorException handle_status( @test PaErrorCode(Pa_HostApiTypeIdToHostApiIndex(paInDevelopment)) ==
Pa_HostApiTypeIdToHostApiIndex(paInDevelopment), paHostApiNotFound
)
@test Pa_HostApiDeviceIndexToDeviceIndex(paInDevelopment, 0) == 0 @test Pa_HostApiDeviceIndexToDeviceIndex(paInDevelopment, 0) == 0
safe_load(Pa_GetLastHostErrorInfo(), ErrorException("no info")) isa PaHostErrorInfo @test safe_load(Pa_GetLastHostErrorInfo(), ErrorException("no info")) isa PaHostErrorInfo
@test PaErrorCode(Pa_IsFormatSupported(C_NULL, C_NULL, 0.0)) == paInvalidDevice
@test PaErrorCode( @test PaErrorCode(
Pa_IsFormatSupported( Pa_OpenDefaultStream(Ref(C_NULL), 0, 0, paFloat32, 0.0, 0, C_NULL, C_NULL),
Ptr{PaStreamParameters}(0),
Ptr{PaStreamParameters}(0),
0.0,
),
) == paInvalidDevice ) == paInvalidDevice
@test PaErrorCode( stream = PortAudioStream(2, 2)
Pa_OpenDefaultStream(
Ref{Ptr{PaStream}}(0),
0,
0,
paFloat32,
0.0,
0,
C_NULL,
C_NULL,
),
) == paInvalidDevice
stream = PortAudioStream(0, 2)
pointer = stream.pointer_ref[] pointer = stream.pointer_ref[]
@test !(Bool(handle_status(Pa_IsStreamStopped(pointer)))) @test !(Bool(handle_status(Pa_IsStreamStopped(pointer))))
@test Bool(handle_status(Pa_IsStreamActive(pointer))) @test Bool(handle_status(Pa_IsStreamActive(pointer)))
@test safe_load(Pa_GetStreamInfo(pointer), ErrorException("no info")) isa @test safe_load(Pa_GetStreamInfo(pointer), ErrorException("no info")) isa
PaStreamInfo PaStreamInfo
Pa_GetStreamTime(pointer) @test Pa_GetStreamTime(pointer) >= 0
Pa_GetStreamCpuLoad(pointer) @test Pa_GetStreamCpuLoad(pointer) >= 0
@test PaErrorCode(handle_status(Pa_AbortStream(pointer))) == paNoError @test PaErrorCode(handle_status(Pa_AbortStream(pointer))) == paNoError
@test PaErrorCode(handle_status(Pa_SetStreamFinishedCallback(pointer, C_NULL))) == @test PaErrorCode(handle_status(Pa_SetStreamFinishedCallback(pointer, C_NULL))) ==
paNoError paNoError
@ -197,10 +183,10 @@ if !isempty(devices())
@test_throws ErrorException PortAudioStream("foobarbaz") @test_throws ErrorException PortAudioStream("foobarbaz")
@test_throws ErrorException PortAudioStream(default_indev, "foobarbaz") @test_throws ErrorException PortAudioStream(default_indev, "foobarbaz")
@test_logs (:warn, "libportaudio: Output underflowed") handle_status( @test_logs (:warn, "libportaudio: Output underflowed") handle_status(
paOutputUnderflowed, PaError(paOutputUnderflowed)
) )
@test_throws ErrorException("libportaudio: PortAudio not initialized") handle_status( @test_throws ErrorException("libportaudio: PortAudio not initialized") handle_status(
paNotInitialized, PaError(paNotInitialized)
) )
@test_throws ErrorException(""" @test_throws ErrorException("""
Could not find ALSA config directory. Searched: Could not find ALSA config directory. Searched: