Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <termios.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7.  
  8. #define SAMPLES_PER_PLOT 200
  9.  
  10. int main(int argc, char* argv[]) {
  11.     int input;
  12.     FILE* gnuplot;
  13.     int i;
  14.     float out_data;
  15.     unsigned char buf[SAMPLES_PER_PLOT];
  16.     unsigned char trig = 128;
  17.     unsigned freq = 1000;
  18.    
  19.     input = open("/dev/midi1", O_RDONLY);
  20.     gnuplot = popen("gnuplot", "w");
  21.    
  22.     fprintf(gnuplot, "set yrange [-0.1:5.1]\n");
  23.     while(1) {
  24.         // Wait for trig
  25.         do {
  26.             if (read(input, buf, 1) != 1) return 0;
  27.         } while (buf[0] >= trig);
  28.         do {
  29.             if (read(input, buf, 1) != 1) return 0;
  30.         } while (buf[0] < trig);
  31.        
  32.         // Read rest of the buffer
  33.         for (i=1; i<SAMPLES_PER_PLOT; i++) {
  34.             if (read(input, buf+i, 1) != 1) return 0;
  35.         }
  36.         //read(input, buf+i, SAMPLES_PER_PLOT-1);
  37.        
  38.         fprintf(gnuplot, "plot '-' with lines\n");
  39.        
  40.         for (i=0; i<SAMPLES_PER_PLOT; i++) {
  41.             out_data = buf[i]*5.0/256;
  42.             fprintf(gnuplot, "%g %g\n", (float)i/freq, out_data);
  43.         }
  44.         fprintf(gnuplot, "e\n"); // Display output
  45.         fflush(gnuplot);
  46.     }
  47.    
  48.     close(input);
  49.     pclose(gnuplot);
  50.     printf("\n");
  51.     return 0;
  52. }
  53.