4_osc_communication.scd 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Module 5: OSC Communication Setup - MODIFIED FOR IDRAW OSC
  2. // Save as "5_osc_communication.scd"
  3. (
  4. // Clear any existing OSC definitions
  5. OSCdef.freeAll;
  6. // Variables to track current pen type and color
  7. ~currentPenType = "\pen";
  8. ~currentColor = (r: 0.0, g: 0.0, b: 1.0); // Default blue
  9. ~currentPadValues = (x: 0.0, y: 0.0, pressure: 1.0, a: 1.0); //Default pad values
  10. // Define OSC responder for iDraw touch data
  11. ~changeEffectParams = {
  12. var x = ~currentPadValues.x;
  13. var y = ~currentPadValues.y;
  14. var pressure = ~currentPadValues.pressure;
  15. var a = ~currentPadValues.a;
  16. // Log the received data
  17. //["Touch data:", x, y, pressure, ~currentPenType, ~currentColor].postln;
  18. // Handle touch based on current pen type
  19. switch(~currentPenType,
  20. // Pen - Controls amplitude envelope
  21. "/pen", {
  22. var ampAttack = y.linexp(-0.5, 0.5, 0.001, 5);
  23. var ampRelease = x.linexp(-0.5, 0.5, 0.001, 10);
  24. ~setSynthsParam.(\ampAttack, ampAttack);
  25. ~setSynthsParam.(\ampRelease, ampRelease);
  26. //["Amplitude envelope:", ampAttack, ampRelease].postln;
  27. },
  28. // Monoline - Controls filter envelope
  29. "/monoline", {
  30. var filterAttack = y.linexp(-0.5, 0.5, 0.001, 5);
  31. var filterDecay = x.linexp(-0.5, 0.5, 0.001, 10);
  32. var envDepth = a.linlin(0, 1, 50, 0.001);
  33. ~filterEnv.set(\attack, filterAttack);
  34. ~filterEnv.set(\decay, filterDecay);
  35. ~filter.set(\envDepth, envDepth);
  36. //["Filter envelope:", filterAttack, filterDecay, envDepth].postln;
  37. },
  38. // Marker - Controls pitch envelope
  39. "/marker", {
  40. var pitchAttack = y.linexp(-0.5, 0.5, 0.001, 5);
  41. var pitchDecay = x.linexp(-0.5, 0.5, 0.001, 10);
  42. var pitchRatio = a.linexp(0, 1, 10, 1);
  43. ~setSynthsParam.(\pitchAttack, pitchAttack);
  44. ~setSynthsParam.(\pitchDecay, pitchDecay);
  45. ~setSynthsParam.(\pitchRatio, pitchRatio);
  46. //["Pitch envelope:", pitchAttack, pitchDecay, pitchRatio].postln;
  47. },
  48. // Pencil - Effect preset 1
  49. "/pencil", {
  50. // Apply Preset 1 effects
  51. ~filter.set(
  52. \cutoff, x.linexp(-0.5, 0.5, 100, 18000),
  53. \resonance, y.linlin(-0.5, 0.5, 0, 1),
  54. \lfoDepth, pressure.linlin(1, 8, 0, 4)
  55. );
  56. ~lfo.set(
  57. \freq, x.linlin(-0.5, 0.5, 0, 15),
  58. \waveform, 2
  59. );
  60. ~ctl.set(8, y.linlin(-0.5, 0.5, 0, 1));
  61. ~ctl.set(9, 0.5);
  62. //["Pencil preset:", "Cutoff", x.linexp(-0.5, 0.5, 20, 18000), "LFO", x.linlin(-0.5, 0.5, 0, 15)].postln;
  63. },
  64. // Crayon - Effect preset 2
  65. "/crayon", {
  66. // Apply Preset 2 effects
  67. ~lfo.set(
  68. \freq, x.linlin(-0.5, 0.5, 15, 1),
  69. \waveform, 3
  70. );
  71. ~filter.set(
  72. \cutoff, y.linexp(-0.5, 0.5, 20, 18000),
  73. \resonance, pressure.linlin(1, 5, 0, 1),
  74. \lfoDepth, x.linlin(-0.5, 0.5, 0, 3)
  75. );
  76. ~ctl.set(9, y.linlin(-0.5, 0.5, 0, 0.5)); // Reverb dry/wet
  77. ~ctl.set(7, x.linlin(-0.5, 0.5, 0.001, 1.0)); // Delay amount
  78. ~ctl.set(9, 0.8);
  79. //["Crayon preset:", "LFO", x.linlin(-0.5, 0.5, 15, 1), "Filter", y.linexp(-0.5, 0.5, 20, 18000)].postln;
  80. },
  81. // Fountain pen - Effect preset 3 (placeholder)
  82. "/fountainPen", {
  83. // Apply Preset 3 effects
  84. ~lfo.set(
  85. \freq, x.linlin(-0.5, 0.5, 1, 15),
  86. \waveform, 0
  87. );
  88. ~filter.set(
  89. \lfoDepth, 0
  90. );
  91. ~setSynthsParam.(\lfoDepth, pressure.linlin(1, 5, 0, 0.5));
  92. //["fountainPen preset:", "LFO Depth", pressure.linlin(1, 5, 0, 0.5), "LFO Freq", x.linlin(-0.5, 0.5, 1, 15)].postln;
  93. },
  94. // Water color - Effect preset 4 (placeholder)
  95. "/waterColor", {
  96. // Apply Preset 4 effects (TBD in documentation)
  97. if(a >= 0 && a < 0.20){
  98. ~ctl.set(0, x.linlin(-0.5, 0.5, -60, 20));
  99. ~ctl.set(1, y.linlin(-0.5, 0.5, -60, 20));
  100. ["Set:", y.linlin(-0.5, 0.5, -60, 20)].postln;
  101. };
  102. if( a >= 0.20 && a < 0.40) {
  103. ~ctl.set(2, x.linlin(-0.5, 0.5, 1, 15));
  104. ~ctl.set(3, y.linlin(-0.5, 0.5, 0, 1));
  105. };
  106. if(a >= 0.40 && a < 0.60) {
  107. ~ctl.set(4, x.linlin(-0.5, 0.5, 0, 1));
  108. ~ctl.set(5, y.linlin(-0.5, 0.5, 0, 1));
  109. };
  110. if(a >= 0.60 && a < 0.80) {
  111. ~ctl.set(6, x.linlin(-0.5, 0.5, 0, 1));
  112. ~ctl.set(7, y.linlin(-0.5, 0.5, 0, 1));
  113. };
  114. if(a >= 0.80 && a < 1){
  115. ~ctl.set(8, x.linlin(-0.5, 0.5, 0, 1));
  116. ~ctl.set(9, y.linlin(-0.5, 0.5, 0, 1));
  117. };
  118. ["waterColor preset:", a].postln;
  119. }
  120. );
  121. };
  122. // ----- OSC Pad Vaues -----
  123. // OSC responder for x coordinate
  124. OSCdef(\xOSC, { |msg, time, addr, port|
  125. var x = msg[1].asFloat;
  126. // Update current pad value and change effects
  127. ~currentPadValues.x = x;
  128. ~changeEffectParams.value;
  129. }, '/aspectX');
  130. // OSC responder for y coordinate
  131. OSCdef(\yOSC, { |msg, time, addr, port|
  132. var y = msg[1].asFloat;
  133. // Update current pad value and change effects
  134. ~currentPadValues.y = y;
  135. ~changeEffectParams.value;
  136. }, '/aspectY');
  137. // OSC responder for pressure coordinate
  138. OSCdef(\pressureOSC, { |msg, time, addr, port|
  139. var pressure = msg[1].asFloat;
  140. // Update current pad value and change effects
  141. ~currentPadValues.pressure = pressure;
  142. ~changeEffectParams.value;
  143. }, '/pressure');
  144. // ----- OSC Pen Types -----
  145. // OSC responder for pen
  146. OSCdef(\penOSC, { |msg, time, addr, port|
  147. var penType = msg[1].asFloat;
  148. if (penType == 1.0) {
  149. ~currentPenType = msg[0].asString;
  150. ["Current pen type:", ~currentPenType].postln;
  151. }
  152. }, '/pen');
  153. // OSC responder for monoline
  154. OSCdef(\monolineOSC, { |msg, time, addr, port|
  155. var penType = msg[1].asFloat;
  156. if (penType == 1.0) {
  157. ~currentPenType = msg[0].asString;
  158. ["Current pen type:", ~currentPenType].postln;
  159. }
  160. }, '/monoline');
  161. // OSC responder for marker
  162. OSCdef(\markerOSC, { |msg, time, addr, port|
  163. var penType = msg[1].asFloat;
  164. if (penType == 1.0) {
  165. ~currentPenType = msg[0].asString;
  166. ["Current pen type:", ~currentPenType].postln;
  167. }
  168. }, '/marker');
  169. // OSC responder for pencil
  170. OSCdef(\pencilOSC, { |msg, time, addr, port|
  171. var penType = msg[1].asFloat;
  172. if (penType == 1.0) {
  173. ~currentPenType = msg[0].asString;
  174. ["Current pen type:", ~currentPenType].postln;
  175. }
  176. }, '/pencil');
  177. // OSC responder for crayon
  178. OSCdef(\crayonOSC, { |msg, time, addr, port|
  179. var penType = msg[1].asFloat;
  180. if (penType == 1.0) {
  181. ~currentPenType = msg[0].asString;
  182. ["Current pen type:", ~currentPenType].postln;
  183. }
  184. }, '/crayon');
  185. // OSC responder for fountainPen
  186. OSCdef(\fountainPenOSC, { |msg, time, addr, port|
  187. var penType = msg[1].asFloat;
  188. if (penType == 1.0) {
  189. ~currentPenType = msg[0].asString;
  190. ["Current pen type:", ~currentPenType].postln;
  191. }
  192. }, '/fountainPen');
  193. // OSC responder for waterColor
  194. OSCdef(\waterColorOSC, { |msg, time, addr, port|
  195. var penType = msg[1].asFloat;
  196. if (penType == 1.0) {
  197. ~currentPenType = msg[0].asString;
  198. ["Current pen type:", ~currentPenType].postln;
  199. }
  200. }, '/waterColor');
  201. // ----- OSC Drawing Width -----
  202. OSCdef(\opacityOSC, { |msg, time, addr, port|
  203. var a = msg[1].asFloat;
  204. ~currentPadValues.a = a;
  205. }, '/a');
  206. // ----- OSC RGB Colors -----
  207. // OSC responder for red changes
  208. OSCdef(\redOSC, { |msg, time, addr, port|
  209. var component = msg[1].asFloat;
  210. // Update current color
  211. ~currentColor.r = component;
  212. ~setSynthsParam.(\redAmt, component);
  213. //["Color changed:", ~currentColor].postln;
  214. }, '/r');
  215. // OSC responder for green changes
  216. OSCdef(\greenOSC, { |msg, time, addr, port|
  217. var component = msg[1].asFloat;
  218. // Update current color
  219. ~currentColor.g = component;
  220. ~setSynthsParam.(\greenAmt, component);
  221. //["Color changed:", ~currentColor].postln;
  222. }, '/g');
  223. // OSC responder for blue changes
  224. OSCdef(\blueOSC, { |msg, time, addr, port|
  225. var component = msg[1].asFloat;
  226. // Update current color
  227. ~currentColor.b = component;
  228. ~setSynthsParam.(\blueAmt, component);
  229. //["Color changed:", ~currentColor].postln;
  230. }, '/b');
  231. // Start the OSC server on port 57120 (default SuperCollider port)
  232. thisProcess.openUDPPort(57120);
  233. "OSC server ready on port 57120".postln;
  234. "Ready to receive data from iDraw OSC app".postln;
  235. )