Logiciels divers autour du protocole « Open Sound Control » aka OSC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

text2osc.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * WARNING, THIS IS JUST A WIP !
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <ctype.h>
  8. #include <lo/lo.h>
  9. #include "functions/senders.h"
  10. #define REMOTE_HOST "localhost" /* just loling */
  11. #define REMOTE_PORT "9000"
  12. #define MY_TEXT_ID "<@TROLL>"
  13. /* ----------------------------------------------------------------- */
  14. int verbosity = 0;
  15. char *my_id = MY_TEXT_ID;
  16. int wait_time = 100; /* in milliseconds */
  17. /* ----------------------------------------------------------------- */
  18. int megaloop(FILE *fp, lo_address loa)
  19. {
  20. int caractere, note;
  21. short x_val, y_val;
  22. int must_send_xy;
  23. int char_count;
  24. x_val = y_val = 0;
  25. must_send_xy = 1;
  26. char_count = 0;
  27. rewind(fp); /* so we can run this code again and again
  28. on the same fopened file */
  29. while (EOF != (caractere=getc(fp))) {
  30. char_count++;
  31. if (verbosity) fprintf(stderr, "car = %4d %c\n",
  32. caractere, caractere);
  33. if (isalpha(caractere)) {
  34. /* Play a sound */
  35. note=toupper(caractere) - 'A';
  36. // fprintf(stderr, "%c -> %3d\n", caractere, note);
  37. send_data_button(loa, note, 1);
  38. usleep(wait_time*1000);
  39. send_data_button(loa, note, 0);
  40. usleep(40*1000);
  41. }
  42. else if (isdigit(caractere)) {
  43. note = caractere - '0';
  44. send_data_button(loa, note, 1);
  45. usleep(wait_time*1000);
  46. send_data_button(loa, note, 0);
  47. usleep(20*1000);
  48. }
  49. else if (isblank(caractere)) {
  50. usleep(200*1000);
  51. }
  52. else if (caractere == '!') {
  53. send_data_id(loa, my_id);
  54. }
  55. /* imagine : we are a XY joystick */
  56. else if (caractere == '+') {
  57. x_val++;
  58. if (x_val > 32000) x_val = 0;
  59. must_send_xy = 1;
  60. }
  61. else if (caractere == '-') {
  62. x_val--;
  63. if (x_val < -32000) x_val = 0;
  64. must_send_xy = 1;
  65. }
  66. else if (caractere == '^') {
  67. y_val++;
  68. if (y_val > 32000) y_val = 0;
  69. must_send_xy = 1;
  70. }
  71. else if (caractere == '$') {
  72. y_val--;
  73. if (y_val < -32000) y_val = 0;
  74. must_send_xy = 1;
  75. }
  76. /* default case */
  77. else {
  78. usleep(wait_time*1000);
  79. }
  80. if (must_send_xy) {
  81. fprintf(stderr, "... X %6d Y %6d\n",
  82. x_val, y_val);
  83. send_data_xy(loa, x_val, y_val);
  84. must_send_xy = 0;
  85. }
  86. }
  87. return 0;
  88. }
  89. /* ----------------------------------------------------------------- */
  90. static void help(int k)
  91. {
  92. puts("\t * text -> osc "__DATE__" *");
  93. puts("\t-r\tremote host ("REMOTE_HOST")");
  94. puts("\t-p\tremote UDP port ("REMOTE_PORT")");
  95. puts("\t-v\tincrease verbosity");
  96. puts("\t-w\twait time unit in ms.");
  97. puts("\t-I\tchange text id (\""MY_TEXT_ID"\")");
  98. exit(0);
  99. }
  100. /* ----------------------------------------------------------------- */
  101. int main(int argc, char *argv[])
  102. {
  103. int opt;
  104. char *remote_host = REMOTE_HOST;
  105. char *remote_port = REMOTE_PORT;
  106. lo_address loaddr;
  107. FILE *fp;
  108. /* parsing command line options */
  109. while ((opt = getopt(argc, argv, "hp:r:vw:I:")) != -1) {
  110. switch (opt) {
  111. case 'h': help(0); break;
  112. case 'r': remote_host = optarg; break;
  113. case 'p': remote_port = optarg; break;
  114. case 'v': verbosity++; break;
  115. case 'w': wait_time=atoi(optarg); break;
  116. case 'I': my_id = optarg; break;
  117. default: exit(1);
  118. }
  119. }
  120. if (optind >= argc) {
  121. fprintf(stderr, "Expected a filename after options\n");
  122. exit(EXIT_FAILURE);
  123. }
  124. loaddr = lo_address_new(remote_host, remote_port);
  125. if (verbosity) {
  126. fprintf(stderr, "%s (%s) is sending to %s:%s\n",
  127. argv[0], my_id, remote_host, remote_port);
  128. }
  129. if (NULL==(fp=fopen(argv[optind], "r"))) {
  130. perror (argv[optind]);
  131. exit(1);
  132. }
  133. if (verbosity) fprintf(stderr, "now playing %s\n", argv[optind]);
  134. megaloop(fp, loaddr);
  135. return 0;
  136. }
  137. /* ----------------------------------------------------------------- */