| 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"); |
|---|
| | 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 | } |
|---|