sxiii icon

Pentatonic With Key Repeat

sxiii | PRO | 06/25/16 05:53:12 PM UTC | 0 ⭐ | 518 👁️ | Never ⏰ | []
C |

6.66 KB

|

None

|

0 👍

/

0 👎

/*
:: :: DESCRIPTION ::
This is C source for small musical pentatonic toy-program for Linux that I wrote.
This program has C major, G major, Eb major and E minor pentatonic scales.
You can play them right from keyboard (12345, QWERT, ASDFG, ZXCVB).
Save this text as pentatonic.c file, navigate with terminal and...
...compile with: gcc -o pentatonic pentatonic.c; then run with ./pentatonic
Tested on Ubuntu 13.04 Alpha with gcc/g++ (Ubuntu/Linaro 4.7.2-19ubuntu1) 
NOTE 1. It does need Sox to work, since command "play" is used.
NOTE 2. The code is in work for now. It is not final version.
NOTE 3. I am totally newbie in C, so there might be many other 
possibilities of replicating my work in other, more correct and easy way.
Created 4 feb 2013 by Security XIII at Gmail Dot Com
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <stdbool.h>
 
#define MAX 6
 
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
 
int peek(){ return intArray[front]; }
 
bool isEmpty(){ return itemCount == 0; }
 
bool isFull(){ return itemCount == MAX; }
 
int size(){ return itemCount; }  
 
void insert(int data){
 
   if(!isFull()){
    
      if(rear == MAX-1){
         rear = -1;            
      }       
 
      intArray[++rear] = data;
      itemCount++;
   }
}
 
int removeData(){
   int data = intArray[front++];
    
   if(front == MAX){
      front = 0;
   }
    
   itemCount--;
   return data;  
}
 
/* 
  // Seems that we don't need the following libs
  #include <time.h>
  #include <unistd.h>
  #include <sys/types.h>
*/
 
// These are arrays for pentatonic scales and also a duration array
int array1[]={262,294,330,392,440}; // C major pentatonic scale (C D E G A)
int array2[]={392,440,494,587,659}; // G major pentatonic scale (G A B D E)
int array3[]={277,349,392,466,523}; // Eb major pentatonic scale (Eb F G Bb C)
int array4[]={277,392,440,494,587}; // E minor pentatonic scale
int darray[]={100,150,200,250,300}; // Duration Array
 
 
 
// This is for random generating if you would like to implement it. Not used here.
int rnd( int max ) {return (rand() % max) + 1;} 
 
// Duration is yet not used (set up to default 0.2 == 200 msec)
int fork_n_execute(int frequency, int duration)
{
  int status;
  pid_t pid;
  pid = fork ();
  
  if(pid == 0)
  {
  char str[80];
  sprintf(str, "play -q -n synth 0.2 square %i", frequency);
  // you can replace "square" (type of waveform generated by Sox) in the previous row with any of the following:
  // sine, square, triangle, sawtooth, trapetz (trapezoidal), exp (exponential), whitenoise, pinknoise, and brownnoise
  system(str);
  }
  return status;
}
 
