Changeset 4131

Show
Ignore:
Timestamp:
08/19/08 22:06:57 (4 months ago)
Author:
hverkuil
Message:

Show left/right volume as well.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ivtv/trunk/test/ivtv-pcm-tester.c

    r3156 r4131  
    2424 
    2525#include <unistd.h> 
    26 #include <features.h>           /* Uses _GNU_SOURCE to define getsubopt in stdlib.h */ 
     26#include <features.h> /* Uses _GNU_SOURCE to define getsubopt in stdlib.h */ 
    2727#include <stdlib.h> 
    2828#include <stdio.h> 
     
    3939#include <math.h> 
    4040#include <linux/types.h> 
    41 #include <sys/klog.h> 
    4241 
    43 #define samples_count (48000/10) 
     42#define SAMPLES_CNT (48000 / 10) 
    4443#define DB_MAX 50 
    45  
    46 /* 
    47                   inf    -50  -45  -40  -35  -30  -25  -20  -15  -10   -5     0 
    48     Freq    Level|....|....|....|....|....|....|....|....|....|....|....|....|! 
    49 |    inf|  -0.000|01234 
    50 */ 
    51  
    52  
    5344 
    5445union 
    5546{ 
    56     char bytes[4];  
    57     struct 
    58    
    59         long left:16; 
    60         long right:16; 
    61     } words; 
     47       char bytes[4];  
     48       struct 
     49       
     50               long left:16; 
     51               long right:16; 
     52       } words; 
    6253}  __attribute__((packed)) audio_sample; 
    6354 
    6455void print_usage(void) 
    6556{ 
    66     printf 
    67     ( 
    68       "Usage:\n" 
    69       "\tivtv-pcm-tester <pcm device>\n" 
    70     ); 
    71 }; 
    72      
    73  
    74 int main(int argc, char** argv) 
    75 
    76     int fd; 
    77     int i,c; 
    78     double f, period, current, peak_max = 0.0, peak_min = 65536.0; 
    79     struct timeval t_start, t_stop; 
    80     char line[DB_MAX+1]; 
    81  
    82     if(argc != 2) 
    83     { 
    84         fprintf(stderr, "Error, incorrect arguments!\n"); 
    85         print_usage(); 
    86         exit(-1); 
    87     }; 
    88  
    89     fd = open(argv[1], O_RDONLY); 
    90     if (fd == -1) 
    91     { 
    92         fprintf 
    93         ( 
    94             stderr, 
    95             "Error, unable to open device '%s'\n", 
    96             argv[1] 
    97         ); 
    98         exit(-1); 
    99     }; 
    100  
    101     printf 
    102     ( 
    103         "               -50  -45  -40  -35  -30  -25  -20  -15  -10   -5    0\n" 
    104         "    Freq    Level|....|....|....|....|....|....|....|....|....|....|!\n" 
    105     ); 
    106  
    107  
    108                                                                                   
    109     while(1) 
    110     { 
    111         /* fix time */ 
    112         gettimeofday(&t_start, NULL); 
    113      
    114         /* read samples and find peaks */ 
    115         for(i = 0, peak_max = 0.0, peak_min = 65536.0; i<samples_count; i++) 
    116         { 
    117             /* read sample */ 
    118             if(read(fd, &audio_sample, sizeof(audio_sample) ) ) 
    119             { 
    120                 current = (double)audio_sample.words.left; 
    121                 if (peak_max < current) peak_max = current; 
    122                 if (peak_min > current) peak_min = current; 
    123             } 
    124             else 
    125             { 
    126                 printf("\n"); 
    127                 close(fd); 
    128                 exit(0); 
    129             }; 
    130         }; 
    131  
    132 /* printf("current=%lf, peak_min=%lf, peak_max=%lf\n", current, peak_min, peak_max); */ 
    133  
    134         /* fix time */ 
    135         gettimeofday(&t_stop, NULL); 
    136          
    137         /* calc time */ 
    138         period =  
    139             (double)(t_stop.tv_sec - t_start.tv_sec) 
    140             + 
    141             (1.0/1000000.0)*( (double)(t_stop.tv_usec - t_start.tv_usec) ); 
    142              
    143         /* calc freq */ 
    144         f =  
    145             (period == 0.0 )  
    146             ? 
    147             0 
    148             : 
    149             ((double)samples_count) / period; 
    150          
    151         /* calc level */ 
    152         double l = 20.0*log( (peak_max - peak_min)/65536.0)/log(10.0); 
    153          
    154  
    155         /* create line */ 
    156         for(i= 0 , c = (long)l; i<DB_MAX ; i++) 
    157             line[i] = ( (i-DB_MAX) < l )?'=':' '; 
    158         line[DB_MAX] = 0; 
    159  
    160         /* output result */ 
    161         printf("|%7.0lf| %7.3lf|%s\r",f,l,line); 
    162          
    163         fflush(stdout); 
    164          
    165  
    166     }; 
    167      
    168      
     57        printf("Usage:\n\tivtv-pcm-tester <pcm device>\n"); 
    16958}; 
    17059 
     60int main(int argc, char **argv) 
     61{ 
     62        int fd; 
     63        int i; 
     64        char line[DB_MAX + 1]; 
    17165 
     66        line[DB_MAX] = 0; 
     67        if (argc != 2) { 
     68                fprintf(stderr, "Error, incorrect arguments!\n"); 
     69                print_usage(); 
     70                exit(-1); 
     71        } 
     72 
     73        fd = open(argv[1], O_RDONLY); 
     74        if (fd == -1) { 
     75                fprintf(stderr, "Error, unable to open device '%s'\n", argv[1]); 
     76                exit(-1); 
     77        } 
     78 
     79        printf("               -50  -45  -40  -35  -30  -25  -20  -15  -10   -5    0\n" 
     80               "    Freq    Level|....|....|....|....|....|....|....|....|....|....|!\n"); 
     81 
     82        while (1) { 
     83                struct timeval t1, t2; 
     84                double peakl_max = 0.0, peakl_min = 65536.0; 
     85                double peakr_max = 0.0, peakr_min = 65536.0; 
     86                double period, f; 
     87 
     88                /* fix time */ 
     89                gettimeofday(&t1, NULL); 
     90 
     91                /* read samples and find peaks */ 
     92                for (i = 0; i < SAMPLES_CNT; i++) { 
     93                        /* read sample */ 
     94                        if (read(fd, &audio_sample, sizeof(audio_sample))) { 
     95                                double current = (double)audio_sample.words.left; 
     96                                if (peakl_max < current) peakl_max = current; 
     97                                if (peakl_min > current) peakl_min = current; 
     98                                current = (double)audio_sample.words.right; 
     99                                if (peakr_max < current) peakr_max = current; 
     100                                if (peakr_min > current) peakr_min = current; 
     101                        } else { 
     102                                printf("\n"); 
     103                                close(fd); 
     104                                exit(0); 
     105                        } 
     106                } 
     107 
     108                /* fix time */ 
     109                gettimeofday(&t2, NULL); 
     110 
     111                /* calc time */ 
     112                period = (double)(t2.tv_sec - t1.tv_sec) + 
     113                         (double)(t2.tv_usec - t1.tv_usec) / 1000000.0; 
     114 
     115                /* calc freq */ 
     116                f = (period == 0.0) ? 0 : ((double)SAMPLES_CNT) / period; 
     117 
     118                /* calc level */ 
     119                double l = 20.0 * log((peakl_max - peakl_min) / 65536.0) / log(10.0); 
     120                double r = 20.0 * log((peakr_max - peakr_min) / 65536.0) / log(10.0); 
     121 
     122                /* create line */ 
     123                for (i = 0; i < DB_MAX; i++) { 
     124                        line[i] = ' '; 
     125                        if (i - DB_MAX < l && i - DB_MAX < r) 
     126                                line[i] = '='; 
     127                        else if (i - DB_MAX < l) 
     128                                line[i] = 'l'; 
     129                        else if (i - DB_MAX < r) 
     130                                line[i] = 'r'; 
     131                } 
     132                 
     133                /* output result */ 
     134                printf("|%7.0lf| %7.3lf|%s\r", f, l, line); 
     135                fflush(stdout); 
     136        } 
     137}