5_osc_communication.scd 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. (
  2. // Clear any existing OSC definitions
  3. OSCdef.freeAll;
  4. // Define OSC responder for mobile touch events
  5. // Mobile touch data format will likely be different from Arduino
  6. OSCdef(\touchOSC, { |msg, time, addr, port|
  7. // Typical mobile touch data might include:
  8. // - touch ID (for multi-touch)
  9. // - x position (normalized 0-1)
  10. // - y position (normalized 0-1)
  11. // - touch state (began, moved, ended)
  12. // - pressure/force (if supported)
  13. var touchId = msg[1];
  14. var x = msg[2];
  15. var y = msg[3];
  16. var state = msg[4];
  17. var pressure = if(msg[5].notNil, { msg[5] }, { 0.7 }); // Default if not provided
  18. // Convert mobile screen coordinates to parameters
  19. var freq = x.linexp(0, 1, 100, 2000);
  20. var amp = pressure.linlin(0, 1, 0.1, 0.8);
  21. var pan = y.linlin(0, 1, -0.8, 0.8);
  22. // Log the received data
  23. ["Mobile touch:", touchId, x, y, state, pressure].postln;
  24. // Handle touch based on state
  25. switch(state,
  26. // Touch began - start a new synth
  27. \began, {
  28. var synthType = (touchId % 5).switch(
  29. 0, { \sineTone },
  30. 1, { \squareTone },
  31. 2, { \sawTone },
  32. 3, { \triTone },
  33. 4, { \fmTone }
  34. );
  35. // Create a new synth and store it by ID
  36. ~touchSynths = ~touchSynths ? ();
  37. ~touchSynths[touchId] = Synth(synthType, [
  38. \out, ~sourceBus ? 0,
  39. \freq, freq,
  40. \amp, amp,
  41. \pan, pan,
  42. \attack, 0.05,
  43. \release, 1.0
  44. ]);
  45. ["Touch began:", touchId, synthType, freq, amp].postln;
  46. },
  47. // Touch moved - update existing synth
  48. \moved, {
  49. if(~touchSynths[touchId].notNil, {
  50. ~touchSynths[touchId].set(\freq, freq, \amp, amp, \pan, pan);
  51. });
  52. },
  53. // Touch ended - release synth
  54. \ended, {
  55. if(~touchSynths[touchId].notNil, {
  56. ~touchSynths[touchId].release;
  57. ~touchSynths[touchId] = nil;
  58. });
  59. }
  60. );
  61. }, '/touch');
  62. // OSC responder for mobile control messages (e.g., UI controls)
  63. OSCdef(\controlOSC, { |msg, time, addr, port|
  64. var control = msg[1].asSymbol;
  65. var value = msg[2];
  66. ["Mobile control:", control, value].postln;
  67. // Handle different control parameters
  68. switch(control,
  69. \reverb_mix, { ~reverbSynth.set(\mix, value); },
  70. \reverb_room, { ~reverbSynth.set(\room, value); },
  71. \delay_time, { ~delaySynth.set(\delaytime, value); },
  72. \delay_feedback, { ~delaySynth.set(\feedback, value); },
  73. \filter_cutoff, { ~filterSynth.set(\cutoff, value.linexp(0, 1, 200, 10000)); }
  74. );
  75. }, '/control');
  76. // Add a separate responder for color changes from mobile UI
  77. OSCdef(\colorOSC, { |msg, time, addr, port|
  78. var colorIndex = msg[1].asInteger;
  79. // Store the current color for new touches
  80. ~currentColor = colorIndex;
  81. ["Color changed:", colorIndex].postln;
  82. }, '/color');
  83. // Start the OSC server on port 57120 (default SuperCollider port)
  84. thisProcess.openUDPPort(57120);
  85. "OSC server ready on port 57120".postln;
  86. "Registered OSC commands: /touch, /control, /color".postln;
  87. // Print the local IP address for connecting the mobile device
  88. "Connect mobile device to: %:%".format(Platform.myHostName, 57120).postln;
  89. )