5_osc_communication.scd 7.3 KB

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