4_osc_communication.scd 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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) {
  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. }
  119. );
  120. };
  121. // ----- OSC Pad Vaues -----
  122. // OSC responder for x coordinate
  123. OSCdef(\xOSC, { |msg, time, addr, port|
  124. var x = msg[1].asFloat;
  125. // Update current pad value and change effects
  126. ~currentPadValues.x = x;
  127. ~changeEffectParams.value;
  128. }, '/aspectX');
  129. // OSC responder for y coordinate
  130. OSCdef(\yOSC, { |msg, time, addr, port|
  131. var y = msg[1].asFloat;
  132. // Update current pad value and change effects
  133. ~currentPadValues.y = y;
  134. ~changeEffectParams.value;
  135. }, '/aspectY');
  136. // OSC responder for pressure coordinate
  137. OSCdef(\pressureOSC, { |msg, time, addr, port|
  138. var pressure = msg[1].asFloat;
  139. // Update current pad value and change effects
  140. ~currentPadValues.pressure = pressure;
  141. ~changeEffectParams.value;
  142. }, '/pressure');
  143. // ----- OSC Pen Types -----
  144. // OSC responder for pen
  145. OSCdef(\penOSC, { |msg, time, addr, port|
  146. var penType = msg[1].asFloat;
  147. if (penType == 1.0) {
  148. ~currentPenType = msg[0].asString;
  149. ["Current pen type:", ~currentPenType].postln;
  150. }
  151. }, '/pen');
  152. // OSC responder for monoline
  153. OSCdef(\monolineOSC, { |msg, time, addr, port|
  154. var penType = msg[1].asFloat;
  155. if (penType == 1.0) {
  156. ~currentPenType = msg[0].asString;
  157. ["Current pen type:", ~currentPenType].postln;
  158. }
  159. }, '/monoline');
  160. // OSC responder for marker
  161. OSCdef(\markerOSC, { |msg, time, addr, port|
  162. var penType = msg[1].asFloat;
  163. if (penType == 1.0) {
  164. ~currentPenType = msg[0].asString;
  165. ["Current pen type:", ~currentPenType].postln;
  166. }
  167. }, '/marker');
  168. // OSC responder for pencil
  169. OSCdef(\pencilOSC, { |msg, time, addr, port|
  170. var penType = msg[1].asFloat;
  171. if (penType == 1.0) {
  172. ~currentPenType = msg[0].asString;
  173. ["Current pen type:", ~currentPenType].postln;
  174. }
  175. }, '/pencil');
  176. // OSC responder for crayon
  177. OSCdef(\crayonOSC, { |msg, time, addr, port|
  178. var penType = msg[1].asFloat;
  179. if (penType == 1.0) {
  180. ~currentPenType = msg[0].asString;
  181. ["Current pen type:", ~currentPenType].postln;
  182. }
  183. }, '/crayon');
  184. // OSC responder for fountainPen
  185. OSCdef(\fountainPenOSC, { |msg, time, addr, port|
  186. var penType = msg[1].asFloat;
  187. if (penType == 1.0) {
  188. ~currentPenType = msg[0].asString;
  189. ["Current pen type:", ~currentPenType].postln;
  190. }
  191. }, '/fountainPen');
  192. // OSC responder for waterColor
  193. OSCdef(\waterColorOSC, { |msg, time, addr, port|
  194. var penType = msg[1].asFloat;
  195. if (penType == 1.0) {
  196. ~currentPenType = msg[0].asString;
  197. ["Current pen type:", ~currentPenType].postln;
  198. }
  199. }, '/waterColor');
  200. // ----- OSC Drawing Width -----
  201. OSCdef(\opacityOSC, { |msg, time, addr, port|
  202. var a = msg[1].asFloat;
  203. ~currentPadValues.a = a;
  204. }, '/a');
  205. // ----- OSC RGB Colors -----
  206. // OSC responder for red changes
  207. OSCdef(\redOSC, { |msg, time, addr, port|
  208. var component = msg[1].asFloat;
  209. // Update current color
  210. ~currentColor.r = component;
  211. ~setSynthsParam.(\redAmt, component);
  212. //["Color changed:", ~currentColor].postln;
  213. }, '/r');
  214. // OSC responder for green changes
  215. OSCdef(\greenOSC, { |msg, time, addr, port|
  216. var component = msg[1].asFloat;
  217. // Update current color
  218. ~currentColor.g = component;
  219. ~setSynthsParam.(\greenAmt, component);
  220. //["Color changed:", ~currentColor].postln;
  221. }, '/g');
  222. // OSC responder for blue changes
  223. OSCdef(\blueOSC, { |msg, time, addr, port|
  224. var component = msg[1].asFloat;
  225. // Update current color
  226. ~currentColor.b = component;
  227. ~setSynthsParam.(\blueAmt, component);
  228. //["Color changed:", ~currentColor].postln;
  229. }, '/b');
  230. // Start the OSC server on port 57120 (default SuperCollider port)
  231. thisProcess.openUDPPort(57120);
  232. "OSC server ready on port 57120".postln;
  233. "Ready to receive data from iDraw OSC app".postln;
  234. )