1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#include <stdio.h> #include <stdlib.h> #include <sys/ioctl.h> #include <ao/ao.h> #include <math.h> #include <pthread.h> #define BUF_SIZE 4096 int play = 0; int stop = 0; int main (int argc, char *argv[]) { //check for whitch effect to print int i = argc; for(i > 0; i--;) { switch(*argv[i]) { case '1': lattus(); break; case '2': normal(); break; case '3': choice(); break; case ' ': break; default: break; } } } char *randstring (char *buffer, int length) { //genertate a random number int i = length; for(i >= 0; i--;) { buffer[i] = (rand() % 2) ? '1' : '0'; } buffer[length] = 0; return buffer; } int normal(){ // normal drawing of 1's and 0's struct winsize w; ioctl(0, TIOCGWINSZ, &w); int width = w.ws_col; int height = w.ws_row; //get terminal width and height char buffer[width*height + 1]; //create a buffer big enough to hold one draw to the screen int i = 25; while(i-- >= 0) { printf("%s\n", randstring(buffer, width*height)); //draw to screen usleep(50000); } system("clear"); //clear screen } void *runner(void *threadid){ //detect thread number and run correct function long tid; tid = (long)threadid; if(tid == 1){ noise(30); }else{ lattus(); } pthread_exit(NULL); } int choice(){ //start the seperate threads pthread_t threads[2]; long t; int rc; int play2; for(t=0;t<2;t++){ rc = pthread_create(&threads[t], NULL, runner, (void *)t); } if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } pthread_exit(NULL); } int noise(int pitch) { //initiate the noise function with pitch option(not implemented atm) ao_device *device; ao_sample_format format; int default_driver; char *buffer; int buf_size; int sample; float freq = 440.0; int i; ao_initialize(); default_driver = ao_default_driver_id(); format.bits = 16; format.channels = 2; format.rate = 44100; format.byte_format = AO_FMT_LITTLE; int length = 2000; buf_size = format.bits/8 * format.channels * length; buffer = calloc(buf_size, sizeof(char)); for (i = 0; i < length; i++) { sample = (int)(1 * 32768.0 * sin(2 * M_PI * freq * ((float) i/format.rate))); buffer[4*i] = buffer[4*i+2] = sample & 0xff; buffer[4*i+1] = buffer[4*i+3] = (sample >> 8) & 0xff; } while(stop != 1){ if(play == 1){ device = ao_open_live(default_driver, &format, NULL /* no options */); ao_play(device, buffer, buf_size); ao_close(device); play = 0; } } pthread_exit(NULL); } int lattus (void) { struct winsize w; ioctl(0, TIOCGWINSZ, &w); int width = w.ws_col; //get the terminal width char *buffer1 = malloc(sizeof(char) * (width + 1)); //create 3 buffers for each segment char *buffer2 = malloc(sizeof(char) * (width + 1)); //each big enough to hold the width of the terminal char *buffer3 = malloc(sizeof(char) * (width + 1)); int first = 1; //how many before the space int second = width - 8; //how many in the middle of the space int third = 1; //how many at the end of the space int i = 1000; //draw 1000 lines int on = 0; //switch for growing and shrinking while(i-- >= 0) { usleep(9000); if(first == 1 && third == 1 && second == width - 8 || second == width - 9) { //is it at min? if(second % 2 == 0) { //is it an even number (had problems with buffer if it was odd) second = second - 2; } else { second = second - 3; } first ++; third ++; on = 0; //keep growing play = 1; } else if(first == (width - 8) / 2 && third == (width - 8) / 2 && second == 2) { //untill it gets to max if(second % 2 == 0) { second = second + 2; } else { second = second + 1; } third --; first --; on = 1; //start shrinking play = 1; } else if(on == 0) { //else if suppost to grow, grow second = second - 2; third ++; first ++; } else if(on == 1) { //else if suppost to shrink shrink second = second + 2; third --; first --; } else { break; } printf("%s %s %s\n", randstring(buffer1, first), randstring(buffer2, second), randstring(buffer3, third)); //print it out wait(); } stop = 1; system("clear"); //clear screen ao_shutdown(); pthread_exit(NULL); }
Refactorings
No refactoring yet !
it works but i think i could be doing things in a better way.
compile with gcc -lao -ldl -lm