sonic-pi icon indicating copy to clipboard operation
sonic-pi copied to clipboard

external synth definition file not working in conjunction with run_file

Open rbnpi opened this issue 3 years ago • 5 comments

I am using an external scsyndef file in a SP project and it works fine when run from a buffer. However when I save the buffer file and use run_file to execute it SP fails to find the synth definition and I get an unknown synth error. I am using a full pathname for the definition file so it should be found, but something prevents the synth from being used. The flag to use external synths is selected.

(I also found an error in the single load_synthdef command but I will address that in a separate pr which solves it).

rbnpi avatar Jun 16 '22 11:06 rbnpi

Hey @rbnpi - is this behaviour similar in older versions of Sonic Pi or is it specific to the latest v4 BETA?

samaaron avatar Jun 16 '22 12:06 samaaron

@samaaron No you get the same problem with 3.3.1 (trying on a Mac)

rbnpi avatar Jun 16 '22 12:06 rbnpi

Problem occurs both in SP3.3.1 and in SP4.0 beta latest

testing this a bit further. Using external scsyndef file lisa.scsyndef saves in ~/.sonic-pi/scsyndefs Used file testlisa.rb

load_synthdef ENV['HOME'] + '/.sonic-pi/synthdefs/lisa.scsyndef'
use_synth_defaults attack: 0.5, sustain: 5, release: 0.5, amp: 1.5
set_mixer_control! hpf_bypass: 1, lpf_bypass: 1, limiter_bypass: 1, leak_dc_bypass: 1
synth :lisa

which worked and plays a note for 5 seconds Then tried this test file.

puts "try run_file"
run_file "/Users/rbn/Documents/Lissajous/lisatest.rb" #doesn't work. Can't find synth
puts"seems to be a problem in __spider_eval (defined in run_time.rb_)"
puts"not setting environment correctly to find external synth"
puts "although log shows it does load the scsyndef file"
puts "now try eval_file"

eval_file "/Users/rbn/Documents/Lissajous/lisatest.rb" #works
puts "this works"

#lisatest.rb contains.

##| load_synthdef ENV['HOME'] + '/.sonic-pi/synthdefs/lisa.scsyndef'
##| use_synth_defaults attack: 0.5, sustain: 5, release: 0.5, amp: 1.5
##| set_mixer_control! hpf_bypass: 1, lpf_bypass: 1, limiter_bypass: 1, leak_dc_bypass: 1
##| synth :lisa

Logged output gives

 ├─ "try run_file"
 ├─ "seems to be a problem in __spider_eval (defined in run_time.rb)"
 ├─ "not setting environment correctly to find external synth"
 ├─ "although log shows it does load the scsyndef file"
 ├─ "now try eval_file"
 └─ "this works"

Error message is:

Runtime Error: [buffer eval, line 4] - RuntimeError
Thread death
Unknown synth :lisa

/Users/rbn/dev/spqt6/sonic-pi/app/server/ruby/lib/sonicpi/lang/sound.rb:1069:in `synth'
eval:4:in `block (2 levels) in __spider_eval'
/Users/rbn/dev/spqt6/sonic-pi/app/server/ruby/lib/sonicpi/runtime.rb:873:in `eval'
/Users/rbn/dev/spqt6/sonic-pi/app/server/ruby/lib/sonicpi/runtime.rb:873:in `block (2 levels) in __spider_eval'
/Users/rbn/dev/spqt6/sonic-pi/app/server/ruby/lib/sonicpi/runtime.rb:1137:in `block (2 levels) in __in_thread'

lisa.scsyn.def file [lisa.scsyndef](https://gist.github.com/emlyn/ebdff933d4690e4d9ac990e534627129#file-lisa-scsyndef

Screen shot: Voila_Original_Image ) NB enable external_synths/FX IS enabled

rbnpi avatar Jun 25 '22 14:06 rbnpi

The issue here is that run_file is designed to run in complete isolation from the script that calls it. This also means that it runs in isolation from the lines that the GUI inserts to honour the preferences on each run.

Unfortunately, given that there's currently no way to distinguish between settings set by the GUI and those set by the user (which we specifically want to be isolated from) it's not possible to pass these preferences onto code executed by run_file.

Instead, I think the approach should be to create a new fn such as use_external_synthdefs which will set the right thread local. For now, as a workaround you could try adding this to to the top of your external scripts:

__thread_locals.set(:sonic_pi_mod_sound_use_external_synths, true)

samaaron avatar Jun 30 '22 16:06 samaaron

Thanks Sam both for the explanation and also for the temporary workable solution for the moment which works fine.

rbnpi avatar Jun 30 '22 16:06 rbnpi