]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/sdr.c
a198d21e7cc613cd55fcf9702a3e72223315b8f5
[dttsp.git] / jDttSP / sdr.c
1 /* sdr.c
2
3 This file is part of a program that implements a Software-Defined Radio.
4
5 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 The authors can be reached by email at
22
23 ab2kt@arrl.net
24 or
25 rwmcgwier@comcast.net
26
27 or by paper mail at
28
29 The DTTS Microwave Society
30 6 Kathleen Place
31 Bridgewater, NJ 08807
32 */
33
34 #include <common.h>
35
36 //========================================================================
37 /* initialization and termination */
38
39 /* global and general info,
40    not specifically attached to
41    tx, rx, or scheduling */
42
43 PRIVATE void
44 setup_all(void) {
45
46   uni.samplerate = loc.def.rate;
47   uni.buflen = loc.def.size;
48   uni.mode.sdr = loc.def.mode;
49   uni.mode.trx = RX;
50
51   if (uni.meter.flag) {
52     uni.meter.chan.path = loc.path.meter;
53     uni.meter.chan.size = loc.mult.ring * sizeof(REAL);
54     uni.meter.val = -200.0;
55     uni.meter.chan.c = openChan(uni.meter.chan.path, uni.meter.chan.size);
56   }
57
58   uni.wisdom.path = loc.path.wisdom;
59   uni.wisdom.bits = FFTW_OUT_OF_PLACE | FFTW_ESTIMATE;
60   {
61     FILE *f = fopen(uni.wisdom.path, "r");
62     if (f) {
63 #define WBUFLEN 2048
64 #define WSTRLEN 64      
65       char *line = alloca(WBUFLEN);
66       fgets(line, WBUFLEN, f);
67       if ((strlen(line) > WSTRLEN) &&
68           (fftw_import_wisdom_from_string(line) != FFTW_FAILURE))
69         uni.wisdom.bits = FFTW_OUT_OF_PLACE | FFTW_MEASURE | FFTW_USE_WISDOM;
70 #undef WSTRLEN
71 #undef WBUFLEN      
72       fclose(f);
73     }
74   }
75
76   uni.tick = 0;
77 }
78
79 /* purely rx */
80
81 PRIVATE void
82 setup_rx(void) {
83
84   /* conditioning */
85   rx.iqfix = newCorrectIQ(0.0, 1.0);
86   rx.filt.coef = newFIR_Bandpass_COMPLEX(-4800.0,
87                                          4800.0,
88                                          uni.samplerate,
89                                          uni.buflen + 1);
90   rx.filt.ovsv = newFiltOvSv(FIRcoef(rx.filt.coef),
91                              FIRsize(rx.filt.coef),
92                              uni.wisdom.bits);
93   normalize_vec_COMPLEX(rx.filt.ovsv->zfvec,
94                         rx.filt.ovsv->fftlen);
95
96   // hack for EQ
97   rx.filt.save = newvec_COMPLEX(rx.filt.ovsv->fftlen, "RX filter cache");
98   memcpy((char *) rx.filt.save,
99          (char *) rx.filt.ovsv->zfvec,
100          rx.filt.ovsv->fftlen * sizeof(COMPLEX));
101
102   /* buffers */
103   /* note we overload the internal filter buffers
104      we just created */
105   rx.buf.i = newCXB(FiltOvSv_fetchsize(rx.filt.ovsv),
106                     FiltOvSv_fetchpoint(rx.filt.ovsv),
107                     "init rx.buf.i");
108   rx.buf.o = newCXB(FiltOvSv_storesize(rx.filt.ovsv),
109                     FiltOvSv_storepoint(rx.filt.ovsv),
110                     "init rx.buf.o");
111   
112   /* conversion */
113   rx.osc.freq = -11025.0;
114   rx.osc.phase = 0.0;
115   rx.osc.gen = newOSC(uni.buflen,
116                       ComplexTone,
117                       rx.osc.freq,
118                       rx.osc.phase,
119                       uni.samplerate,
120                       "SDR RX Oscillator");
121
122   rx.agc.gen = newDigitalAgc(agcMED,    // Mode
123                              7,         // Hang
124                              7,         // Size
125                              48,        // Ramp
126                              3,         // Over
127                              3,         // Rcov
128                              CXBsize(rx.buf.o), // BufSize
129                              100.0,     // MaxGain
130                              0.707,     // Limit
131                              1.0,       // CurGain
132                              CXBbase(rx.buf.o));
133   rx.agc.flag = TRUE;
134
135   /* demods */
136   rx.am.gen = newAMD(48000.0,   // REAL samprate
137                      0.0,       // REAL f_initial
138                      -500.0,    // REAL f_lobound,
139                      500.0,     // REAL f_hibound,
140                      400.0,     // REAL f_bandwid,
141                      CXBsize(rx.buf.o), // int size,
142                      CXBbase(rx.buf.o), // COMPLEX *ivec,
143                      CXBbase(rx.buf.o), // COMPLEX *ovec,
144                      AMdet,     // AM Mode AMdet == rectifier,
145                                 //         SAMdet == synchronous detector
146                              "AM detector blew");       // char *tag
147   rx.fm.gen = newFMD(48000,     // REAL samprate
148                      0.0,       // REAL f_initial
149                      -6000.0,   // REAL f_lobound
150                      6000.0,    // REAL f_hibound
151                      10000.0,   // REAL f_bandwid
152                      CXBsize(rx.buf.o), // int size
153                      CXBbase(rx.buf.o), // COMPLEX *ivec
154                      CXBbase(rx.buf.o), // COMPLEX *ovec
155                      "New FM Demod structure"); // char *error message;
156
157   /* noise reduction */
158   rx.anf.gen = new_lmsr(rx.buf.o,       // CXB signal,
159                         64,             // int delay,
160                         0.01,           // REAL adaptation_rate,
161                         0.00001,        // REAL leakage,
162                         45,             // int adaptive_filter_size,
163                         LMADF_INTERFERENCE);
164   rx.anf.flag = FALSE;
165   rx.anr.gen = new_lmsr(rx.buf.o,       // CXB signal,
166                         64,             // int delay,
167                         0.01,           // REAL adaptation_rate,
168                         0.00001,        // REAL leakage,
169                         45,             // int adaptive_filter_size,
170                         LMADF_NOISE);
171   rx.anr.flag = FALSE;
172
173   rx.nb.thresh = 3.3;
174   rx.nb.gen = new_noiseblanker(rx.buf.i, rx.nb.thresh);
175   rx.nb.flag = FALSE;
176
177   rx.nb_sdrom.thresh = 2.5;
178   rx.nb_sdrom.gen = new_noiseblanker(rx.buf.i, rx.nb_sdrom.thresh);
179   rx.nb_sdrom.flag = FALSE;
180
181   rx.spot.gen = newSpotToneGen(-12.0,   // gain
182                                700.0,   // freq
183                                5.0,     // ms rise
184                                5.0,     // ms fall
185                                uni.buflen,
186                                uni.samplerate);
187
188   rx.scl.pre.val = 1.0;
189   rx.scl.pre.flag = FALSE;
190   rx.scl.post.val = 1.0;
191   rx.scl.post.flag = FALSE;
192
193   memset((char *) &rx.squelch, 0, sizeof(rx.squelch));
194   rx.squelch.thresh = -30.0;
195   rx.squelch.power = 0.0;
196   rx.squelch.flag = rx.squelch.running = rx.squelch.set = FALSE;
197   rx.squelch.num = (int) (0.0395 * uni.samplerate + 0.5);
198
199   rx.mode = uni.mode.sdr;
200   rx.bin.flag = FALSE;
201
202   rx.tick = 0;
203 }
204
205 /* purely tx */
206
207 PRIVATE void
208 setup_tx(void) {
209
210   /* conditioning */
211   tx.iqfix = newCorrectIQ(0.0, 1.0);
212   tx.filt.coef = newFIR_Bandpass_COMPLEX(300.0,
213                                          3000.0,
214                                          uni.samplerate,
215                                          uni.buflen + 1);
216   tx.filt.ovsv = newFiltOvSv(FIRcoef(tx.filt.coef),
217                              FIRsize(tx.filt.coef),
218                              uni.wisdom.bits);
219   normalize_vec_COMPLEX(tx.filt.ovsv->zfvec,
220                         tx.filt.ovsv->fftlen);
221
222   // hack for EQ
223   tx.filt.save = newvec_COMPLEX(tx.filt.ovsv->fftlen, "TX filter cache");
224   memcpy((char *) tx.filt.save,
225          (char *) tx.filt.ovsv->zfvec,
226          tx.filt.ovsv->fftlen * sizeof(COMPLEX));
227
228   /* buffers */
229   tx.buf.i = newCXB(FiltOvSv_fetchsize(tx.filt.ovsv),
230                     FiltOvSv_fetchpoint(tx.filt.ovsv),
231                     "init tx.buf.i");
232   tx.buf.o = newCXB(FiltOvSv_storesize(tx.filt.ovsv),
233                     FiltOvSv_storepoint(tx.filt.ovsv),
234                     "init tx.buf.o");
235   
236   /* conversion */
237   tx.osc.freq = 0.0;
238   tx.osc.phase = 0.0;
239   tx.osc.gen = newOSC(uni.buflen,
240                       ComplexTone,
241                       tx.osc.freq,
242                       tx.osc.phase,
243                       uni.samplerate,
244                       "SDR TX Oscillator");
245
246   tx.agc.gen = newDigitalAgc(agcFAST,   // Mode
247                              3,         // Hang
248                              3,         // Size
249                              3,         // Over
250                              3,         // Rcov
251                              48,        // Ramp
252                              CXBsize(tx.buf.o), // BufSize
253                              1.0,       // MaxGain
254                              0.900,     // Limit
255                              1.0,       // CurGain
256                              CXBbase(tx.buf.o));
257   tx.agc.flag = TRUE;
258
259   tx.spr.gen = newSpeechProc(0.4, 10.0, CXBbase(tx.buf.i), CXBsize(tx.buf.i));
260   tx.spr.flag = FALSE;
261
262   tx.scl.dc = cxzero;
263   tx.scl.pre.val = 1.0;
264   tx.scl.pre.flag = FALSE;
265   tx.scl.post.val = 1.0;
266   tx.scl.post.flag = FALSE;
267
268   tx.mode = uni.mode.sdr;
269
270   tx.tick = 0;
271   /* not much else to do for TX */
272 }
273
274 /* how the outside world sees it */
275
276 void
277 setup_workspace(void) {
278   setup_all(), setup_rx(), setup_tx();
279 }
280
281 void
282 destroy_workspace(void) {
283
284   /* TX */
285   delSpeechProc(tx.spr.gen);
286   delDigitalAgc(tx.agc.gen);
287   delOSC(tx.osc.gen);
288   delvec_COMPLEX(tx.filt.save);
289   delFiltOvSv(tx.filt.ovsv);
290   delFIR_Bandpass_COMPLEX(tx.filt.coef);
291   delCorrectIQ(tx.iqfix);
292   delCXB(tx.buf.o);
293   delCXB(tx.buf.i);
294
295   /* RX */
296   delSpotToneGen(rx.spot.gen);
297   delDigitalAgc(rx.agc.gen);
298   del_nb(rx.nb_sdrom.gen);
299   del_nb(rx.nb.gen);
300   del_lmsr(rx.anf.gen);
301   del_lmsr(rx.anr.gen);
302   delAMD(rx.am.gen);
303   delFMD(rx.fm.gen);
304   delOSC(rx.osc.gen);
305   delvec_COMPLEX(rx.filt.save);
306   delFiltOvSv(rx.filt.ovsv);
307   delFIR_Bandpass_COMPLEX(rx.filt.coef);
308   delCorrectIQ(rx.iqfix);
309   delCXB(rx.buf.o);
310   delCXB(rx.buf.i);
311
312   /* all */
313   if (uni.meter.flag)
314     closeChan(uni.meter.chan.c);
315 }
316
317 //////////////////////////////////////////////////////////////////////////
318 // execution
319 //////////////////////////////////////////////////////////////////////////
320
321 //========================================================================
322 // util
323
324 PRIVATE REAL
325 CXBnorm(CXB buff) {
326   int i;
327   double sum = 0.0;
328   for (i = 0; i < CXBhave(buff); i++)
329     sum += Csqrmag(CXBdata(buff, i));
330   return sqrt(sum);
331 }
332
333 //========================================================================
334 /* all */
335
336 /* tap off S-meter from some buf */
337
338 PRIVATE void
339 do_meter(COMPLEX *vec, int len) {
340   int i;
341   
342   uni.meter.val = 0;
343
344   switch (uni.meter.type) {
345   case AVG_SIGNAL_STRENGTH:
346     for (i = 0; i < len; i++)
347       uni.meter.val += Csqrmag(vec[i]);
348     uni.meter.val =
349       uni.meter.avgval = 0.9 * uni.meter.avgval + log10(uni.meter.val + 1e-20);
350     break;
351   case SIGNAL_STRENGTH:
352     for (i = 0; i < len; i++)
353       uni.meter.val += Csqrmag(vec[i]);
354     uni.meter.avgval = uni.meter.val = 10.0 * log10(uni.meter.val + 1e-20);
355     break;
356   case ADC_REAL:
357     for(i = 0; i < len; i++)
358       uni.meter.val = max(fabs(vec[i].re), uni.meter.val);
359     uni.meter.val = 20.0 * log10(uni.meter.val + 1e-10);
360     break;
361   case ADC_IMAG:
362     for(i = 0; i < len; i++)
363       uni.meter.val = max(fabs(vec[i].im), uni.meter.val);
364     uni.meter.val = 20.0 * log10(uni.meter.val + 1e-10);
365     break;
366   default:
367     break;
368   }
369
370   putChan_nowait(uni.meter.chan.c,
371                  (char *) &uni.meter.val,
372                  sizeof(uni.meter.val));
373 }
374
375 //========================================================================
376 /* RX processing */ 
377
378 PRIVATE BOOLEAN
379 should_do_rx_squelch(void) {
380   if (rx.squelch.flag) {
381     int i, n = CXBhave(rx.buf.o);
382     rx.squelch.power = 0.0;
383     for (i = 0; i < n; i++)
384       rx.squelch.power += Csqrmag(CXBdata(rx.buf.o, i));
385     return rx.squelch.thresh > 10.0 * log10(rx.squelch.power);
386   } else
387     return rx.squelch.set = FALSE;
388 }
389
390 // apply squelch
391 // slew into silence first time
392
393 PRIVATE void
394 do_squelch(void) {
395   rx.squelch.set = TRUE;
396   if (!rx.squelch.running) {
397     int i, m = rx.squelch.num, n = CXBhave(rx.buf.o) - m;
398     for (i = 0; i < m; i++)
399       CXBdata(rx.buf.o, i) = Cscl(CXBdata(rx.buf.o, i), 1.0 - (REAL) i / m);
400     memset((void *) (CXBbase(rx.buf.o) + m), 0, n * sizeof(COMPLEX));
401     rx.squelch.running = TRUE;
402   } else
403     memset((void *) CXBbase(rx.buf.o), 0, CXBhave(rx.buf.o) * sizeof(COMPLEX));
404 }
405
406 // lift squelch
407 // slew out from silence to full scale
408
409 PRIVATE void
410 no_squelch(void) {
411   if (rx.squelch.running) {
412     int i, m = rx.squelch.num;
413     for (i = 0; i < m; i++)
414       CXBdata(rx.buf.o, i) = Cscl(CXBdata(rx.buf.o, i), (REAL) i / m);
415     rx.squelch.running = FALSE;
416   }
417 }
418
419 /* pre-condition for (nearly) all RX modes */
420
421 PRIVATE void
422 do_rx_pre(void) {
423   int i, n = min(CXBhave(rx.buf.i), uni.buflen);
424
425   //
426   // do shrinkage here
427   //
428
429   if (rx.scl.pre.flag)
430     for (i = 0; i < n; i++)
431       CXBdata(rx.buf.i, i) = Cscl(CXBdata(rx.buf.i, i),
432                                   rx.scl.pre.val); 
433
434   if (rx.nb.flag) noiseblanker(rx.nb.gen);
435   if (rx.nb_sdrom.flag) SDROMnoiseblanker(rx.nb_sdrom.gen);
436
437   correctIQ(rx.buf.i, rx.iqfix);
438
439   /* 2nd if conversion happens here */
440   if (rx.osc.gen->Frequency != 0.0) {
441     ComplexOSC(rx.osc.gen);
442     for (i = 0; i < n; i++)
443       CXBdata(rx.buf.i, i) = Cmul(CXBdata(rx.buf.i, i),
444                                   OSCCdata(rx.osc.gen, i));
445   } 
446
447   /* filtering, metering, & AGC */
448   if (rx.mode != SPEC) {
449     if (rx.tick == 0) reset_OvSv(rx.filt.ovsv);
450     filter_OvSv(rx.filt.ovsv);
451     CXBhave(rx.buf.o) = CXBhave(rx.buf.i);
452     if (uni.meter.flag) do_meter(CXBbase(rx.buf.o), uni.buflen);
453     if (rx.agc.flag) DigitalAgc(rx.agc.gen, rx.tick);
454   } else if (uni.meter.flag)
455     do_meter(CXBbase(rx.buf.o), uni.buflen);
456 }
457
458 PRIVATE void
459 do_rx_post(void) {
460   int i, n = CXBhave(rx.buf.o);
461
462   if (!rx.squelch.set)  {
463     no_squelch();
464     // spotting tone
465     if (rx.spot.flag) {
466       // remember whether it's turned itself off during this pass
467       rx.spot.flag = SpotTone(rx.spot.gen);
468       for (i = 0; i < n; i++)
469         CXBdata(rx.buf.o, i) = Cadd(CXBdata(rx.buf.o, i),
470                                     CXBdata(rx.spot.gen->buf, i));
471     }
472   }
473
474   //
475   // mix in sidetone here
476   //
477
478   if (rx.scl.post.flag)
479     for (i = 0; i < n; i++)
480       CXBdata(rx.buf.o, i) = Cscl(CXBdata(rx.buf.o, i),
481                                   rx.scl.post.val);
482
483   // not binaural? collapse
484   if (!rx.bin.flag)
485     for (i = 0; i < n; i++)
486       CXBimag(rx.buf.o, i) = CXBreal(rx.buf.o, i);
487 }
488
489 /* demod processing */
490
491 PRIVATE void
492 do_rx_SBCW(void) {
493   if (rx.anr.flag) lmsr_adapt(rx.anr.gen);
494   if (rx.anf.flag) lmsr_adapt(rx.anf.gen);
495 }
496
497 PRIVATE void
498 do_rx_AM(void) { AMDemod(rx.am.gen); }
499
500 PRIVATE void
501 do_rx_FM(void) { FMDemod(rx.fm.gen); }
502
503 PRIVATE void
504 do_rx_DRM(void) {}
505
506 PRIVATE void
507 do_rx_SPEC(void) {
508   memcpy(CXBbase(rx.buf.o),
509          CXBbase(rx.buf.i),
510          sizeof(COMPLEX) * CXBhave(rx.buf.i));
511   if (rx.agc.flag) DigitalAgc(rx.agc.gen, rx.tick);
512 }
513
514 PRIVATE void
515 do_rx_NIL(void) {
516   int i, n = min(CXBhave(rx.buf.i), uni.buflen);
517   for (i = 0; i < n; i++) CXBdata(rx.buf.o, i) = cxzero;
518 }
519
520 /* overall dispatch for RX processing */
521
522 PRIVATE void
523 do_rx(void) {
524   do_rx_pre();
525   switch (rx.mode) {
526   case USB:
527   case LSB:
528   case CWU:
529   case CWL:
530   case DSB:  do_rx_SBCW(); break;
531   case AM:
532   case SAM:  do_rx_AM(); break;
533   case FMN:  do_rx_FM();   break;
534   case DRM:  do_rx_DRM();  break;
535   case SPEC:
536     default: do_rx_SPEC(); break;
537   }
538   do_rx_post();
539 }  
540
541 //==============================================================
542 /* TX processing */
543
544 /* pre-condition for (nearly) all TX modes */
545
546 PRIVATE void
547 do_tx_pre(void) {
548
549   if (tx.scl.pre.flag) {
550     int i, n = CXBhave(tx.buf.i);
551     for (i = 0; i < n; i++)
552       CXBdata(tx.buf.i, i) = Cmplx(CXBreal(tx.buf.i, i) * tx.scl.pre.val, 0.0);
553   }
554
555   //
556   // mix in CW tone here?
557   //
558
559   correctIQ(tx.buf.i, tx.iqfix);
560
561   if (tx.spr.flag) SpeechProcessor(tx.spr.gen);
562
563   if (tx.tick == 0) reset_OvSv(tx.filt.ovsv);
564   filter_OvSv(tx.filt.ovsv);
565 }
566
567 PRIVATE void
568 do_tx_post(void) {
569   CXBhave(tx.buf.o) = CXBhave(tx.buf.i);
570
571   if (tx.agc.flag) DigitalAgc(tx.agc.gen, tx.tick);
572   if (tx.scl.post.flag) {
573     int i, n = CXBhave(tx.buf.o);
574     for (i = 0; i < n; i++)
575       CXBdata(tx.buf.o, i) = Cscl(CXBdata(tx.buf.o, i), tx.scl.post.val);
576   }
577   if (uni.meter.flag) do_meter(CXBbase(tx.buf.o), uni.buflen);
578   if (tx.osc.gen->Frequency != 0.0) {
579     int i;
580     ComplexOSC(tx.osc.gen);
581     for (i = 0; i < CXBhave(tx.buf.o); i++)
582       CXBdata(tx.buf.o, i) = Cmul(CXBdata(tx.buf.o, i), OSCCdata(tx.osc.gen, i));
583   }
584 }
585
586 /* modulator processing */
587
588 PRIVATE void
589 do_tx_SBCW(void) {
590   int i, n = min(CXBhave(tx.buf.o), uni.buflen); 
591
592   if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
593     for (i = 0; i < n; i++) {
594       tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.99),
595                        Cscl(CXBdata(tx.buf.o, i), -0.01));
596       CXBdata(tx.buf.o, i) = Cadd(CXBdata(tx.buf.o, i), tx.scl.dc);
597     }
598 }
599
600 PRIVATE void
601 do_tx_AM(void) {
602   int i, n = min(CXBhave(tx.buf.o), uni.buflen); 
603
604   if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
605     for (i = 0; i < n; i++) { 
606       tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.999),
607                        Cscl(CXBdata(tx.buf.o, i), -0.001));
608       CXBreal(tx.buf.o, i) =
609         0.49995 + 0.49995 * (CXBreal(tx.buf.o, i) - tx.scl.dc.re);
610       CXBimag(tx.buf.o, i) = 0.0;
611     }
612 }
613
614 PRIVATE void
615 do_tx_FM(void) {
616   int i, n = min(CXBhave(tx.buf.o), uni.buflen);
617   if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
618     for (i = 0; i < n; i++) {
619       tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.999),
620                        Cscl(CXBdata(tx.buf.o, i), 0.001));
621       tx.osc.phase += (CXBreal(tx.buf.o, i) - tx.scl.dc.re) * CvtMod2Freq;
622       if (tx.osc.phase >= TWOPI) tx.osc.phase -= TWOPI;
623       if (tx.osc.phase < 0.0) tx.osc.phase += TWOPI;
624       CXBdata(tx.buf.o, i) =
625         Cscl(Cmplx(cos(tx.osc.phase), sin(tx.osc.phase)), 0.99999);
626     }
627 }
628
629 PRIVATE void
630 do_tx_NIL(void) {
631   int i, n = min(CXBhave(tx.buf.i), uni.buflen);
632   for (i = 0; i < n; i++) CXBdata(tx.buf.o, i) = cxzero;
633 }
634
635 /* general TX processing dispatch */
636
637 PRIVATE void
638 do_tx(void) {
639   do_tx_pre();
640   switch (tx.mode) {
641   case USB:
642   case LSB:
643   case CWU:
644   case CWL:
645   case DSB:  do_tx_SBCW(); break;
646   case AM:
647   case SAM:  do_tx_AM();   break;
648   case FMN:  do_tx_FM();   break;
649   case DRM:
650   case SPEC:
651     default: do_tx_NIL(); break;
652   }
653   do_tx_post();
654 }
655
656 //========================================================================
657 /* overall buffer processing;
658    come here when there are buffers to work on */
659
660 void
661 process_samples(float *bufl, float *bufr, int n) {
662   int i;
663
664   switch (uni.mode.trx) {
665
666   case RX:
667     for (i = 0; i < n; i++)
668       CXBimag(rx.buf.i, i) = bufl[i], CXBreal(rx.buf.i, i) = bufr[i];
669     CXBhave(rx.buf.i) = n;
670
671     do_rx(), rx.tick++;
672
673     for (i = 0; i < n; i++)
674       bufl[i] = (float)CXBimag(rx.buf.o, i), bufr[i] = (float)CXBreal(rx.buf.o, i);
675     CXBhave(rx.buf.o) = n;
676     break;
677
678   case TX:
679     for (i = 0; i < n; i++)
680       CXBimag(tx.buf.i, i) = bufl[i], CXBreal(tx.buf.i, i) = bufr[i];
681     CXBhave(tx.buf.i) = n;
682
683     do_tx(), tx.tick++;
684
685     for (i = 0; i < n; i++)
686       bufl[i] = (float)CXBimag(tx.buf.o, i), bufr[i] = (float)CXBreal(tx.buf.o, i);
687     CXBhave(tx.buf.o) = n;
688     break;
689   }
690
691   uni.tick++;
692 }