Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Code:
Selected Language:
Show Linenumbers:
Short link for Twitter:
HTML:

HTML view:

Copy Source | Copy HTML
  1. //#include <complex.h>
  2. #include <math.h>
  3. #include <fftw3.h>
  4. #include <string.h>
  5.  
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <sys/ioctl.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <linux/soundcard.h>
  13.  
  14. #define LENGTH 1 /* how many seconds of speech to store */
  15. #define RATE 44000 /* the sampling rate */
  16. #define READ_ONCE 2000
  17. #define SIZE 8 /* sample size: 8 or 16 bits */
  18. #define CHANNELS 1 /* 1 = mono 2 = stereo */
  19.  
  20. unsigned char buf[LENGTH*READ_ONCE*SIZE*CHANNELS/8];
  21.  
  22.  
  23. bool print_grayscale_prefix(int intensivity, bool background){
  24.     // intensivity must be in 232..256
  25.     if( intensivity > (256-232) ) return false;
  26.     intensivity += 232;
  27.     int background_val =  0;
  28.     if( background )
  29.         background_val = 48; // background
  30.     else
  31.         background_val = 38; //fogground
  32.  
  33.     printf("%c[%d;%d;%dm", 0x1B, background_val, 5, intensivity);
  34. };
  35.  
  36.  
  37. void reset_grayscale(){
  38.     printf("%c[%d;%d;%dm", 0x1B, 0, 37, 40);
  39. //    printf("%c[%d;%d;%dm", 0x1B, 38, 5, 256);
  40. }
  41.  
  42.  
  43. void fill_double(double *data, int size){
  44.     static bool odd = true;
  45.     for(int i=0; i<size; i++){
  46.         double x = i;
  47.         data[i] = sin(x/10) + sin(x*3);
  48.         if(odd) data[i] += sin(x);
  49.     }
  50.     odd = !odd;
  51. }
  52.  
  53. int main(){
  54.  
  55.     int fd; /* sound device file descriptor */
  56.     int arg; /* argument for ioctl calls */
  57.     int status; /* return status of system calls */
  58.  
  59.     /* open sound device */
  60.     fd = open("/dev/dsp", O_RDWR);
  61.     if (fd < 0) {
  62.         perror("open of /dev/dsp failed");
  63.         exit(1);
  64.     }
  65.     /* set sampling parameters */
  66.     arg = SIZE; /* sample size */
  67.     status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
  68.     if (status == -1)
  69.         perror("SOUND_PCM_WRITE_BITS ioctl failed");
  70.     if (arg != SIZE)
  71.         perror("unable to set sample size");
  72.  
  73.     arg = CHANNELS; /* mono or stereo */
  74.     status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
  75.     if (status == -1)
  76.         perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
  77.     if (arg != CHANNELS)
  78.         perror("unable to set number of channels");
  79.  
  80.     arg = RATE; /* sampling rate */
  81.     status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
  82.     if (status == -1)
  83.         perror("SOUND_PCM_WRITE_WRITE ioctl failed");
  84.  
  85.     /*
        while (1) { //  loop until Control-C
            printf("Say something:\n");
            status = read(fd, buf, sizeof(buf)); // record some sound
            if (status != sizeof(buf))
                perror("read wrong number of bytes");

            printf("You said:\n");
            status = write(fd, buf, sizeof(buf)); // play it back
            if (status != sizeof(buf))
                perror("wrote wrong number of bytes");
            // wait for playback to complete before recording again
            status = ioctl(fd, SOUND_PCM_SYNC, 0);
            if (status == -1)
                perror("SOUND_PCM_SYNC ioctl failed");
                */
  86.  
  87.     const int screen_size = 100;
  88.     double screen_line[screen_size];
  89.     const int N = READ_ONCE;
  90.     const double max_value = (double)N / (double)screen_size * 0.5;
  91.     const int packet = N /2 / screen_size;
  92.     fftw_complex *out;
  93.     fftw_plan p;
  94.  
  95. //    in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
  96.     double in[N];
  97.     out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
  98.     p = fftw_plan_dft_r2c_1d(N, in, out, FFTW_ESTIMATE);
  99.  
  100.  
  101. while(1){
  102.     status = read(fd, buf, sizeof(buf)); /* record some sound */
  103.     if (status != sizeof(buf))
  104.         perror("read wrong number of bytes");
  105.  
  106. for(int i=0; i<N; i++)
  107.     in[i] = buf[i];
  108. //    fill_double(in, N);
  109.     fftw_execute(p); /* repeat as needed */
  110.  
  111.     memset(screen_line, 0, sizeof(screen_line));
  112.     int screen_it = 0;
  113.     for(int i=0; i<N/2; i++){
  114.         double val = sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]) / N;
  115.         screen_line[ screen_it ] += val;
  116.         if( i % packet == 0 && i != 0) screen_it++;
  117.         if( i!=0 && val > 0.4 )
  118.             printf("%d = %f : ", i, val);
  119.  
  120.     }
  121.  
  122.     for(int i=0; i<screen_size; i++){
  123.         screen_line[i] = screen_line[i] /2 * 24;
  124.         //printf("vvvv %d  %f\n", i, screen_line[i]); // 24 - max_colors
  125.     }
  126.     for(int i=0; i<screen_size; i++){
  127.         print_grayscale_prefix(screen_line[i], true);
  128.         printf(".");
  129.     }
  130.     printf("\n");
  131.  
  132. }
  133.     reset_grayscale();
  134.  
  135.     fftw_destroy_plan(p);
  136.     //fftw_free(in);
  137.     fftw_free(out);
  138.     return  0;
  139.  
  140. }
  141.  

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2010
Some API could be found at http://s-c.me/WS/HighLight.asmx