1 
2 /*------------------------------------------------------------------
3  *
4  * Module:      audio.h
5  *
6  * Purpose:   	Interface to audio device commonly called a "sound card"
7  *		for historical reasons.
8  *
9  *---------------------------------------------------------------*/
10 
11 
12 #ifndef AUDIO_H
13 #define AUDIO_H 1
14 
15 #ifdef USE_HAMLIB
16 #include <hamlib/rig.h>
17 #endif
18 
19 #include "direwolf.h"		/* for MAX_CHANS used throughout the application. */
20 #include "ax25_pad.h"		/* for AX25_MAX_ADDR_LEN */
21 #include "version.h"
22 
23 
24 /*
25  * PTT control.
26  */
27 
28 enum ptt_method_e {
29 	PTT_METHOD_NONE,	/* VOX or no transmit. */
30 	PTT_METHOD_SERIAL,	/* Serial port RTS or DTR. */
31 	PTT_METHOD_GPIO,	/* General purpose I/O, Linux only. */
32 	PTT_METHOD_LPT,	    	/* Parallel printer port, Linux only. */
33 	PTT_METHOD_HAMLIB, 	/* HAMLib, Linux only. */
34 	PTT_METHOD_CM108 };	/* GPIO pin of CM108/CM119/etc.  Linux only. */
35 
36 typedef enum ptt_method_e ptt_method_t;
37 
38 enum ptt_line_e { PTT_LINE_NONE = 0, PTT_LINE_RTS = 1, PTT_LINE_DTR = 2 };	  //  Important: 0 for neither.
39 typedef enum ptt_line_e ptt_line_t;
40 
41 enum audio_in_type_e {
42 	AUDIO_IN_TYPE_SOUNDCARD,
43 	AUDIO_IN_TYPE_SDR_UDP,
44 	AUDIO_IN_TYPE_STDIN };
45 
46 /* For option to try fixing frames with bad CRC. */
47 
48 typedef enum retry_e {
49 		RETRY_NONE=0,
50 		RETRY_INVERT_SINGLE=1,
51 		RETRY_INVERT_DOUBLE=2,
52 		RETRY_INVERT_TRIPLE=3,
53 		RETRY_INVERT_TWO_SEP=4,
54 		RETRY_MAX = 5}  retry_t;
55 
56 // Type of communication medium associated with the channel.
57 
58 enum medium_e { MEDIUM_NONE = 0,	// Channel is not valid for use.
59 		MEDIUM_RADIO,		// Internal modem for radio.
60 		MEDIUM_IGATE,		// Access IGate as ordinary channel.
61 		MEDIUM_NETTNC };	// Remote network TNC.  (possible future)
62 
63 
64 typedef enum sanity_e { SANITY_APRS, SANITY_AX25, SANITY_NONE } sanity_t;
65 
66 
67 struct audio_s {
68 
69 	/* Previously we could handle only a single audio device. */
70 	/* In version 1.2, we generalize this to handle multiple devices. */
71 	/* This means we can now have more than 2 radio channels. */
72 
73 	struct adev_param_s {
74 
75 	    /* Properites of the sound device. */
76 
77 	    int defined;		/* Was device defined? */
78 					/* First one defaults to yes. */
79 
80 	    char adevice_in[80];	/* Name of the audio input device (or file?). */
81 					/* TODO: Can be "-" to read from stdin. */
82 
83 	    char adevice_out[80];	/* Name of the audio output device (or file?). */
84 
85 	    int num_channels;		/* Should be 1 for mono or 2 for stereo. */
86 	    int samples_per_sec;	/* Audio sampling rate.  Typically 11025, 22050, or 44100. */
87 	    int bits_per_sample;	/* 8 (unsigned char) or 16 (signed short). */
88 
89 	} adev[MAX_ADEVS];
90 
91 
92 	/* Common to all channels. */
93 
94 	char tts_script[80];		/* Script for text to speech. */
95 
96 	int statistics_interval;	/* Number of seconds between the audio */
97 					/* statistics reports.  This is set by */
98 					/* the "-a" option.  0 to disable feature. */
99 
100 	int xmit_error_rate;		/* For testing purposes, we can generate frames with an invalid CRC */
101 					/* to simulate corruption while going over the air. */
102 					/* This is the probability, in per cent, of randomly corrupting it. */
103 					/* Normally this is 0.  25 would mean corrupt it 25% of the time. */
104 
105 	int recv_error_rate;		/* Similar but the % probablity of dropping a received frame. */
106 
107 	float recv_ber;			/* Receive Bit Error Rate (BER). */
108 					/* Probability of inverting a bit coming out of the modem. */
109 
110 	int fx25_xmit_enable;		/* Enable transmission of FX.25.  */
111 					/* See fx25_init.c for explanation of values. */
112 					/* Initially this applies to all channels. */
113 					/* This should probably be per channel. One step at a time. */
114 
115 	int fx25_auto_enable;		/* Turn on FX.25 for current connected mode session */
116 					/* under poor conditions. */
117 					/* Set to 0 to disable feature. */
118 					/* I put it here, rather than with the rest of the link layer */
119 					/* parameters because it is really a part of the HDLC layer */
120 					/* and is part of the KISS TNC functionality rather than our data link layer. */
121 
122 	char timestamp_format[40];	/* -T option */
123 					/* Precede received & transmitted frames with timestamp. */
124 					/* Command line option uses "strftime" format string. */
125 
126 
127 	/* Properties for each channel, common to receive and transmit. */
128 	/* Can be different for each radio channel. */
129 
130 	/* originally a "channel" was always connected to an internal modem. */
131 	/* In version 1.6, this is generalized so that a channel (as seen by client application) */
132 	/* can be connected to something else.  Initially, this will allow application */
133 	/* access to the IGate.  Later we might have network TNCs or other internal functions. */
134 
135 
136 	struct achan_param_s {
137 
138 	    // Originally there was a boolean, called "valid", to indicate that the
139 	    // channel is valid.  This has been replaced with the new "medium" which
140 	    // will allow channels to correspond to things other than internal modems.
141 
142 	    enum medium_e medium;	// MEDIUM_NONE for invalid.
143 					// MEDIUM_RADIO for internal modem.  (only possibility earlier)
144 					// MEDIUM_IGATE allows application access to IGate.
145 
146 
147 	    char mycall[AX25_MAX_ADDR_LEN];      /* Call associated with this radio channel. */
148                                 	/* Could all be the same or different. */
149 
150 
151 	    enum modem_t { MODEM_AFSK, MODEM_BASEBAND, MODEM_SCRAMBLE, MODEM_QPSK, MODEM_8PSK, MODEM_OFF, MODEM_16_QAM, MODEM_64_QAM, MODEM_AIS, MODEM_EAS } modem_type;
152 
153 					/* Usual AFSK. */
154 					/* Baseband signal. Not used yet. */
155 					/* Scrambled http://www.amsat.org/amsat/articles/g3ruh/109/fig03.gif */
156 					/* Might try MFJ-2400 / CCITT v.26 / Bell 201 someday. */
157 					/* No modem.  Might want this for DTMF only channel. */
158 
159 	    enum v26_e { V26_UNSPECIFIED=0, V26_A, V26_B } v26_alternative;
160 
161 					// Original implementaion used alternative A for 2400 bbps PSK.
162 					// Years later, we discover that MFJ-2400 used alternative B.
163 					// It's likely the others did too.  it also works a little better.
164 					// Default to MFJ compatible and print warning if user did not
165 					// pick one explicitly.
166 
167 #define V26_DEFAULT V26_B
168 
169 	    enum dtmf_decode_t { DTMF_DECODE_OFF, DTMF_DECODE_ON } dtmf_decode;
170 
171 					/* Originally the DTMF ("Touch Tone") decoder was always */
172 					/* enabled because it took a negligible amount of CPU. */
173 					/* There were complaints about the false positives when */
174 					/* hearing other modulation schemes on HF SSB so now it */
175 					/* is enabled only when needed. */
176 
177 					/* "On" will send special "t" packet to attached applications */
178 					/* and process as APRStt.  Someday we might want to separate */
179 					/* these but for now, we have a single off/on. */
180 
181 	    int decimate;		/* Reduce AFSK sample rate by this factor to */
182 					/* decrease computational requirements. */
183 
184 	    int upsample;		/* Upsample by this factor for G3RUH. */
185 
186             int mark_freq;		/* Two tones for AFSK modulation, in Hz. */
187 	    int space_freq;		/* Standard tones are 1200 and 2200 for 1200 baud. */
188 
189 	    int baud;			/* Data bits per second. */
190 					/* Standard rates are 1200 for VHF and 300 for HF. */
191 					/* This should really be called bits per second. */
192 
193 	/* Next 3 come from config file or command line. */
194 
195 	    char profiles[16];		/* zero or more of ABC etc, optional + */
196 
197 	    int num_freq;		/* Number of different frequency pairs for decoders. */
198 
199 	    int offset;			/* Spacing between filter frequencies. */
200 
201 	    int num_slicers;		/* Number of different threshold points to decide */
202 					/* between mark or space. */
203 
204 	/* This is derived from above by demod_init. */
205 
206 	    int num_subchan;		/* Total number of modems for each channel. */
207 
208 
209 	/* These are for dealing with imperfect frames. */
210 
211 	    enum retry_e fix_bits;	/* Level of effort to recover from */
212 					/* a bad FCS on the frame. */
213 					/* 0 = no effort */
214 					/* 1 = try fixing a single bit */
215 					/* 2... = more techniques... */
216 
217 	    enum sanity_e sanity_test;	/* Sanity test to apply when finding a good */
218 					/* CRC after making a change. */
219 					/* Must look like APRS, AX.25, or anything. */
220 
221 	    int passall;		/* Allow thru even with bad CRC. */
222 
223 
224 
225 	/* Additional properties for transmit. */
226 
227 	/* Originally we had control outputs only for PTT. */
228 	/* In version 1.2, we generalize this to allow others such as DCD. */
229 	/* In version 1.4 we add CON for connected to another station. */
230 	/* Index following structure by one of these: */
231 
232 
233 #define OCTYPE_PTT 0
234 #define OCTYPE_DCD 1
235 #define OCTYPE_CON 2
236 
237 #define NUM_OCTYPES 3		/* number of values above.   i.e. last value +1. */
238 
239 	    struct {
240 
241 	        ptt_method_t ptt_method; /* none, serial port, GPIO, LPT, HAMLIB, CM108. */
242 
243 	        char ptt_device[100];	/* Serial device name for PTT.  e.g. COM1 or /dev/ttyS0 */
244 					/* Also used for HAMLIB.  Could be host:port when model is 1. */
245 					/* For years, 20 characters was plenty then we start getting extreme names like this: */
246 					/* /dev/serial/by-id/usb-FTDI_Navigator__CAT___2nd_PTT__00000000-if00-port0 */
247 					/* /dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller_D-if00-port0 */
248 					/* Issue 104, changed to 100 bytes in version 1.5. */
249 
250 					/* This same field is also used for CM108 GPIO PTT which will */
251 					/* have a name like /dev/hidraw1. */
252 
253 	        ptt_line_t ptt_line;	/* Control line when using serial port. PTT_LINE_RTS, PTT_LINE_DTR. */
254 	        ptt_line_t ptt_line2;	/* Optional second one:  PTT_LINE_NONE when not used. */
255 
256 	        int out_gpio_num;	/* GPIO number.  Originally this was only for PTT. */
257 					/* It is now more general. */
258 					/* octrl array is indexed by PTT, DCD, or CONnected indicator. */
259 					/* For CM108, this should be in range of 1-8. */
260 
261 #define MAX_GPIO_NAME_LEN 20	// 12 would cover any case I've seen so this should be safe
262 
263 		char out_gpio_name[MAX_GPIO_NAME_LEN];
264 					/* orginally, gpio number NN was assumed to simply */
265 					/* have the name gpioNN but this turned out not to be */
266 					/* the case for CubieBoard where it was longer. */
267 					/* This is filled in by ptt_init so we don't have to */
268 					/* recalculate it each time we access it. */
269 
270 					/* This could probably be collapsed into ptt_device instead of being separate. */
271 
272 	        int ptt_lpt_bit;	/* Bit number for parallel printer port.  */
273 					/* Bit 0 = pin 2, ..., bit 7 = pin 9. */
274 
275 	        int ptt_invert;		/* Invert the output. */
276 	        int ptt_invert2;	/* Invert the secondary output. */
277 
278 #ifdef USE_HAMLIB
279 
280 	        int ptt_model;		/* HAMLIB model.  -1 for AUTO.  2 for rigctld.  Others are radio model. */
281 	        int ptt_rate;		/* Serial port speed when using hamlib CAT control for PTT. */
282 					/* If zero, hamlib will come up with a default for pariticular rig. */
283 #endif
284 
285 	    } octrl[NUM_OCTYPES];
286 
287 
288 	/* Each channel can also have associated input lines. */
289 	/* So far, we just have one for transmit inhibit. */
290 
291 #define ICTYPE_TXINH 0
292 
293 #define NUM_ICTYPES 1		/* number of values above. i.e. last value +1. */
294 
295 	    struct {
296 		ptt_method_t method;	/* none, serial port, GPIO, LPT. */
297 
298 		int in_gpio_num;	/* GPIO number */
299 
300 		char in_gpio_name[MAX_GPIO_NAME_LEN];
301 					/* orginally, gpio number NN was assumed to simply */
302 					/* have the name gpioNN but this turned out not to be */
303 					/* the case for CubieBoard where it was longer. */
304 					/* This is filled in by ptt_init so we don't have to */
305 					/* recalculate it each time we access it. */
306 
307 		int invert;		/* 1 = active low */
308 	    } ictrl[NUM_ICTYPES];
309 
310 	/* Transmit timing. */
311 
312 	    int dwait;			/* First wait extra time for receiver squelch. */
313 					/* Default 0 units of 10 mS each . */
314 
315 	    int slottime;		/* Slot time in 10 mS units for persistance algorithm. */
316 					/* Typical value is 10 meaning 100 milliseconds. */
317 
318 	    int persist;		/* Sets probability for transmitting after each */
319 					/* slot time delay.  Transmit if a random number */
320 					/* in range of 0 - 255 <= persist value.  */
321 					/* Otherwise wait another slot time and try again. */
322 					/* Default value is 63 for 25% probability. */
323 
324 	    int txdelay;		/* After turning on the transmitter, */
325 					/* send "flags" for txdelay * 10 mS. */
326 					/* Default value is 30 meaning 300 milliseconds. */
327 
328 	    int txtail;			/* Amount of time to keep transmitting after we */
329 					/* are done sending the data.  This is to avoid */
330 					/* dropping PTT too soon and chopping off the end */
331 					/* of the frame.  Again 10 mS units. */
332 					/* At this point, I'm thinking of 10 (= 100 mS) as the default */
333 					/* because we're not quite sure when the soundcard audio stops. */
334 
335 	    int fulldup;		/* Full Duplex. */
336 
337 	} achan[MAX_CHANS];
338 
339 #ifdef USE_HAMLIB
340     int rigs;               /* Total number of configured rigs */
341     RIG *rig[MAX_RIGS];     /* HAMLib rig instances */
342 #endif
343 
344 };
345 
346 
347 #if __WIN32__
348 #define DEFAULT_ADEVICE	""		/* Windows: Empty string = default audio device. */
349 #elif __APPLE__
350 #define DEFAULT_ADEVICE	""		/* Mac OSX: Empty string = default audio device. */
351 #elif USE_ALSA
352 #define DEFAULT_ADEVICE	"default"	/* Use default device for ALSA. */
353 #elif __OpenBSD__
354 #define DEFAULT_ADEVICE	"default"	/* Use default device for OpenBSD-portaudio. */
355 #else
356 #define DEFAULT_ADEVICE	"/dev/dsp"	/* First audio device for OSS.  (FreeBSD) */
357 #endif
358 
359 
360 
361 /*
362  * UDP audio receiving port.  Couldn't find any standard or usage precedent.
363  * Got the number from this example:   http://gqrx.dk/doc/streaming-audio-over-udp
364  * Any better suggestions?
365  */
366 
367 #define DEFAULT_UDP_AUDIO_PORT 7355
368 
369 
370 // Maximum size of the UDP buffer (for allowing IP routing, udp packets are often limited to 1472 bytes)
371 
372 #define SDR_UDP_BUF_MAXLEN 2000
373 
374 
375 
376 #define DEFAULT_NUM_CHANNELS 	1
377 #define DEFAULT_SAMPLES_PER_SEC	44100	/* Very early observations.  Might no longer be valid. */
378 					/* 22050 works a lot better than 11025. */
379 					/* 44100 works a little better than 22050. */
380 					/* If you have a reasonable machine, use the highest rate. */
381 #define MIN_SAMPLES_PER_SEC	8000
382 //#define MAX_SAMPLES_PER_SEC	48000	/* Originally 44100.  Later increased because */
383 					/* Software Defined Radio often uses 48000. */
384 
385 #define MAX_SAMPLES_PER_SEC	192000	/* The cheap USB-audio adapters (e.g. CM108) can handle 44100 and 48000. */
386 					/* The "soundcard" in my desktop PC can do 96kHz or even 192kHz. */
387 					/* We will probably need to increase the sample rate to go much above 9600 baud. */
388 
389 #define DEFAULT_BITS_PER_SAMPLE	16
390 
391 #define DEFAULT_FIX_BITS RETRY_INVERT_SINGLE
392 
393 /*
394  * Standard for AFSK on VHF FM.
395  * Reversing mark and space makes no difference because
396  * NRZI encoding only cares about change or lack of change
397  * between the two tones.
398  *
399  * HF SSB uses 300 baud and 200 Hz shift.
400  * 1600 & 1800 Hz is a popular tone pair, sometimes
401  * called the KAM tones.
402  */
403 
404 #define DEFAULT_MARK_FREQ	1200
405 #define DEFAULT_SPACE_FREQ	2200
406 #define DEFAULT_BAUD		1200
407 
408 /* Used for sanity checking in config file and command line options. */
409 /* 9600 baud is known to work.  */
410 /* TODO: Is 19200 possible with a soundcard at 44100 samples/sec or do we need a higher sample rate? */
411 
412 #define MIN_BAUD		100
413 //#define MAX_BAUD		10000
414 #define MAX_BAUD		40000		// Anyone want to try 38.4 k baud?
415 
416 /*
417  * Typical transmit timings for VHF.
418  */
419 
420 #define DEFAULT_DWAIT		0
421 #define DEFAULT_SLOTTIME	10
422 #define DEFAULT_PERSIST		63
423 #define DEFAULT_TXDELAY		30
424 #define DEFAULT_TXTAIL		10
425 #define DEFAULT_FULLDUP		0
426 
427 /*
428  * Note that we have two versions of these in audio.c and audio_win.c.
429  * Use one or the other depending on the platform.
430  */
431 
432 int audio_open (struct audio_s *pa);
433 
434 int audio_get (int a);		/* a = audio device, 0 for first */
435 
436 int audio_put (int a, int c);
437 
438 int audio_flush (int a);
439 
440 void audio_wait (int a);
441 
442 int audio_close (void);
443 
444 
445 #endif  /* ifdef AUDIO_H */
446 
447 
448 /* end audio.h */
449 
450