Merge pull request #71 from bramtayl/handle_null

handle C_NULL errors
This commit is contained in:
Spencer Russell 2021-05-13 20:41:38 -04:00 committed by GitHub
commit 94a8a7f283
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View file

@ -116,8 +116,14 @@ mutable struct PaHostApiInfo
defaultOutputDevice::PaDeviceIndex
end
Pa_GetHostApiInfo(i) = unsafe_load(@locked ccall((:Pa_GetHostApiInfo, libportaudio),
Ptr{PaHostApiInfo}, (PaHostApiIndex,), i))
function Pa_GetHostApiInfo(i)
result = @locked ccall((:Pa_GetHostApiInfo, libportaudio),
Ptr{PaHostApiInfo}, (PaHostApiIndex,), i)
if result == C_NULL
throw(BoundsError(Pa_GetHostApiInfo, i))
end
unsafe_load(result)
end
# Device Functions
@ -136,8 +142,14 @@ end
Pa_GetDeviceCount() = @locked ccall((:Pa_GetDeviceCount, libportaudio), PaDeviceIndex, ())
Pa_GetDeviceInfo(i) = unsafe_load(@locked ccall((:Pa_GetDeviceInfo, libportaudio),
Ptr{PaDeviceInfo}, (PaDeviceIndex,), i))
function Pa_GetDeviceInfo(i)
result = @locked ccall((:Pa_GetDeviceInfo, libportaudio),
Ptr{PaDeviceInfo}, (PaDeviceIndex,), i)
if result == C_NULL
throw(BoundsError(Pa_GetDeviceInfo, i))
end
unsafe_load(result)
end
Pa_GetDefaultInputDevice() = @locked ccall((:Pa_GetDefaultInputDevice, libportaudio),
PaDeviceIndex, ())
@ -260,7 +272,9 @@ end
# function Pa_GetStreamInfo(stream::PaStream)
# infoptr = ccall((:Pa_GetStreamInfo, libportaudio), Ptr{PaStreamInfo},
# (PaStream, ), stream)
#
# if infoptr == C_NULL
# error("Error getting stream info. Is the stream already closed?")
# end
# unsafe_load(infoptr)
# end
#

View file

@ -1,7 +1,7 @@
#!/usr/bin/env julia
using PortAudio
using PortAudio: Pa_GetDefaultInputDevice, Pa_GetDefaultOutputDevice, Pa_GetDeviceInfo, PortAudioDevice
using PortAudio: Pa_GetDefaultInputDevice, Pa_GetDefaultOutputDevice, Pa_GetDeviceInfo, Pa_GetHostApiInfo, PortAudioDevice
using Test
using SampledSignals
@ -17,6 +17,11 @@ using SampledSignals
@testset "Can list devices without crashing" begin
PortAudio.devices()
end
@testset "Null errors" begin
@test_throws BoundsError Pa_GetDeviceInfo(-1)
@test_throws BoundsError Pa_GetHostApiInfo(-1)
end
end
if !isempty(PortAudio.devices())