5_osc_communication.scd 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, g: 0, b: 1); // Default blue
  9. // Define OSC responder for iDraw touch data
  10. OSCdef(\touchOSC, { |msg, time, addr, port|
  11. var x = msg[1].asFloat;
  12. var y = msg[2].asFloat;
  13. var pressure = msg[3].asFloat;
  14. // Log the received data
  15. ["Touch data:", x, y, pressure, ~currentPenType, ~currentColor].postln;
  16. // Handle touch based on current pen type
  17. switch(~currentPenType,
  18. // Pen - Controls amplitude envelope
  19. \pen, {
  20. var attack = x.linlin(-0.5, 0.5, 0, 5);
  21. var release = y.linlin(-0.5, 0.5, 0, 10);
  22. var freq = x.linexp(-0.5, 0.5, 100, 2000);
  23. // Generate sound with RGB-controlled waveform mix
  24. var synth = Synth(\rgbSynth, [
  25. \out, ~sourceBus ? 0,
  26. \freq, freq,
  27. \amp, pressure.linlin(1, 8, 0.1, 0.8),
  28. \attack, attack,
  29. \release, release,
  30. \redAmt, ~currentColor.r,
  31. \greenAmt, ~currentColor.g,
  32. \blueAmt, ~currentColor.b
  33. ]);
  34. ["Amplitude envelope:", attack, release].postln;
  35. },
  36. // Monoline - Controls filter envelope
  37. \monoline, {
  38. var attack = x.linlin(-0.5, 0.5, 0, 5);
  39. var release = y.linlin(-0.5, 0.5, 0, 10);
  40. var freq = x.linexp(-0.5, 0.5, 100, 2000);
  41. // Generate sound with filter envelope
  42. var synth = Synth(\rgbFilterSynth, [
  43. \out, ~sourceBus ? 0,
  44. \freq, freq,
  45. \amp, pressure.linlin(1, 8, 0.1, 0.8),
  46. \attack, attack,
  47. \release, release,
  48. \filterAttack, attack,
  49. \filterRelease, release,
  50. \redAmt, ~currentColor.r,
  51. \greenAmt, ~currentColor.g,
  52. \blueAmt, ~currentColor.b
  53. ]);
  54. ["Filter envelope:", attack, release].postln;
  55. },
  56. // Marker - Controls pitch envelope
  57. \marker, {
  58. var attack = x.linlin(-0.5, 0.5, 0, 5);
  59. var release = y.linlin(-0.5, 0.5, 0, 10);
  60. var freq = x.linexp(-0.5, 0.5, 100, 2000);
  61. // Generate sound with pitch envelope
  62. var synth = Synth(\rgbPitchSynth, [
  63. \out, ~sourceBus ? 0,
  64. \freq, freq,
  65. \amp, pressure.linlin(1, 8, 0.1, 0.8),
  66. \attack, attack,
  67. \release, release,
  68. \pitchAttack, attack,
  69. \pitchRelease, release,
  70. \redAmt, ~currentColor.r,
  71. \greenAmt, ~currentColor.g,
  72. \blueAmt, ~currentColor.b
  73. ]);
  74. ["Pitch envelope:", attack, release].postln;
  75. },
  76. // Pencil - Effect preset 1
  77. \pencil, {
  78. // Apply Preset 1 effects
  79. ~filterSynth.set(
  80. \cutoff, x.linexp(-0.5, 0.5, 20, 18000),
  81. \res, y.linlin(-0.5, 0.5, 0, 1)
  82. );
  83. ~lfoSynth.set(
  84. \freq, x.linlin(-0.5, 0.5, 0, 15),
  85. \intensity, pressure.linlin(1, 8, 0, 1)
  86. );
  87. ~reverbSynth.set(
  88. \roomsize, y.linlin(-0.5, 0.5, 0.1, 0.9)
  89. );
  90. ["Pencil preset:", "Cutoff", x.linexp(-0.5, 0.5, 20, 18000), "LFO", x.linlin(-0.5, 0.5, 0, 15)].postln;
  91. },
  92. // Crayon - Effect preset 2
  93. \crayon, {
  94. // Apply Preset 2 effects
  95. ~lfoSynth.set(
  96. \freq, x.linlin(-0.5, 0.5, 15, 1),
  97. \intensity, x.linlin(-0.5, 0.5, 0, 1)
  98. );
  99. ~delaySynth.set(
  100. \delaytime, x.linlin(-0.5, 0.5, 0.01, 1.0)
  101. );
  102. ~filterSynth.set(
  103. \cutoff, y.linexp(-0.5, 0.5, 20, 18000),
  104. \res, pressure.linlin(1, 5, 0, 1)
  105. );
  106. ~reverbSynth.set(
  107. \mix, y.linlin(-0.5, 0.5, 0, 1)
  108. );
  109. ["Crayon preset:", "LFO", x.linlin(-0.5, 0.5, 15, 1), "Filter", y.linexp(-0.5, 0.5, 20, 18000)].postln;
  110. },
  111. // Fountain pen - Effect preset 3 (placeholder)
  112. \fountainPen, {
  113. // Apply Preset 3 effects (TBD in documentation)
  114. ["Fountain pen preset (TBD)"].postln;
  115. },
  116. // Water color - Effect preset 4 (placeholder)
  117. \waterColor, {
  118. // Apply Preset 4 effects (TBD in documentation)
  119. ["Water color preset (TBD)"].postln;
  120. }
  121. );
  122. }, '/touch');
  123. // OSC responder for pen type selection
  124. OSCdef(\penTypeOSC, { |msg, time, addr, port|
  125. var penType = msg[1].asSymbol;
  126. // Update current pen type
  127. ~currentPenType = penType;
  128. ["Pen type changed:", penType].postln;
  129. // Initialize relevant effects based on pen type
  130. switch(penType,
  131. \pencil, { ~initializePreset1.value; },
  132. \crayon, { ~initializePreset2.value; },
  133. \fountainPen, { ~initializePreset3.value; },
  134. \waterColor, { ~initializePreset4.value; }
  135. );
  136. }, '/pen');
  137. // OSC responder for color changes
  138. OSCdef(\colorOSC, { |msg, time, addr, port|
  139. var r = msg[1].asFloat;
  140. var g = msg[2].asFloat;
  141. var b = msg[3].asFloat;
  142. // Update current color
  143. ~currentColor = (r: r, g: g, b: b);
  144. ["Color changed:", r, g, b].postln;
  145. }, '/color');
  146. // Start the OSC server on port 57120 (default SuperCollider port)
  147. thisProcess.openUDPPort(57120);
  148. "OSC server ready on port 57120".postln;
  149. "Registered OSC commands: /touch, /pen, /color".postln;
  150. "Ready to receive data from iDraw OSC app".postln;
  151. )