JuliaAudio / PortAudio.jl

PortAudio wrapper for the Julia programming language, compatible with the JuliaAudio family of packages
Other
115 stars 19 forks source link

Problems when doing multi-threading #81

Open sletz opened 3 years ago

sletz commented 3 years ago

I'm trying to use PortAudio.jl in an application where the running audio code control parameters have to be updated in real-time, like with a GTK base GUI, or an OSC controller (or even possibly both at the same time).

The PA template code is here: https://github.com/grame-cncm/faust/blob/master-dev/architecture/julia/audio/portaudio.jl

And I have a OSC controller here: https://github.com/grame-cncm/faust/blob/master-dev/architecture/julia/gui/OSCUI.jl

petres commented 3 years ago

same for me, I tried @async as well as @spawn, the read still blocks.

rob-luke commented 3 years ago

Please provide a minimum working example to demonstrate what you are reporting.

petres commented 3 years ago
using PortAudio
using Base.Threads

stream = PortAudioStream()
buffer = Array{Float32, 2}(undef,  (44100 * 10, 2))

function r()
    println("before read!")
    read!(stream, buffer)
    println("after read!")
end

println("calling @async r ")
@async r()
#Threads.@spawn r()
#println("after @async") 

while true 
    sleep(0.5) 
    println("doing something") 
end

produces:

calling @async r 
after @async
before read!
doing something
after read!
doing something
doing something
doing something
doing something
doing something
doing something
doing something
doing something
petres commented 3 years ago

btw playing ascyn is also not working

rob-luke commented 3 years ago

Have you tested this without PortAudio to confirm your understanding of how async works is correct?

petres commented 3 years ago

Actually I have, but to be honest I am a novice in Julia, so maybe I misunderstood some basics. I come from python and used their portaudio wrapper pyaudio.

The code @async function above is useless but should run in a different thread, right? I would assume that this thread shouldn't block the program for 10 secs, but it does.

Maybe I will try it instead of threads with different processes.

rob-luke commented 3 years ago

Does something like this work?

https://github.com/rob-luke/AuditoryStimuli.jl/blob/6020655ef2355c8a084849bf47f2bc51eb7f1bef/examples/test_streamer.jl#L79-L84

petres commented 3 years ago

Thanks! Writing is working, sorry was my fault. Reading not, but actually additionally I have still no clue how I could manage some producer consumer pattern between the shared buffers in Julia.