static int v17_v21_rx(void *user_data, const int16_t amp[], int len) { fax_state_t *t; fax_modems_state_t *s; t = (fax_state_t *) user_data; s = &t->modems; v17_rx(&s->v17_rx, amp, len); if (t->t30.rx_trained) { /* The fast modem has trained, so we no longer need to run the slow one in parallel. */ span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&s->v17_rx)); set_rx_handler(t, (span_rx_handler_t *) &v17_rx, (span_rx_fillin_handler_t *) &v17_rx_fillin, &s->v17_rx); } else { fsk_rx(&s->v21_rx, amp, len); if (t->t30.rx_frame_received) { /* We have received something, and the fast modem has not trained. We must be receiving valid V.21 */ span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx)); set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, (span_rx_fillin_handler_t *) &fsk_rx_fillin, &s->v21_rx); } } return 0; }
SPAN_DECLARE_NONSTD(int) fax_modems_v17_v21_rx(void *user_data, const int16_t amp[], int len) { fax_modems_state_t *s; s = (fax_modems_state_t *) user_data; v17_rx(&s->fast_modems.v17_rx, amp, len); fsk_rx(&s->v21_rx, amp, len); if (s->rx_frame_received) { /* We have received something, and the fast modem has not trained. We must be receiving valid V.21 */ span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx)); fax_modems_set_rx_handler(s, (span_rx_handler_t) &fsk_rx, &s->v21_rx, (span_rx_fillin_handler_t) &fsk_rx_fillin, &s->v21_rx); } /*endif*/ return 0; }
static int early_v17_rx(void *user_data, const int16_t amp[], int len) { fax_state_t *s; s = (fax_state_t *) user_data; v17_rx(&(s->v17rx), amp, len); fsk_rx(&(s->v21rx), amp, len); if (s->t30_state.rx_trained) { /* The fast modem has trained, so we no longer need to run the slow one in parallel. */ span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&(s->v17rx))); s->rx_handler = (span_rx_handler_t *) &v17_rx; s->rx_user_data = &(s->v17rx); } return len; }
int main(int argc, char *argv[]) { v17_rx_state_t *rx; v17_tx_state_t *tx; bert_results_t bert_results; int16_t gen_amp[BLOCK_LEN]; int16_t amp[BLOCK_LEN]; SNDFILE *inhandle; SNDFILE *outhandle; int outframes; int samples; int tep; int block_no; int noise_level; int signal_level; int bits_per_test; int line_model_no; int log_audio; int channel_codec; int rbs_pattern; int opt; logging_state_t *logging; channel_codec = MUNGE_CODEC_NONE; rbs_pattern = 0; test_bps = 14400; tep = FALSE; line_model_no = 0; decode_test_file = NULL; use_gui = FALSE; noise_level = -70; signal_level = -13; bits_per_test = 50000; log_audio = FALSE; while ((opt = getopt(argc, argv, "b:B:c:d:glm:n:r:s:t")) != -1) { switch (opt) { case 'b': test_bps = atoi(optarg); if (test_bps != 14400 && test_bps != 12000 && test_bps != 9600 && test_bps != 7200 && test_bps != 4800) { /* 4800 is an extension of V.17, to provide full coverage of the V.32bis modes */ fprintf(stderr, "Invalid bit rate specified\n"); exit(2); } break; case 'B': bits_per_test = atoi(optarg); break; case 'c': channel_codec = atoi(optarg); break; case 'd': decode_test_file = optarg; break; case 'g': #if defined(ENABLE_GUI) use_gui = TRUE; #else fprintf(stderr, "Graphical monitoring not available\n"); exit(2); #endif break; case 'l': log_audio = TRUE; break; case 'm': line_model_no = atoi(optarg); break; case 'n': noise_level = atoi(optarg); break; case 'r': rbs_pattern = atoi(optarg); break; case 's': signal_level = atoi(optarg); break; case 't': tep = TRUE; break; default: //usage(); exit(2); break; } } inhandle = NULL; outhandle = NULL; #if defined(HAVE_FENV_H) fpe_trap_setup(); #endif if (log_audio) { if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); exit(2); } } if (decode_test_file) { /* We will decode the audio from a file. */ tx = NULL; if ((inhandle = sf_open_telephony_read(decode_test_file, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", decode_test_file); exit(2); } } else { /* We will generate V.17 audio, and add some noise to it. */ tx = v17_tx_init(NULL, test_bps, tep, v17getbit, NULL); logging = v17_tx_get_logging_state(tx); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW); span_log_set_tag(logging, "V.17-tx"); v17_tx_power(tx, signal_level); v17_tx_set_modem_status_handler(tx, v17_tx_status, (void *) tx); #if defined(WITH_SPANDSP_INTERNALS) /* Move the carrier off a bit */ tx->carrier_phase_rate = dds_phase_ratef(1792.0f); tx->carrier_phase = 0x40000000; #endif bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20); bert_set_report(&bert, 10000, reporter, NULL); if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, rbs_pattern)) == NULL) { fprintf(stderr, " Failed to create line model\n"); exit(2); } one_way_line_model_set_dc(line_model, 0.0f); #if defined(ADD_MAINS_INTERFERENCE) one_way_line_model_set_mains_pickup(line_model, 50, -40.0f); #endif } rx = v17_rx_init(NULL, test_bps, v17putbit, NULL); logging = v17_rx_get_logging_state(rx); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW); span_log_set_tag(logging, "V.17-rx"); v17_rx_set_modem_status_handler(rx, v17_rx_status, (void *) rx); v17_rx_set_qam_report_handler(rx, qam_report, (void *) rx); #if defined(ENABLE_GUI) if (use_gui) { qam_monitor = qam_monitor_init(10.0f, NULL); if (!decode_test_file) { start_line_model_monitor(129); line_model_monitor_line_model_update(line_model->near_filter, line_model->near_filter_len); } } #endif memset(&latest_results, 0, sizeof(latest_results)); for (block_no = 0; block_no < 100000000; block_no++) { if (decode_test_file) { samples = sf_readf_short(inhandle, amp, BLOCK_LEN); #if defined(ENABLE_GUI) if (use_gui) qam_monitor_update_audio_level(qam_monitor, amp, samples); #endif if (samples == 0) break; } else { samples = v17_tx(tx, gen_amp, BLOCK_LEN); #if defined(ENABLE_GUI) if (use_gui) qam_monitor_update_audio_level(qam_monitor, gen_amp, samples); #endif if (samples == 0) { printf("Restarting on zero output\n"); /* Push a little silence through, to ensure all the data bits get out of the buffers */ vec_zeroi16(amp, BLOCK_LEN); v17_rx(rx, amp, BLOCK_LEN); /* Note that we might get a few bad bits as the carrier shuts down. */ bert_result(&bert, &bert_results); fprintf(stderr, "Final result %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs); fprintf(stderr, "Last report %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs); /* See if bit errors are appearing yet. Also check we are getting enough bits out of the receiver. The last regular report should be error free, though the final report will generally contain bits errors as the carrier was dying. The total number of bits out of the receiver should be at least the number we sent. Also, since BERT sync should have occurred rapidly at the start of transmission, the last report should have occurred at not much less than the total number of bits we sent. */ if (bert_results.total_bits < bits_per_test || latest_results.total_bits < bits_per_test - 100 || latest_results.bad_bits != 0) { break; } memset(&latest_results, 0, sizeof(latest_results)); #if defined(WITH_SPANDSP_INTERNALS) signal_level--; /* Bump the receiver AGC gain by 1dB, to compensate for the above */ rx->agc_scaling_save *= 1.122f; #endif v17_tx_restart(tx, test_bps, tep, TRUE); v17_tx_power(tx, signal_level); v17_rx_restart(rx, test_bps, TRUE); //rx.eq_put_step = rand()%(192*10/3); bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20); bert_set_report(&bert, 10000, reporter, NULL); one_way_line_model_release(line_model); if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, 0)) == NULL) { fprintf(stderr, " Failed to create line model\n"); exit(2); } } if (log_audio) { outframes = sf_writef_short(outhandle, gen_amp, samples); if (outframes != samples) { fprintf(stderr, " Error writing audio file\n"); exit(2); } } one_way_line_model(line_model, amp, gen_amp, samples); } #if defined(ENABLE_GUI) if (use_gui && !decode_test_file) line_model_monitor_line_spectrum_update(amp, samples); #endif v17_rx(rx, amp, samples); } if (!decode_test_file) { bert_result(&bert, &bert_results); fprintf(stderr, "At completion:\n"); fprintf(stderr, "Final result %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs); fprintf(stderr, "Last report %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs); one_way_line_model_release(line_model); if (signal_level > -43) { printf("Tests failed.\n"); exit(2); } printf("Tests passed.\n"); } #if defined(ENABLE_GUI) if (use_gui) qam_wait_to_end(qam_monitor); #endif if (decode_test_file) { if (sf_close_telephony(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", decode_test_file); exit(2); } } if (log_audio) { if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); exit(2); } } return 0; }
int main(int argc, char *argv[]) { fsk_rx_state_t *fsk; v17_rx_state_t *v17; v29_rx_state_t *v29; v27ter_rx_state_t *v27ter_4800; v27ter_rx_state_t *v27ter_2400; int16_t amp[SAMPLES_PER_CHUNK]; SNDFILE *inhandle; SF_INFO info; int len; const char *filename; logging_state_t *logging; filename = "fax_samp.wav"; if (argc > 1) filename = argv[1]; memset(&info, 0, sizeof(info)); if ((inhandle = sf_open(filename, SFM_READ, &info)) == NULL) { fprintf(stderr, " Cannot open audio file '%s' for reading\n", filename); exit(2); } if (info.samplerate != SAMPLE_RATE) { fprintf(stderr, " Unexpected sample rate in audio file '%s'\n", filename); exit(2); } if (info.channels != 1) { fprintf(stderr, " Unexpected number of channels in audio file '%s'\n", filename); exit(2); } memset(&t30_dummy, 0, sizeof(t30_dummy)); span_log_init(&t30_dummy.logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(&t30_dummy.logging, "T.30"); hdlc_rx_init(&hdlcrx, FALSE, TRUE, 5, hdlc_accept, NULL); fsk = fsk_rx_init(NULL, &preset_fsk_specs[FSK_V21CH2], FSK_FRAME_MODE_SYNC, v21_put_bit, NULL); v17 = v17_rx_init(NULL, 14400, v17_put_bit, NULL); v29 = v29_rx_init(NULL, 9600, v29_put_bit, NULL); //v29 = v29_rx_init(NULL, 7200, v29_put_bit, NULL); v27ter_4800 = v27ter_rx_init(NULL, 4800, v27ter_put_bit, NULL); v27ter_2400 = v27ter_rx_init(NULL, 2400, v27ter_put_bit, NULL); fsk_rx_signal_cutoff(fsk, -45.5); v17_rx_signal_cutoff(v17, -45.5); v29_rx_signal_cutoff(v29, -45.5); v27ter_rx_signal_cutoff(v27ter_4800, -40.0); v27ter_rx_signal_cutoff(v27ter_2400, -40.0); #if 1 logging = v17_rx_get_logging_state(v17); span_log_init(logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(logging, "V.17"); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW); logging = v29_rx_get_logging_state(v29); span_log_init(logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(logging, "V.29"); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW); logging = v27ter_rx_get_logging_state(v27ter_4800); span_log_init(logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(logging, "V.27ter-4800"); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW); logging = v27ter_rx_get_logging_state(v27ter_2400); span_log_init(logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(logging, "V.27ter-2400"); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW); #endif if (t4_rx_init(&t4_rx_state, "fax_decode.tif", T4_COMPRESSION_ITU_T4_2D) == NULL) { fprintf(stderr, "Failed to init\n"); exit(0); } for (;;) { len = sf_readf_short(inhandle, amp, SAMPLES_PER_CHUNK); if (len < SAMPLES_PER_CHUNK) break; fsk_rx(fsk, amp, len); v17_rx(v17, amp, len); v29_rx(v29, amp, len); v27ter_rx(v27ter_4800, amp, len); v27ter_rx(v27ter_2400, amp, len); } t4_rx_release(&t4_rx_state); if (sf_close(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", filename); exit(2); } return 0; }
int main(int argc, char *argv[]) { fsk_rx_state_t *fsk; v17_rx_state_t *v17; v29_rx_state_t *v29; v27ter_rx_state_t *v27ter; int16_t amp[SAMPLES_PER_CHUNK]; AFfilehandle inhandle; int len; const char *filename; float x; logging_state_t *logging; filename = "fax_samp.wav"; if (argc > 1) filename = argv[1]; if ((inhandle = afOpenFile(filename, "r", NULL)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot open wave file '%s'\n", filename); exit(2); } if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0) { printf(" Unexpected frame size in speech file '%s' (%f)\n", filename, x); exit(2); } if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE) { printf(" Unexpected sample rate in speech file '%s' (%f)\n", filename, x); exit(2); } if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0) { printf(" Unexpected number of channels in speech file '%s' (%f)\n", filename, x); exit(2); } memset(&t30_dummy, 0, sizeof(t30_dummy)); span_log_init(&t30_dummy.logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(&t30_dummy.logging, "T.30"); hdlc_rx_init(&hdlcrx, FALSE, TRUE, 5, hdlc_accept, NULL); fsk = fsk_rx_init(NULL, &preset_fsk_specs[FSK_V21CH2], TRUE, v21_put_bit, NULL); v17 = v17_rx_init(NULL, 14400, v17_put_bit, NULL); v29 = v29_rx_init(NULL, 9600, v29_put_bit, NULL); //v29 = v29_rx_init(NULL, 7200, v29_put_bit, NULL); v27ter = v27ter_rx_init(NULL, 4800, v27ter_put_bit, NULL); fsk_rx_signal_cutoff(fsk, -45.5); v17_rx_signal_cutoff(v17, -45.5); v29_rx_signal_cutoff(v29, -45.5); v27ter_rx_signal_cutoff(v27ter, -40.0); #if 1 logging = v17_rx_get_logging_state(v17); span_log_init(logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(logging, "V.17"); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW); logging = v29_rx_get_logging_state(v29); span_log_init(logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(logging, "V.29"); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW); logging = v27ter_rx_get_logging_state(v27ter); span_log_init(logging, SPAN_LOG_FLOW, NULL); span_log_set_protocol(logging, "V.27ter"); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW); #endif if (t4_rx_init(&t4_state, "fax_decode.tif", T4_COMPRESSION_ITU_T4_2D) == NULL) { fprintf(stderr, "Failed to init\n"); exit(0); } for (;;) { len = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, SAMPLES_PER_CHUNK); if (len < SAMPLES_PER_CHUNK) break; fsk_rx(fsk, amp, len); v17_rx(v17, amp, len); v29_rx(v29, amp, len); //v27ter_rx(v27ter, amp, len); } t4_rx_release(&t4_state); if (afCloseFile(inhandle) != 0) { fprintf(stderr, " Cannot close wave file '%s'\n", filename); exit(2); } return 0; }