5_osc_communication.scd 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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); //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. // Log the received data
  16. ["Touch data:", x, y, pressure, ~currentPenType, ~currentColor].postln;
  17. // Handle touch based on current pen type
  18. switch(~currentPenType,
  19. // Pen - Controls amplitude envelope
  20. "/pen", {
  21. var ampAttack = y.linexp(-0.5, 0.5, 0.001, 5);
  22. var ampRelease = x.linexp(-0.5, 0.5, 0.001, 10);
  23. ~setSynthsParam.(\ampAttack, ampAttack);
  24. ~setSynthsParam.(\ampRelease, ampRelease);
  25. ["Amplitude envelope:", ampAttack, ampRelease].postln;
  26. },
  27. // Monoline - Controls filter envelope
  28. "/monoline", {
  29. var filterAttack = y.linexp(-0.5, 0.5, 0.001, 5);
  30. var filterRelease = x.linexp(-0.5, 0.5, 0.001, 10);
  31. ~setSynthsParam.(\filterAttack, filterAttack);
  32. ~setSynthsParam.(\filterRelease, filterRelease);
  33. ["Filter envelope:", filterAttack, filterRelease].postln;
  34. },
  35. // Marker - Controls pitch envelope
  36. "/marker", {
  37. var pitchAttack = y.linexp(-0.5, 0.5, 0.001, 5);
  38. var pitchRelease = x.linexp(-0.5, 0.5, 0.001, 10);
  39. ~setSynthsParam.(\pitchAttack, pitchAttack);
  40. ~setSynthsParam.(\pitchRelease, pitchRelease);
  41. ["Pitch envelope:", pitchAttack, pitchRelease].postln;
  42. },
  43. // Pencil - Effect preset 1
  44. "/pencil", {
  45. // Apply Preset 1 effects
  46. ~filterSynth.set(
  47. \cutoff, x.linexp(-0.5, 0.5, 20, 18000),
  48. \res, y.linlin(-0.5, 0.5, 0, 1)
  49. );
  50. ~lfoSynth.set(
  51. \freq, x.linlin(-0.5, 0.5, 0, 15),
  52. \intensity, pressure.linlin(1, 8, 0, 1)
  53. );
  54. ~reverbSynth.set(
  55. \roomsize, y.linlin(-0.5, 0.5, 0.1, 0.9)
  56. );
  57. ["Pencil preset:", "Cutoff", x.linexp(-0.5, 0.5, 20, 18000), "LFO", x.linlin(-0.5, 0.5, 0, 15)].postln;
  58. },
  59. // Crayon - Effect preset 2
  60. "/crayon", {
  61. // Apply Preset 2 effects
  62. ~lfoSynth.set(
  63. \freq, x.linlin(-0.5, 0.5, 15, 1),
  64. \intensity, x.linlin(-0.5, 0.5, 0, 1)
  65. );
  66. ~delaySynth.set(
  67. \delaytime, x.linlin(-0.5, 0.5, 0.01, 1.0)
  68. );
  69. ~filterSynth.set(
  70. \cutoff, y.linexp(-0.5, 0.5, 20, 18000),
  71. \res, pressure.linlin(1, 5, 0, 1)
  72. );
  73. ~reverbSynth.set(
  74. \mix, y.linlin(-0.5, 0.5, 0, 1)
  75. );
  76. ["Crayon preset:", "LFO", x.linlin(-0.5, 0.5, 15, 1), "Filter", y.linexp(-0.5, 0.5, 20, 18000)].postln;
  77. },
  78. // Fountain pen - Effect preset 3 (placeholder)
  79. "/fountainPen", {
  80. // Apply Preset 3 effects (TBD in documentation)
  81. ["Fountain pen preset (TBD)"].postln;
  82. },
  83. // Water color - Effect preset 4 (placeholder)
  84. "/waterColor", {
  85. // Apply Preset 4 effects (TBD in documentation)
  86. ["Water color preset (TBD)"].postln;
  87. }
  88. );
  89. };
  90. // ----- OSC Pad Vaues -----
  91. // OSC responder for x coordinate
  92. OSCdef(\xOSC, { |msg, time, addr, port|
  93. var x = msg[1].asFloat;
  94. // Update current pad value and change effects
  95. ~currentPadValues.x = x;
  96. ~changeEffectParams.value;
  97. }, '/aspectX');
  98. // OSC responder for y coordinate
  99. OSCdef(\yOSC, { |msg, time, addr, port|
  100. var y = msg[1].asFloat;
  101. // Update current pad value and change effects
  102. ~currentPadValues.y = y;
  103. ~changeEffectParams.value;
  104. }, '/aspectY');
  105. // OSC responder for pressure coordinate
  106. OSCdef(\pressureOSC, { |msg, time, addr, port|
  107. var pressure = msg[1].asFloat;
  108. // Update current pad value and change effects
  109. ~currentPadValues.pressure = pressure;
  110. ~changeEffectParams.value;
  111. }, '/pressure');
  112. // ----- OSC Pen Types -----
  113. // OSC responder for pen
  114. OSCdef(\penOSC, { |msg, time, addr, port|
  115. var penType = msg[1].asFloat;
  116. if (penType == 1.0) {
  117. ~currentPenType = msg[0].asString;
  118. ["Current pen type:", ~currentPenType].postln;
  119. }
  120. }, '/pen');
  121. // OSC responder for monoline
  122. OSCdef(\monolineOSC, { |msg, time, addr, port|
  123. var penType = msg[1].asFloat;
  124. if (penType == 1.0) {
  125. ~currentPenType = msg[0].asString;
  126. ["Current pen type:", ~currentPenType].postln;
  127. }
  128. }, '/monoline');
  129. // OSC responder for marker
  130. OSCdef(\markerOSC, { |msg, time, addr, port|
  131. var penType = msg[1].asFloat;
  132. if (penType == 1.0) {
  133. ~currentPenType = msg[0].asString;
  134. ["Current pen type:", ~currentPenType].postln;
  135. }
  136. }, '/marker');
  137. // OSC responder for pencil
  138. OSCdef(\pencilOSC, { |msg, time, addr, port|
  139. var penType = msg[1].asFloat;
  140. if (penType == 1.0) {
  141. ~currentPenType = msg[0].asString;
  142. ~initializePreset1.value;
  143. ["Current pen type:", ~currentPenType].postln;
  144. }
  145. }, '/pencil');
  146. // OSC responder for crayon
  147. OSCdef(\crayonOSC, { |msg, time, addr, port|
  148. var penType = msg[1].asFloat;
  149. if (penType == 1.0) {
  150. ~currentPenType = msg[0].asString;
  151. ~initializePreset2.value;
  152. ["Current pen type:", ~currentPenType].postln;
  153. }
  154. }, '/crayon');
  155. // OSC responder for fountainPen
  156. OSCdef(\fountainPenOSC, { |msg, time, addr, port|
  157. var penType = msg[1].asFloat;
  158. if (penType == 1.0) {
  159. ~currentPenType = msg[0].asString;
  160. ~initializePreset3.value;
  161. ["Current pen type:", ~currentPenType].postln;
  162. }
  163. }, '/fountainPen');
  164. // OSC responder for waterColor
  165. OSCdef(\waterColorOSC, { |msg, time, addr, port|
  166. var penType = msg[1].asFloat;
  167. if (penType == 1.0) {
  168. ~currentPenType = msg[0].asString;
  169. ~initializePreset4.value;
  170. ["Current pen type:", ~currentPenType].postln;
  171. }
  172. }, '/waterColor');
  173. // ----- OSC RGB Colors -----
  174. // OSC responder for red changes
  175. OSCdef(\redOSC, { |msg, time, addr, port|
  176. var component = msg[1].asFloat;
  177. // Update current color
  178. ~currentColor.r = component;
  179. ~setSynthsParam.(\redAmt, component);
  180. ["Color changed:", ~currentColor].postln;
  181. }, '/r');
  182. // OSC responder for green changes
  183. OSCdef(\greenOSC, { |msg, time, addr, port|
  184. var component = msg[1].asFloat;
  185. // Update current color
  186. ~currentColor.g = component;
  187. ~setSynthsParam.(\greenAmt, component);
  188. ["Color changed:", ~currentColor].postln;
  189. }, '/g');
  190. // OSC responder for blue changes
  191. OSCdef(\blueOSC, { |msg, time, addr, port|
  192. var component = msg[1].asFloat;
  193. // Update current color
  194. ~currentColor.b = component;
  195. ~setSynthsParam.(\blueAmt, component);
  196. ["Color changed:", ~currentColor].postln;
  197. }, '/b');
  198. // Start the OSC server on port 57120 (default SuperCollider port)
  199. thisProcess.openUDPPort(57120);
  200. "OSC server ready on port 57120".postln;
  201. "Registered OSC commands: /touch, /pen, /color".postln;
  202. "Ready to receive data from iDraw OSC app".postln;
  203. )