SPAN_DECLARE(void) fax_modems_start_slow_modem(fax_modems_state_t *s, int which) { switch (which) { case FAX_MODEM_V21_RX: fsk_rx_init(&s->v21_rx, &preset_fsk_specs[FSK_V21CH2], FSK_FRAME_MODE_SYNC, (put_bit_func_t) hdlc_rx_put_bit, &s->hdlc_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); fsk_rx_signal_cutoff(&s->v21_rx, -39.09f); s->rx_frame_received = false; break; case FAX_MODEM_CED_TONE_RX: modem_connect_tones_rx_init(&s->connect_rx, MODEM_CONNECT_TONES_FAX_CED, s->tone_callback, s->tone_callback_user_data); fax_modems_set_rx_handler(s, (span_rx_handler_t) &modem_connect_tones_rx, &s->connect_rx, (span_rx_fillin_handler_t) &modem_connect_tones_rx_fillin, &s->connect_rx); break; case FAX_MODEM_CNG_TONE_RX: modem_connect_tones_rx_init(&s->connect_rx, MODEM_CONNECT_TONES_FAX_CNG, s->tone_callback, s->tone_callback_user_data); fax_modems_set_rx_handler(s, (span_rx_handler_t) &modem_connect_tones_rx, &s->connect_rx, (span_rx_fillin_handler_t) &modem_connect_tones_rx_fillin, &s->connect_rx); break; case FAX_MODEM_V21_TX: fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx); fax_modems_set_tx_handler(s, (span_tx_handler_t) &fsk_tx, &s->v21_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; case FAX_MODEM_CED_TONE_TX: modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CED); fax_modems_set_tx_handler(s, (span_tx_handler_t) &modem_connect_tones_tx, &s->connect_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; case FAX_MODEM_CNG_TONE_TX: modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG); fax_modems_set_tx_handler(s, (span_tx_handler_t) &modem_connect_tones_tx, &s->connect_tx); fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL); break; } }
static void fax_set_rx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc) { fax_state_t *s; put_bit_func_t put_bit_func; void *put_bit_user_data; fax_modems_state_t *t; s = (fax_state_t *) user_data; t = &s->modems; span_log(&s->logging, SPAN_LOG_FLOW, "Set rx type %d\n", type); if (t->current_rx_type == type) return; /*endif*/ t->current_rx_type = type; t->rx_bit_rate = bit_rate; if (use_hdlc) { put_bit_func = (put_bit_func_t) hdlc_rx_put_bit; put_bit_user_data = (void *) &t->hdlc_rx; hdlc_rx_init(&t->hdlc_rx, false, true, HDLC_FRAMING_OK_THRESHOLD, t30_hdlc_accept, &s->t30); } else { put_bit_func = t30_non_ecm_put_bit; put_bit_user_data = (void *) &s->t30; } /*endif*/ switch (type) { case T30_MODEM_V21: fsk_rx_init(&t->v21_rx, &preset_fsk_specs[FSK_V21CH2], FSK_FRAME_MODE_SYNC, (put_bit_func_t) hdlc_rx_put_bit, put_bit_user_data); fsk_rx_signal_cutoff(&t->v21_rx, -45.5f); set_rx_handler(s, (span_rx_handler_t *) &fsk_rx, (span_rx_fillin_handler_t *) &fsk_rx_fillin, &t->v21_rx); break; case T30_MODEM_V17: v17_rx_restart(&t->fast_modems.v17_rx, bit_rate, short_train); v17_rx_set_put_bit(&t->fast_modems.v17_rx, put_bit_func, put_bit_user_data); set_rx_handler(s, &v17_v21_rx, &v17_v21_rx_fillin, s); break; case T30_MODEM_V27TER: v27ter_rx_restart(&t->fast_modems.v27ter_rx, bit_rate, false); v27ter_rx_set_put_bit(&t->fast_modems.v27ter_rx, put_bit_func, put_bit_user_data); set_rx_handler(s, &v27ter_v21_rx, &v27ter_v21_rx_fillin, s); break; case T30_MODEM_V29: v29_rx_restart(&t->fast_modems.v29_rx, bit_rate, false); v29_rx_set_put_bit(&t->fast_modems.v29_rx, put_bit_func, put_bit_user_data); set_rx_handler(s, &v29_v21_rx, &v29_v21_rx_fillin, s); break; case T30_MODEM_DONE: span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n"); default: set_rx_handler(s, (span_rx_handler_t *) &span_dummy_rx, (span_rx_fillin_handler_t *) &span_dummy_rx_fillin, s); break; } /*endswitch*/ }
SPAN_DECLARE(fax_modems_state_t *) fax_modems_init(fax_modems_state_t *s, int use_tep, hdlc_frame_handler_t hdlc_accept, hdlc_underflow_handler_t hdlc_tx_underflow, put_bit_func_t non_ecm_put_bit, get_bit_func_t non_ecm_get_bit, tone_report_func_t tone_callback, void *user_data) { if (s == NULL) { if ((s = (fax_modems_state_t *) malloc(sizeof(*s))) == NULL) return NULL; } memset(s, 0, sizeof(*s)); s->use_tep = use_tep; hdlc_rx_init(&s->hdlc_rx, FALSE, FALSE, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept, user_data); hdlc_tx_init(&s->hdlc_tx, FALSE, 2, FALSE, hdlc_tx_underflow, user_data); fsk_rx_init(&s->v21_rx, &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &s->hdlc_rx); fsk_rx_signal_cutoff(&s->v21_rx, -39.09f); fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx); v17_rx_init(&s->v17_rx, 14400, non_ecm_put_bit, user_data); v17_tx_init(&s->v17_tx, 14400, s->use_tep, non_ecm_get_bit, user_data); v29_rx_init(&s->v29_rx, 9600, non_ecm_put_bit, user_data); v29_rx_signal_cutoff(&s->v29_rx, -45.5f); v29_tx_init(&s->v29_tx, 9600, s->use_tep, non_ecm_get_bit, user_data); v27ter_rx_init(&s->v27ter_rx, 4800, non_ecm_put_bit, user_data); v27ter_tx_init(&s->v27ter_tx, 4800, s->use_tep, non_ecm_get_bit, user_data); silence_gen_init(&s->silence_gen, 0); modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG); if (tone_callback) { modem_connect_tones_rx_init(&s->connect_rx, MODEM_CONNECT_TONES_FAX_CNG, tone_callback, user_data); } dc_restore_init(&s->dc_restore); s->rx_signal_present = FALSE; s->rx_handler = (span_rx_handler_t *) &span_dummy_rx; s->rx_user_data = NULL; s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &s->silence_gen; return s; }
SPAN_DECLARE(modem_connect_tones_rx_state_t *) modem_connect_tones_rx_init(modem_connect_tones_rx_state_t *s, int tone_type, tone_report_func_t tone_callback, void *user_data) { if (s == NULL) { if ((s = (modem_connect_tones_rx_state_t *) malloc(sizeof(*s))) == NULL) return NULL; } s->tone_type = tone_type; switch (s->tone_type) { case MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE: fsk_rx_init(&(s->v21rx), &preset_fsk_specs[FSK_V21CH2], TRUE, v21_put_bit, s); fsk_rx_signal_cutoff(&(s->v21rx), -45.5f); break; case MODEM_CONNECT_TONES_ANS_PR: case MODEM_CONNECT_TONES_ANSAM: case MODEM_CONNECT_TONES_ANSAM_PR: /* Treat these all the same for receive purposes */ s->tone_type = MODEM_CONNECT_TONES_ANS; break; } s->channel_level = 0; s->notch_level = 0; s->tone_present = MODEM_CONNECT_TONES_NONE; s->tone_cycle_duration = 0; s->good_cycles = 0; s->hit = MODEM_CONNECT_TONES_NONE; s->tone_on = FALSE; s->tone_callback = tone_callback; s->callback_data = user_data; s->z1 = 0.0f; s->z2 = 0.0f; s->num_bits = 0; s->flags_seen = 0; s->framing_ok_announced = FALSE; s->raw_bit_stream = 0; return s; }
int main(int argc, char *argv[]) { fsk_tx_state_t *caller_tx; fsk_rx_state_t *caller_rx; fsk_tx_state_t *answerer_tx; fsk_rx_state_t *answerer_rx; bert_state_t caller_bert; bert_state_t answerer_bert; bert_results_t bert_results; power_meter_t caller_meter; power_meter_t answerer_meter; int16_t caller_amp[BLOCK_LEN]; int16_t answerer_amp[BLOCK_LEN]; int16_t caller_model_amp[BLOCK_LEN]; int16_t answerer_model_amp[BLOCK_LEN]; int16_t out_amp[2*BLOCK_LEN]; AFfilehandle inhandle; AFfilehandle outhandle; int outframes; int i; int j; int samples; int test_bps; int noise_level; int noise_sweep; int bits_per_test; int line_model_no; int modem_under_test_1; int modem_under_test_2; int modems_set; int log_audio; int channel_codec; int rbs_pattern; int on_at; int off_at; tone_gen_descriptor_t tone_desc; tone_gen_state_t tone_tx; int opt; channel_codec = MUNGE_CODEC_NONE; rbs_pattern = 0; line_model_no = 0; decode_test_file = NULL; noise_sweep = FALSE; modem_under_test_1 = FSK_V21CH1; modem_under_test_2 = FSK_V21CH2; log_audio = FALSE; modems_set = 0; while ((opt = getopt(argc, argv, "c:dlm:nr:s:")) != -1) { switch (opt) { case 'c': channel_codec = atoi(optarg); break; case 'd': decode_test_file = optarg; break; case 'l': log_audio = TRUE; break; case 'm': line_model_no = atoi(optarg); break; case 'n': noise_sweep = TRUE; break; case 'r': rbs_pattern = atoi(optarg); break; case 's': switch (modems_set++) { case 0: modem_under_test_1 = atoi(optarg); break; case 1: modem_under_test_2 = atoi(optarg); break; } break; default: //usage(); exit(2); break; } } if (modem_under_test_1 >= 0) printf("Modem channel 1 is '%s'\n", preset_fsk_specs[modem_under_test_1].name); if (modem_under_test_2 >= 0) printf("Modem channel 2 is '%s'\n", preset_fsk_specs[modem_under_test_2].name); outhandle = AF_NULL_FILEHANDLE; if (log_audio) { if ((outhandle = afOpenFile_telephony_write(OUTPUT_FILE_NAME, 2)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot create wave file '%s'\n", OUTPUT_FILE_NAME); exit(2); } } noise_level = -200; bits_per_test = 0; inhandle = NULL; memset(caller_amp, 0, sizeof(*caller_amp)); memset(answerer_amp, 0, sizeof(*answerer_amp)); memset(caller_model_amp, 0, sizeof(*caller_model_amp)); memset(answerer_model_amp, 0, sizeof(*answerer_model_amp)); power_meter_init(&caller_meter, 7); power_meter_init(&answerer_meter, 7); if (decode_test_file) { if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot open wave file '%s'\n", decode_test_file); exit(2); } caller_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_1], TRUE, put_bit, NULL); fsk_rx_set_modem_status_handler(caller_rx, rx_status, (void *) &caller_rx); test_bps = preset_fsk_specs[modem_under_test_1].baud_rate; for (;;) { samples = afReadFrames(inhandle, AF_DEFAULT_TRACK, caller_model_amp, BLOCK_LEN); if (samples < BLOCK_LEN) break; for (i = 0; i < samples; i++) power_meter_update(&caller_meter, caller_model_amp[i]); fsk_rx(caller_rx, caller_model_amp, samples); } if (afCloseFile(inhandle) != 0) { fprintf(stderr, " Cannot close wave file '%s'\n", decode_test_file); exit(2); } } else { printf("Test cutoff level\n"); caller_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_1], TRUE, cutoff_test_put_bit, NULL); fsk_rx_signal_cutoff(caller_rx, -30.0f); fsk_rx_set_modem_status_handler(caller_rx, cutoff_test_rx_status, (void *) &caller_rx); on_at = 0; for (i = -40; i < -25; i++) { make_tone_gen_descriptor(&tone_desc, 1500, i, 0, 0, 1, 0, 0, 0, TRUE); tone_gen_init(&tone_tx, &tone_desc); for (j = 0; j < 10; j++) { samples = tone_gen(&tone_tx, caller_model_amp, 160); fsk_rx(caller_rx, caller_model_amp, samples); } if (cutoff_test_carrier) break; } on_at = i; off_at = 0; for ( ; i > -40; i--) { make_tone_gen_descriptor(&tone_desc, 1500, i, 0, 0, 1, 0, 0, 0, TRUE); tone_gen_init(&tone_tx, &tone_desc); for (j = 0; j < 10; j++) { samples = tone_gen(&tone_tx, caller_model_amp, 160); fsk_rx(caller_rx, caller_model_amp, samples); } if (!cutoff_test_carrier) break; } off_at = i; printf("Carrier on at %d, off at %d\n", on_at, off_at); if (on_at < -29 || on_at > -26 || off_at < -35 || off_at > -31) { printf("Tests failed.\n"); exit(2); } printf("Test with BERT\n"); test_bps = preset_fsk_specs[modem_under_test_1].baud_rate; if (modem_under_test_1 >= 0) { caller_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem_under_test_1], (get_bit_func_t) bert_get_bit, &caller_bert); fsk_tx_set_modem_status_handler(caller_tx, tx_status, (void *) &caller_tx); answerer_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_1], TRUE, (put_bit_func_t) bert_put_bit, &answerer_bert); fsk_rx_set_modem_status_handler(answerer_rx, rx_status, (void *) &answerer_rx); } if (modem_under_test_2 >= 0) { answerer_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem_under_test_2], (get_bit_func_t) bert_get_bit, &answerer_bert); fsk_tx_set_modem_status_handler(answerer_tx, tx_status, (void *) &answerer_tx); caller_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_2], TRUE, (put_bit_func_t) bert_put_bit, &caller_bert); fsk_rx_set_modem_status_handler(caller_rx, rx_status, (void *) &caller_rx); } test_bps = preset_fsk_specs[modem_under_test_1].baud_rate; bits_per_test = 500000; noise_level = -24; bert_init(&caller_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20); bert_set_report(&caller_bert, 100000, reporter, (void *) (intptr_t) 1); bert_init(&answerer_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20); bert_set_report(&answerer_bert, 100000, reporter, (void *) (intptr_t) 2); if ((model = both_ways_line_model_init(line_model_no, (float) noise_level, line_model_no, (float) noise_level, channel_codec, rbs_pattern)) == NULL) { fprintf(stderr, " Failed to create line model\n"); exit(2); } for (;;) { samples = fsk_tx(caller_tx, caller_amp, BLOCK_LEN); for (i = 0; i < samples; i++) power_meter_update(&caller_meter, caller_amp[i]); samples = fsk_tx(answerer_tx, answerer_amp, BLOCK_LEN); for (i = 0; i < samples; i++) power_meter_update(&answerer_meter, answerer_amp[i]); both_ways_line_model(model, caller_model_amp, caller_amp, answerer_model_amp, answerer_amp, samples); //printf("Powers %10.5fdBm0 %10.5fdBm0\n", power_meter_current_dbm0(&caller_meter), power_meter_current_dbm0(&answerer_meter)); fsk_rx(answerer_rx, caller_model_amp, samples); for (i = 0; i < samples; i++) out_amp[2*i] = caller_model_amp[i]; for ( ; i < BLOCK_LEN; i++) out_amp[2*i] = 0; fsk_rx(caller_rx, answerer_model_amp, samples); for (i = 0; i < samples; i++) out_amp[2*i + 1] = answerer_model_amp[i]; for ( ; i < BLOCK_LEN; i++) out_amp[2*i + 1] = 0; if (log_audio) { outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, out_amp, BLOCK_LEN); if (outframes != BLOCK_LEN) { fprintf(stderr, " Error writing wave file\n"); exit(2); } } if (samples < BLOCK_LEN) { bert_result(&caller_bert, &bert_results); fprintf(stderr, "%ddB AWGN, %d bits, %d bad bits, %d resyncs\n", noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs); if (!noise_sweep) { if (bert_results.total_bits != bits_per_test - 43 || bert_results.bad_bits != 0 || bert_results.resyncs != 0) { printf("Tests failed.\n"); exit(2); } } bert_result(&answerer_bert, &bert_results); fprintf(stderr, "%ddB AWGN, %d bits, %d bad bits, %d resyncs\n", noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs); if (!noise_sweep) { if (bert_results.total_bits != bits_per_test - 43 || bert_results.bad_bits != 0 || bert_results.resyncs != 0) { printf("Tests failed.\n"); exit(2); } break; } /* Put a little silence between the chunks in the file. */ memset(out_amp, 0, sizeof(out_amp)); if (log_audio) { for (i = 0; i < 200; i++) { outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, out_amp, BLOCK_LEN); } } if (modem_under_test_1 >= 0) { caller_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem_under_test_1], (get_bit_func_t) bert_get_bit, &caller_bert); fsk_tx_set_modem_status_handler(caller_tx, tx_status, (void *) &caller_tx); answerer_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_1], TRUE, (put_bit_func_t) bert_put_bit, &answerer_bert); fsk_rx_set_modem_status_handler(answerer_rx, rx_status, (void *) &answerer_rx); } if (modem_under_test_2 >= 0) { answerer_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem_under_test_2], (get_bit_func_t) bert_get_bit, &answerer_bert); fsk_tx_set_modem_status_handler(answerer_tx, tx_status, (void *) &answerer_tx); caller_rx = fsk_rx_init(NULL, &preset_fsk_specs[modem_under_test_2], TRUE, (put_bit_func_t) bert_put_bit, &caller_bert); fsk_rx_set_modem_status_handler(caller_rx, rx_status, (void *) &caller_rx); } noise_level++; if ((model = both_ways_line_model_init(line_model_no, (float) noise_level, line_model_no, noise_level, channel_codec, 0)) == NULL) { fprintf(stderr, " Failed to create line model\n"); exit(2); } bert_init(&caller_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20); bert_set_report(&caller_bert, 100000, reporter, (void *) (intptr_t) 1); bert_init(&answerer_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20); bert_set_report(&answerer_bert, 100000, reporter, (void *) (intptr_t) 2); } } printf("Tests passed.\n"); } if (log_audio) { if (afCloseFile(outhandle) != 0) { fprintf(stderr, " Cannot close wave file '%s'\n", OUTPUT_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; }
fax_state_t *fax_init(fax_state_t *s, int calling_party) { if (s == NULL) { if ((s = (fax_state_t *) malloc(sizeof(*s))) == NULL) return NULL; } memset(s, 0, sizeof(*s)); span_log_init(&s->logging, SPAN_LOG_NONE, NULL); span_log_set_protocol(&s->logging, "FAX"); t30_init(&(s->t30_state), calling_party, fax_set_rx_type, (void *) s, fax_set_tx_type, (void *) s, fax_send_hdlc, (void *) s); t30_set_supported_modems(&(s->t30_state), T30_SUPPORT_V27TER | T30_SUPPORT_V29); hdlc_rx_init(&(s->hdlcrx), FALSE, FALSE, 5, t30_hdlc_accept, &(s->t30_state)); fsk_rx_init(&(s->v21rx), &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &(s->hdlcrx)); fsk_rx_signal_cutoff(&(s->v21rx), -45.5); hdlc_tx_init(&(s->hdlctx), FALSE, 2, FALSE, hdlc_underflow_handler, &(s->t30_state)); s->first_tx_hdlc_frame = TRUE; fsk_tx_init(&(s->v21tx), &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &(s->hdlctx)); v17_rx_init(&(s->v17rx), 14400, t30_non_ecm_put_bit, &(s->t30_state)); v17_tx_init(&(s->v17tx), 14400, s->use_tep, t30_non_ecm_get_bit, &(s->t30_state)); v29_rx_init(&(s->v29rx), 9600, t30_non_ecm_put_bit, &(s->t30_state)); v29_rx_signal_cutoff(&(s->v29rx), -45.5); v29_tx_init(&(s->v29tx), 9600, s->use_tep, t30_non_ecm_get_bit, &(s->t30_state)); v27ter_rx_init(&(s->v27ter_rx), 4800, t30_non_ecm_put_bit, &(s->t30_state)); v27ter_tx_init(&(s->v27ter_tx), 4800, s->use_tep, t30_non_ecm_get_bit, &(s->t30_state)); silence_gen_init(&(s->silence_gen), 0); dc_restore_init(&(s->dc_restore)); t30_restart(&(s->t30_state)); #if defined(LOG_FAX_AUDIO) { char buf[100 + 1]; struct tm *tm; time_t now; time(&now); tm = localtime(&now); sprintf(buf, "/tmp/fax-rx-audio-%x-%02d%02d%02d%02d%02d%02d", s, tm->tm_year%100, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); s->fax_audio_rx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666); sprintf(buf, "/tmp/fax-tx-audio-%x-%02d%02d%02d%02d%02d%02d", s, tm->tm_year%100, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); s->fax_audio_tx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666); } #endif return s; }
static void fax_set_rx_type(void *user_data, int type, int short_train, int use_hdlc) { fax_state_t *s; put_bit_func_t put_bit_func; void *put_bit_user_data; s = (fax_state_t *) user_data; span_log(&s->logging, SPAN_LOG_FLOW, "Set rx type %d\n", type); if (s->current_rx_type == type) return; s->current_rx_type = type; if (use_hdlc) { put_bit_func = (put_bit_func_t) hdlc_rx_put_bit; put_bit_user_data = (void *) &(s->hdlcrx); hdlc_rx_init(&(s->hdlcrx), FALSE, FALSE, 5, t30_hdlc_accept, &(s->t30_state)); } else { put_bit_func = t30_non_ecm_put_bit; put_bit_user_data = (void *) &(s->t30_state); } switch (type) { case T30_MODEM_V21: if (s->flush_handler) s->flush_handler(s, s->flush_user_data, 3); fsk_rx_init(&(s->v21rx), &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, put_bit_user_data); fsk_rx_signal_cutoff(&(s->v21rx), -45.5); s->rx_handler = (span_rx_handler_t *) &fsk_rx; s->rx_user_data = &(s->v21rx); break; case T30_MODEM_V27TER_2400: v27ter_rx_restart(&(s->v27ter_rx), 2400, FALSE); v27ter_rx_set_put_bit(&(s->v27ter_rx), put_bit_func, put_bit_user_data); s->rx_handler = (span_rx_handler_t *) &early_v27ter_rx; s->rx_user_data = s; break; case T30_MODEM_V27TER_4800: v27ter_rx_restart(&(s->v27ter_rx), 4800, FALSE); v27ter_rx_set_put_bit(&(s->v27ter_rx), put_bit_func, put_bit_user_data); s->rx_handler = (span_rx_handler_t *) &early_v27ter_rx; s->rx_user_data = s; break; case T30_MODEM_V29_7200: v29_rx_restart(&(s->v29rx), 7200, FALSE); v29_rx_set_put_bit(&(s->v29rx), put_bit_func, put_bit_user_data); s->rx_handler = (span_rx_handler_t *) &early_v29_rx; s->rx_user_data = s; break; case T30_MODEM_V29_9600: v29_rx_restart(&(s->v29rx), 9600, FALSE); v29_rx_set_put_bit(&(s->v29rx), put_bit_func, put_bit_user_data); s->rx_handler = (span_rx_handler_t *) &early_v29_rx; s->rx_user_data = s; break; case T30_MODEM_V17_7200: v17_rx_restart(&(s->v17rx), 7200, short_train); v17_rx_set_put_bit(&(s->v17rx), put_bit_func, put_bit_user_data); s->rx_handler = (span_rx_handler_t *) &early_v17_rx; s->rx_user_data = s; break; case T30_MODEM_V17_9600: v17_rx_restart(&(s->v17rx), 9600, short_train); v17_rx_set_put_bit(&(s->v17rx), put_bit_func, put_bit_user_data); s->rx_handler = (span_rx_handler_t *) &early_v17_rx; s->rx_user_data = s; break; case T30_MODEM_V17_12000: v17_rx_restart(&(s->v17rx), 12000, short_train); v17_rx_set_put_bit(&(s->v17rx), put_bit_func, put_bit_user_data); s->rx_handler = (span_rx_handler_t *) &early_v17_rx; s->rx_user_data = s; break; case T30_MODEM_V17_14400: v17_rx_restart(&(s->v17rx), 14400, short_train); v17_rx_set_put_bit(&(s->v17rx), put_bit_func, put_bit_user_data); s->rx_handler = (span_rx_handler_t *) &early_v17_rx; s->rx_user_data = s; break; case T30_MODEM_DONE: span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n"); default: s->rx_handler = (span_rx_handler_t *) &dummy_rx; s->rx_user_data = s; break; } }