4_osc_communication.scd 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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); //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(9, y.linlin(-0.5, 0.5, 0, 1));
  61. //["Pencil preset:", "Cutoff", x.linexp(-0.5, 0.5, 20, 18000), "LFO", x.linlin(-0.5, 0.5, 0, 15)].postln;
  62. },
  63. // Crayon - Effect preset 2
  64. "/crayon", {
  65. // Apply Preset 2 effects
  66. ~lfo.set(
  67. \freq, x.linlin(-0.5, 0.5, 15, 1),
  68. \waveform, 3
  69. );
  70. ~filter.set(
  71. \cutoff, y.linexp(-0.5, 0.5, 20, 18000),
  72. \resonance, pressure.linlin(1, 5, 0, 1),
  73. \lfoDepth, x.linlin(-0.5, 0.5, 0, 3)
  74. );
  75. ~ctl.set(10, y.linlin(-0.5, 0.5, 0, 1)); // Reverb dry/wet
  76. ~ctl.set(8, x.linlin(-0.5, 0.5, 0.001, 1.0)); // Delay amount
  77. //["Crayon preset:", "LFO", x.linlin(-0.5, 0.5, 15, 1), "Filter", y.linexp(-0.5, 0.5, 20, 18000)].postln;
  78. },
  79. // Fountain pen - Effect preset 3 (placeholder)
  80. "/fountainPen", {
  81. // Apply Preset 3 effects
  82. ~lfo.set(
  83. \freq, x.linlin(-0.5, 0.5, 1, 15),
  84. \waveform, 0
  85. );
  86. ~filter.set(
  87. \lfoDepth, 0
  88. );
  89. ~setSynthsParam.(\lfoDepth, pressure.linlin(1, 5, 0, 0.5));
  90. //["fountainPen preset:", "LFO Depth", pressure.linlin(1, 5, 0, 0.5), "LFO Freq", x.linlin(-0.5, 0.5, 1, 15)].postln;
  91. },
  92. // Water color - Effect preset 4 (placeholder)
  93. "/waterColor", {
  94. // Apply Preset 4 effects (TBD in documentation)
  95. ["Water color preset (TBD)"].postln;
  96. }
  97. );
  98. };
  99. // ----- OSC Pad Vaues -----
  100. // OSC responder for x coordinate
  101. OSCdef(\xOSC, { |msg, time, addr, port|
  102. var x = msg[1].asFloat;
  103. // Update current pad value and change effects
  104. ~currentPadValues.x = x;
  105. ~changeEffectParams.value;
  106. }, '/aspectX');
  107. // OSC responder for y coordinate
  108. OSCdef(\yOSC, { |msg, time, addr, port|
  109. var y = msg[1].asFloat;
  110. // Update current pad value and change effects
  111. ~currentPadValues.y = y;
  112. ~changeEffectParams.value;
  113. }, '/aspectY');
  114. // OSC responder for pressure coordinate
  115. OSCdef(\pressureOSC, { |msg, time, addr, port|
  116. var pressure = msg[1].asFloat;
  117. // Update current pad value and change effects
  118. ~currentPadValues.pressure = pressure;
  119. ~changeEffectParams.value;
  120. }, '/pressure');
  121. // ----- OSC Pen Types -----
  122. // OSC responder for pen
  123. OSCdef(\penOSC, { |msg, time, addr, port|
  124. var penType = msg[1].asFloat;
  125. if (penType == 1.0) {
  126. ~currentPenType = msg[0].asString;
  127. ["Current pen type:", ~currentPenType].postln;
  128. }
  129. }, '/pen');
  130. // OSC responder for monoline
  131. OSCdef(\monolineOSC, { |msg, time, addr, port|
  132. var penType = msg[1].asFloat;
  133. if (penType == 1.0) {
  134. ~currentPenType = msg[0].asString;
  135. ["Current pen type:", ~currentPenType].postln;
  136. }
  137. }, '/monoline');
  138. // OSC responder for marker
  139. OSCdef(\markerOSC, { |msg, time, addr, port|
  140. var penType = msg[1].asFloat;
  141. if (penType == 1.0) {
  142. ~currentPenType = msg[0].asString;
  143. ["Current pen type:", ~currentPenType].postln;
  144. }
  145. }, '/marker');
  146. // OSC responder for pencil
  147. OSCdef(\pencilOSC, { |msg, time, addr, port|
  148. var penType = msg[1].asFloat;
  149. if (penType == 1.0) {
  150. ~currentPenType = msg[0].asString;
  151. ~initializePreset1.value;
  152. ["Current pen type:", ~currentPenType].postln;
  153. }
  154. }, '/pencil');
  155. // OSC responder for crayon
  156. OSCdef(\crayonOSC, { |msg, time, addr, port|
  157. var penType = msg[1].asFloat;
  158. if (penType == 1.0) {
  159. ~currentPenType = msg[0].asString;
  160. ~initializePreset2.value;
  161. ["Current pen type:", ~currentPenType].postln;
  162. }
  163. }, '/crayon');
  164. // OSC responder for fountainPen
  165. OSCdef(\fountainPenOSC, { |msg, time, addr, port|
  166. var penType = msg[1].asFloat;
  167. if (penType == 1.0) {
  168. ~currentPenType = msg[0].asString;
  169. ~initializePreset3.value;
  170. ["Current pen type:", ~currentPenType].postln;
  171. }
  172. }, '/fountainPen');
  173. // OSC responder for waterColor
  174. OSCdef(\waterColorOSC, { |msg, time, addr, port|
  175. var penType = msg[1].asFloat;
  176. if (penType == 1.0) {
  177. ~currentPenType = msg[0].asString;
  178. ~initializePreset4.value;
  179. ["Current pen type:", ~currentPenType].postln;
  180. }
  181. }, '/waterColor');
  182. // ----- OSC Drawing Width -----
  183. OSCdef(\opacityOSC, { |msg, time, addr, port|
  184. var a = msg[1].asFloat;
  185. ~currentPadValues.a = a;
  186. }, '/a');
  187. // ----- OSC RGB Colors -----
  188. // OSC responder for red changes
  189. OSCdef(\redOSC, { |msg, time, addr, port|
  190. var component = msg[1].asFloat;
  191. // Update current color
  192. ~currentColor.r = component;
  193. ~setSynthsParam.(\redAmt, component);
  194. //["Color changed:", ~currentColor].postln;
  195. }, '/r');
  196. // OSC responder for green changes
  197. OSCdef(\greenOSC, { |msg, time, addr, port|
  198. var component = msg[1].asFloat;
  199. // Update current color
  200. ~currentColor.g = component;
  201. ~setSynthsParam.(\greenAmt, component);
  202. //["Color changed:", ~currentColor].postln;
  203. }, '/g');
  204. // OSC responder for blue changes
  205. OSCdef(\blueOSC, { |msg, time, addr, port|
  206. var component = msg[1].asFloat;
  207. // Update current color
  208. ~currentColor.b = component;
  209. ~setSynthsParam.(\blueAmt, component);
  210. //["Color changed:", ~currentColor].postln;
  211. }, '/b');
  212. // Start the OSC server on port 57120 (default SuperCollider port)
  213. thisProcess.openUDPPort(57120);
  214. "OSC server ready on port 57120".postln;
  215. "Ready to receive data from iDraw OSC app".postln;
  216. )