add swap method / add global dict
authorEugene Petkevich <nasedil.genio.code@gmail.com>
Thu, 11 Jan 2024 14:48:49 +0000 (15:48 +0100)
committerEugene Petkevich <nasedil.genio.code@gmail.com>
Thu, 11 Jan 2024 14:48:49 +0000 (15:48 +0100)
Swap method makes it easy to quickly change Pdefn to temporary alternative, for example to mute it after the first beat, with possibility to resume again

extensions/classes/globaldict.sc [new file with mode: 0644]
extensions/classes/patutils.sc [new file with mode: 0644]
temp.scd

diff --git a/extensions/classes/globaldict.sc b/extensions/classes/globaldict.sc
new file mode 100644 (file)
index 0000000..b758973
--- /dev/null
@@ -0,0 +1,19 @@
+GlobalDict {
+  classvar <>all;
+  *initClass {
+    all = IdentityDictionary.new;
+  }
+
+  *removeAll {
+               this.all.clear;
+               this.all.makeEmpty;
+       }
+
+  *at { arg key;
+    ^this.all.at(key)
+       }
+
+  *put { arg key, value;
+    ^this.all.put(key, value)
+       }
+}
\ No newline at end of file
diff --git a/extensions/classes/patutils.sc b/extensions/classes/patutils.sc
new file mode 100644 (file)
index 0000000..c00a1a4
--- /dev/null
@@ -0,0 +1,19 @@
++ Pdefn {
+  swap {|newPattern|
+    var tempSource, keyName;
+    keyName = (\Pdefn++this.key).asSymbol;
+    if(newPattern.isNil) {
+      tempSource = source;
+      this.source = GlobalDict.at(keyName);
+      GlobalDict.put(keyName, tempSource);
+               } {
+      switch (newPattern,
+        \fbs, { newPattern = Pseq([1, Pseq([Rest(1)], inf)]) },
+        \s, { newPattern = Rest(1) }
+      );
+      GlobalDict.put(keyName, source);
+      this.source = newPattern;
+               }
+    ^this
+  }
+}
index daf730f..515d92a 100644 (file)
--- a/temp.scd
+++ b/temp.scd
@@ -99,7 +99,11 @@ Pdef(\beat, Pmono(\qoba, *[
 ])).quant_(4).play;
 
 TempoClock.tempo = 2;
-Pdefn(\durtest, Pseq([1, Pseq([Rest(1)], inf)], inf)).quant_(4);
+Pdefn(\durtest, 1).quant_(4);
+//Pdefn(\durtest, Rest(1)).quant_(4);
+//Pdefn(\durtest, Pseq([1], 1)).quant_(4).play;
+Pdefn(\durtest).swap;
+Pdefn(\durtest).swap(\fb);
 Pdef(\temp, Pbind(*[
   instrument: \dyti,
   db: -19,