[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-dev] SF.net SVN: quarks:[2835] wslib/wslib-classes/Experimental/ReceiveTrig.sc



Revision: 2835
          http://sourceforge.net/p/quarks/code/2835
Author:   wsnoei
Date:     2021-01-19 14:01:20 +0000 (Tue, 19 Jan 2021)
Log Message:
-----------
refactor ReceiveReply/ReceiveTrig

Modified Paths:
--------------
    wslib/wslib-classes/Experimental/ReceiveTrig.sc

Modified: wslib/wslib-classes/Experimental/ReceiveTrig.sc
===================================================================
--- wslib/wslib-classes/Experimental/ReceiveTrig.sc	2021-01-19 10:06:26 UTC (rev 2834)
+++ wslib/wslib-classes/Experimental/ReceiveTrig.sc	2021-01-19 14:01:20 UTC (rev 2835)
@@ -1,182 +1,193 @@
-// wslib 2010
-//
-// the lang-side counterpart of SendTrig and SendReply
-//
+{\rtf1\ansi\ansicpg1252\cocoartf1671\cocoasubrtf600
+\cocoascreenfonts1{\fonttbl\f0\fnil\fcharset0 Monaco;}
+{\colortbl;\red255\green255\blue255;\red191\green0\blue0;\red0\green0\blue0;\red0\green0\blue191;
+\red0\green0\blue255;\red51\green51\blue191;\red0\green115\blue0;\red102\green102\blue191;}
+{\*\expandedcolortbl;;\csgenericrgb\c75000\c0\c0;\csgenericrgb\c0\c0\c0;\csgenericrgb\c0\c0\c75000;
+\csgenericrgb\c0\c0\c100000;\csgenericrgb\c20000\c20000\c75000;\csgenericrgb\c0\c45000\c0;\csgenericrgb\c40000\c40000\c75000;}
+\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural
 
-/*
-// example 1:
-
-x = { SendTrig.kr( Impulse.kr(1), 10, WhiteNoise.kr ) }.play;
-
-ReceiveTrig( x, _.postln );
-
-x.free; // automatic removal
-
-// example 2:
-
-r = ReceiveTrig( s, { |val, time, responder, msg| msg.postln } ); // receive any trig from s
-
-x = { SendTrig.kr( Impulse.kr(1), 10, WhiteNoise.kr ) }.play;
-y = { SendTrig.kr( Impulse.kr(1), 11, WhiteNoise.kr ) }.play;
-
-r.id = 10; // only from x
-
-r.remove; // stop receiving (or cmd-.)
-
-
-// example 3: as a method for Node or Synth
-
-y = { SendTrig.kr( Impulse.kr(1), 10, WhiteNoise.kr ) }.play;
-
-y.onTrig_( _.postln ); // adds a ReceiveTrig to the Synth
-
-y.free; // also removes the ReceiveTrig
-
-
-// example 4: ReceiveReply
-
-y = { SendReply.kr( Impulse.kr(1), 'noise', WhiteNoise.kr(1.dup) ) }.play;
-
-y.onReply_( _.postln, 'noise' ); // adds a ReceiveReply to the Synth
-
-y.free; // also removes the ReceiveReply
-
-
-// example 5: even shorter
-
-y = { SendTrig.kr( Impulse.kr(1), 10, WhiteNoise.kr(1) ) }.play.onTrig_( _.postln );
-
-y.free;
-
-*/
-
-ReceiveReply {
-	classvar <all;
-
-	var <source;
-	var <responder, <>action, <>id, <value = 0, <>removeAtReceive = false;
-	var <endResponder;
-	var <cmdName;
-
-	*defaultCmdName { ^'/reply' }
-
-	*initClass { CmdPeriod.add( this ); }
-
-	*new { |source, action, cmdName, id|
-		^super.new.init( source, cmdName )
-			.id_( id )
-			.action_( action ? { |value, time, responder, msg| value.postln; } )
-			.addToAll
-		}
-
-	init { |inSource, inCmdName|
-		source = inSource;
-		cmdName = inCmdName ? this.class.defaultCmdName;
-		this.startResponders;
-	}
-
-	startResponders {
-		var addr;
-
-		case { (source.class == Synth) or: { source.isNumber } } // Synth or nodeID
-		{	
-			addr = NetAddr( source.server.addr.hostname, source.server.addr.port );
-			responder = OSCresponderNode( addr, cmdName,
-				{ arg time, responder, msg;
-					if( msg[1] == source.nodeID ) { this.doAction( time, responder, msg ) }
-				}).add;
-
-			 endResponder = OSCresponderNode( addr, '/n_end', // remove on end
-			 	 { arg time, responder, msg;
-				 	 if( msg[1] == source.nodeID ) { this.remove };
-			 	 }).add;
-			}
-			{ source.respondsTo( \addr ) } // a Server
-			{ 
-				addr = NetAddr( source.addr.hostname, source.addr.port );
-				responder = OSCresponderNode(addr, cmdName,
-				{ arg time, responder, msg;
-					this.doAction( time, responder, msg ) 				}).add;
-			}
-			{ source.class == NetAddr } // a NetAddr
-			{ responder = OSCresponderNode(source, cmdName,
-				{ arg time, responder, msg;
-					this.doAction( time, responder, msg ) 				}).add;
-			}
-			{ true } // anything else (including nil)
-			{ responder = OSCresponderNode( nil, cmdName,
-				{ arg time, responder, msg;
-					this.doAction( time, responder, msg )
-				}).add;
-			};
-		}
-
-	doAction { |time, responder, msg|
-		if( id.isNil or: { msg[2] == id } )
-			{ value =  msg[3..];
-			if( value.size == 1 ) { value = value[0]; };
-			action.value( value, time, responder, msg );
-			if( removeAtReceive ) { this.remove };
-		};
-	}
-
-	addToAll { all = all.asCollection ++ [ this ] }
-
-	remove { responder.remove; endResponder.remove; all.remove( this ); }
-	*remove { all.do({ |obj| obj.remove }); all = []; }
-
-	*cmdPeriod { this.remove; }
-
-	oneShot { removeAtReceive = true; }
-	*oneShot { |source, action, cmdName, id| ^this.new( source, action, cmdName, id ).oneShot; }
-
-	}
-
-ReceiveTrig : ReceiveReply {
-
-	*defaultCmdName { ^'/tr' }
-
-	*new { |source, action, id|
-		^super.new( source, action, this.defaultCmdName, id );
-		}
-
-	// more optimized; SendTrig doesn't do multichannel expansion
-	doAction { |time, responder, msg|
-		if( id.isNil or: { msg[2] == id } )
-			{ value = msg[3];
-			action.value( value, time, responder, msg );
-			if( removeAtReceive ) { this.remove };
-			};
-	}
-
-}
-
-// Node support
-
-+ Node {
-
-	onReply_ { |action, cmdName, id|
-			var rt;
-			if( ( rt = this.onReply( cmdName ) ).notNil )
-				{ rt.id = id; rt.action = action; }
-				{ ReceiveReply( this, action, cmdName, id ); };
-	}
-
-	onReply { |cmdName|
-		cmdName = cmdName ? '/reply';
-		^ReceiveReply.all.detect({ |item|
-			item.source == this && { item.cmdName == cmdName }; });
-		}
-
-	onTrig_ { |action, id|
-			var rt;
-			if( ( rt = this.onTrig ).notNil )
-				{ rt.id = id; rt.action = action; }
-				{ ReceiveTrig( this, action, id ); };
-	}
-
-	onTrig { ^ReceiveTrig.all.detect({ |item|
-		 item.source == this && { item.cmdName == '/tr' }; });
-		 }
-
-}
\ No newline at end of file
+\f0\fs20 \cf2 // wslib 2010\cf3 \
+\cf2 //\cf3 \
+\cf2 // the lang-side counterpart of SendTrig and SendReply\cf3 \
+\cf2 //\cf3 \
+\
+\cf2 /*\
+// example 1:\
+\
+x = \{ SendTrig.kr( Impulse.kr(1), 10, WhiteNoise.kr ) \}.play;\
+\
+ReceiveTrig( x, _.postln );\
+\
+x.free; // automatic removal\
+\
+// example 2:\
+\
+r = ReceiveTrig( s, \{ |val, time, responder, msg| msg.postln \} ); // receive any trig from s\
+\
+x = \{ SendTrig.kr( Impulse.kr(1), 10, WhiteNoise.kr ) \}.play;\
+y = \{ SendTrig.kr( Impulse.kr(1), 11, WhiteNoise.kr ) \}.play;\
+\
+r.id = 10; // only from x\
+\
+r.remove; // stop receiving (or cmd-.)\
+\
+\
+// example 3: as a method for Node or Synth\
+\
+y = \{ SendTrig.kr( Impulse.kr(1), 10, WhiteNoise.kr ) \}.play;\
+\
+y.onTrig_( _.postln ); // adds a ReceiveTrig to the Synth\
+\
+y.free; // also removes the ReceiveTrig\
+\
+\
+// example 4: ReceiveReply\
+\
+y = \{ SendReply.kr( Impulse.kr(1), '/noise', WhiteNoise.kr(1.dup) ) \}.play;\
+\
+y.onReply_( _.postln, '/noise' ); // adds a ReceiveReply to the Synth\
+\
+y.free; // also removes the ReceiveReply\
+\
+\
+// example 5: even shorter\
+\
+y = \{ SendTrig.kr( Impulse.kr(1), 10, WhiteNoise.kr(1) ) \}.play.onTrig_( _.postln );\
+\
+y.free;\
+\
+*/\cf3 \
+\
+\cf4 ReceiveReply\cf3  \{\
+	\cf5 classvar\cf3  <all;\
+\
+	\cf5 var\cf3  <source;\
+	\cf5 var\cf3  <oscFunc, <>action, <>id, <value = 0, <>removeAtReceive = \cf6 false\cf3 ;\
+	\cf5 var\cf3  <endOSCFunc;\
+	\cf5 var\cf3  <cmdName;\
+\
+	*defaultCmdName \{ ^\cf7 '/reply'\cf3  \}\
+\
+	*new \{ \cf5 |source, action, cmdName, id|\cf3 \
+		^\cf8 super\cf3 .new.init( source, cmdName )\
+			.id_( id )\
+			.action_( action ? \{ \cf5 |value, time, responder, msg|\cf3  value.postln; \} )\
+			.addToAll\
+		\}\
+		\
+	*initClass \{ \cf4 CmdPeriod\cf3 .add( \cf8 this\cf3  ); \}\
+\
+	init \{ \cf5 |inSource, inCmdName|\cf3 \
+		source = inSource;\
+		cmdName = inCmdName ? \cf8 this\cf3 .class.defaultCmdName;\
+		\cf8 this\cf3 .startResponders;\
+	\}\
+\
+	startResponders \{\
+		\cf5 var\cf3  addr;\
+\
+		case \{ (source.class == \cf4 Synth\cf3 ) or: \{ source.isNumber \} \} \cf2 // Synth or nodeID\cf3 \
+			\{	\
+				addr = \cf4 NetAddr\cf3 ( source.server.addr.hostname, source.server.addr.port );\
+				oscFunc = \cf4 OSCFunc\cf3 (\{ \cf5 arg\cf3  msg, time;\
+					if( removeAtReceive == \cf6 true\cf3  ) \{ \cf8 this\cf3 .free \};\
+					\cf8 this\cf3 .doAction( time, msg );\
+				\}, cmdName, addr, argTemplate: [ source.nodeID ]);\
+	\
+				endOSCFunc = \cf4 OSCFunc\cf3 (\{ \cf5 arg\cf3  time, responder, msg;\
+					 \cf8 this\cf3 .free;\
+				\}, \cf7 '/n_end'\cf3 , addr, argTemplate: [ source.nodeID ]).oneShot;\
+			\}\
+			\{ source.respondsTo( \cf7 \\addr\cf3  ) \} \cf2 // a Server\cf3 \
+			\{ \
+				addr = \cf4 NetAddr\cf3 ( source.addr.hostname, source.addr.port );\
+				oscFunc = \cf4 OSCFunc\cf3 (\{ \cf5 arg\cf3  msg, time;\
+					if( removeAtReceive == \cf6 true\cf3  ) \{ \cf8 this\cf3 .free \};\
+					\cf8 this\cf3 .doAction( time, msg );\
+				\}, cmdName, addr );\
+			\}\
+			\{ source.class == \cf4 NetAddr\cf3  \} \cf2 // a NetAddr\cf3 \
+			\{ \
+				oscFunc = \cf4 OSCFunc\cf3 (\{ \cf5 arg\cf3  msg, time;\
+					if( removeAtReceive == \cf6 true\cf3  ) \{ \cf8 this\cf3 .free \};\
+					\cf8 this\cf3 .doAction( time, msg ); 	\
+				\}, cmdName, source );\
+			\}\
+			\{ \
+				oscFunc = \cf4 OSCFunc\cf3 (\{ \cf5 arg\cf3  msg, time;\
+					if( removeAtReceive == \cf6 true\cf3  ) \{ \cf8 this\cf3 .free \};\
+					\cf8 this\cf3 .doAction( time, msg );\
+				\}, cmdName );\
+			\};\
+		\}\
+\
+	doAction \{ \cf5 |time, msg|\cf3 \
+		if( id.isNil or: \{ msg[2] == id \} )\
+			\{ value =  msg[3..];\
+			if( value.size == 1 ) \{ value = value[0]; \};\
+			action.value( value, time, \cf8 this\cf3 , msg );\
+		\};\
+	\}\
+\
+	addToAll \{ all = all.asCollection ++ [ \cf8 this\cf3  ] \}\
+	\
+	free \{ oscFunc.free; endOSCFunc.free; all.remove( \cf8 this\cf3  ); \}\
+	*free \{ all.do(\{ \cf5 |obj|\cf3  obj.free \}); all = []; \}\
+\
+	remove \{ \cf8 this\cf3 .free \} \cf2 // shortcut for backwards compatibility\cf3 \
+	*remove \{ \cf8 this\cf3 .free \}\
+	\
+	*cmdPeriod \{ \cf8 this\cf3 .free; \}\
+\
+	oneShot \{ removeAtReceive = \cf6 true\cf3  \}\
+	*oneShot \{ \cf5 |source, action, cmdName, id|\cf3  ^\cf8 this\cf3 .new( source, action, cmdName, id ).oneShot; \}\
+\
+	\}\
+\
+\cf4 ReceiveTrig\cf3  : \cf4 ReceiveReply\cf3  \{\
+\
+	*defaultCmdName \{ ^\cf7 '/tr'\cf3  \}\
+\
+	*new \{ \cf5 |source, action, id|\cf3 \
+		^\cf8 super\cf3 .new( source, action, \cf8 this\cf3 .defaultCmdName, id );\
+		\}\
+\
+	\cf2 // more optimized; SendTrig doesn't do multichannel expansion\cf3 \
+	doAction \{ \cf5 |time, msg|\cf3 \
+		if( id.isNil or: \{ msg[2] == id \} ) \{ \
+			value = msg[3];\
+			action.value( value, time, \cf8 this\cf3 , msg );\
+		\};\
+	\}\
+\
+\}\
+\
+\cf2 // Node support\cf3 \
+\
++ \cf4 Node\cf3  \{\
+\
+	onReply_ \{ \cf5 |action, cmdName, id|\cf3 \
+			\cf5 var\cf3  rt;\
+			if( ( rt = \cf8 this\cf3 .onReply( cmdName ) ).notNil )\
+				\{ rt.id = id; rt.action = action; \}\
+				\{ \cf4 ReceiveReply\cf3 ( \cf8 this\cf3 , action, cmdName, id ); \};\
+	\}\
+\
+	onReply \{ \cf5 |cmdName|\cf3 \
+		cmdName = cmdName ? \cf7 '/reply'\cf3 ;\
+		^\cf4 ReceiveReply\cf3 .all.detect(\{ \cf5 |item|\cf3 \
+			item.source == \cf8 this\cf3  && \{ item.cmdName == cmdName \}; \});\
+		\}\
+\
+	onTrig_ \{ \cf5 |action, id|\cf3 \
+			\cf5 var\cf3  rt;\
+			if( ( rt = \cf8 this\cf3 .onTrig ).notNil )\
+				\{ rt.id = id; rt.action = action; \}\
+				\{ \cf4 ReceiveTrig\cf3 ( \cf8 this\cf3 , action, id ); \};\
+	\}\
+\
+	onTrig \{ ^\cf4 ReceiveTrig\cf3 .all.detect(\{ \cf5 |item|\cf3 \
+		 item.source == \cf8 this\cf3  && \{ item.cmdName == \cf7 '/tr'\cf3  \}; \});\
+		 \}\
+\
+\}}
\ No newline at end of file

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


_______________________________________________
sc-dev mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: https://listarc.bham.ac.uk/marchives/sc-dev/
search: https://listarc.bham.ac.uk/lists/sc-dev/search/