4_osc_communication.scd 9.3 KB

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