Advertisement
sxiii

Pentatonic With Key Repeat

Jun 25th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.66 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. #include <stdbool.h>
  21.  
  22. #define MAX 6
  23.  
  24. int intArray[MAX];
  25. int front = 0;
  26. int rear = -1;
  27. int itemCount = 0;
  28.  
  29. int peek(){ return intArray[front]; }
  30.  
  31. bool isEmpty(){ return itemCount == 0; }
  32.  
  33. bool isFull(){ return itemCount == MAX; }
  34.  
  35. int size(){ return itemCount; }  
  36.  
  37. void insert(int data){
  38.  
  39.    if(!isFull()){
  40.    
  41.       if(rear == MAX-1){
  42.          rear = -1;            
  43.       }      
  44.  
  45.       intArray[++rear] = data;
  46.       itemCount++;
  47.    }
  48. }
  49.  
  50. int removeData(){
  51.    int data = intArray[front++];
  52.    
  53.    if(front == MAX){
  54.       front = 0;
  55.    }
  56.    
  57.    itemCount--;
  58.    return data;  
  59. }
  60.  
  61. /*
  62.   // Seems that we don't need the following libs
  63.   #include <time.h>
  64.   #include <unistd.h>
  65.   #include <sys/types.h>
  66. */
  67.  
  68. // These are arrays for pentatonic scales and also a duration array
  69. int array1[]={262,294,330,392,440}; // C major pentatonic scale (C D E G A)
  70. int array2[]={392,440,494,587,659}; // G major pentatonic scale (G A B D E)
  71. int array3[]={277,349,392,466,523}; // Eb major pentatonic scale (Eb F G Bb C)
  72. int array4[]={277,392,440,494,587}; // E minor pentatonic scale
  73. int darray[]={100,150,200,250,300}; // Duration Array
  74.  
  75.  
  76.  
  77. // This is for random generating if you would like to implement it. Not used here.
  78. int rnd( int max ) {return (rand() % max) + 1;}
  79.  
  80. // Duration is yet not used (set up to default 0.2 == 200 msec)
  81. int fork_n_execute(int frequency, int duration)
  82. {
  83.   int status;
  84.   pid_t pid;
  85.   pid = fork ();
  86.  
  87.   if(pid == 0)
  88.   {
  89.   char str[80];
  90.   sprintf(str, "play -q -n synth 0.2 square %i", frequency);
  91.   // you can replace "square" (type of waveform generated by Sox) in the previous row with any of the following:
  92.   // sine, square, triangle, sawtooth, trapetz (trapezoidal), exp (exponential), whitenoise, pinknoise, and brownnoise
  93.   system(str);
  94.   }
  95.   return status;
  96. }
  97.  
  98. int main(int argc, char *argv[])
  99. {
  100.  
  101.   while (1) {
  102.   int key = getkey();
  103.   //int dar = darray[rnd(5)];
  104.   //int del = darray[rnd(5)]*1000;
  105.   int dar = 200;
  106.   int del = 100000;
  107.  
  108.             if (key == 0x20) {printf("Key space pressed. Just test. Feel likeaboss?\n");}
  109.             if (key == 0x1B) {printf("Exit key (ESC) pressed, BB, CU!\n");exit (1);}
  110.      
  111.             if (key == 0x31) {printf("Key 1 Tone C Freq 262\n");fork_n_execute(array1[0],dar);insert(array1[0]);usleep(del);}
  112.             if (key == 0x32) {printf("Key 2 Tone D Freq 294\n");fork_n_execute(array1[1],dar);insert(array1[1]);usleep(del);}
  113.             if (key == 0x33) {printf("Key 3 Tone E Freq 330\n");fork_n_execute(array1[2],dar);insert(array1[2]);usleep(del);}
  114.             if (key == 0x34) {printf("Key 4 Tone G Freq 392\n");fork_n_execute(array1[3],dar);insert(array1[3]);usleep(del);}
  115.             if (key == 0x35) {printf("Key 5 Tone A Freq 440\n");fork_n_execute(array1[4],dar);insert(array1[4]);usleep(del);}
  116.             if (key == 0x36) {printf("Key 6 Test Repeat\n");int n = removeData(); fork_n_execute(n,dar); usleep(del); }
  117.  
  118.             if (key == 0x71) {printf("Key Q Tone G Freq 392\n");fork_n_execute(array2[0],dar);insert(array2[0]);usleep(del);}
  119.             if (key == 0x77) {printf("Key W Tone A Freq 440\n");fork_n_execute(array2[1],dar);insert(array2[1]);usleep(del);}
  120.             if (key == 0x65) {printf("Key E Tone B Freq 494\n");fork_n_execute(array2[2],dar);insert(array2[2]);usleep(del);}
  121.             if (key == 0x72) {printf("Key R Tone D Freq 587\n");fork_n_execute(array2[3],dar);insert(array2[3]);usleep(del);}
  122.             if (key == 0x74) {printf("Key T Tone E Freq 659\n");fork_n_execute(array2[4],dar);insert(array2[4]);usleep(del);}
  123.             if (key == 0x79) {printf("Key Y Test Repeat\n");int n = removeData(); fork_n_execute(n,dar); usleep(del); }
  124.      
  125.             if (key == 0x61) {printf("Key A Tone Eb Freq 277\n");fork_n_execute(array3[0],dar);insert(array3[0]);usleep(del);}
  126.             if (key == 0x73) {printf("Key S Tone F Freq 349\n"); fork_n_execute(array3[1],dar);insert(array3[1]);usleep(del);}
  127.             if (key == 0x64) {printf("Key D Tone G Freq 392\n"); fork_n_execute(array3[2],dar);insert(array3[2]);usleep(del);}
  128.             if (key == 0x66) {printf("Key F Tone Bb Freq 466\n");fork_n_execute(array3[3],dar);insert(array3[3]);usleep(del);}
  129.             if (key == 0x67) {printf("Key G Tone C Freq 523\n"); fork_n_execute(array3[4],dar);insert(array3[4]);usleep(del);}
  130.             if (key == 0x68) {printf("Key H Test Repeat\n");int n = removeData(); fork_n_execute(n,dar); usleep(del); }  
  131.  
  132.             if (key == 0x7a) {printf("Key Z Tone Eb Freq 277\n");fork_n_execute(array4[0],dar);insert(array4[0]);usleep(del);}
  133.             if (key == 0x78) {printf("Key X Tone G Freq 392\n");fork_n_execute(array4[1],dar);insert(array4[1]);usleep(del);}
  134.             if (key == 0x63) {printf("Key C Tone A Freq 440\n");fork_n_execute(array4[2],dar);insert(array4[2]);usleep(del);}
  135.             if (key == 0x76) {printf("Key V Tone B Freq 494\n");fork_n_execute(array4[3],dar);insert(array4[3]);usleep(del);}
  136.             if (key == 0x62) {printf("Key B Tone D Freq 587\n");fork_n_execute(array4[4],dar);insert(array4[4]);usleep(del);}
  137.             if (key == 0x6E) {printf("Key N Test Repeat\n");int n = removeData(); fork_n_execute(n,dar); usleep(del); }
  138.   }
  139.   return 0;
  140. }
  141.  
  142. // Function to parse keypresses. Probably a good place to improve.
  143.  
  144.     int getkey() {
  145.         int character;
  146.         struct termios orig_term_attr;
  147.         struct termios new_term_attr;
  148.         tcgetattr(fileno(stdin), &orig_term_attr);
  149.         memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  150.         new_term_attr.c_lflag &= ~(ECHO|ICANON);
  151.         new_term_attr.c_cc[VTIME] = 0;
  152.         new_term_attr.c_cc[VMIN] = 0;
  153.         tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  154.         character = fgetc(stdin);
  155.         tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  156.         return character;
  157.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement