Skip to content

Instantly share code, notes, and snippets.

@antonhornquist
Last active May 19, 2017 11:39
Show Gist options
  • Save antonhornquist/d8bfa789b664d60826bd4161fc4ab8ea to your computer and use it in GitHub Desktop.
Save antonhornquist/d8bfa789b664d60826bd4161fc4ab8ea to your computer and use it in GitHub Desktop.
run and patch pd from ruby
require 'socket'
require 'tempfile'
file = Tempfile.new('foo')
# note: using pd netreceive is unsafe, only do this if you know what you're doing
lines =<<-PATCH
#N canvas 87 360 450 300 10;
#X obj 24 25 netreceive 2000 1;
#X obj 24 51 s pd-#{File.basename(file.path)};
#X connect 0 0 1 0;
PATCH
begin
file.write(lines)
file.close
pd_path = case RUBY_PLATFORM
when /darwin/ then "/Applications/Pd-0.47-1-64bit.app/Contents/Resources/bin/pd" # assumes pd installed on this path
when /mingw32/ then "pd.exe" # assumes pd.exe is in PATH
end
opts = "-nogui -send \"pd dsp 1;\" \"#{file.path}\""
cmd = pd_path + " " + opts
pid = case RUBY_PLATFORM
when /darwin/ then fork { exec(cmd) }
when /mingw32/ then Process.spawn(cmd)
end
puts "wait for pd to start..."
sleep 1
freq = 200 + rand(400)
puts "patch [osc~ #{freq}] =< [dac~]"
a=UDPSocket.new
a.connect("127.0.0.1", 2000)
a.send("obj 10 10 osc~ #{freq};\n", 0)
a.send("obj 10 10 dac~;\n", 0)
a.send("connect 2 0 3 0;\n", 0)
a.send("connect 2 0 3 1;\n", 0)
puts "wait 5 seconds..."
sleep 5
ensure
puts "close pd..."
Process.kill("KILL", pid)
file.unlink
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment