Update of /cvsroot/pure-data/externals/gridflow/extra
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21117/extra
Added Files:
jmax_format.rb puredata_format.rb server_1_grid.rb
server_1_ppm.rb server_2.rb smpte.rb
Log Message:
starting to commit gridflow 0.8.0 ...
if you know how to use "cvs import" please mail me and i'll use it for 0.8.1
--- NEW FILE: server_1_grid.rb ---
# $Id: server_1_grid.rb,v 1.1 2005/10/04 02:02:15 matju Exp $
require "socket"
require "smpte" # in this folder
picture = "\x7fGRID \000\003"
picture << [240,320,3].pack("N*")
make_smpte(picture) {|*rgb| rgb.pack "N*" }
# File.open("blah.grid","w") {|f| f.write picture }
serv = TCPServer.new 4242
loop {
puts "waiting for connection (port 4242)"
sock = serv.accept
puts "incoming connection"
begin
loop {
sock.write picture
puts "wrote one picture"
}
rescue Errno::EPIPE # Broken Pipe
puts "connection closed (by client)"
# it's ok, go back to waiting.
end
}
--- NEW FILE: server_2.rb ---
# a server program to connect 2 or more clients together.
# by Mathieu Bouchard
require "fcntl"
require "socket"
class IO
def nonblock=flag
bit = Fcntl::O_NONBLOCK
fcntl(Fcntl::F_SETFL, (fcntl(Fcntl::F_GETFL) & ~bit) |
if flag then bit else 0 end)
end
# does not work with any ruby version, due to a bug. see below.
def read_at_most n
s=""
k=1<<(Math.log(n)/Math.log(2)).to_i
while k>0
unless k+s.length>n
puts "trying #{k}"
(s << read(k)) rescue Errno::EWOULDBLOCK
end
k>>=1
end
s
end
# this one works but is slow.
def bugfree_read_at_most n
s=""
(s << (read 1) while s.length<n) rescue Errno::EWOULDBLOCK
s
end
end
serv = TCPServer.new 4242
socks = [serv]
loop {
puts "waiting for connection (port 4242)"
begin
loop {
puts "waiting"
ready,blah,crap = IO.select socks, [], socks, 1
(ready||[]).each {|s|
if s==serv then
sock = serv.accept
sock.nonblock=true
socks << sock
puts "incoming connection (total: #{socks.length-1})"
else
other = socks.find_all{|x|not TCPServer===x} - [s]
stuff = s.bugfree_read_at_most 1024
p stuff
(s.close; socks.delete s) if not stuff or stuff.length==0
other.each {|x|
p x
x.write stuff
}
end
}
}
rescue Errno::EPIPE # Broken Pipe
puts "connection closed (by client)"
# it's ok, go back to waiting.
end
}
--- NEW FILE: jmax_format.rb ---
=begin
$Id: jmax_format.rb,v 1.1 2005/10/04 02:02:15 matju Exp $
GridFlow
Copyright (c) 2001,2002,2003 by Mathieu Bouchard
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See file ../COPYING for further informations on licensing terms.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=end
class JMaxFileHandler
Size = [4,1,2,4]
Packer = ["N","c","n","N"]
OpTable = [
[0, :return],
[1, :push, :int],
[2, :push, :float],
[3, :push, :symbol],
[5, :set, :int],
[6, :set, :float],
[7, :set, :symbol],
[9, :pop_args, :int],
[10, :push_obj, :int],
[11, :mv_obj, :int],
[12, :pop_objs, :int],
[13, :make_obj, :int],
[14, :put_prop, :symbol],
[16, :obj_mess, :int, :symbol, :int],
[18, :push_obj_table, :int],
[19, :pop_obj_table],
[20, :connect],
[21, :make_top_obj, :int],
]
OpTableById = {}
OpTableByName = {}
OpTable.each {|entry|
id,name,arg = entry
OpTableById[id] = entry
OpTableByName[name] = entry
}
end
class JObject
attr_reader :properties
attr_accessor :parent_patcher
attr_reader :init_messages
attr_reader :connections
def self.[](*args) new(*args) end
def initialize(*args)
@args = args
@properties = {}
@init_messages = []
end
def send_in(inlet,*args)
@init_messages << [inlet,*args]
end
def connect(inlet,target,outlet)
end
def subobjects
@subobjects={} if not defined? @subobjects
@subobjects
end
end
class JMaxFileReader < JMaxFileHandler
def initialize(f,factory=JObject)
@f = f
@symbols = []
@estack = []
@ostack = []
@tstack = []
@factory = factory
end
def parse
magic, code_size, n_symbols = @f.read(12).unpack("a4NN")
case magic
when "bMax"; #ok
when "bMa2"; raise "bMa2 format (jMax 4) is not supported yet"
else raise "not a jMax file"
end
@code = @f.read code_size
@symbols = @f.read.split(/\0/).map {|x| x.intern }
@index = 0
while @index < @code.size
read_opcode
end
raise "@estack.size!=0" if @estack.size!=0
raise "@ostack.size!=1" if @ostack.size!=1
raise "@tstack.size!=0" if @tstack.size!=0
@ostack[0]
end
def read_opcode
#puts "#{@index} of #{(a)code.size}"
op = @code[@index]; @index+=1
op1,op2 = op&0x3f,op>>6
entry = OpTableById[op1]
if not entry
puts "skipping unknown opcode #{op1},#{op2}"
return
end
args = []
(entry.length-2).times {|i|
args << (case entry[2+i]
when :int
n=Size[op2]; v=@code[@index,n].unpack(Packer[op2])[0]
x = if v[8*n-1]!=0 then ~(~v&((1<<(8*n-1))-1)) else v end
#STDERR.puts "WARNING: #{v} -> #{x}" if x<0
x
when :float
n=4; @code[@index,4].unpack("g")[0]
when :symbol
n=Size[op2]; @symbols[@code[@index,n].unpack(Packer[op2])[0]]
when nil
end)
@index+=n
}
#text = sprintf "%05d: %2d,%1d: %s", @index, op1, op2, entry[1]
#text << "(" << args.map{|x|x.inspect}.join(",") << ")"
#puts text
send entry[1], *args
end
def push x; @estack << x end
def set x
if @estack.size>0 then @estack[-1]=x else @estack << x end
end
def put_prop x; @ostack[-1].properties[x] = @estack[-1] end
def make_obj x
patcher = @ostack[-1] if @ostack.size>0
baby = @factory[*(@estack[-x,x].reverse)]
@ostack << baby
@ostack[-1].parent_patcher = patcher
patcher.subobjects[baby]=true if patcher
end
alias :make_top_obj :make_obj
def pop_args x; @estack[-x,x]=[] end
def push_obj_table x; @tstack<<[] end
def mv_obj x; @tstack[-1][x]=@ostack[-1] end
def pop_objs x; @ostack[-x,x]=[] end
def obj_mess i,s,n
o = @ostack[-1]
m = @estack[-n,n].reverse
if i<0 then o.send s,*m else o.send_in i,s,*m end
end
def push_obj x; @ostack<<@tstack[-1][x] end
def connect; @ostack[-1].connect @estack[-1],@ostack[-2],@estack[-2] end
def pop_obj_table; @tstack.pop end
def return; end
end
if $0 == __FILE__
jff = JMaxFileReader.new File.open("samples/fire.jmax")
jff.parse
end
--- NEW FILE: smpte.rb ---
# Copyright 2001 by Mathieu Bouchard
# $Id: smpte.rb,v 1.1 2005/10/04 02:02:15 matju Exp $
# The standard SMPTE color test pattern.
# AS SEEN ON TV !!! (but this is a cheap plastic imitation)
def make_smpte(picture="")
row_1 = ""
row_2 = ""
row_3 = ""
row_3_c = [[0,63,105],[255,255,255],[64,0,119]]
(0...320).each {|x|
n_barre_1 = 7 - x*7/320
n_barre_2 = if n_barre_1&1==0 then 0 else 8 - n_barre_1 end
row_1 << yield (*([1,2,0].map{|c| 255 * ((n_barre_1 >> c)&1) }))
row_2 << yield (*([1,2,0].map{|c| 255 * ((n_barre_2 >> c)&1) }))
row_3 << yield (*(row_3_c[x/57] || [0,0,0]))
}
160.times { picture << row_1 }
20 .times { picture << row_2 }
60 .times { picture << row_3 }
picture
end
--- NEW FILE: server_1_ppm.rb ---
# $Id: server_1_ppm.rb,v 1.1 2005/10/04 02:02:15 matju Exp $
require "socket"
picture = File.open("../images/teapot.ppm") {|x| x.read }
serv = TCPServer.new 4242
loop {
puts "waiting for connection (port 4242)"
sock = serv.accept
begin
loop {
sock.write picture
puts "wrote one picture"
}
rescue Errno::EPIPE # Broken Pipe
puts "connection closed (by client)"
# it's ok, go back to waiting.
end
}
--- NEW FILE: puredata_format.rb ---
=begin
$Id: puredata_format.rb,v 1.1 2005/10/04 02:02:15 matju Exp $
GridFlow
Copyright (c) 2001,2002,2003 by Mathieu Bouchard
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See file ../COPYING for further informations on licensing terms.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=end
class PureDataFileWriter
def initialize filename
@f = File.open filename, "w"
end
def close; @f.close end
def write_patcher o
pr = o.properties
@f.puts "#N canvas #{pr[:wx]} #{pr[:wy]} #{pr[:ww]} #{pr[:wh]} 10;"
ol = o.subobjects.keys
x=0
ol.find_all {|a| a.classname=="inlet"}.sort {|a,b| a.argv[0] <=> b.argv[0] }.each {|a|
if x>a.properties[:x]
a.properties[:x]=x+16
STDERR.puts "warning: moving inlet #{a.argv[0]} to the right"
end
x=a.properties[:x]
}
x=0
ol.find_all {|a| a.classname=="outlet"}.sort {|a,b| a.argv[0] <=> b.argv[0] }.each {|a|
if x>a.properties[:x]
a.properties[:x]=x+16
STDERR.puts "warning: moving outlet #{a.argv[0]} to the right"
end
x=a.properties[:x]
}
ol.each {|so| write_object so }
ol.each_with_index {|so,i|
next if not so.instance_eval{defined? @outlets}
so.outlets.each_with_index {|conns,outlet|
next if not conns
conns.each {|target,inlet|
@f.puts "#X connect #{i} #{outlet} #{ol.index target} #{inlet};"
}
}
}
end
def list_to_s l
l.map {|x|
if Array===x then "( " + list_to_s(x) + " )" else escape(x.to_s) end
}.join " "
end
# what am i supposed to do?
def escape x; x.gsub(/[;,]/) {|x| " \\#{x}" }.gsub(/\n/) {"\\\n"} end
def escape2 x; x.gsub(/[,]/) {|x| " \\#{x} " }.gsub(/[;\$\"]/) {|x| "\\#{x}" }.gsub(/\n/) {"\\\n"} end
def write_object o
pr = o.properties
#classname = o.class.instance_eval{@foreign_name}
classname = o.classname
if classname=="jpatcher"
#(a)f.print "#N canvas 0 0 "
write_patcher o
end
case classname
when "display"; classname="print"
when "list"; classname="listmake"
end
t = case classname
when "jcomment"; "text"
when "messbox"; "msg"
when "jpatcher"; "restore"
when "intbox"; "floatatom"
else "obj"
end
@f.print "#X #{t} #{pr[:x]} #{pr[:y]} "
case classname
when "button"
@f.print "bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 -1"
when "jcomment"
@f.print escape2(pr[:comment].to_s)
when "messbox"
av=o.argv[0]
i=0
dollar="$".intern
while i<av.length
if av[i]==dollar then av[i,2]=("\\$"+av[i+1].to_s).intern else i+=1 end
end
@f.print(list_to_s(av))
when "slider"
#doradio = pr[:maxValue]-pr[:minValue]<=10
doradio = false
#p doradio
name = case pr[:orientation]
when 1; if doradio then "hradio" else "hsl" end
when 2,nil; if doradio then "vradio" else "vsl" end
else raise "bogus slider orientation?" end
@f.print "#{name} "+
"#{pr[:w]} #{pr[:h]} #{pr[:minValue]} #{pr[:maxValue]} 0 0 "+
"empty empty empty -2 -6 0 8 -262144 -1 -1 0 1"
when "intbox"
@f.print "5 0 0 0 - - -;"
when "inlet"; @f.print "inlet"
when "outlet"; @f.print "outlet"
when "jpatcher"
@f.print("pd ",list_to_s(o.argv))
else
@f.print(classname," ",list_to_s(o.argv))
end
@f.puts ";"
end
end