5_osc_communication.scd 7.1 KB

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