Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is C source for small musical pentatonic toy-program for Linux that I wrote.
- // :: :: DESCRIPTION ::
- // 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)
- // It has several problems to fix but anyway it works.
- // :: :: DEPENDENCIES :: Not really but a sort of...
- // You might need to attach the "pcspkr" module (sudo modprobe pcspkr)
- // And maybe also to grant 777 permissions to /dev/console (sudo chmod 777 /dev/console)
- // Check if PC Speaker works correctly by "beep" program:
- // sudo apt-get install beep
- // beep -f 100 -l 1000
- // :: :: PROBLEMS :: (I'll be really glad if someone can help me fix them) :: ::
- // Problem 1: too large code for such a small program
- // Solution 1: improve code, make it smaller and smarter
- // 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:
- // Solution 2: execute in bash: while true; do ./pentatonic; done
- // Authored 23.01.2013 by Security XIII [at gmail dot com]
- // P. S. Sorry, I have not much experience in C
- #include <stdio.h> // I am not sure maybe we don't need some of the libs,
- #include <stdlib.h> // But I'd better leave them here for now.
- #include <string.h>
- #include <termios.h>
- #include <time.h>
- #include <unistd.h>
- #define ESC 27
- int array1[]={262,294,330,392,440}; // C major pentatonic scale
- int array2[]={392,440,494,587,659}; // G major pentatonic scale
- int array3[]={277,349,392,466,523}; // Eb major pentatonic scale
- int array4[]={277,392,440,494,587}; // E minor pentatonic scale
- int a1 = 50; // This is note value, the larger it is, the longer note sounds.
- int key;
- int getkey() { // Probably somewhere here starts the code that prevents program from
- int character; // sounding correctly
- 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;
- }
- void beep (int frequency, int duration) // this code seems to be small enough and is okay
- {
- FILE *tty;
- if ( NULL == (tty = fopen ("/dev/console", "w")) ) {fprintf (stderr, "Cannot write to /dev/console!\n" ); exit (1); }
- fprintf(tty, "%c[10;%d]%c[11;%d]\a", ESC, frequency, ESC, duration);
- }
- int main()
- {
- while (1) {
- key = getkey();
- // Here all the magic starts.
- // Note: I have to use break condition because otherwise program just
- // prints the buttons that I press but there are no sound until you
- // exit it with ESC or Ctrl+C. So I leave break here... Have to do smth.
- // If I replace "break;" with "execl("pentatonic",NULL,NULL);" (self re-run) it
- // also doesen't work... >,,<
- if (key == 0x20) {printf("Key space pressed. Just test. Feel likeaboss?\n");}
- if (key == 0x1B) {printf("Exit key (ESC) pressed, BB!\n");exit (1);}
- if (key == 0x31) {printf("Key 1 Tone C Freq 262\n");beep(array1[0],a1);break;}
- if (key == 0x32) {printf("Key 2 Tone D Freq 294\n");beep(array1[1],a1);break;}
- if (key == 0x33) {printf("Key 3 Tone E Freq 330\n");beep(array1[2],a1);break;}
- if (key == 0x34) {printf("Key 4 Tone G Freq 392\n");beep(array1[3],a1);break;}
- if (key == 0x35) {printf("Key 5 Tone A Freq 440\n");beep(array1[4],a1);break;}
- if (key == 0x71) {printf("Key Q Tone G Freq 392\n");beep(array2[0],a1);break;}
- if (key == 0x77) {printf("Key W Tone A Freq 440\n");beep(array2[1],a1);break;}
- if (key == 0x65) {printf("Key E Tone B Freq 494\n");beep(array2[2],a1);break;}
- if (key == 0x72) {printf("Key R Tone D Freq 587\n");beep(array2[3],a1);break;}
- if (key == 0x74) {printf("Key T Tone E Freq 659\n");beep(array2[4],a1);break;}
- if (key == 0x61) {printf("Key A Tone Eb Freq 277\n");beep(array3[0],a1);break;}
- if (key == 0x73) {printf("Key S Tone F Freq 349\n");beep(array3[1],a1);break;}
- if (key == 0x64) {printf("Key D Tone G Freq 392\n");beep(array3[2],a1);break;}
- if (key == 0x66) {printf("Key F Tone Bb Freq 466\n");beep(array3[3],a1);break;}
- if (key == 0x67) {printf("Key G Tone C Freq 523\n");beep(array3[4],a1);break;}
- if (key == 0x7a) {printf("Key Z Tone Eb Freq 277\n");beep(array4[0],a1);break;}
- if (key == 0x78) {printf("Key X Tone G Freq 392\n");beep(array4[1],a1);break;}
- if (key == 0x63) {printf("Key C Tone A Freq 440\n");beep(array4[2],a1);break;}
- if (key == 0x76) {printf("Key V Tone B Freq 494\n");beep(array4[3],a1);break;}
- if (key == 0x62) {printf("Key B Tone D Freq 587\n");beep(array4[4],a1);break;}
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment