sxiii

Pentatonic Music Box on C for Linux & PC-Speaker (Hang Drum)

Jan 23rd, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 KB | None | 0 0
  1. // This is C source for small musical pentatonic toy-program for Linux that I wrote.
  2. // :: :: DESCRIPTION ::
  3. // This program has C major, G major, Eb major and E minor pentatonic scales.
  4. // You can play them right from keyboard (12345, QWERT, ASDFG, ZXCVB).
  5. // Save this text as pentatonic.c file, navigate with terminal and...
  6. // ...compile with: gcc -o pentatonic pentatonic.c; then run with ./pentatonic
  7. // Tested on Ubuntu 13.04 Alpha with gcc/g++ (Ubuntu/Linaro 4.7.2-19ubuntu1)
  8. // It has several problems to fix but anyway it works.
  9. // :: :: DEPENDENCIES :: Not really but a sort of...
  10. // You might need to attach the "pcspkr" module (sudo modprobe pcspkr)
  11. // And maybe also to grant 777 permissions to /dev/console (sudo chmod 777 /dev/console)
  12. // Check if PC Speaker works correctly by "beep" program:
  13. // sudo apt-get install beep
  14. // beep -f 100 -l 1000
  15. // :: :: PROBLEMS :: (I'll be really glad if someone can help me fix them) :: ::
  16. // Problem 1: too large code for such a small program
  17. // Solution 1: improve code, make it smaller and smarter
  18. // Problem 2: program don't want to play the tunes until exited, so I have to exit after each key-press and re-run the program again with the following bash script:
  19. // Solution 2: execute in bash: while true; do ./pentatonic; done
  20. // Authored 23.01.2013 by Security XIII [at gmail dot com]
  21. // P. S. Sorry, I have not much experience in C
  22.  
  23. #include <stdio.h> // I am not sure maybe we don't need some of the libs,
  24. #include <stdlib.h> // But I'd better leave them here for now.
  25. #include <string.h>
  26. #include <termios.h>
  27. #include <time.h>
  28. #include <unistd.h>
  29. #define ESC 27
  30. int array1[]={262,294,330,392,440}; // C major pentatonic scale
  31. int array2[]={392,440,494,587,659}; // G major pentatonic scale
  32. int array3[]={277,349,392,466,523}; // Eb major pentatonic scale
  33. int array4[]={277,392,440,494,587}; // E minor pentatonic scale
  34. int a1 = 50; // This is note value, the larger it is, the longer note sounds.
  35. int key;
  36.  
  37. int getkey() { // Probably somewhere here starts the code that prevents program from
  38.     int character;  // sounding correctly
  39.     struct termios orig_term_attr;
  40.     struct termios new_term_attr;
  41.     tcgetattr(fileno(stdin), &orig_term_attr);
  42.     memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  43.     new_term_attr.c_lflag &= ~(ECHO|ICANON);
  44.     new_term_attr.c_cc[VTIME] = 0;
  45.     new_term_attr.c_cc[VMIN] = 0;
  46.     tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  47.     character = fgetc(stdin);
  48.     tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  49.     return character;
  50. }
  51.  
  52. void beep (int frequency, int duration) // this code seems to be small enough and is okay
  53. {
  54.   FILE *tty;
  55.   if ( NULL == (tty = fopen ("/dev/console", "w")) ) {fprintf (stderr, "Cannot write to /dev/console!\n" ); exit (1); }
  56.   fprintf(tty, "%c[10;%d]%c[11;%d]\a", ESC, frequency, ESC, duration);
  57. }
  58.  
  59. int main()
  60. {
  61.     while (1) {
  62.         key = getkey();
  63.  
  64.         // Here all the magic starts.
  65.         // Note: I have to use break condition because otherwise program just
  66.         // prints the buttons that I press but there are no sound until you
  67.         // exit it with ESC or Ctrl+C. So I leave break here... Have to do smth.
  68.         // If I replace "break;" with "execl("pentatonic",NULL,NULL);" (self re-run) it
  69.         // also doesen't work... >,,<
  70.  
  71.         if (key == 0x20) {printf("Key space pressed. Just test. Feel likeaboss?\n");}
  72.         if (key == 0x1B) {printf("Exit key (ESC) pressed, BB!\n");exit (1);}
  73.  
  74.     if (key == 0x31) {printf("Key 1 Tone C Freq 262\n");beep(array1[0],a1);break;}
  75.     if (key == 0x32) {printf("Key 2 Tone D Freq 294\n");beep(array1[1],a1);break;}
  76.     if (key == 0x33) {printf("Key 3 Tone E Freq 330\n");beep(array1[2],a1);break;}
  77.     if (key == 0x34) {printf("Key 4 Tone G Freq 392\n");beep(array1[3],a1);break;}
  78.     if (key == 0x35) {printf("Key 5 Tone A Freq 440\n");beep(array1[4],a1);break;}
  79.  
  80.     if (key == 0x71) {printf("Key Q Tone G Freq 392\n");beep(array2[0],a1);break;}
  81.     if (key == 0x77) {printf("Key W Tone A Freq 440\n");beep(array2[1],a1);break;}
  82.     if (key == 0x65) {printf("Key E Tone B Freq 494\n");beep(array2[2],a1);break;}
  83.     if (key == 0x72) {printf("Key R Tone D Freq 587\n");beep(array2[3],a1);break;}
  84.     if (key == 0x74) {printf("Key T Tone E Freq 659\n");beep(array2[4],a1);break;}
  85.  
  86.     if (key == 0x61) {printf("Key A Tone Eb Freq 277\n");beep(array3[0],a1);break;}
  87.     if (key == 0x73) {printf("Key S Tone F Freq 349\n");beep(array3[1],a1);break;}
  88.     if (key == 0x64) {printf("Key D Tone G Freq 392\n");beep(array3[2],a1);break;}
  89.     if (key == 0x66) {printf("Key F Tone Bb Freq 466\n");beep(array3[3],a1);break;}
  90.     if (key == 0x67) {printf("Key G Tone C Freq 523\n");beep(array3[4],a1);break;}
  91.  
  92.     if (key == 0x7a) {printf("Key Z Tone Eb Freq 277\n");beep(array4[0],a1);break;}
  93.     if (key == 0x78) {printf("Key X Tone G Freq 392\n");beep(array4[1],a1);break;}
  94.     if (key == 0x63) {printf("Key C Tone A Freq 440\n");beep(array4[2],a1);break;}
  95.     if (key == 0x76) {printf("Key V Tone B Freq 494\n");beep(array4[3],a1);break;}
  96.     if (key == 0x62) {printf("Key B Tone D Freq 587\n");beep(array4[4],a1);break;}
  97.     }
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment