Lines Matching refs:bat
47 static void get_snr_thd_db(struct bat *bat, char *thd)
56 fprintf(bat->err, _("Invalid threshold '%s':%d\n"), thd, err);
59 bat->snr_thd_db = thd_db;
63 static void get_snr_thd_pc(struct bat *bat, char *thd)
72 fprintf(bat->err, _("Invalid threshold '%s':%d\n"), thd, err);
75 bat->snr_thd_db = 20.0 * log10f(100.0 / thd_pc);
78 static int get_duration(struct bat *bat)
85 duration_f = strtof(bat->narg, &ptrf);
91 duration_i = strtol(bat->narg, &ptri, 10);
96 bat->frames = duration_f * bat->rate;
98 bat->frames = duration_i;
100 bat->frames = -1;
102 if (bat->frames <= 0 || bat->frames > MAX_FRAMES) {
103 fprintf(bat->err, _("Invalid duration. Range: (0, %d(%fs))\n"),
104 MAX_FRAMES, (float)MAX_FRAMES / bat->rate);
111 fprintf(bat->err, _("Duration overflow/underflow: %d\n"), err);
116 static void get_sine_frequencies(struct bat *bat, char *freq)
122 bat->target_freq[1] = bat->target_freq[0] = atof(optarg);
125 bat->target_freq[0] = atof(optarg);
126 bat->target_freq[1] = atof(tmp1 + 1);
130 static void get_format(struct bat *bat, char *optarg)
133 bat->format = BAT_PCM_FORMAT_S16_LE;
134 bat->rate = 44100;
135 bat->channels = 2;
136 bat->sample_size = 2;
138 bat->format = BAT_PCM_FORMAT_S16_LE;
139 bat->rate = 48000;
140 bat->channels = 2;
141 bat->sample_size = 2;
143 bat->format = BAT_PCM_FORMAT_U8;
144 bat->sample_size = 1;
146 bat->format = BAT_PCM_FORMAT_S16_LE;
147 bat->sample_size = 2;
149 bat->format = BAT_PCM_FORMAT_S24_3LE;
150 bat->sample_size = 3;
152 bat->format = BAT_PCM_FORMAT_S32_LE;
153 bat->sample_size = 4;
155 bat->format = BAT_PCM_FORMAT_UNKNOWN;
156 fprintf(bat->err, _("wrong extended format '%s'\n"), optarg);
161 static inline int thread_wait_completion(struct bat *bat ATTRIBUTE_UNUSED,
174 static void test_loopback(struct bat *bat)
182 (void *) bat->playback.fct, bat);
184 fprintf(bat->err, _("Cannot create playback thread: %d\n"),
194 err = pthread_create(&capture_id, NULL, (void *) bat->capture.fct, bat);
196 fprintf(bat->err, _("Cannot create capture thread: %d\n"), err);
202 err = thread_wait_completion(bat, playback_id, &thread_result_playback);
204 fprintf(bat->err, _("Cannot join playback thread: %d\n"), err);
212 fprintf(bat->err, _("Exit playback thread fail: %d\n"),
217 fprintf(bat->log, _("Playback completed.\n"));
222 err = thread_wait_completion(bat, capture_id, &thread_result_capture);
224 fprintf(bat->err, _("Cannot join capture thread: %d\n"), err);
231 fprintf(bat->log, _("Capture canceled.\n"));
237 fprintf(bat->err, _("Exit capture thread fail: %d\n"),
241 fprintf(bat->log, _("Capture completed.\n"));
246 static void test_playback(struct bat *bat)
254 (void *) bat->playback.fct, bat);
256 fprintf(bat->err, _("Cannot create playback thread: %d\n"),
262 err = thread_wait_completion(bat, playback_id, &thread_result);
264 fprintf(bat->err, _("Cannot join playback thread: %d\n"), err);
271 fprintf(bat->err, _("Exit playback thread fail: %d\n"),
275 fprintf(bat->log, _("Playback completed.\n"));
280 static void test_capture(struct bat *bat)
287 err = pthread_create(&capture_id, NULL, (void *) bat->capture.fct, bat);
289 fprintf(bat->err, _("Cannot create capture thread: %d\n"), err);
296 err = thread_wait_completion(bat, capture_id, &thread_result);
298 fprintf(bat->err, _("Cannot join capture thread: %d\n"), err);
305 fprintf(bat->err, _("Exit capture thread fail: %d\n"),
309 fprintf(bat->log, _("Capture completed.\n"));
313 static void usage(struct bat *bat)
315 fprintf(bat->log,
342 fprintf(bat->log, _("Recognized sample formats are: "));
343 fprintf(bat->log, _("U8 S16_LE S24_3LE S32_LE\n"));
344 fprintf(bat->log, _("The available format shotcuts are:\n"));
345 fprintf(bat->log, _("-f cd (16 bit little endian, 44100, stereo)\n"));
346 fprintf(bat->log, _("-f dat (16 bit little endian, 48000, stereo)\n"));
349 static void set_defaults(struct bat *bat)
351 memset(bat, 0, sizeof(struct bat));
354 bat->rate = 44100;
355 bat->frame_size = 2;
356 bat->sample_size = 2;
357 bat->format = BAT_PCM_FORMAT_S16_LE;
358 bat->convert_float_to_sample = convert_float_to_int16;
359 bat->convert_sample_to_float = convert_int16_to_float;
360 bat->frames = bat->rate * 2;
361 bat->target_freq[0] = 997.0;
362 bat->target_freq[1] = 997.0;
363 bat->sigma_k = 3.0;
364 bat->snr_thd_db = SNR_DB_INVALID;
365 bat->playback.device = NULL;
366 bat->capture.device = NULL;
367 bat->buf = NULL;
368 bat->local = false;
369 bat->buffer_size = 0;
370 bat->period_size = 0;
371 bat->roundtriplatency = false;
373 bat->channels = 2;
374 bat->playback.fct = &playback_tinyalsa;
375 bat->capture.fct = &record_tinyalsa;
377 bat->channels = 1;
378 bat->playback.fct = &playback_alsa;
379 bat->capture.fct = &record_alsa;
381 bat->playback.mode = MODE_LOOPBACK;
382 bat->capture.mode = MODE_LOOPBACK;
383 bat->period_is_limited = false;
384 bat->log = stdout;
385 bat->err = stderr;
388 static void parse_arguments(struct bat *bat, int argc, char *argv[])
410 bat->logarg = optarg;
413 bat->playback.file = optarg;
416 bat->debugplay = optarg;
419 bat->capturefile = optarg;
420 bat->capture.mode = MODE_ANALYZE_ONLY;
421 bat->playback.mode = MODE_ANALYZE_ONLY;
424 bat->local = true;
427 bat->standalone = true;
430 bat->roundtriplatency = true;
433 get_snr_thd_db(bat, optarg);
436 get_snr_thd_pc(bat, optarg);
439 if (bat->playback.device == NULL)
440 bat->playback.device = optarg;
441 if (bat->capture.device == NULL)
442 bat->capture.device = optarg;
445 if (bat->capture.mode == MODE_SINGLE)
446 bat->capture.mode = MODE_LOOPBACK;
448 bat->playback.mode = MODE_SINGLE;
449 bat->playback.device = optarg;
452 if (bat->playback.mode == MODE_SINGLE)
453 bat->playback.mode = MODE_LOOPBACK;
455 bat->capture.mode = MODE_SINGLE;
456 bat->capture.device = optarg;
459 bat->narg = optarg;
462 get_sine_frequencies(bat, optarg);
465 bat->channels = atoi(optarg);
468 bat->rate = atoi(optarg);
471 get_format(bat, optarg);
474 bat->sigma_k = atof(optarg);
477 bat->periods_total = atoi(optarg);
478 bat->period_is_limited = true;
482 bat->buffer_size = err >= MIN_BUFFERSIZE
487 bat->period_size = err >= MIN_PERIODSIZE
492 usage(bat);
498 static int validate_options(struct bat *bat)
504 if ((bat->local == true) && (bat->capture.file == NULL)) {
505 fprintf(bat->err, _("no input file for local testing\n"));
510 if (bat->channels > MAX_CHANNELS || bat->channels < MIN_CHANNELS) {
511 fprintf(bat->err, _("%d channels not supported\n"),
512 bat->channels);
517 if ((bat->playback.mode == MODE_SINGLE)
518 && (bat->capture.mode == MODE_SINGLE)) {
519 fprintf(bat->err, _("single ended mode is simplex\n"));
525 freq_high = bat->rate * RATE_FACTOR;
526 for (c = 0; c < bat->channels; c++) {
527 if (bat->target_freq[c] < freq_low
528 || bat->target_freq[c] > freq_high) {
529 fprintf(bat->err, _("sine wave frequency out of"));
530 fprintf(bat->err, _(" range: (%.1f, %.1f)\n"),
539 static int bat_init(struct bat *bat)
546 if (bat->logarg) {
547 bat->log = NULL;
548 bat->log = fopen(bat->logarg, "wb");
549 if (bat->log == NULL) {
551 fprintf(bat->err, _("Cannot open file: %s %d\n"),
552 bat->logarg, err);
555 bat->err = bat->log;
559 if (bat->narg) {
560 err = get_duration(bat);
566 if (bat->playback.device == NULL && bat->capture.device == NULL)
567 bat->playback.device = bat->capture.device = DEFAULT_DEV_NAME;
570 if (bat->local) {
571 bat->capture.file = bat->playback.file;
577 fprintf(bat->err, _("Fail to create record file: %d\n"),
582 bat->capture.file = strdup(name);
583 if (bat->capture.file == NULL)
590 if (bat->playback.file == NULL) {
592 if (bat->frames) {
593 if (bat->playback.mode == MODE_SINGLE) {
595 bat->sinus_duration = bat->frames;
599 bat->sinus_duration = bat->rate *
601 bat->sinus_duration +=
602 (bat->frames + bat->frames / 2);
607 bat->sinus_duration = 0;
608 bat->playback.mode = MODE_SINGLE;
611 bat->fp = fopen(bat->playback.file, "rb");
612 if (bat->fp == NULL) {
614 fprintf(bat->err, _("Cannot open file: %s %d\n"),
615 bat->playback.file, err);
618 err = read_wav_header(bat, bat->playback.file, bat->fp, false);
619 fclose(bat->fp);
624 bat->frame_size = bat->sample_size * bat->channels;
627 switch (bat->sample_size) {
629 bat->convert_float_to_sample = convert_float_to_uint8;
630 bat->convert_sample_to_float = convert_uint8_to_float;
633 bat->convert_float_to_sample = convert_float_to_int16;
634 bat->convert_sample_to_float = convert_int16_to_float;
637 bat->convert_float_to_sample = convert_float_to_int24;
638 bat->convert_sample_to_float = convert_int24_to_float;
641 bat->convert_float_to_sample = convert_float_to_int32;
642 bat->convert_sample_to_float = convert_int32_to_float;
645 fprintf(bat->err, _("Invalid PCM format: size=%d\n"),
646 bat->sample_size);
655 struct bat bat;
658 set_defaults(&bat);
665 fprintf(bat.log, _("%s version %s\n\n"), PACKAGE_NAME, PACKAGE_VERSION);
667 parse_arguments(&bat, argc, argv);
669 err = bat_init(&bat);
673 err = validate_options(&bat);
678 if (bat.roundtriplatency) {
680 fprintf(bat.log,
682 roundtrip_latency_init(&bat);
683 test_loopback(&bat);
685 if (bat.latency.xrun_error == false)
690 bat.period_size += bat.rate / 1000;
691 bat.buffer_size =
692 bat.period_size * DIV_BUFFERSIZE;
696 if (bat.period_size > bat.rate * 0.2)
707 if (bat.playback.mode == MODE_SINGLE) {
708 test_playback(&bat);
713 if (bat.capture.mode == MODE_SINGLE) {
714 test_capture(&bat);
718 if (bat.capture.mode == MODE_ANALYZE_ONLY && bat.capturefile) {
719 bat.capture.file = strdup(bat.capturefile);
720 fprintf(bat.log,
722 bat.capture.file);
723 fprintf(bat.log, _("Skipping playback and capture\n"));
728 if (bat.local == false)
729 test_loopback(&bat);
733 if (!bat.standalone || snr_is_valid(bat.snr_thd_db))
734 err = analyze_capture(&bat);
736 fprintf(bat.log, _("No libfftw3 library. Exit without analysis.\n"));
739 fprintf(bat.log, _("\nReturn value is %d\n"), err);
741 if (bat.logarg)
742 fclose(bat.log);
743 if (!bat.local)
744 free(bat.capture.file);