d51aa6dca327250601ddf230f47bd8bd106aeb7b
[asterisk/asterisk.git] / channels / xpmr / xpmr.h
1 /*
2  * xpmr.h - for Xelatec Private Mobile Radio Processes
3  * 
4  * All Rights Reserved. Copyright (C)2007, Xelatec, LLC
5  * 
6  * 20070808 1235 Steven Henke, W9SH, sph@xelatec.com
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *               
22  * This version may be optionally licenced under the GNU LGPL licence.
23  *
24  * A license has been granted to Digium (via disclaimer) for the use of
25  * this code.
26  *
27  */
28
29 /*! \file
30  *
31  * \brief Private Land Mobile Radio Channel Voice and Signaling Processor
32  *
33  * \author Steven Henke, W9SH <sph@xelatec.com> Xelatec, LLC
34  */
35
36 #ifndef XPMR_H
37 #define XPMR_H                  1
38
39 #ifdef  CHAN_USBRADIO
40 #define XPMR_DEBUG0             1
41 #define XPMR_TRACE              0
42 #else
43 #define XPMR_DEBUG0             1
44 #define XPMR_TRACE              1
45 #endif
46
47 #if(XPMR_TRACE == 1)
48 #define TRACEX(a) {printf a;}
49 #define TRACEXL(a) {printf("%s @ %u : ",__FILE__ ,__LINE__); printf a; }
50 #define TRACEXT(a) { struct timeval hack; gettimeofday(&hack,NULL); printf("%ld.",hack.tv_sec%100000); printf("%i : ",(int)hack.tv_usec); printf a; }
51 #else                                             
52 #define TRACEX(a)
53 #define TRACEXL(a)
54 #define TRACEXT(a)
55 #endif
56
57 #define i8      int8_t
58 #define u8      u_int8_t
59 #define i16             int16_t
60 #define u16     u_int16_t
61 #define i32             int32_t
62 #define u32     u_int32_t
63 #define i64     int64_t
64 #define u64     u_int64_t
65                         
66 #define M_Q24                   0x01000000              //
67 #define M_Q23                   0x00800000              //
68 #define M_Q22                   0x00400000              //                                      
69 #define M_Q21                   0x00200000              //              
70 #define M_Q20                   0x00100000              // 1048576
71 #define M_Q19                   0x00080000              // 524288                  
72 #define M_Q18                   0x00040000              // 262144
73 #define M_Q17           0x00020000              // 131072
74 #define M_Q16           0x00010000              // 65536
75 #define M_Q15           0x00008000              // 32768
76 #define M_Q14           0x00004000              // 16384
77 #define M_Q13           0x00002000              // 8182
78 #define M_Q12           0x00001000              // 4096
79 #define M_Q11           0x00000800              // 2048
80 #define M_Q10           0x00000400              // 1024
81 #define M_Q9            0x00000200              // 512
82 #define M_Q8            0x00000100              // 256
83 #define M_Q7            0x00000080              // 128
84 #define M_Q6            0x00000040              // 64
85 #define M_Q5            0x00000020              // 32
86 #define M_Q4            0x00000010              // 16
87 #define M_Q3            0x00000008              // 16
88 #define M_Q2            0x00000004              // 16
89 #define M_Q1            0x00000002              // 16
90 #define M_Q0            0x00000001              // 16
91
92 #define RADIANS_PER_CYCLE               (2*M_PI)
93
94 #define SAMPLE_RATE_INPUT       48000
95 #define SAMPLE_RATE_NETWORK     8000
96
97 #define SAMPLES_PER_BLOCK       160
98 #define MS_PER_FRAME            20
99
100 #define CTCSS_NUM_CODES                 38
101 #define CTCSS_SCOUNT_MUL                100
102 #define CTCSS_INTEGRATE                 3932       // 32767*.120 // 120/1000  // 0.120
103 #define CTCSS_INPUT_LIMIT               1000
104 #define CTCSS_DETECT_POINT              1989
105 #define CTCSS_HYSTERSIS             200
106
107 #define CTCSS_TURN_OFF_TIME             160                     // ms
108 #define CTCSS_TURN_OFF_SHIFT    240                     // degrees
109 #define TOC_NOTONE_TIME                 600                     // ms
110
111 #ifndef CHAN_USBRADIO  
112 enum {RX_AUDIO_NONE,RX_AUDIO_SPEAKER,RX_AUDIO_FLAT};
113 enum {TX_AUDIO_NONE,TX_AUDIO_FLAT,TX_AUDIO_FILTERED,TX_AUDIO_PROC};
114 enum {CD_IGNORE,CD_XPMR_NOISE,CD_XPMR_VOX,CD_HID,CD_HID_INVERT};
115 enum {SD_IGNORE,SD_HID,SD_HID_INVERT,SD_XPMR};                                   // no,external,externalinvert,software
116 enum {RX_KEY_CARRIER,RX_KEY_CARRIER_CODE};
117 enum {TX_OUT_OFF,TX_OUT_VOICE,TX_OUT_LSD,TX_OUT_COMPOSITE,TX_OUT_AUX};
118 enum {TOC_NONE,TOC_PHASE,TOC_NOTONE};
119 #endif
120
121 /*
122         one structure for each ctcss tone to decode 
123 */
124 typedef struct
125 {
126         i16 counter;                    // counter to next sample
127         i16 counterFactor;              // full divisor used to increment counter
128         i16 binFactor;
129         i16 fudgeFactor;
130         i16 peak;                               // peak amplitude now   maw sph now
131         i16 enabled;
132         i16 state;                              // dead, running, error                          
133         i16 zIndex;                             // z bucket index
134         i16 z[4];                               // maw sph today
135         i16 zi;
136         i16 dvu;
137         i16 dvd;
138         i16 zd;
139         i16 setpt;
140         i16 hyst;
141         i16 decode;
142         i16 diffpeak;
143         i16 debug;                              // value held from last pass
144         i16 *pDebug0;                   // pointer to debug output
145         i16 *pDebug1;                   // pointer to debug output
146         i16 *pDebug2;                   // pointer to debug output
147
148 } t_tdet;
149
150 typedef struct
151 {
152         i16 enabled;                                            // if 0 none, 0xFFFF all tones, or single tone
153         i16 *input;
154         i16 clamplitude;
155         i16 center;
156         i16 decode;                                                     // current ctcss decode index
157         i32 BlankingTimer;
158         u32 TurnOffTimer;
159         t_tdet tdet[CTCSS_NUM_CODES];   
160         i16 gain;
161         i16 limit;
162         i16 *pDebug0;
163         i16 *pDebug1;
164         i16 *pDebug2;
165         i16 testIndex;
166         i16 multiFreq;
167         i8 relax;
168
169 } t_dec_ctcss;
170
171 typedef struct
172 {
173         i16 enabled;                                            // if 0 none, 0xFFFF all tones, or single tone
174         i16 clamplitude;
175         i16 center;
176         i16 decode;                                             // current ctcss decode value
177         i32 BlankingTimer;
178         u32 TurnOffTimer;
179         i16 gain;
180         i16 limit;
181         i16 *pDebug0;
182         i16 *pDebug1;
183         i16 rxPolarity;
184 } t_dec_dcs;
185
186 /*
187         Low Speed Data decoding both polarities
188 */
189 typedef struct
190 {
191         i16 counter;                    // counter to next sample
192         i16 synced;
193         u32 syncCorr[2];
194         u32 data[2];
195         i16 state;                              // disabled, enabled,
196         i16 decode;
197         i16 debug;
198
199         i16 polarity;
200         u32 frameNum;
201
202         u16 area;
203         u16 chan;
204         u16 home;
205         u16 id;
206         u16 free;
207
208         u16 crc;
209         i16 rssi;
210
211 } t_decLsd;
212
213
214 /* general purpose pmr signal processing element */
215
216 struct t_pmr_chan;
217
218 typedef struct t_pmr_sps
219 {
220         i16  index;                     // unique to each instance
221
222         i16  enabled;           // enabled/disabled
223
224         struct t_pmr_chan *parentChan;
225         
226         i16  *source;           // source buffer
227         i16  *sourceB;          // source buffer B
228         i16  *sink;                     // sink buffer
229
230         i16  numChanOut;        // allows output direct to interleaved buffer
231         i16  selChanOut;
232
233         u32  ticks;
234
235         void *buff;                     // this structure's internal buffer
236
237         i16  *debugBuff0;       // debug buffer
238         i16  *debugBuff1;       // debug buffer
239         i16  *debugBuff2;       // debug buffer
240         i16  *debugBuff3;       // debug buffer
241
242         i16  nSamples;          // number of samples in the buffer
243
244         u32      buffSize;              // buffer maximum index
245         u32  buffInIndex;       // index to current input point
246         u32  buffOutIndex;      // index to current output point
247         u32  buffLead;          // lead of input over output through cb
248
249         i16  decimate;          // decimation or interpolation factor (could be put in coef's)
250         i16  interpolate;
251         i16      decimator;             // like the state this must be saved between calls (could be put in x's)
252
253         u32  sampleRate;    // in Hz for elements in this structure
254         u32  freq;                      // in 0.1 Hz
255
256         i16  measPeak;          // do measure Peak
257         i16  amax;                      // buffer amplitude maximum
258         i16  amin;                      // buffer amplitude minimum
259         i16  apeak;                     // buffer amplitude peak value (peak to peak)/2
260         i16  setpt;                     // amplitude set point for amplitude comparator
261         i16  hyst;                      // hysterysis for amplitude comparator
262         i16  compOut;           // amplitude comparator output
263
264         i32  discounteru;       // amplitude detector integrator discharge counter upper
265         i32  discounterl;       // amplitude detector integrator discharge counter lower
266         i32  discfactor;        // amplitude detector integrator discharge factor
267
268         i16  err;                       // error condition
269         i16  option;            // option / request zero
270         i16  state;         // stopped, start, stopped assumes zero'd
271
272         i16  cleared;           // output buffer cleared
273
274         i16  delay;
275         i16  decode;
276
277         i32  inputGain;         // apply to input data   ? in Q7.8 format
278         i32  inputGainB;        // apply to input data   ? in Q7.8 format
279         i32  outputGain;        // apply to output data  ? in Q7.8 format
280         i16  mixOut;
281         i16  monoOut;
282
283         i16  filterType;        // iir, fir, 1, 2, 3, 4 ...
284
285         i16 (*sigProc)(struct t_pmr_sps *sps);  // function to call
286
287         i32      calcAdjust;    // final adjustment
288         i16      nx;                    // number of x history elements
289         i16  ncoef;                     // number of coefficients
290         i16  size_x;            // size of each x history element
291         i16  size_coef;         // size of each coefficient
292         void  *x;                       // history registers
293         void  *x2;                      // history registers, 2nd bank 
294         void  *coef;            // coefficients
295         void  *coef2;           // coefficients 2
296
297         void  *nextSps;         // next Sps function
298
299 } t_pmr_sps;
300
301 /*
302         pmr channel
303 */
304 typedef struct  t_pmr_chan
305 {
306         i16 index;                              // which one
307         i16 enabled;                    // enabled/disabled
308         i16 status;                             // ok, error, busy, idle, initializing
309
310         i16 nSamplesRx;                 // max frame size
311         i16 nSamplesTx;
312
313         i32 inputSampleRate;    // in S/s  48000
314         i32 baseSampleRate;             // in S/s   8000 
315
316         i16 inputGain;
317         i16 inputOffset;
318
319         u32  frameCountRx;              // number processed
320         u32  frameCountTx;
321
322         i32  txHangTime;
323         i32  txTurnOff;
324
325         i16 rxDC;                           // average DC value of input
326         i16 rxSqSet;                    // carrier squelch threshold
327         i16 rxSqHyst;                   // carrier squelch hysterysis
328         i16 rxRssi;                             // current Rssi level
329         i16 rxQuality;                  // signal quality metric
330         i16 rxCarrierDetect;    // carrier detect
331         i16 rxCdType;
332         i16 rxExtCarrierDetect; 
333         i32 inputBlanking;      // Tx pulse eliminator
334
335         i16 rxDemod;            // see enum
336         i16 txMod;                      //
337
338         i16 rxNoiseSquelchEnable;
339         i16 rxHpfEnable;
340         i16 rxDeEmpEnable;
341         i16 rxCenterSlicerEnable;
342         i16 rxCtcssDecodeEnable;
343         i16 rxDcsDecodeEnable;
344         i16 rxDelayLineEnable;
345
346         i16 txHpfEnable;
347         i16 txLimiterEnable;
348         i16 txPreEmpEnable;
349         i16 txLpfEnable;
350
351         char radioDuplex;
352
353         struct {
354                 unsigned pmrNoiseSquelch:1;
355                 unsigned rxHpf:1;
356                 unsigned txHpf:1;
357                 unsigned txLpf:1;
358                 unsigned rxDeEmphasis:1;
359                 unsigned txPreEmphasis:1;
360                 unsigned startSpecialTone:1;
361                 unsigned stopSpecialTone:1;
362                 unsigned doingSpecialTone:1;
363                 unsigned extCarrierDetect:1;
364                 unsigned txCapture:1;
365                 unsigned rxCapture:1;
366         }b;
367
368         i16 dummy;
369
370         i32 txScramFreq;
371         i32 rxScramFreq;
372
373         i16 gainVoice;
374         i16 gainSubAudible;
375
376         i16 txMixA;                             // Off, Ctcss, Voice, Composite
377         i16 txMixB;                             // Off, Ctcss, Voice, Composite
378         
379         i16 rxMuting;
380
381         i16 rxCpuSaver;
382         i16 txCpuSaver;
383
384         i8      rxSqMode;                       // 0 open, 1 carrier, 2 coded
385
386         i8      cdMethod;
387
388         i16     rxSquelchPoint;
389
390         i16 rxCarrierPoint;
391         i16 rxCarrierHyst;
392
393         i16 rxCtcssMap[CTCSS_NUM_CODES];
394         
395         i16 txCtcssTocShift;
396         i16 txCtcssTocTime;
397         i8      txTocType;
398
399         float txCtcssFreq;
400         float rxCtcssFreq;
401         float rxInputGain;
402         
403         i16 rxCtcssIndex;
404
405         i16 txPttIn;                    // from external request
406         i16 txPttOut;                   // to radio hardware
407
408         i16 bandwidth;                  // wide/narrow
409         i16 txCompand;                  // type
410         i16 rxCompand;                  // 
411         
412         i16 txEqRight;                  // muted, flat, pre-emp limited filtered
413         i16 txEqLeft;
414
415         i16 txPotRight;                 // 
416         i16 txPotLeft;                  //
417
418         i16 rxPotRight;                 // 
419         i16 rxPotLeft;                  //
420
421         i16 function;
422
423         i16 txState;                    // off,settling,on,hangtime,turnoff
424
425         t_pmr_sps *spsMeasure;  // measurement block
426
427         t_pmr_sps *spsRx;                       // 1st signal processing struct
428         t_pmr_sps *spsRxLsd;
429         t_pmr_sps *spsRxDeEmp;
430         t_pmr_sps *spsRxHpf;
431         t_pmr_sps *spsRxVox;
432         t_pmr_sps *spsDelayLine;        // Last signal processing struct
433         t_pmr_sps *spsRxOut;            // Last signal processing struct
434
435         t_pmr_sps *spsTx;                       // 1st  signal processing struct
436
437         t_pmr_sps *spsTxLsdLpf;
438         t_pmr_sps *spsTxOutA;           // Last signal processing struct
439
440         t_pmr_sps *spsTxOutB;           // Last signal processing struct
441
442         t_pmr_sps *spsSigGen0;          // ctcss
443         t_pmr_sps *spsSigGen1;          // test and other tones
444
445         // tune tweaks
446
447         i32 rxVoxTimer;                         // Vox Hang Timer
448
449         i16     *prxSquelchAdjust;
450
451         // i16  *prxNoiseMeasure;       // for autotune
452         // i32  *prxNoiseAdjust;
453
454         i16     *prxVoiceMeasure;
455         i32     *prxVoiceAdjust;        
456         
457         i16     *prxCtcssMeasure;
458         i32     *prxCtcssAdjust;                 
459         
460         i16     *ptxVoiceAdjust;                // from calling application
461         i32     *ptxCtcssAdjust;                // from calling application
462
463         i32     *ptxLimiterAdjust;              // from calling application
464
465         i16 *pRxDemod;                          // buffers
466         i16 *pRxBase;                           // decimated lpf input
467         i16 *pRxNoise;   
468         i16 *pRxLsd;                            // subaudible only 
469         i16 *pRxHpf;                            // subaudible removed
470         i16 *pRxDeEmp;                  // EIA Audio
471         i16 *pRxSpeaker;                // EIA Audio
472         i16 *pRxDcTrack;                        // DC Restored LSD
473         i16 *pRxLsdLimit;               // LSD Limited
474         i16 *pRxCtcss;                          //
475         i16 *pRxSquelch;
476
477         i16 *pTxBase;                           // input data
478         i16 *pTxHpf;
479         i16 *pTxPreEmp;
480         i16 *pTxLimiter;
481         i16 *pTxLsd;
482         i16 *pTxLsdLpf;
483         i16 *pTxComposite;
484         i16 *pTxMod;                    // upsampled, low pass filtered
485         
486         i16 *pTxOut;                    // 
487         
488         i16 *pTxPttIn;
489         i16 *pTxPttOut;
490         i16 *pTxHang;
491         i16 *pTxCode;
492
493         i16     *pSigGen0;
494         i16     *pSigGen1;
495
496         i16 *pAlt0;
497         i16 *pAlt1;
498
499         i16 *pNull;
500
501         i16 *prxDebug;                  // consolidated debug buffer
502         i16 *ptxDebug;                  // consolidated debug buffer
503
504         i16 *prxDebug0;
505         i16 *prxDebug1;
506         i16 *prxDebug2;
507         i16 *prxDebug3;
508
509         i16 *ptxDebug0;
510         i16 *ptxDebug1;
511         i16 *ptxDebug2;
512         i16 *ptxDebug3;
513
514         t_dec_ctcss     *rxCtcss;
515                           
516         i16 clamplitudeDcs;
517         i16 centerDcs;
518         u32 dcsBlankingTimer;
519         i16 dcsDecode;                                                  // current dcs decode value
520
521         i16 clamplitudeLsd;
522         i16 centerLsd;
523         t_decLsd decLsd[2];                                             // for both polarities
524
525 } t_pmr_chan;
526
527 static i16                      TxTestTone(t_pmr_chan *pChan, i16 function);
528
529 t_pmr_chan      *createPmrChannel(t_pmr_chan *tChan, i16 numSamples);
530 t_pmr_sps       *createPmrSps(void);
531 i16                     destroyPmrChannel(t_pmr_chan *pChan);
532 i16                     destroyPmrSps(t_pmr_sps  *pSps);
533 i16             pmr_rx_frontend(t_pmr_sps *mySps);
534 i16             pmr_gp_fir(t_pmr_sps *mySps);
535 i16             pmr_gp_iir(t_pmr_sps *mySps);
536 i16             gp_inte_00(t_pmr_sps *mySps);
537 i16             gp_diff(t_pmr_sps *mySps);
538 i16             CenterSlicer(t_pmr_sps *mySps);
539 i16             ctcss_detect(t_pmr_chan *pmrChan);
540 i16             SoftLimiter(t_pmr_sps *mySps);
541 i16                     SigGen(t_pmr_sps *mySps);
542 i16             pmrMixer(t_pmr_sps *mySps);
543 i16             DelayLine(t_pmr_sps *mySps);
544 i16                     PmrRx(t_pmr_chan *PmrChan, i16 *input, i16 *output);
545 i16                     PmrTx(t_pmr_chan *PmrChan, i16 *input, i16 *output);
546 i16             CtcssFreqIndex(float freq);
547 i16             MeasureBlock(t_pmr_sps *mySps);
548 #endif /* ! XPMR_H */
549
550 /* end of file */
551
552
553