int main(int argc, char *argv[]) { v27ter_rx_state_t rx; v27ter_tx_state_t tx; bert_results_t bert_results; int16_t gen_amp[BLOCK_LEN]; int16_t amp[BLOCK_LEN]; AFfilehandle inhandle; AFfilehandle outhandle; AFfilesetup filesetup; int outframes; int samples; int tep; int test_bps; int noise_level; int signal_level; int bits_per_test; int line_model_no; int block; int log_audio; int channel_codec; int rbs_pattern; float x; int opt; channel_codec = MUNGE_CODEC_NONE; rbs_pattern = 0; test_bps = 4800; 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:c:d:glm:n:r:s:t")) != -1) { switch (opt) { 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; } } argc -= optind; argv += optind; if (argc > 0) { if (strcmp(argv[0], "4800") == 0) test_bps = 4800; else if (strcmp(argv[0], "2400") == 0) test_bps = 2400; else { fprintf(stderr, "Invalid bit rate\n"); exit(2); } } inhandle = NULL; outhandle = NULL; filesetup = AF_NULL_FILESETUP; if (log_audio) { if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP) { fprintf(stderr, " Failed to create file setup\n"); exit(2); } afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE); afInitFileFormat(filesetup, AF_FILE_WAVE); afInitChannels(filesetup, AF_DEFAULT_TRACK, 1); if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME); exit(2); } } if (decode_test_file) { /* We will decode the audio from a wave file. */ if ((inhandle = afOpenFile(decode_test_file, "r", NULL)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot open wave file '%s'\n", decode_test_file); exit(2); } if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0f) { printf(" Unexpected frame size in speech file '%s' (%f)\n", decode_test_file, x); exit(2); } if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE) { printf(" Unexpected sample rate in speech file '%s' (%f)\n", decode_test_file, x); exit(2); } if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0f) { printf(" Unexpected number of channels in speech file '%s' (%f)\n", decode_test_file, x); exit(2); } } else { /* We will generate V.27ter audio, and add some noise to it. */ v27ter_tx_init(&tx, test_bps, tep, v27tergetbit, NULL); v27ter_tx_power(&tx, signal_level); v27ter_tx_set_modem_status_handler(&tx, v27ter_tx_status, (void *) &tx); /* Move the carrier off a bit */ tx.carrier_phase_rate = dds_phase_ratef(1810.0f); 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); } } v27ter_rx_init(&rx, test_bps, v27terputbit, NULL); v27ter_rx_set_modem_status_handler(&rx, v27ter_rx_status, (void *) &rx); v27ter_rx_set_qam_report_handler(&rx, qam_report, (void *) &rx); span_log_set_level(&rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW); span_log_set_tag(&rx.logging, "V.27ter-rx"); #if defined(ENABLE_GUI) if (use_gui) { qam_monitor = qam_monitor_init(2.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 = 0; ; block++) { if (decode_test_file) { samples = afReadFrames(inhandle, AF_DEFAULT_TRACK, 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 = v27ter_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 */ memset(amp, 0, BLOCK_LEN*sizeof(int16_t)); v27ter_rx(&rx, amp, BLOCK_LEN); v27ter_rx(&rx, amp, BLOCK_LEN); v27ter_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, %d bits, %d bad bits, %d resyncs\n", signal_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs); fprintf(stderr, "Last report %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_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)); signal_level--; v27ter_tx_restart(&tx, test_bps, tep); v27ter_tx_power(&tx, signal_level); v27ter_rx_restart(&rx, test_bps, FALSE); 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 = afWriteFrames(outhandle, AF_DEFAULT_TRACK, gen_amp, samples); if (outframes != samples) { fprintf(stderr, " Error writing wave 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 v27ter_rx(&rx, amp, samples); if (decode_test_file == NULL && block%500 == 0) printf("Noise level is %d\n", noise_level); } if (!decode_test_file) { bert_result(&bert, &bert_results); fprintf(stderr, "At completion:\n"); fprintf(stderr, "Final result %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs); fprintf(stderr, "Last report %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_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 (log_audio) { if (afCloseFile(outhandle)) { fprintf(stderr, " Cannot close wave file '%s'\n", OUT_FILE_NAME); exit(2); } afFreeFileSetup(filesetup); } return 0; }
SPAN_DECLARE(void) fax_modems_start_fast_modem(fax_modems_state_t *s, int which, int bit_rate, int short_train, int hdlc_mode) { put_bit_func_t put_bit; get_bit_func_t get_bit; void *get_bit_user_data; void *put_bit_user_data; s->bit_rate = bit_rate; if (hdlc_mode) { get_bit = (get_bit_func_t) hdlc_tx_get_bit; get_bit_user_data = (void *) &s->hdlc_tx; put_bit = (put_bit_func_t) hdlc_rx_put_bit; put_bit_user_data = (void *) &s->hdlc_rx; } else { get_bit = s->get_bit; get_bit_user_data = s->get_bit_user_data; put_bit = s->put_bit; put_bit_user_data = s->put_bit_user_data; } /*endif*/ /* If we change modems we need to do a complete reinitialisation of the modem, because the modems use overlapping memory. */ if (s->fast_modem != which) { s->current_rx_type = which; s->short_train = false; s->fast_modem = which; if (hdlc_mode) s->rx_frame_received = false; switch (s->fast_modem) { case FAX_MODEM_V27TER_RX: v27ter_rx_init(&s->fast_modems.v27ter_rx, s->bit_rate, put_bit, put_bit_user_data); v27ter_rx_set_modem_status_handler(&s->fast_modems.v27ter_rx, v27ter_rx_status_handler, s); fax_modems_set_rx_handler(s, (span_rx_handler_t) &fax_modems_v27ter_v21_rx, s, (span_rx_fillin_handler_t) &fax_modems_v27ter_v21_rx_fillin, s); break; case FAX_MODEM_V29_RX: v29_rx_init(&s->fast_modems.v29_rx, s->bit_rate, put_bit, put_bit_user_data); v29_rx_signal_cutoff(&s->fast_modems.v29_rx, -45.5f); v29_rx_set_modem_status_handler(&s->fast_modems.v29_rx, v29_rx_status_handler, s); fax_modems_set_rx_handler(s, (span_rx_handler_t) &fax_modems_v29_v21_rx, s, (span_rx_fillin_handler_t) &fax_modems_v29_v21_rx_fillin, s); break; case FAX_MODEM_V17_RX: v17_rx_init(&s->fast_modems.v17_rx, s->bit_rate, put_bit, put_bit_user_data); v17_rx_set_modem_status_handler(&s->fast_modems.v17_rx, v17_rx_status_handler, s); fax_modems_set_rx_handler(s, (span_rx_handler_t) &fax_modems_v17_v21_rx, s, (span_rx_fillin_handler_t) &fax_modems_v17_v21_rx_fillin, s); break; case FAX_MODEM_V27TER_TX: v27ter_tx_init(&s->fast_modems.v27ter_tx, s->bit_rate, s->use_tep, get_bit, get_bit_user_data); fax_modems_set_tx_handler(s, (span_tx_handler_t) &v27ter_tx, &s->fast_modems.v27ter_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; case FAX_MODEM_V29_TX: v29_tx_init(&s->fast_modems.v29_tx, s->bit_rate, s->use_tep, get_bit, get_bit_user_data); fax_modems_set_tx_handler(s, (span_tx_handler_t) &v29_tx, &s->fast_modems.v29_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; case FAX_MODEM_V17_TX: v17_tx_init(&s->fast_modems.v17_tx, s->bit_rate, s->use_tep, get_bit, get_bit_user_data); fax_modems_set_tx_handler(s, (span_tx_handler_t) &v17_tx, &s->fast_modems.v17_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; } /*endswitch*/ } else { s->short_train = short_train; switch (s->fast_modem) { case FAX_MODEM_V27TER_RX: v27ter_rx_restart(&s->fast_modems.v27ter_rx, s->bit_rate, false); v27ter_rx_set_put_bit(&s->fast_modems.v27ter_rx, put_bit, put_bit_user_data); v27ter_rx_set_modem_status_handler(&s->fast_modems.v27ter_rx, v27ter_rx_status_handler, s); fax_modems_set_rx_handler(s, (span_rx_handler_t) &fax_modems_v27ter_v21_rx, s, (span_rx_fillin_handler_t) &fax_modems_v27ter_v21_rx_fillin, s); break; case FAX_MODEM_V29_RX: v29_rx_restart(&s->fast_modems.v29_rx, s->bit_rate, false); v29_rx_set_put_bit(&s->fast_modems.v29_rx, put_bit, put_bit_user_data); v29_rx_set_modem_status_handler(&s->fast_modems.v29_rx, v29_rx_status_handler, s); fax_modems_set_rx_handler(s, (span_rx_handler_t) &fax_modems_v29_v21_rx, s, (span_rx_fillin_handler_t) &fax_modems_v29_v21_rx_fillin, s); break; case FAX_MODEM_V17_RX: v17_rx_restart(&s->fast_modems.v17_rx, s->bit_rate, s->short_train); v17_rx_set_put_bit(&s->fast_modems.v17_rx, put_bit, put_bit_user_data); v17_rx_set_modem_status_handler(&s->fast_modems.v17_rx, v17_rx_status_handler, s); fax_modems_set_rx_handler(s, (span_rx_handler_t) &fax_modems_v17_v21_rx, s, (span_rx_fillin_handler_t) &fax_modems_v17_v21_rx_fillin, s); break; case FAX_MODEM_V27TER_TX: v27ter_tx_restart(&s->fast_modems.v27ter_tx, s->bit_rate, s->use_tep); v27ter_tx_set_get_bit(&s->fast_modems.v27ter_tx, get_bit, get_bit_user_data); fax_modems_set_tx_handler(s, (span_tx_handler_t) &v27ter_tx, &s->fast_modems.v27ter_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; case FAX_MODEM_V29_TX: v29_tx_restart(&s->fast_modems.v29_tx, s->bit_rate, s->use_tep); v29_tx_set_get_bit(&s->fast_modems.v29_tx, get_bit, get_bit_user_data); fax_modems_set_tx_handler(s, (span_tx_handler_t) &v29_tx, &s->fast_modems.v29_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; case FAX_MODEM_V17_TX: v17_tx_restart(&s->fast_modems.v17_tx, s->bit_rate, s->use_tep, s->short_train); v17_tx_set_get_bit(&s->fast_modems.v17_tx, get_bit, get_bit_user_data); fax_modems_set_tx_handler(s, (span_tx_handler_t) &v17_tx, &s->fast_modems.v17_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; } /*endswitch*/ } /*endif*/ }
static void fax_set_tx_type(void *user_data, int type, int short_train, int use_hdlc) { fax_state_t *s; tone_gen_descriptor_t tone_desc; get_bit_func_t get_bit_func; void *get_bit_user_data; s = (fax_state_t *) user_data; span_log(&s->logging, SPAN_LOG_FLOW, "Set tx type %d\n", type); if (s->current_tx_type == type) return; s->first_tx_hdlc_frame = TRUE; if (use_hdlc) { get_bit_func = (get_bit_func_t) hdlc_tx_get_bit; get_bit_user_data = (void *) &(s->hdlctx); } else { get_bit_func = t30_non_ecm_get_bit; get_bit_user_data = (void *) &(s->t30_state); } switch (type) { case T30_MODEM_PAUSE: silence_gen_alter(&(s->silence_gen), ms_to_samples(short_train)); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = NULL; s->transmit = TRUE; break; case T30_MODEM_CNG: /* 0.5s of 1100Hz+-38Hz + 3.0s of silence repeating. Timing +-15% */ make_tone_gen_descriptor(&tone_desc, 1100, -11, 0, 0, 500, 3000, 0, 0, TRUE); tone_gen_init(&(s->tone_gen), &tone_desc); s->tx_handler = (span_tx_handler_t *) &tone_gen; s->tx_user_data = &(s->tone_gen); s->next_tx_handler = NULL; s->transmit = TRUE; break; case T30_MODEM_CED: /* 0.2s of silence, then 2.6s to 4s of 2100Hz+-15Hz tone, then 75ms of silence. The 75ms of silence will be inserted by the pre V.21 pause we use for any switch to V.21. */ silence_gen_alter(&(s->silence_gen), ms_to_samples(200)); make_tone_gen_descriptor(&tone_desc, 2100, -11, 0, 0, 2600, 0, 0, 0, FALSE); tone_gen_init(&(s->tone_gen), &tone_desc); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &tone_gen; s->next_tx_user_data = &(s->tone_gen); s->transmit = TRUE; break; case T30_MODEM_V21: fsk_tx_init(&(s->v21tx), &preset_fsk_specs[FSK_V21CH2], get_bit_func, get_bit_user_data); /* The spec says 1s +-15% of preamble. So, the minimum is 32 octets. */ hdlc_tx_flags(&(s->hdlctx), 32); /* Pause before switching from phase C, as per T.30 5.3.2.2. If we omit this, the receiver might not see the carrier fall between the high speed and low speed sections. In practice, a 75ms gap before any V.21 transmission is harmless, adds little to the overall length of a call, and ensures the receiving end is ready. */ silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &fsk_tx; s->next_tx_user_data = &(s->v21tx); s->transmit = TRUE; break; case T30_MODEM_V27TER_2400: silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); v27ter_tx_restart(&(s->v27ter_tx), 2400, s->use_tep); v27ter_tx_set_get_bit(&(s->v27ter_tx), get_bit_func, get_bit_user_data); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &v27ter_tx; s->next_tx_user_data = &(s->v27ter_tx); hdlc_tx_flags(&(s->hdlctx), 60); s->transmit = TRUE; break; case T30_MODEM_V27TER_4800: silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); v27ter_tx_restart(&(s->v27ter_tx), 4800, s->use_tep); v27ter_tx_set_get_bit(&(s->v27ter_tx), get_bit_func, get_bit_user_data); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &v27ter_tx; s->next_tx_user_data = &(s->v27ter_tx); hdlc_tx_flags(&(s->hdlctx), 120); s->transmit = TRUE; break; case T30_MODEM_V29_7200: silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); v29_tx_restart(&(s->v29tx), 7200, s->use_tep); v29_tx_set_get_bit(&(s->v29tx), get_bit_func, get_bit_user_data); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &v29_tx; s->next_tx_user_data = &(s->v29tx); hdlc_tx_flags(&(s->hdlctx), 180); s->transmit = TRUE; break; case T30_MODEM_V29_9600: silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); v29_tx_restart(&(s->v29tx), 9600, s->use_tep); v29_tx_set_get_bit(&(s->v29tx), get_bit_func, get_bit_user_data); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &v29_tx; s->next_tx_user_data = &(s->v29tx); hdlc_tx_flags(&(s->hdlctx), 240); s->transmit = TRUE; break; case T30_MODEM_V17_7200: silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); v17_tx_restart(&(s->v17tx), 7200, s->use_tep, short_train); v17_tx_set_get_bit(&(s->v17tx), get_bit_func, get_bit_user_data); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &v17_tx; s->next_tx_user_data = &(s->v17tx); hdlc_tx_flags(&(s->hdlctx), 180); s->transmit = TRUE; break; case T30_MODEM_V17_9600: silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); v17_tx_restart(&(s->v17tx), 9600, s->use_tep, short_train); v17_tx_set_get_bit(&(s->v17tx), get_bit_func, get_bit_user_data); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &v17_tx; s->next_tx_user_data = &(s->v17tx); hdlc_tx_flags(&(s->hdlctx), 240); s->transmit = TRUE; break; case T30_MODEM_V17_12000: silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); v17_tx_restart(&(s->v17tx), 12000, s->use_tep, short_train); v17_tx_set_get_bit(&(s->v17tx), get_bit_func, get_bit_user_data); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &v17_tx; s->next_tx_user_data = &(s->v17tx); hdlc_tx_flags(&(s->hdlctx), 300); s->transmit = TRUE; break; case T30_MODEM_V17_14400: silence_gen_alter(&(s->silence_gen), ms_to_samples(75)); v17_tx_restart(&(s->v17tx), 14400, s->use_tep, short_train); v17_tx_set_get_bit(&(s->v17tx), get_bit_func, get_bit_user_data); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = (span_tx_handler_t *) &v17_tx; s->next_tx_user_data = &(s->v17tx); hdlc_tx_flags(&(s->hdlctx), 360); s->transmit = TRUE; break; case T30_MODEM_DONE: span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n"); /* Fall through */ default: silence_gen_alter(&(s->silence_gen), 0); s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &(s->silence_gen); s->next_tx_handler = NULL; s->transmit = FALSE; break; } s->current_tx_type = type; }
static void fax_set_tx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc) { fax_state_t *s; get_bit_func_t get_bit_func; void *get_bit_user_data; fax_modems_state_t *t; int tone; s = (fax_state_t *) user_data; t = &s->modems; span_log(&s->logging, SPAN_LOG_FLOW, "Set tx type %d\n", type); if (t->current_tx_type == type) return; if (use_hdlc) { get_bit_func = (get_bit_func_t) hdlc_tx_get_bit; get_bit_user_data = (void *) &t->hdlc_tx; } else { get_bit_func = t30_non_ecm_get_bit; get_bit_user_data = (void *) &s->t30; } switch (type) { case T30_MODEM_PAUSE: silence_gen_alter(&t->silence_gen, ms_to_samples(short_train)); set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); t->transmit = TRUE; break; case T30_MODEM_CED: case T30_MODEM_CNG: if (type == T30_MODEM_CED) tone = MODEM_CONNECT_TONES_FAX_CED; else tone = MODEM_CONNECT_TONES_FAX_CNG; modem_connect_tones_tx_init(&t->connect_tx, tone); set_tx_handler(s, (span_tx_handler_t *) &modem_connect_tones_tx, &t->connect_tx); set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); t->transmit = TRUE; break; case T30_MODEM_V21: fsk_tx_init(&t->v21_tx, &preset_fsk_specs[FSK_V21CH2], get_bit_func, get_bit_user_data); /* The spec says 1s +-15% of preamble. So, the minimum is 32 octets. */ hdlc_tx_flags(&t->hdlc_tx, 32); /* Pause before switching from phase C, as per T.30 5.3.2.2. If we omit this, the receiver might not see the carrier fall between the high speed and low speed sections. In practice, a 75ms gap before any V.21 transmission is harmless, adds little to the overall length of a call, and ensures the receiving end is ready. */ silence_gen_alter(&t->silence_gen, ms_to_samples(75)); set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); set_next_tx_handler(s, (span_tx_handler_t *) &fsk_tx, &t->v21_tx); t->transmit = TRUE; break; case T30_MODEM_V27TER: silence_gen_alter(&t->silence_gen, ms_to_samples(75)); /* For any fast modem, set 200ms of preamble flags */ hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5)); v27ter_tx_restart(&t->v27ter_tx, bit_rate, t->use_tep); v27ter_tx_set_get_bit(&t->v27ter_tx, get_bit_func, get_bit_user_data); set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); set_next_tx_handler(s, (span_tx_handler_t *) &v27ter_tx, &t->v27ter_tx); t->transmit = TRUE; break; case T30_MODEM_V29: silence_gen_alter(&t->silence_gen, ms_to_samples(75)); /* For any fast modem, set 200ms of preamble flags */ hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5)); v29_tx_restart(&t->v29_tx, bit_rate, t->use_tep); v29_tx_set_get_bit(&t->v29_tx, get_bit_func, get_bit_user_data); set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); set_next_tx_handler(s, (span_tx_handler_t *) &v29_tx, &t->v29_tx); t->transmit = TRUE; break; case T30_MODEM_V17: silence_gen_alter(&t->silence_gen, ms_to_samples(75)); /* For any fast modem, set 200ms of preamble flags */ hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5)); v17_tx_restart(&t->v17_tx, bit_rate, t->use_tep, short_train); v17_tx_set_get_bit(&t->v17_tx, get_bit_func, get_bit_user_data); set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); set_next_tx_handler(s, (span_tx_handler_t *) &v17_tx, &t->v17_tx); t->transmit = TRUE; break; case T30_MODEM_DONE: span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n"); /* Fall through */ default: silence_gen_alter(&t->silence_gen, 0); set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); t->transmit = FALSE; break; } t->tx_bit_rate = bit_rate; t->current_tx_type = type; }
int main(int argc, char *argv[]) { v27ter_rx_state_t *rx; v27ter_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 test_bps; int noise_level; int signal_level; int bits_per_test; int line_model_no; int block_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 = 4800; 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 != 4800 && test_bps != 2400) { 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.27ter audio, and add some noise to it. */ tx = v27ter_tx_init(NULL, test_bps, tep, v27tergetbit, NULL); v27ter_tx_power(tx, signal_level); v27ter_tx_set_modem_status_handler(tx, v27ter_tx_status, (void *) tx); /* Move the carrier off a bit */ #if defined(WITH_SPANDSP_INTERNALS) tx->carrier_phase_rate = dds_phase_ratef(1810.0f); #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); } } rx = v27ter_rx_init(NULL, test_bps, v27terputbit, NULL); v27ter_rx_set_modem_status_handler(rx, v27ter_rx_status, (void *) rx); v27ter_rx_set_qam_report_handler(rx, qam_report, (void *) rx); logging = v27ter_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.27ter-rx"); #if defined(ENABLE_GUI) if (use_gui) { qam_monitor = qam_monitor_init(2.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++) { 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 = v27ter_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); v27ter_rx(rx, amp, BLOCK_LEN); v27ter_rx(rx, amp, BLOCK_LEN); v27ter_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)); signal_level--; v27ter_tx_restart(tx, test_bps, tep); v27ter_tx_power(tx, signal_level); v27ter_rx_restart(rx, test_bps, FALSE); 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 v27ter_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(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", decode_test_file); exit(2); } } if (log_audio) { if (sf_close(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); exit(2); } } return 0; }