Skip to content

Commit

Permalink
wraps IO operations in @spawn
Browse files Browse the repository at this point in the history
fixes #15
  • Loading branch information
ssfrr committed Nov 20, 2019
1 parent 30f644f commit a0f1004
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
74 changes: 42 additions & 32 deletions src/libsndfile_h.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,19 @@ end

SF_INFO() = SF_INFO(0, 0, 0, 0, 0, 0)

"""
Call the given expression in a separate thread, waiting on the result. This is
useful when running code that would otherwise block the Julia process (like a
`ccall` into a function that does IO).
"""
macro tcall(ex)
:(fetch(Base.Threads.@spawn $(esc(ex))))
end

function sf_open(fname::String, mode, sfinfo)
filePtr = ccall((:sf_open, libsndfile), Ptr{Cvoid},
(Cstring, Int32, Ref{SF_INFO}),
fname, mode, sfinfo)
filePtr = @tcall ccall((:sf_open, libsndfile), Ptr{Cvoid},
(Cstring, Int32, Ref{SF_INFO}),
fname, mode, sfinfo)

if filePtr == C_NULL
error("LibSndFile.jl error while opening $fname: ", sf_strerror(C_NULL))
Expand All @@ -136,9 +145,9 @@ include("virtualio.jl")

function sf_open(io::T, mode, sfinfo) where T <: IO
virtio = SF_VIRTUAL_IO(T)
filePtr = ccall((:sf_open_virtual, libsndfile), Ptr{Cvoid},
(Ref{SF_VIRTUAL_IO}, Int32, Ref{SF_INFO}, Ptr{T}),
virtio, mode, sfinfo, pointer_from_objref(io))
filePtr = @tcall ccall((:sf_open_virtual, libsndfile), Ptr{Cvoid},
(Ref{SF_VIRTUAL_IO}, Int32, Ref{SF_INFO}, Ptr{T}),
virtio, mode, sfinfo, pointer_from_objref(io))
if filePtr == C_NULL
error("LibSndFile.jl error while opening stream: ", sf_strerror(C_NULL))
end
Expand All @@ -147,7 +156,7 @@ function sf_open(io::T, mode, sfinfo) where T <: IO
end

function sf_close(filePtr)
err = ccall((:sf_close, libsndfile), Int32, (Ptr{Cvoid},), filePtr)
err = @tcall ccall((:sf_close, libsndfile), Int32, (Ptr{Cvoid},), filePtr)
if err != 0
error("LibSndFile.jl error: Failed to close file: ", sf_strerror(filePtr))
end
Expand All @@ -160,24 +169,24 @@ of frames into the given array. Returns the number of frames read.
function sf_readf end

sf_readf(filePtr, dest::Array{T}, nframes) where T <: Union{Int16, PCM16Sample} =
ccall((:sf_readf_short, libsndfile), Int64,
(Ptr{Cvoid}, Ptr{T}, Int64),
filePtr, dest, nframes)
@tcall ccall((:sf_readf_short, libsndfile), Int64,
(Ptr{Cvoid}, Ref{T}, Int64),
filePtr, dest, nframes)

sf_readf(filePtr, dest::Array{T}, nframes) where T <: Union{Int32, PCM32Sample} =
ccall((:sf_readf_int, libsndfile), Int64,
(Ptr{Cvoid}, Ptr{T}, Int64),
filePtr, dest, nframes)
@tcall ccall((:sf_readf_int, libsndfile), Int64,
(Ptr{Cvoid}, Ref{T}, Int64),
filePtr, dest, nframes)

sf_readf(filePtr, dest::Array{Float32}, nframes) =
ccall((:sf_readf_float, libsndfile), Int64,
(Ptr{Cvoid}, Ptr{Float32}, Int64),
filePtr, dest, nframes)
@tcall ccall((:sf_readf_float, libsndfile), Int64,
(Ptr{Cvoid}, Ref{Float32}, Int64),
filePtr, dest, nframes)

sf_readf(filePtr, dest::Array{Float64}, nframes) =
ccall((:sf_readf_double, libsndfile), Int64,
(Ptr{Cvoid}, Ptr{Float64}, Int64),
filePtr, dest, nframes)
@tcall ccall((:sf_readf_double, libsndfile), Int64,
(Ptr{Cvoid}, Ref{Float64}, Int64),
filePtr, dest, nframes)

"""
Wrappers for the family of sf_writef_* functions, which write the given number
Expand All @@ -186,26 +195,27 @@ of frames into the given array. Returns the number of frames written.
function sf_writef end

sf_writef(filePtr, src::Array{T}, nframes) where T <: Union{Int16, PCM16Sample} =
ccall((:sf_writef_short, libsndfile), Int64,
(Ptr{Cvoid}, Ptr{T}, Int64),
filePtr, src, nframes)
@tcall ccall((:sf_writef_short, libsndfile), Int64,
(Ptr{Cvoid}, Ref{T}, Int64),
filePtr, src, nframes)

sf_writef(filePtr, src::Array{T}, nframes) where T <: Union{Int32, PCM32Sample} =
ccall((:sf_writef_int, libsndfile), Int64,
(Ptr{Cvoid}, Ptr{T}, Int64),
filePtr, src, nframes)
@tcall ccall((:sf_writef_int, libsndfile), Int64,
(Ptr{Cvoid}, Ref{T}, Int64),
filePtr, src, nframes)

sf_writef(filePtr, src::Array{Float32}, nframes) =
ccall((:sf_writef_float, libsndfile), Int64,
(Ptr{Cvoid}, Ptr{Float32}, Int64),
filePtr, src, nframes)
@tcall ccall((:sf_writef_float, libsndfile), Int64,
(Ptr{Cvoid}, Ref{Float32}, Int64),
filePtr, src, nframes)

sf_writef(filePtr, src::Array{Float64}, nframes) =
ccall((:sf_writef_double, libsndfile), Int64,
(Ptr{Cvoid}, Ptr{Float64}, Int64),
filePtr, src, nframes)
@tcall ccall((:sf_writef_double, libsndfile), Int64,
(Ptr{Cvoid}, Ref{Float64}, Int64),
filePtr, src, nframes)

function sf_strerror(filePtr)
errmsg = ccall((:sf_strerror, libsndfile), Ptr{UInt8}, (Ptr{Cvoid},), filePtr)
errmsg = ccall((:sf_strerror, libsndfile), Ptr{UInt8},
(Ref{Cvoid},), filePtr)
unsafe_string(errmsg)
end
2 changes: 1 addition & 1 deletion test/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
deps = ["FileIO", "Libdl", "LinearAlgebra", "Printf", "SampledSignals", "libsndfile_jll"]
path = "/home/sfr/Dropbox/juliadev/LibSndFile"
uuid = "b13ce0c6-77b0-50c6-a2db-140568b8d1a5"
version = "2.1.0"
version = "2.2.0"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Expand Down

0 comments on commit a0f1004

Please sign in to comment.