| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjmedia.h> |
| 20 |
#include <pjlib-util.h> |
| 21 |
#include <pjlib.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
#define app_perror(a,b,c) printf("%s: %s (%d)", a, b, c) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#define THIS_FILE "cmp_wav.c" |
| 30 |
#define BYTES_PER_FRAME 512 |
| 31 |
|
| 32 |
static const char *desc = |
| 33 |
" FILE \n" |
| 34 |
" \n" |
| 35 |
" cmp_wav.c \n" |
| 36 |
" \n" |
| 37 |
" PURPOSE \n" |
| 38 |
" \n" |
| 39 |
" Compare two WAV files. \n" |
| 40 |
" \n" |
| 41 |
" USAGE \n" |
| 42 |
" \n" |
| 43 |
" cmp_wav ORIGINAL_WAV DEGRADED_WAV [TIME] [DETAIL] \n" |
| 44 |
" \n" |
| 45 |
" ORIGINAL_WAV The original WAV file as reference. \n" |
| 46 |
" DEGRADED_WAV The degraded WAV file. \n" |
| 47 |
" TIME Compare only some part of the files \n" |
| 48 |
" (in ms, since the beginning). \n" |
| 49 |
" Specify 0 (default) to compare the whole time. \n" |
| 50 |
" DETAIL Show detail result, 1 or 0 (default=0, means no)\n" |
| 51 |
" \n" |
| 52 |
" Both files must have same clock rate and must contain \n" |
| 53 |
" uncompressed (i.e. 16bit) PCM. \n"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
double sum_mult_sig(pj_int16_t *buf1, pj_int16_t *buf2, unsigned nsamples) |
| 58 |
{ |
| 59 |
double mag = 0; |
| 60 |
|
| 61 |
while (nsamples--) |
| 62 |
mag += (double)*buf1++ * (double)*buf2++; |
| 63 |
|
| 64 |
return mag; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
int main(int argc, char *argv[]) |
| 72 |
{ |
| 73 |
pj_caching_pool cp; |
| 74 |
pjmedia_endpt *med_endpt; |
| 75 |
pj_pool_t *pool; |
| 76 |
pjmedia_port *file_ori_port; |
| 77 |
pjmedia_port *file_deg_port; |
| 78 |
pj_status_t status; |
| 79 |
unsigned first_nsamples = 0; |
| 80 |
unsigned samples_compared = 0; |
| 81 |
|
| 82 |
char buf1[BYTES_PER_FRAME]; |
| 83 |
char buf2[BYTES_PER_FRAME]; |
| 84 |
|
| 85 |
double ref_mag = 0; |
| 86 |
double deg_mag = 0; |
| 87 |
double mix_mag = 0; |
| 88 |
|
| 89 |
int detail = 0; |
| 90 |
int res_deg, res_mix, res_overall; |
| 91 |
|
| 92 |
if (argc < 3) { |
| 93 |
puts("Error: original & degraded filename required"); |
| 94 |
puts(desc); |
| 95 |
return 1; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
pj_log_set_level(3); |
| 100 |
|
| 101 |
|
| 102 |
status = pj_init(); |
| 103 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 104 |
|
| 105 |
|
| 106 |
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 113 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 114 |
|
| 115 |
|
| 116 |
pool = pj_pool_create( &cp.factory, |
| 117 |
"wav", |
| 118 |
4000, |
| 119 |
4000, |
| 120 |
NULL |
| 121 |
); |
| 122 |
|
| 123 |
|
| 124 |
status = pjmedia_wav_player_port_create( pool, |
| 125 |
argv[1], |
| 126 |
40, |
| 127 |
PJMEDIA_FILE_NO_LOOP, |
| 128 |
0, |
| 129 |
&file_ori_port |
| 130 |
); |
| 131 |
if (status != PJ_SUCCESS) { |
| 132 |
app_perror(THIS_FILE, "Unable to use WAV file", status); |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
status = pjmedia_wav_player_port_create( pool, |
| 138 |
argv[2], |
| 139 |
40, |
| 140 |
PJMEDIA_FILE_NO_LOOP, |
| 141 |
0, |
| 142 |
&file_deg_port |
| 143 |
); |
| 144 |
if (status != PJ_SUCCESS) { |
| 145 |
app_perror(THIS_FILE, "Unable to use WAV file", status); |
| 146 |
return 1; |
| 147 |
} |
| 148 |
|
| 149 |
if (file_ori_port->info.fmt.det.aud.clock_rate != |
| 150 |
file_deg_port->info.fmt.det.aud.clock_rate) |
| 151 |
{ |
| 152 |
app_perror(THIS_FILE, "Clock rates must be same.", PJ_EINVAL); |
| 153 |
return 1; |
| 154 |
} |
| 155 |
|
| 156 |
if (argc > 3) |
| 157 |
first_nsamples = atoi(argv[3]) * |
| 158 |
file_ori_port->info.fmt.det.aud.clock_rate / 1000; |
| 159 |
|
| 160 |
if (argc > 4) |
| 161 |
detail = atoi(argv[4]); |
| 162 |
|
| 163 |
while (1) { |
| 164 |
pjmedia_frame f1, f2; |
| 165 |
|
| 166 |
f1.buf = buf1; |
| 167 |
f1.size = BYTES_PER_FRAME; |
| 168 |
f2.buf = buf2; |
| 169 |
f2.size = BYTES_PER_FRAME; |
| 170 |
|
| 171 |
status = pjmedia_port_get_frame(file_ori_port, &f1); |
| 172 |
if (status == PJ_EEOF) { |
| 173 |
break; |
| 174 |
} else if (status != PJ_SUCCESS) { |
| 175 |
app_perror(THIS_FILE, "Error occured while reading file", status); |
| 176 |
break; |
| 177 |
} |
| 178 |
status = pjmedia_port_get_frame(file_deg_port, &f2); |
| 179 |
if (status == PJ_EEOF) { |
| 180 |
break; |
| 181 |
} else if (status != PJ_SUCCESS) { |
| 182 |
app_perror(THIS_FILE, "Error occured while reading file", status); |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
ref_mag += sum_mult_sig(f1.buf, f1.buf, BYTES_PER_FRAME >> 1); |
| 188 |
deg_mag += sum_mult_sig(f2.buf, f2.buf, BYTES_PER_FRAME >> 1); |
| 189 |
mix_mag += sum_mult_sig(f1.buf, f2.buf, BYTES_PER_FRAME >> 1); |
| 190 |
|
| 191 |
samples_compared += BYTES_PER_FRAME >> 1; |
| 192 |
if (first_nsamples && samples_compared >= first_nsamples) |
| 193 |
break; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
res_deg = (int) (deg_mag / ref_mag * 100.0); |
| 199 |
if (res_deg < 0) |
| 200 |
res_deg = -1; |
| 201 |
else if (res_deg >= 81) |
| 202 |
res_deg = 9; |
| 203 |
else |
| 204 |
res_deg = pj_isqrt(res_deg); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
res_mix = (int) (mix_mag / ref_mag * 100.0); |
| 210 |
if (res_mix < 0) |
| 211 |
res_mix = -1; |
| 212 |
else if (res_mix >= 81) |
| 213 |
res_mix = 9; |
| 214 |
else |
| 215 |
res_mix = pj_isqrt(res_mix); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
if (res_mix == -1) |
| 222 |
res_overall = -1; |
| 223 |
else |
| 224 |
res_overall = (res_mix*1 + res_deg*1) / 2; |
| 225 |
|
| 226 |
if (detail) { |
| 227 |
printf("Reference = %.0f\n", ref_mag); |
| 228 |
printf("Degraded = %.0f\n", deg_mag); |
| 229 |
printf("Mixed = %.0f\n", mix_mag); |
| 230 |
|
| 231 |
printf("\n"); |
| 232 |
|
| 233 |
printf("Score 1 = %d\n", res_deg); |
| 234 |
printf("Score 2 = %d\n", res_mix); |
| 235 |
|
| 236 |
printf("\n"); |
| 237 |
} |
| 238 |
|
| 239 |
printf("Overall = %d\n", res_overall); |
| 240 |
|
| 241 |
|
| 242 |
status = pjmedia_port_destroy( file_ori_port ); |
| 243 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 244 |
|
| 245 |
status = pjmedia_port_destroy( file_deg_port ); |
| 246 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 247 |
|
| 248 |
|
| 249 |
pj_pool_release( pool ); |
| 250 |
|
| 251 |
|
| 252 |
pjmedia_endpt_destroy( med_endpt ); |
| 253 |
|
| 254 |
|
| 255 |
pj_caching_pool_destroy( &cp ); |
| 256 |
|
| 257 |
|
| 258 |
pj_shutdown(); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
return 0; |
| 263 |
} |
| 264 |
|