dancasimiro / WAV.jl

Julia package for working with WAV files
Other
84 stars 35 forks source link

Buggy interaction with Gtk.jl #105

Open JeffFessler opened 2 years ago

JeffFessler commented 2 years ago

The following code works as expected for me in a fresh Julia session if the first line is commented out.

#using Gtk # uncommenting this causes problems with wavplay!?
using WAV: wavplay
S = 8192
x = cos.(2pi*(1:S÷2)*440/S)
y = cos.(2pi*(1:S÷2)*660/S)
wavplay(x, S)
wavplay(y, S)

But uncommenting the first line causes intermittent problems, usually that the first tone x is never played. I then have to restart Julia and comment out the first line for it to work again.

I am using Julia v1.6.4 with WAV v1.1.1 and Gtk v1.1.9, under MacOS Mojave 10.14.6.

I suspect that the issue is that both Gtk and WAV use a ccall with some type of run loop, like https://github.com/dancasimiro/WAV.jl/blob/0dc24f66ac0fab970d621f790fc6d6fa8f609fa0/src/wavplay-audioqueue.jl#L374 and perhaps those interfere with each other, but I am surprised that merely adding using Gkt without calling any Gtk functions would cause such interference.

I am preparing to teach a course where students make a simply GUI-based synthesizer using Gtk buttons that produce tones when clicked, so I very much hope there is a solution to this.

dancasimiro commented 2 years ago

Hi Jeff, thanks for reporting this issue. I suspect that your hunch is correct and the WAV and Gtk code must coordinate how to invoke the core foundation run loop here. Does everything work if you comment out the WAV.jl invocation on line 374 and the "dispose" function call on line 375?

JeffFessler commented 2 years ago

When I comment out those two lines, in both julia v1.6.4 and v1.7, both tones x and y are played (essentially?) simultaneously rather than sequentially! That is a potentially useful "feature" but is not what I was expecting. But maybe it helps you figure out what to try next?

If I comment out 375, but leave in 374, then it hangs (with GTK). If I comment out 374 and leave in 375, then I get almost no sound (tiny blip the first time), but it does not hang.

Just to let you know, I have found an alternative for now using PortAudio that seems to play OK with Gtk. See https://github.com/JeffFessler/Sound.jl. So this is no longer urgent, but still it would be useful if wavplay would work with Gtk.