// // This file is part of Dire Wolf, an amateur radio packet TNC. // // Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 John Langner, WB2OSZ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // /*------------------------------------------------------------------- * * Name: atest.c * * Purpose: Test fixture for the AFSK demodulator. * * Inputs: Takes audio from a .WAV file insted of the audio device. * * Description: This can be used to test the AFSK demodulator under * controlled and reproducable conditions for tweaking. * * For example * * (1) Download WA8LMF's TNC Test CD image file from * http://wa8lmf.net/TNCtest/index.htm * * (2) Burn a physical CD. * * (3) "Rip" the desired tracks with Windows Media Player. * Select .WAV file format. * * "Track 2" is used for most tests because that is more * realistic for most people using the speaker output. * * * Without ONE_CHAN defined: * * Notice that the number of packets decoded, as reported by * this test program, will be twice the number expected because * we are decoding the left and right audio channels separately. * * * With ONE_CHAN defined: * * Only process one channel. * *--------------------------------------------------------------------*/ // #define X 1 #include "direwolf.h" #include #include #include #include #include #include #include #define ATEST_C 1 #include "audio.h" #include "demod.h" #include "multi_modem.h" #include "textcolor.h" #include "ax25_pad.h" #include "hdlc_rec2.h" #include "dlq.h" #include "ptt.h" #include "dtime_now.h" #if 0 /* Typical but not flexible enough. */ struct wav_header { /* .WAV file header. */ char riff[4]; /* "RIFF" */ int filesize; /* file length - 8 */ char wave[4]; /* "WAVE" */ char fmt[4]; /* "fmt " */ int fmtsize; /* 16. */ short wformattag; /* 1 for PCM. */ short nchannels; /* 1 for mono, 2 for stereo. */ int nsamplespersec; /* sampling freq, Hz. */ int navgbytespersec; /* = nblockalign*nsamplespersec. */ short nblockalign; /* = wbitspersample/8 * nchannels. */ short wbitspersample; /* 16 or 8. */ char data[4]; /* "data" */ int datasize; /* number of bytes following. */ } ; #endif /* 8 bit samples are unsigned bytes */ /* in range of 0 .. 255. */ /* 16 bit samples are signed short */ /* in range of -32768 .. +32767. */ static struct { char riff[4]; /* "RIFF" */ int filesize; /* file length - 8 */ char wave[4]; /* "WAVE" */ } header; static struct { char id[4]; /* "LIST" or "fmt " */ int datasize; } chunk; static struct { short wformattag; /* 1 for PCM. */ short nchannels; /* 1 for mono, 2 for stereo. */ int nsamplespersec; /* sampling freq, Hz. */ int navgbytespersec; /* = nblockalign*nsamplespersec. */ short nblockalign; /* = wbitspersample/8 * nchannels. */ short wbitspersample; /* 16 or 8. */ char extras[4]; } format; static struct { char data[4]; /* "data" */ int datasize; } wav_data; static FILE *fp; static int e_o_f; static int packets_decoded = 0; static int decimate = 0; /* Reduce that sampling rate if set. */ /* 1 = normal, 2 = half, etc. */ static struct audio_s my_audio_config; static int error_if_less_than = -1; /* Exit with error status if this minimum not reached. */ /* Can be used to check that performance has not decreased. */ static int error_if_greater_than = -1; /* Exit with error status if this maximum exceeded. */ /* Can be used to check that duplicate removal is not broken. */ //#define EXPERIMENT_G 1 //#define EXPERIMENT_H 1 #if defined(EXPERIMENT_G) || defined(EXPERIMENT_H) static int count[MAX_SUBCHANS]; #if EXPERIMENT_H extern float space_gain[MAX_SUBCHANS]; #endif #endif static void usage (void); static int decode_only = 0; /* Set to 0 or 1 to decode only one channel. 2 for both. */ static int sample_number = -1; /* Sample number from the file. */ /* Incremented only for channel 0. */ /* Use to print timestamp, relative to beginning */ /* of file, when frame was decoded. */ int main (int argc, char *argv[]) { int err; int c; int channel; double start_time; // Time when we started so we can measure elapsed time. double duration; // Length of the audio file in seconds. double elapsed; // Time it took us to process it. #if defined(EXPERIMENT_G) || defined(EXPERIMENT_H) int j; for (j=0; j MAX_BAUD) { text_color_set(DW_COLOR_ERROR); dw_printf ("Use a more reasonable bit rate in range of %d - %d.\n", MIN_BAUD, MAX_BAUD); exit (EXIT_FAILURE); } /* We have similar logic in direwolf.c, config.c, gen_packets.c, and atest.c, */ /* that need to be kept in sync. Maybe it could be a common function someday. */ if (my_audio_config.achan[0].baud == 100) { my_audio_config.achan[0].modem_type = MODEM_AFSK; my_audio_config.achan[0].mark_freq = 1615; my_audio_config.achan[0].space_freq = 1785; strlcpy (my_audio_config.achan[0].profiles, "D", sizeof(my_audio_config.achan[0].profiles)); } else if (my_audio_config.achan[0].baud < 600) { my_audio_config.achan[0].modem_type = MODEM_AFSK; my_audio_config.achan[0].mark_freq = 1600; my_audio_config.achan[0].space_freq = 1800; strlcpy (my_audio_config.achan[0].profiles, "D", sizeof(my_audio_config.achan[0].profiles)); } else if (my_audio_config.achan[0].baud < 1800) { my_audio_config.achan[0].modem_type = MODEM_AFSK; my_audio_config.achan[0].mark_freq = DEFAULT_MARK_FREQ; my_audio_config.achan[0].space_freq = DEFAULT_SPACE_FREQ; // Should default to E+ or something similar later. } else if (my_audio_config.achan[0].baud < 3600) { my_audio_config.achan[0].modem_type = MODEM_QPSK; my_audio_config.achan[0].mark_freq = 0; my_audio_config.achan[0].space_freq = 0; strlcpy (my_audio_config.achan[0].profiles, "", sizeof(my_audio_config.achan[0].profiles)); dw_printf ("Using V.26 QPSK rather than AFSK.\n"); } else if (my_audio_config.achan[0].baud < 7200) { my_audio_config.achan[0].modem_type = MODEM_8PSK; my_audio_config.achan[0].mark_freq = 0; my_audio_config.achan[0].space_freq = 0; strlcpy (my_audio_config.achan[0].profiles, "", sizeof(my_audio_config.achan[0].profiles)); dw_printf ("Using V.27 8PSK rather than AFSK.\n"); } else { my_audio_config.achan[0].modem_type = MODEM_SCRAMBLE; my_audio_config.achan[0].mark_freq = 0; my_audio_config.achan[0].space_freq = 0; strlcpy (my_audio_config.achan[0].profiles, " ", sizeof(my_audio_config.achan[0].profiles)); // avoid getting default later. dw_printf ("Using scrambled baseband signal rather than AFSK.\n"); } break; case 'P': /* -P for modem profile. */ dw_printf ("Demodulator profile set to \"%s\"\n", optarg); strlcpy (my_audio_config.achan[0].profiles, optarg, sizeof(my_audio_config.achan[0].profiles)); break; case 'D': /* -D reduce sampling rate for lower CPU usage. */ decimate = atoi(optarg); dw_printf ("Divide audio sample rate by %d\n", decimate); if (decimate < 1 || decimate > 8) { text_color_set(DW_COLOR_ERROR); dw_printf ("Unreasonable value for -D.\n"); exit (EXIT_FAILURE); } dw_printf ("Divide audio sample rate by %d\n", decimate); my_audio_config.achan[0].decimate = decimate; break; case 'F': /* -D set "fix bits" level. */ my_audio_config.achan[0].fix_bits = atoi(optarg); if (my_audio_config.achan[0].fix_bits < RETRY_NONE || my_audio_config.achan[0].fix_bits >= RETRY_MAX) { text_color_set(DW_COLOR_ERROR); dw_printf ("Invalid Fix Bits level.\n"); exit (EXIT_FAILURE); } break; case 'L': /* -L error if less than this number decoded. */ error_if_less_than = atoi(optarg); break; case 'G': /* -G error if greater than this number decoded. */ error_if_greater_than = atoi(optarg); break; case '0': /* channel 0, left from stereo */ decode_only = 0; break; case '1': /* channel 1, right from stereo */ decode_only = 1; break; case '2': /* decode both from stereo */ decode_only = 2; break; case '?': /* Unknown option message was already printed. */ usage (); break; default: /* Should not be here. */ text_color_set(DW_COLOR_ERROR); dw_printf("?? getopt returned character code 0%o ??\n", c); usage (); } } memcpy (&my_audio_config.achan[1], &my_audio_config.achan[0], sizeof(my_audio_config.achan[0])); if (optind >= argc) { text_color_set(DW_COLOR_ERROR); dw_printf ("Specify .WAV file name on command line.\n"); usage (); } fp = fopen(argv[optind], "rb"); if (fp == NULL) { text_color_set(DW_COLOR_ERROR); dw_printf ("Couldn't open file for read: %s\n", argv[optind]); //perror ("more info?"); exit (EXIT_FAILURE); } start_time = dtime_now(); /* * Read the file header. * Doesn't handle all possible cases but good enough for our purposes. */ err= fread (&header, (size_t)12, (size_t)1, fp); (void)(err); if (strncmp(header.riff, "RIFF", 4) != 0 || strncmp(header.wave, "WAVE", 4) != 0) { text_color_set(DW_COLOR_ERROR); dw_printf ("This is not a .WAV format file.\n"); exit (EXIT_FAILURE); } err = fread (&chunk, (size_t)8, (size_t)1, fp); if (strncmp(chunk.id, "LIST", 4) == 0) { err = fseek (fp, (long)chunk.datasize, SEEK_CUR); err = fread (&chunk, (size_t)8, (size_t)1, fp); } if (strncmp(chunk.id, "fmt ", 4) != 0) { text_color_set(DW_COLOR_ERROR); dw_printf ("WAV file error: Found \"%4.4s\" where \"fmt \" was expected.\n", chunk.id); exit(EXIT_FAILURE); } if (chunk.datasize != 16 && chunk.datasize != 18) { text_color_set(DW_COLOR_ERROR); dw_printf ("WAV file error: Need fmt chunk datasize of 16 or 18. Found %d.\n", chunk.datasize); exit(EXIT_FAILURE); } err = fread (&format, (size_t)chunk.datasize, (size_t)1, fp); err = fread (&wav_data, (size_t)8, (size_t)1, fp); if (strncmp(wav_data.data, "data", 4) != 0) { text_color_set(DW_COLOR_ERROR); dw_printf ("WAV file error: Found \"%4.4s\" where \"data\" was expected.\n", wav_data.data); exit(EXIT_FAILURE); } if (format.wformattag != 1) { text_color_set(DW_COLOR_ERROR); dw_printf ("Sorry, I only understand audio format 1 (PCM). This file has %d.\n", format.wformattag); exit (EXIT_FAILURE); } if (format.nchannels != 1 && format.nchannels != 2) { text_color_set(DW_COLOR_ERROR); dw_printf ("Sorry, I only understand 1 or 2 channels. This file has %d.\n", format.nchannels); exit (EXIT_FAILURE); } if (format.wbitspersample != 8 && format.wbitspersample != 16) { text_color_set(DW_COLOR_ERROR); dw_printf ("Sorry, I only understand 8 or 16 bits per sample. This file has %d.\n", format.wbitspersample); exit (EXIT_FAILURE); } my_audio_config.adev[0].samples_per_sec = format.nsamplespersec; my_audio_config.adev[0].bits_per_sample = format.wbitspersample; my_audio_config.adev[0].num_channels = format.nchannels; my_audio_config.achan[0].valid = 1; if (format.nchannels == 2) my_audio_config.achan[1].valid = 1; text_color_set(DW_COLOR_INFO); dw_printf ("%d samples per second. %d bits per sample. %d audio channels.\n", my_audio_config.adev[0].samples_per_sec, my_audio_config.adev[0].bits_per_sample, my_audio_config.adev[0].num_channels); duration = (double) wav_data.datasize / ((my_audio_config.adev[0].bits_per_sample / 8) * my_audio_config.adev[0].num_channels * my_audio_config.adev[0].samples_per_sec); dw_printf ("%d audio bytes in file. Duration = %.1f seconds.\n", (int)(wav_data.datasize), duration); dw_printf ("Fix Bits level = %d\n", my_audio_config.achan[0].fix_bits); /* * Initialize the AFSK demodulator and HDLC decoder. */ multi_modem_init (&my_audio_config); e_o_f = 0; while ( ! e_o_f) { int audio_sample; int c; for (c=0; c= 256 * 256) { e_o_f = 1; continue; } if (c == 0) sample_number++; if (decode_only == 0 && c != 0) continue; if (decode_only == 1 && c != 1) continue; multi_modem_process_sample(c,audio_sample); } /* When a complete frame is accumulated, */ /* process_rec_frame, below, is called. */ } text_color_set(DW_COLOR_INFO); dw_printf ("\n\n"); #if EXPERIMENT_G for (j=0; j error_if_greater_than) { text_color_set(DW_COLOR_ERROR); dw_printf ("\n * * * TEST FAILED: number decoded is greater than %d * * * \n", error_if_greater_than); exit (EXIT_FAILURE); } exit (EXIT_SUCCESS); } /* * Simulate sample from the audio device. */ int audio_get (int a) { int ch; if (wav_data.datasize <= 0) { e_o_f = 1; return (-1); } ch = getc(fp); wav_data.datasize--; if (ch < 0) { text_color_set(DW_COLOR_ERROR); dw_printf ("Unexpected end of file.\n"); e_o_f = 1; } return (ch); } /* * Rather than queuing up frames with bad FCS, * try to fix them immediately. */ void rdq_append (rrbb_t rrbb) { int chan, subchan, slice; alevel_t alevel; chan = rrbb_get_chan(rrbb); subchan = rrbb_get_subchan(rrbb); slice = rrbb_get_slice(rrbb); alevel = rrbb_get_audio_level(rrbb); hdlc_rec2_try_to_fix_later (rrbb, chan, subchan, slice, alevel); rrbb_delete (rrbb); } /* * This is called when we have a good frame. */ void dlq_rec_frame (int chan, int subchan, int slice, packet_t pp, alevel_t alevel, retry_t retries, char *spectrum) { char stemp[500]; unsigned char *pinfo; int info_len; int h; char heard[AX25_MAX_ADDR_LEN]; char alevel_text[AX25_ALEVEL_TO_TEXT_SIZE]; packets_decoded++; ax25_format_addrs (pp, stemp); info_len = ax25_get_info (pp, &pinfo); /* Print so we can see what is going on. */ //TODO: quiet option - suppress packet printing, only the count at the end. #if 1 /* Display audio input level. */ /* Who are we hearing? Original station or digipeater? */ if (ax25_get_num_addr(pp) == 0) { /* Not AX.25. No station to display below. */ h = -1; strlcpy (heard, "", sizeof(heard)); } else { h = ax25_get_heard(pp); ax25_get_addr_with_ssid(pp, h, heard); } text_color_set(DW_COLOR_DEBUG); dw_printf ("\n"); dw_printf("DECODED[%d] ", packets_decoded ); /* Insert time stamp relative to start of file. */ double sec = (double)sample_number / my_audio_config.adev[0].samples_per_sec; int min = (int)(sec / 60.); sec -= min * 60; dw_printf ("%d:%07.4f ", min, sec); if (h != AX25_SOURCE) { dw_printf ("Digipeater "); } ax25_alevel_to_text (alevel, alevel_text); if (my_audio_config.achan[chan].fix_bits == RETRY_NONE && my_audio_config.achan[chan].passall == 0) { dw_printf ("%s audio level = %s %s\n", heard, alevel_text, spectrum); } else { dw_printf ("%s audio level = %s [%s] %s\n", heard, alevel_text, retry_text[(int)retries], spectrum); } #endif //#if defined(EXPERIMENT_G) || defined(EXPERIMENT_H) // int j; // // for (j=0; j 1 && my_audio_config.achan[chan].num_slicers == 1) { dw_printf ("[%d.%d] ", chan, subchan); } else if (my_audio_config.achan[chan].num_subchan == 1 && my_audio_config.achan[chan].num_slicers > 1) { dw_printf ("[%d.%d] ", chan, slice); } else if (my_audio_config.achan[chan].num_subchan > 1 && my_audio_config.achan[chan].num_slicers > 1) { dw_printf ("[%d.%d.%d] ", chan, subchan, slice); } else { dw_printf ("[%d] ", chan); } dw_printf ("%s", stemp); /* stations followed by : */ ax25_safe_print ((char *)pinfo, info_len, 0); dw_printf ("\n"); #if 1 // temp experiment TODO: remove this. #include "decode_aprs.h" #include "log.h" if (ax25_is_aprs(pp)) { decode_aprs_t A; decode_aprs (&A, pp, 0); // Temp experiment to see how different systems set the RR bits in the source and destination. // log_rr_bits (&A, pp); } #endif ax25_delete (pp); } /* end fake dlq_append */ void ptt_set (int ot, int chan, int ptt_signal) { return; } int get_input (int it, int chan) { return -1; } static void usage (void) { text_color_set(DW_COLOR_ERROR); dw_printf ("\n"); dw_printf ("atest is a test application which decodes AX.25 frames from an audio\n"); dw_printf ("recording. This provides an easy way to test Dire Wolf decoding\n"); dw_printf ("performance much quicker than normal real-time. \n"); dw_printf ("\n"); dw_printf ("usage:\n"); dw_printf ("\n"); dw_printf (" atest [ options ] wav-file-in\n"); dw_printf ("\n"); dw_printf (" -B n Bits/second for data. Proper modem automatically selected for speed.\n"); dw_printf (" 300 baud uses 1600/1800 Hz AFSK.\n"); dw_printf (" 1200 (default) baud uses 1200/2200 Hz AFSK.\n"); dw_printf (" 9600 baud uses K9NG/G2RUH standard.\n"); dw_printf ("\n"); dw_printf (" -D n Divide audio sample rate by n.\n"); dw_printf ("\n"); dw_printf (" -F n Amount of effort to try fixing frames with an invalid CRC. \n"); dw_printf (" 0 (default) = consider only correct frames. \n"); dw_printf (" 1 = Try to fix only a single bit. \n"); dw_printf (" more = Try modifying more bits to get a good CRC.\n"); dw_printf ("\n"); dw_printf (" -P m Select the demodulator type such as A, B, C, D (default for 300 baud),\n"); dw_printf (" E (default for 1200 baud), F, A+, B+, C+, D+, E+, F+.\n"); dw_printf ("\n"); dw_printf (" -0 Use channel 0 (left) of stereo audio (default).\n"); dw_printf (" -1 use channel 1 (right) of stereo audio.\n"); dw_printf (" -2 decode both channels of stereo audio.\n"); dw_printf ("\n"); dw_printf (" wav-file-in is a WAV format audio file.\n"); dw_printf ("\n"); dw_printf ("Examples:\n"); dw_printf ("\n"); dw_printf (" gen_packets -o test1.wav\n"); dw_printf (" atest test1.wav\n"); dw_printf ("\n"); dw_printf (" gen_packets -B 300 -o test3.wav\n"); dw_printf (" atest -B 300 test3.wav\n"); dw_printf ("\n"); dw_printf (" gen_packets -B 9600 -o test9.wav\n"); dw_printf (" atest -B 9600 test9.wav\n"); dw_printf ("\n"); dw_printf (" This generates and decodes 3 test files with 1200, 300, and 9600\n"); dw_printf (" bits per second.\n"); dw_printf ("\n"); dw_printf (" atest 02_Track_2.wav\n"); dw_printf (" atest -P C+ 02_Track_2.wav\n"); dw_printf (" atest -F 1 02_Track_2.wav\n"); dw_printf (" atest -P C+ -F 1 02_Track_2.wav\n"); dw_printf ("\n"); dw_printf (" Try different combinations of options to find the best decoding\n"); dw_printf (" performance.\n"); exit (1); } /* end atest.c */