-
Code's Tags
-
Your Codes
-
Reffers
-
Linked Codes
|
Code:
Short link for Twitter:
HTML:
HTML view:
Copy Source | Copy HTML- //#include <complex.h>
- #include <math.h>
- #include <fftw3.h>
- #include <string.h>
-
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <linux/soundcard.h>
-
- #define LENGTH 1 /* how many seconds of speech to store */
- #define RATE 44000 /* the sampling rate */
- #define READ_ONCE 2000
- #define SIZE 8 /* sample size: 8 or 16 bits */
- #define CHANNELS 1 /* 1 = mono 2 = stereo */
-
- unsigned char buf[LENGTH*READ_ONCE*SIZE*CHANNELS/8];
-
-
- bool print_grayscale_prefix(int intensivity, bool background){
- // intensivity must be in 232..256
- if( intensivity > (256-232) ) return false;
- intensivity += 232;
- int background_val = 0;
- if( background )
- background_val = 48; // background
- else
- background_val = 38; //fogground
-
- printf("%c[%d;%d;%dm", 0x1B, background_val, 5, intensivity);
- };
-
-
- void reset_grayscale(){
- printf("%c[%d;%d;%dm", 0x1B, 0, 37, 40);
- // printf("%c[%d;%d;%dm", 0x1B, 38, 5, 256);
- }
-
-
- void fill_double(double *data, int size){
- static bool odd = true;
- for(int i=0; i<size; i++){
- double x = i;
- data[i] = sin(x/10) + sin(x*3);
- if(odd) data[i] += sin(x);
- }
- odd = !odd;
- }
-
- int main(){
-
- int fd; /* sound device file descriptor */
- int arg; /* argument for ioctl calls */
- int status; /* return status of system calls */
-
- /* open sound device */
- fd = open("/dev/dsp", O_RDWR);
- if (fd < 0) {
- perror("open of /dev/dsp failed");
- exit(1);
- }
- /* set sampling parameters */
- arg = SIZE; /* sample size */
- status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
- if (status == -1)
- perror("SOUND_PCM_WRITE_BITS ioctl failed");
- if (arg != SIZE)
- perror("unable to set sample size");
-
- arg = CHANNELS; /* mono or stereo */
- status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
- if (status == -1)
- perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
- if (arg != CHANNELS)
- perror("unable to set number of channels");
-
- arg = RATE; /* sampling rate */
- status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
- if (status == -1)
- perror("SOUND_PCM_WRITE_WRITE ioctl failed");
-
- /*
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"); */
-
- const int screen_size = 100;
- double screen_line[screen_size];
- const int N = READ_ONCE;
- const double max_value = (double)N / (double)screen_size * 0.5;
- const int packet = N /2 / screen_size;
- fftw_complex *out;
- fftw_plan p;
-
- // in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
- double in[N];
- out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
- p = fftw_plan_dft_r2c_1d(N, in, out, FFTW_ESTIMATE);
-
-
- while(1){
- status = read(fd, buf, sizeof(buf)); /* record some sound */
- if (status != sizeof(buf))
- perror("read wrong number of bytes");
-
- for(int i=0; i<N; i++)
- in[i] = buf[i];
- // fill_double(in, N);
- fftw_execute(p); /* repeat as needed */
-
- memset(screen_line, 0, sizeof(screen_line));
- int screen_it = 0;
- for(int i=0; i<N/2; i++){
- double val = sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]) / N;
- screen_line[ screen_it ] += val;
- if( i % packet == 0 && i != 0) screen_it++;
- if( i!=0 && val > 0.4 )
- printf("%d = %f : ", i, val);
-
- }
-
- for(int i=0; i<screen_size; i++){
- screen_line[i] = screen_line[i] /2 * 24;
- //printf("vvvv %d %f\n", i, screen_line[i]); // 24 - max_colors
- }
- for(int i=0; i<screen_size; i++){
- print_grayscale_prefix(screen_line[i], true);
- printf(".");
- }
- printf("\n");
-
- }
- reset_grayscale();
-
- fftw_destroy_plan(p);
- //fftw_free(in);
- fftw_free(out);
- return 0;
-
- }
-
|