int main(int argc, char *argv[])
{
 
  while (1) {
  int key = getkey();
  //int dar = darray[rnd(5)];
  //int del = darray[rnd(5)]*1000;
  int dar = 200;
  int del = 100000;
  
            if (key == 0x20) {printf("Key space pressed. Just test. Feel likeaboss?\n");}
            if (key == 0x1B) {printf("Exit key (ESC) pressed, BB, CU!\n");exit (1);}
     
            if (key == 0x31) {printf("Key 1 Tone C Freq 262\n");fork_n_execute(array1[0],dar);insert(array1[0]);usleep(del);}
            if (key == 0x32) {printf("Key 2 Tone D Freq 294\n");fork_n_execute(array1[1],dar);insert(array1[1]);usleep(del);}
            if (key == 0x33) {printf("Key 3 Tone E Freq 330\n");fork_n_execute(array1[2],dar);insert(array1[2]);usleep(del);}
            if (key == 0x34) {printf("Key 4 Tone G Freq 392\n");fork_n_execute(array1[3],dar);insert(array1[3]);usleep(del);}
            if (key == 0x35) {printf("Key 5 Tone A Freq 440\n");fork_n_execute(array1[4],dar);insert(array1[4]);usleep(del);}
            if (key == 0x36) {printf("Key 6 Test Repeat\n");int n = removeData(); fork_n_execute(n,dar); usleep(del); } 
 
            if (key == 0x71) {printf("Key Q Tone G Freq 392\n");fork_n_execute(array2[0],dar);insert(array2[0]);usleep(del);}
            if (key == 0x77) {printf("Key W Tone A Freq 440\n");fork_n_execute(array2[1],dar);insert(array2[1]);usleep(del);}
            if (key == 0x65) {printf("Key E Tone B Freq 494\n");fork_n_execute(array2[2],dar);insert(array2[2]);usleep(del);}
            if (key == 0x72) {printf("Key R Tone D Freq 587\n");fork_n_execute(array2[3],dar);insert(array2[3]);usleep(del);}
            if (key == 0x74) {printf("Key T Tone E Freq 659\n");fork_n_execute(array2[4],dar);insert(array2[4]);usleep(del);}
            if (key == 0x79) {printf("Key Y Test Repeat\n");int n = removeData(); fork_n_execute(n,dar); usleep(del); }
     
            if (key == 0x61) {printf("Key A Tone Eb Freq 277\n");fork_n_execute(array3[0],dar);insert(array3[0]);usleep(del);}
            if (key == 0x73) {printf("Key S Tone F Freq 349\n"); fork_n_execute(array3[1],dar);insert(array3[1]);usleep(del);}
            if (key == 0x64) {printf("Key D Tone G Freq 392\n"); fork_n_execute(array3[2],dar);insert(array3[2]);usleep(del);}
            if (key == 0x66) {printf("Key F Tone Bb Freq 466\n");fork_n_execute(array3[3],dar);insert(array3[3]);usleep(del);}
            if (key == 0x67) {printf("Key G Tone C Freq 523\n"); fork_n_execute(array3[4],dar);insert(array3[4]);usleep(del);}
            if (key == 0x68) {printf("Key H Test Repeat\n");int n = removeData(); fork_n_execute(n,dar); usleep(del); }  
 
            if (key == 0x7a) {printf("Key Z Tone Eb Freq 277\n");fork_n_execute(array4[0],dar);insert(array4[0]);usleep(del);}
            if (key == 0x78) {printf("Key X Tone G Freq 392\n");fork_n_execute(array4[1],dar);insert(array4[1]);usleep(del);}
            if (key == 0x63) {printf("Key C Tone A Freq 440\n");fork_n_execute(array4[2],dar);insert(array4[2]);usleep(del);}
            if (key == 0x76) {printf("Key V Tone B Freq 494\n");fork_n_execute(array4[3],dar);insert(array4[3]);usleep(del);}
            if (key == 0x62) {printf("Key B Tone D Freq 587\n");fork_n_execute(array4[4],dar);insert(array4[4]);usleep(del);}
            if (key == 0x6E) {printf("Key N Test Repeat\n");int n = removeData(); fork_n_execute(n,dar); usleep(del); }
  }
  return 0;
}
 
// Function to parse keypresses. Probably a good place to improve.
 
    int getkey() {
        int character;
        struct termios orig_term_attr;
        struct termios new_term_attr;
        tcgetattr(fileno(stdin), &orig_term_attr);
        memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
        new_term_attr.c_lflag &= ~(ECHO|ICANON);
        new_term_attr.c_cc[VTIME] = 0;
        new_term_attr.c_cc[VMIN] = 0;
        tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
        character = fgetc(stdin);
        tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
        return character;
    }

Comments