sxiii

Keyboard Pentatonic Jukebox (requires sox for playback!)

Feb 4th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 KB | None | 0 0
  1. /*
  2. :: :: DESCRIPTION ::
  3. This is C source for small musical pentatonic toy-program for Linux that I wrote.
  4. This program has C major, G major, Eb major and E minor pentatonic scales.
  5. You can play them right from keyboard (12345, QWERT, ASDFG, ZXCVB).
  6. Save this text as pentatonic.c file, navigate with terminal and...
  7. ...compile with: gcc -o pentatonic pentatonic.c; then run with ./pentatonic
  8. Tested on Ubuntu 13.04 Alpha with gcc/g++ (Ubuntu/Linaro 4.7.2-19ubuntu1)
  9. NOTE 1. It does need Sox to work, since command "play" is used.
  10. NOTE 2. The code is in work for now. It is not final version.
  11. NOTE 3. I am totally newbie in C, so there might be many other
  12. possibilities of replicating my work in other, more correct and easy way.
  13. Created 4 feb 2013 by Security XIII at Gmail Dot Com
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <termios.h>
  20.  
  21. /*
  22.   // Seems that we don't need the following libs
  23.   #include <time.h>
  24.   #include <unistd.h>
  25.   #include <sys/types.h>
  26. */
  27.  
  28. // These are arrays for pentatonic scales and also a duration array
  29. int array1[]={262,294,330,392,440}; // C major pentatonic scale (C D E G A)
  30. int array2[]={392,440,494,587,659}; // G major pentatonic scale (G A B D E)
  31. int array3[]={277,349,392,466,523}; // Eb major pentatonic scale (Eb F G Bb C)
  32. int array4[]={277,392,440,494,587}; // E minor pentatonic scale
  33. int darray[]={100,150,200,250,300}; // Duration Array
  34.  
  35. // This is for random generating if you would like to implement it. Not used here.
  36. int rnd( int max ) {return (rand() % max) + 1;}
  37.  
  38. // Duration is yet not used (set up to default 0.2 == 200 msec)
  39. int fork_n_execute(int frequency, int duration)
  40. {
  41.   int status;
  42.   pid_t pid;
  43.   pid = fork ();
  44.  
  45.   if(pid == 0)
  46.   {
  47.   char str[80];
  48.   sprintf(str, "play -q -n synth 0.2 square %i", frequency);
  49.   // you can replace "square" (type of waveform generated by Sox) in the previous row with any of the following:
  50.   // sine, square, triangle, sawtooth, trapetz (trapezoidal), exp (exponential), whitenoise, pinknoise, and brownnoise
  51.   system(str);
  52.   }
  53.   return status;
  54. }
  55.  
  56. int main(int argc, char *argv[])
  57. {
  58.  
  59.   while (1) {
  60.   int key = getkey();
  61.   //int dar = darray[rnd(5)];
  62.   //int del = darray[rnd(5)]*1000;
  63.   int dar = 200;
  64.   int del = 100000;
  65.  
  66.             if (key == 0x20) {printf("Key space pressed. Just test. Feel likeaboss?\n");}
  67.             if (key == 0x1B) {printf("Exit key (ESC) pressed, BB, CU!\n");exit (1);}
  68.      
  69.             if (key == 0x31) {printf("Key 1 Tone C Freq 262\n");fork_n_execute(array1[0],dar);usleep(del);}
  70.             if (key == 0x32) {printf("Key 2 Tone D Freq 294\n");fork_n_execute(array1[1],dar);usleep(del);}
  71.             if (key == 0x33) {printf("Key 3 Tone E Freq 330\n");fork_n_execute(array1[2],dar);usleep(del);}
  72.             if (key == 0x34) {printf("Key 4 Tone G Freq 392\n");fork_n_execute(array1[3],dar);usleep(del);}
  73.             if (key == 0x35) {printf("Key 5 Tone A Freq 440\n");fork_n_execute(array1[4],dar);usleep(del);}
  74.      
  75.             if (key == 0x71) {printf("Key Q Tone G Freq 392\n");fork_n_execute(array2[0],dar);usleep(del);}
  76.             if (key == 0x77) {printf("Key W Tone A Freq 440\n");fork_n_execute(array2[1],dar);usleep(del);}
  77.             if (key == 0x65) {printf("Key E Tone B Freq 494\n");fork_n_execute(array2[2],dar);usleep(del);}
  78.             if (key == 0x72) {printf("Key R Tone D Freq 587\n");fork_n_execute(array2[3],dar);usleep(del);}
  79.             if (key == 0x74) {printf("Key T Tone E Freq 659\n");fork_n_execute(array2[4],dar);usleep(del);}
  80.      
  81.             if (key == 0x61) {printf("Key A Tone Eb Freq 277\n");fork_n_execute(array3[0],dar);usleep(del);}
  82.             if (key == 0x73) {printf("Key S Tone F Freq 349\n"); fork_n_execute(array3[1],dar);usleep(del);}
  83.             if (key == 0x64) {printf("Key D Tone G Freq 392\n"); fork_n_execute(array3[2],dar);usleep(del);}
  84.             if (key == 0x66) {printf("Key F Tone Bb Freq 466\n");fork_n_execute(array3[3],dar);usleep(del);}
  85.             if (key == 0x67) {printf("Key G Tone C Freq 523\n"); fork_n_execute(array3[4],dar);usleep(del);}
  86.      
  87.             if (key == 0x7a) {printf("Key Z Tone Eb Freq 277\n");fork_n_execute(array4[0],dar);usleep(del);}
  88.             if (key == 0x78) {printf("Key X Tone G Freq 392\n");fork_n_execute(array4[1],dar);usleep(del);}
  89.             if (key == 0x63) {printf("Key C Tone A Freq 440\n");fork_n_execute(array4[2],dar);usleep(del);}
  90.             if (key == 0x76) {printf("Key V Tone B Freq 494\n");fork_n_execute(array4[3],dar);usleep(del);}
  91.             if (key == 0x62) {printf("Key B Tone D Freq 587\n");fork_n_execute(array4[4],dar);usleep(del);}
  92.   }
  93.   return 0;
  94. }
  95.  
  96. // Function to parse keypresses. Probably a good place to improve.
  97.  
  98.     int getkey() {
  99.         int character;
  100.         struct termios orig_term_attr;
  101.         struct termios new_term_attr;
  102.         tcgetattr(fileno(stdin), &orig_term_attr);
  103.         memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  104.         new_term_attr.c_lflag &= ~(ECHO|ICANON);
  105.         new_term_attr.c_cc[VTIME] = 0;
  106.         new_term_attr.c_cc[VMIN] = 0;
  107.         tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  108.         character = fgetc(stdin);
  109.         tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  110.         return character;
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment