GNU Radio's DSD Package
dsd.h
Go to the documentation of this file.
1 #ifndef DSD_H
2 #define DSD_H
3 /*
4  * Copyright (C) 2010 DSD Author
5  * GPG Key ID: 0x3F1D7FD0 (74EF 430D F7F2 0A48 FCE6 F630 FAA2 635D 3F1D 7FD0)
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <signal.h>
23 #include <string.h>
24 #define __USE_XOPEN
25 #include <time.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #ifdef SOLARIS
33 #include <sys/audioio.h>
34 #endif
35 #if defined(BSD) && !defined(__APPLE__)
36 #include <sys/soundcard.h>
37 #endif
38 #include <math.h>
39 #include <mbelib.h>
40 #include <sndfile.h>
41 
42 #include "p25p1_heuristics.h"
43 
44 
45 #define SAMPLE_RATE_IN 48000
46 #define SAMPLE_RATE_OUT 8000
47 
48 #ifdef USE_PORTAUDIO
49 #include "portaudio.h"
50 #define PA_FRAMES_PER_BUFFER 64
51 //Buffer needs to be large enough to prevent input buffer overruns while DSD is doing other struff (like outputting voice)
52 //else you get skipped samples which result in incomplete/erronous decodes and a mountain of error messages.
53 #define PA_LATENCY_IN 0.500
54 //Buffer needs to be large enough to prevent output buffer underruns while DSD is doing other stuff (like decoding input)
55 //else you get choppy audio and in 'extreme' cases errors.
56 //Buffer also needs to be as small as possible so we don't have a lot of audio delay.
57 #define PA_LATENCY_OUT 0.100
58 #endif
59 
60 
61 typedef struct
62 {
63  int onesymbol;
64  char mbe_in_file[1024];
65  FILE *mbe_in_f;
66  int errorbars;
67  int datascope;
69  int verbose;
70  int p25enc;
71  int p25lc;
72  int p25status;
73  int p25tg;
74  int scoperate;
75  char audio_in_dev[1024];
77  SNDFILE *audio_in_file;
79 #ifdef USE_PORTAUDIO
80  PaStream* audio_in_pa_stream;
81 #endif
82  int audio_in_type; // 0 for device, 1 for file, 2 for portaudio
83  char audio_out_dev[1024];
85  SNDFILE *audio_out_file;
87 #ifdef USE_PORTAUDIO
88  PaStream* audio_out_pa_stream;
89 #endif
90  int audio_out_type; // 0 for device, 1 for file, 2 for portaudio
91  int split;
93  char mbe_out_dir[1024];
94  char mbe_out_file[1024];
95  char mbe_out_path[1024];
96  FILE *mbe_out_f;
97  float audio_gain;
98  int audio_out;
99  char wav_out_file[1024];
100  SNDFILE *wav_out_f;
101  //int wav_out_fd;
103  char serial_dev[1024];
105  int resume;
113  int mod_c4fm;
114  int mod_qpsk;
115  int mod_gfsk;
120  int ssize;
121  int msize;
123  int delay;
126 } dsd_opts;
127 
128 typedef struct
129 {
130  int *dibit_buf;
132  int repeat;
137  float audio_out_temp_buf[160];
141  //int wav_out_bytes;
142  int center;
143  int jitter;
144  int synctype;
145  int min;
146  int max;
147  int lmid;
148  int umid;
149  int minref;
150  int maxref;
152  int sbuf[128];
153  int sidx;
154  int maxbuf[1024];
155  int minbuf[1024];
156  int midx;
157  char err_str[64];
158  char fsubtype[16];
159  char ftype[16];
161  int rf_mod;
162  int numflips;
165  int offset;
166  int carrier;
167  char tg[25][16];
168  int tgcount;
169  int lasttg;
170  int lastsrc;
171  int nac;
172  int errs;
173  int errs2;
175  int optind;
176  int numtdulc;
178  char slot0light[8];
179  char slot1light[8];
180  float aout_gain;
181  float aout_max_buf[200];
186  char algid[9];
187  char keyid[17];
192  int p25kid;
193 
194  unsigned int debug_audio_errors;
195  unsigned int debug_header_errors;
196  unsigned int debug_header_critical_errors;
197 
198  // Last dibit read
200 
201  // Heuristics state data for +P5 signals
203 
204  // Heuristics state data for -P5 signals
206 
207 #ifdef TRACE_DSD
208  char debug_prefix;
209  char debug_prefix_2;
210  unsigned int debug_sample_index;
211  unsigned int debug_sample_left_edge;
212  unsigned int debug_sample_right_edge;
213  FILE* debug_label_file;
214  FILE* debug_label_dibit_file;
215  FILE* debug_label_imbe_file;
216 #endif
217 
218  pthread_mutex_t input_mutex;
219  pthread_cond_t input_ready;
220  const float *input_samples;
223 
224  pthread_mutex_t output_mutex;
225  pthread_cond_t output_ready;
232 } dsd_state;
233 
234 /*
235  * Frame sync patterns
236  */
237 #define INV_P25P1_SYNC "333331331133111131311111"
238 #define P25P1_SYNC "111113113311333313133333"
239 
240 #define X2TDMA_BS_VOICE_SYNC "113131333331313331113311"
241 #define X2TDMA_BS_DATA_SYNC "331313111113131113331133"
242 #define X2TDMA_MS_DATA_SYNC "313113333111111133333313"
243 #define X2TDMA_MS_VOICE_SYNC "131331111333333311111131"
244 
245 #define DSTAR_HD "131313131333133113131111"
246 #define INV_DSTAR_HD "313131313111311331313333"
247 #define DSTAR_SYNC "313131313133131113313111"
248 #define INV_DSTAR_SYNC "131313131311313331131333"
249 
250 #define NXDN_MS_DATA_SYNC "313133113131111333"
251 #define INV_NXDN_MS_DATA_SYNC "131311331313333111"
252 #define NXDN_MS_VOICE_SYNC "313133113131113133"
253 #define INV_NXDN_MS_VOICE_SYNC "131311331313331311"
254 #define INV_NXDN_BS_DATA_SYNC "131311331313333131"
255 #define NXDN_BS_DATA_SYNC "313133113131111313"
256 #define INV_NXDN_BS_VOICE_SYNC "131311331313331331"
257 #define NXDN_BS_VOICE_SYNC "313133113131113113"
258 
259 #define DMR_BS_DATA_SYNC "313333111331131131331131"
260 #define DMR_BS_VOICE_SYNC "131111333113313313113313"
261 #define DMR_MS_DATA_SYNC "311131133313133331131113"
262 #define DMR_MS_VOICE_SYNC "133313311131311113313331"
263 
264 #define INV_PROVOICE_SYNC "31313111333133133311331133113311"
265 #define PROVOICE_SYNC "13131333111311311133113311331133"
266 #define INV_PROVOICE_EA_SYNC "13313133113113333311313133133311"
267 #define PROVOICE_EA_SYNC "31131311331331111133131311311133"
268 
269 /*
270  * function prototypes
271  */
272 void processDMRdata (dsd_opts * opts, dsd_state * state);
273 void processDMRvoice (dsd_opts * opts, dsd_state * state);
274 void processAudio (dsd_opts * opts, dsd_state * state);
275 void writeSynthesizedVoice (dsd_opts * opts, dsd_state * state);
276 void playSynthesizedVoice (dsd_opts * opts, dsd_state * state);
277 void openAudioOutDevice (dsd_opts * opts, int speed);
278 void openAudioInDevice (dsd_opts * opts);
279 
280 int getDibit (dsd_opts * opts, dsd_state * state);
281 int get_dibit_and_analog_signal (dsd_opts * opts, dsd_state * state, int * out_analog_signal);
282 
283 void skipDibit (dsd_opts * opts, dsd_state * state, int count);
284 void saveImbe4400Data (dsd_opts * opts, dsd_state * state, char *imbe_d);
285 void saveAmbe2450Data (dsd_opts * opts, dsd_state * state, char *ambe_d);
286 int readImbe4400Data (dsd_opts * opts, dsd_state * state, char *imbe_d);
287 int readAmbe2450Data (dsd_opts * opts, dsd_state * state, char *ambe_d);
288 void openMbeInFile (dsd_opts * opts, dsd_state * state);
289 void closeMbeOutFile (dsd_opts * opts, dsd_state * state);
290 void openMbeOutFile (dsd_opts * opts, dsd_state * state);
291 void openWavOutFile (dsd_opts * opts, dsd_state * state);
292 void closeWavOutFile (dsd_opts * opts, dsd_state * state);
293 void printFrameInfo (dsd_opts * opts, dsd_state * state);
294 void processFrame (dsd_opts * opts, dsd_state * state);
295 void printFrameSync (dsd_opts * opts, dsd_state * state, char *frametype, int offset, char *modulation);
296 int getFrameSync (dsd_opts * opts, dsd_state * state);
297 int comp (const void *a, const void *b);
298 void noCarrier (dsd_opts * opts, dsd_state * state);
299 extern void initOpts (dsd_opts * opts);
300 extern void initState (dsd_state * state);
301 void usage ();
302 extern void liveScanner (dsd_opts * opts, dsd_state * state);
303 void cleanupAndExit (dsd_opts * opts, dsd_state * state);
304 int main (int argc, char **argv);
305 void playMbeFiles (dsd_opts * opts, dsd_state * state, int argc, char **argv);
306 void processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char ambe_fr[4][24], char imbe7100_fr[7][24]);
307 void openSerial (dsd_opts * opts, dsd_state * state);
308 void resumeScan (dsd_opts * opts, dsd_state * state);
309 int getSymbol (dsd_opts * opts, dsd_state * state, int have_sync);
310 void upsample (dsd_state * state, float invalue);
311 void processDSTAR (dsd_opts * opts, dsd_state * state);
312 void processNXDNVoice (dsd_opts * opts, dsd_state * state);
313 void processNXDNData (dsd_opts * opts, dsd_state * state);
314 void processP25lcw (dsd_opts * opts, dsd_state * state, char *lcformat, char *mfid, char *lcinfo);
315 void processHDU (dsd_opts * opts, dsd_state * state);
316 void processLDU1 (dsd_opts * opts, dsd_state * state);
317 void processLDU2 (dsd_opts * opts, dsd_state * state);
318 void processTDU (dsd_opts * opts, dsd_state * state);
319 void processTDULC (dsd_opts * opts, dsd_state * state);
320 void processProVoice (dsd_opts * opts, dsd_state * state);
321 void processX2TDMAdata (dsd_opts * opts, dsd_state * state);
322 void processX2TDMAvoice (dsd_opts * opts, dsd_state * state);
323 void processDSTAR_HD (dsd_opts * opts, dsd_state * state);
324 short dmr_filter(short sample);
325 short nxdn_filter(short sample);
326 
327 #endif // DSD_H
const float * input_samples
Definition: dsd.h:220
int audio_out_type
Definition: dsd.h:90
float * audio_out_temp_buf_p
Definition: dsd.h:138
int audio_in_type
Definition: dsd.h:82
void openAudioOutDevice(dsd_opts *opts, int speed)
int input_offset
Definition: dsd.h:222
int lastsample
Definition: dsd.h:151
void processDMRvoice(dsd_opts *opts, dsd_state *state)
P25Heuristics inv_p25_heuristics
Definition: dsd.h:205
void noCarrier(dsd_opts *opts, dsd_state *state)
int aout_max_buf_idx
Definition: dsd.h:183
short * audio_out_buf
Definition: dsd.h:133
int jitter
Definition: dsd.h:143
int last_dibit
Definition: dsd.h:199
void processX2TDMAdata(dsd_opts *opts, dsd_state *state)
int numtdulc
Definition: dsd.h:176
void openMbeOutFile(dsd_opts *opts, dsd_state *state)
void processDSTAR(dsd_opts *opts, dsd_state *state)
int comp(const void *a, const void *b)
void closeWavOutFile(dsd_opts *opts, dsd_state *state)
void openMbeInFile(dsd_opts *opts, dsd_state *state)
int center
Definition: dsd.h:142
void processMbeFrame(dsd_opts *opts, dsd_state *state, char imbe_fr[8][23], char ambe_fr[4][24], char imbe7100_fr[7][24])
int getDibit(dsd_opts *opts, dsd_state *state)
int midx
Definition: dsd.h:156
int frame_p25p1
Definition: dsd.h:108
int output_offset
Definition: dsd.h:227
int readAmbe2450Data(dsd_opts *opts, dsd_state *state, char *ambe_d)
void saveAmbe2450Data(dsd_opts *opts, dsd_state *state, char *ambe_d)
float * aout_max_buf_p
Definition: dsd.h:182
int serial_fd
Definition: dsd.h:104
int main(int argc, char **argv)
int mod_gfsk
Definition: dsd.h:115
int mod_c4fm
Definition: dsd.h:113
Definition: mbelib.h:23
int get_dibit_and_analog_signal(dsd_opts *opts, dsd_state *state, int *out_analog_signal)
int frame_dmr
Definition: dsd.h:111
void skipDibit(dsd_opts *opts, dsd_state *state, int count)
void processTDU(dsd_opts *opts, dsd_state *state)
void processLDU2(dsd_opts *opts, dsd_state *state)
void writeSynthesizedVoice(dsd_opts *opts, dsd_state *state)
int repeat
Definition: dsd.h:132
int synctype
Definition: dsd.h:144
FILE * mbe_out_f
Definition: dsd.h:96
Definition: p25p1_heuristics.h:16
float * output_samples
Definition: dsd.h:228
int lastp25type
Definition: dsd.h:164
pthread_mutex_t output_mutex
Definition: dsd.h:224
void processAudio(dsd_opts *opts, dsd_state *state)
void processDMRdata(dsd_opts *opts, dsd_state *state)
SNDFILE * wav_out_f
Definition: dsd.h:100
int delay
Definition: dsd.h:123
int readImbe4400Data(dsd_opts *opts, dsd_state *state, char *imbe_d)
int lasttg
Definition: dsd.h:169
Definition: dsd.h:128
int mbe_file_type
Definition: dsd.h:174
int tgcount
Definition: dsd.h:168
int mod_threshold
Definition: dsd.h:119
void processFrame(dsd_opts *opts, dsd_state *state)
int p25kid
Definition: dsd.h:192
int mod_qpsk
Definition: dsd.h:114
mbe_parms * prev_mp
Definition: dsd.h:190
FILE * mbe_in_f
Definition: dsd.h:65
int umid
Definition: dsd.h:148
void openAudioInDevice(dsd_opts *opts)
int resume
Definition: dsd.h:105
P25Heuristics p25_heuristics
Definition: dsd.h:202
int symbolCenter
Definition: dsd.h:185
mbe_parms * cur_mp
Definition: dsd.h:189
int optind
Definition: dsd.h:175
void playMbeFiles(dsd_opts *opts, dsd_state *state, int argc, char **argv)
int * dibit_buf_p
Definition: dsd.h:131
int nac
Definition: dsd.h:171
int output_finished
Definition: dsd.h:231
int frame_dstar
Definition: dsd.h:106
float aout_gain
Definition: dsd.h:180
void printFrameInfo(dsd_opts *opts, dsd_state *state)
int maxref
Definition: dsd.h:150
int unmute_encrypted_p25
Definition: dsd.h:125
mbe_parms * prev_mp_enhanced
Definition: dsd.h:191
int inverted_dmr
Definition: dsd.h:118
float audio_gain
Definition: dsd.h:97
void processLDU1(dsd_opts *opts, dsd_state *state)
int firstframe
Definition: dsd.h:177
int frame_provoice
Definition: dsd.h:112
int max
Definition: dsd.h:146
int carrier
Definition: dsd.h:166
int audio_out_idx2
Definition: dsd.h:140
pthread_cond_t input_ready
Definition: dsd.h:219
int playoffset
Definition: dsd.h:92
int symboltiming
Definition: dsd.h:68
int getSymbol(dsd_opts *opts, dsd_state *state, int have_sync)
SNDFILE * audio_in_file
Definition: dsd.h:77
int lmid
Definition: dsd.h:147
void upsample(dsd_state *state, float invalue)
float * audio_out_float_buf_p
Definition: dsd.h:136
void closeMbeOutFile(dsd_opts *opts, dsd_state *state)
int audio_in_fd
Definition: dsd.h:76
int getFrameSync(dsd_opts *opts, dsd_state *state)
short * audio_out_buf_p
Definition: dsd.h:134
void processP25lcw(dsd_opts *opts, dsd_state *state, char *lcformat, char *mfid, char *lcinfo)
int samplesPerSymbol
Definition: dsd.h:184
void resumeScan(dsd_opts *opts, dsd_state *state)
int output_length
Definition: dsd.h:230
int numflips
Definition: dsd.h:162
void initState(dsd_state *state)
int offset
Definition: dsd.h:165
void openWavOutFile(dsd_opts *opts, dsd_state *state)
int frame_nxdn96
Definition: dsd.h:110
SF_INFO * audio_in_file_info
Definition: dsd.h:78
int audio_out_idx
Definition: dsd.h:139
void processX2TDMAvoice(dsd_opts *opts, dsd_state *state)
void liveScanner(dsd_opts *opts, dsd_state *state)
int verbose
Definition: dsd.h:69
void processProVoice(dsd_opts *opts, dsd_state *state)
int audio_out
Definition: dsd.h:98
SF_INFO * audio_out_file_info
Definition: dsd.h:86
int minref
Definition: dsd.h:149
int min
Definition: dsd.h:145
int errs
Definition: dsd.h:172
int p25enc
Definition: dsd.h:70
SNDFILE * audio_out_file
Definition: dsd.h:85
void playSynthesizedVoice(dsd_opts *opts, dsd_state *state)
void processHDU(dsd_opts *opts, dsd_state *state)
int p25lc
Definition: dsd.h:71
int playfiles
Definition: dsd.h:122
int datascope
Definition: dsd.h:67
int serial_baud
Definition: dsd.h:102
short nxdn_filter(short sample)
void initOpts(dsd_opts *opts)
short dmr_filter(short sample)
int * dibit_buf
Definition: dsd.h:130
void processNXDNVoice(dsd_opts *opts, dsd_state *state)
int ssize
Definition: dsd.h:120
int sidx
Definition: dsd.h:153
int lastsrc
Definition: dsd.h:170
int use_cosine_filter
Definition: dsd.h:124
int msize
Definition: dsd.h:121
int errorbars
Definition: dsd.h:66
void processNXDNData(dsd_opts *opts, dsd_state *state)
short * output_buffer
Definition: dsd.h:226
int split
Definition: dsd.h:91
void saveImbe4400Data(dsd_opts *opts, dsd_state *state, char *imbe_d)
int frame_x2tdma
Definition: dsd.h:107
int scoperate
Definition: dsd.h:74
pthread_mutex_t input_mutex
Definition: dsd.h:218
float * audio_out_float_buf
Definition: dsd.h:135
int symbolcnt
Definition: dsd.h:160
int onesymbol
Definition: dsd.h:63
int currentslot
Definition: dsd.h:188
int output_num_samples
Definition: dsd.h:229
void printFrameSync(dsd_opts *opts, dsd_state *state, char *frametype, int offset, char *modulation)
void processDSTAR_HD(dsd_opts *opts, dsd_state *state)
int rf_mod
Definition: dsd.h:161
void cleanupAndExit(dsd_opts *opts, dsd_state *state)
void usage()
void processTDULC(dsd_opts *opts, dsd_state *state)
int uvquality
Definition: dsd.h:116
pthread_cond_t output_ready
Definition: dsd.h:225
int errs2
Definition: dsd.h:173
int frame_nxdn48
Definition: dsd.h:109
Definition: dsd.h:61
int inverted_x2tdma
Definition: dsd.h:117
int lastsynctype
Definition: dsd.h:163
int p25status
Definition: dsd.h:72
void openSerial(dsd_opts *opts, dsd_state *state)
int input_length
Definition: dsd.h:221
int audio_out_fd
Definition: dsd.h:84
int p25tg
Definition: dsd.h:73