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