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
61typedef struct
62{
64 char mbe_in_file[1024];
65 FILE *mbe_in_f;
70 int p25enc;
71 int p25lc;
73 int p25tg;
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];
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;
99 char wav_out_file[1024];
100 SNDFILE *wav_out_f;
101 //int wav_out_fd;
103 char serial_dev[1024];
120 int ssize;
121 int msize;
123 int delay;
126} dsd_opts;
127
128typedef struct
129{
141 //int wav_out_bytes;
145 int min;
146 int max;
147 int lmid;
148 int umid;
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];
167 char tg[25][16];
171 int nac;
172 int errs;
173 int errs2;
178 char slot0light[8];
179 char slot1light[8];
181 float aout_max_buf[200];
186 char algid[9];
187 char keyid[17];
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 */
272void processDMRdata (dsd_opts * opts, dsd_state * state);
273void processDMRvoice (dsd_opts * opts, dsd_state * state);
274void processAudio (dsd_opts * opts, dsd_state * state);
277void openAudioOutDevice (dsd_opts * opts, int speed);
279
280int getDibit (dsd_opts * opts, dsd_state * state);
281int get_dibit_and_analog_signal (dsd_opts * opts, dsd_state * state, int * out_analog_signal);
282
283void skipDibit (dsd_opts * opts, dsd_state * state, int count);
284void saveImbe4400Data (dsd_opts * opts, dsd_state * state, char *imbe_d);
285void saveAmbe2450Data (dsd_opts * opts, dsd_state * state, char *ambe_d);
286int readImbe4400Data (dsd_opts * opts, dsd_state * state, char *imbe_d);
287int readAmbe2450Data (dsd_opts * opts, dsd_state * state, char *ambe_d);
288void openMbeInFile (dsd_opts * opts, dsd_state * state);
289void closeMbeOutFile (dsd_opts * opts, dsd_state * state);
290void openMbeOutFile (dsd_opts * opts, dsd_state * state);
291void openWavOutFile (dsd_opts * opts, dsd_state * state);
292void closeWavOutFile (dsd_opts * opts, dsd_state * state);
293void printFrameInfo (dsd_opts * opts, dsd_state * state);
294void processFrame (dsd_opts * opts, dsd_state * state);
295void printFrameSync (dsd_opts * opts, dsd_state * state, char *frametype, int offset, char *modulation);
296int getFrameSync (dsd_opts * opts, dsd_state * state);
297int comp (const void *a, const void *b);
298void noCarrier (dsd_opts * opts, dsd_state * state);
299extern void initOpts (dsd_opts * opts);
300extern void initState (dsd_state * state);
301void usage ();
302extern void liveScanner (dsd_opts * opts, dsd_state * state);
303void cleanupAndExit (dsd_opts * opts, dsd_state * state);
304int main (int argc, char **argv);
305void playMbeFiles (dsd_opts * opts, dsd_state * state, int argc, char **argv);
306void processMbeFrame (dsd_opts * opts, dsd_state * state, char imbe_fr[8][23], char ambe_fr[4][24], char imbe7100_fr[7][24]);
307void openSerial (dsd_opts * opts, dsd_state * state);
308void resumeScan (dsd_opts * opts, dsd_state * state);
309int getSymbol (dsd_opts * opts, dsd_state * state, int have_sync);
310void upsample (dsd_state * state, float invalue);
311void processDSTAR (dsd_opts * opts, dsd_state * state);
312void processNXDNVoice (dsd_opts * opts, dsd_state * state);
313void processNXDNData (dsd_opts * opts, dsd_state * state);
314void processP25lcw (dsd_opts * opts, dsd_state * state, char *lcformat, char *mfid, char *lcinfo);
315void processHDU (dsd_opts * opts, dsd_state * state);
316void processLDU1 (dsd_opts * opts, dsd_state * state);
317void processLDU2 (dsd_opts * opts, dsd_state * state);
318void processTDU (dsd_opts * opts, dsd_state * state);
319void processTDULC (dsd_opts * opts, dsd_state * state);
320void processProVoice (dsd_opts * opts, dsd_state * state);
321void processX2TDMAdata (dsd_opts * opts, dsd_state * state);
322void processX2TDMAvoice (dsd_opts * opts, dsd_state * state);
323void processDSTAR_HD (dsd_opts * opts, dsd_state * state);
324short dmr_filter(short sample);
325short nxdn_filter(short sample);
326
327#endif // DSD_H
void liveScanner(dsd_opts *opts, dsd_state *state)
void saveImbe4400Data(dsd_opts *opts, dsd_state *state, char *imbe_d)
void closeMbeOutFile(dsd_opts *opts, dsd_state *state)
void openMbeOutFile(dsd_opts *opts, dsd_state *state)
void processP25lcw(dsd_opts *opts, dsd_state *state, char *lcformat, char *mfid, char *lcinfo)
void openAudioInDevice(dsd_opts *opts)
void processHDU(dsd_opts *opts, dsd_state *state)
void processAudio(dsd_opts *opts, dsd_state *state)
void openMbeInFile(dsd_opts *opts, dsd_state *state)
void processFrame(dsd_opts *opts, dsd_state *state)
int readAmbe2450Data(dsd_opts *opts, dsd_state *state, char *ambe_d)
void playMbeFiles(dsd_opts *opts, dsd_state *state, int argc, char **argv)
void skipDibit(dsd_opts *opts, dsd_state *state, int count)
void printFrameInfo(dsd_opts *opts, dsd_state *state)
void usage()
int main(int argc, char **argv)
void saveAmbe2450Data(dsd_opts *opts, dsd_state *state, char *ambe_d)
int getDibit(dsd_opts *opts, dsd_state *state)
void processDMRdata(dsd_opts *opts, dsd_state *state)
void cleanupAndExit(dsd_opts *opts, dsd_state *state)
void upsample(dsd_state *state, float invalue)
void processTDU(dsd_opts *opts, dsd_state *state)
void processDMRvoice(dsd_opts *opts, dsd_state *state)
int getFrameSync(dsd_opts *opts, dsd_state *state)
int getSymbol(dsd_opts *opts, dsd_state *state, int have_sync)
void noCarrier(dsd_opts *opts, dsd_state *state)
void processDSTAR(dsd_opts *opts, dsd_state *state)
void initState(dsd_state *state)
void closeWavOutFile(dsd_opts *opts, dsd_state *state)
void processMbeFrame(dsd_opts *opts, dsd_state *state, char imbe_fr[8][23], char ambe_fr[4][24], char imbe7100_fr[7][24])
void openAudioOutDevice(dsd_opts *opts, int speed)
void processProVoice(dsd_opts *opts, dsd_state *state)
void openSerial(dsd_opts *opts, dsd_state *state)
int comp(const void *a, const void *b)
void writeSynthesizedVoice(dsd_opts *opts, dsd_state *state)
void processLDU2(dsd_opts *opts, dsd_state *state)
void processDSTAR_HD(dsd_opts *opts, dsd_state *state)
void playSynthesizedVoice(dsd_opts *opts, dsd_state *state)
void processNXDNData(dsd_opts *opts, dsd_state *state)
void processTDULC(dsd_opts *opts, dsd_state *state)
void processNXDNVoice(dsd_opts *opts, dsd_state *state)
int readImbe4400Data(dsd_opts *opts, dsd_state *state, char *imbe_d)
void openWavOutFile(dsd_opts *opts, dsd_state *state)
void processX2TDMAdata(dsd_opts *opts, dsd_state *state)
void processX2TDMAvoice(dsd_opts *opts, dsd_state *state)
int get_dibit_and_analog_signal(dsd_opts *opts, dsd_state *state, int *out_analog_signal)
short nxdn_filter(short sample)
short dmr_filter(short sample)
void resumeScan(dsd_opts *opts, dsd_state *state)
void processLDU1(dsd_opts *opts, dsd_state *state)
void initOpts(dsd_opts *opts)
void printFrameSync(dsd_opts *opts, dsd_state *state, char *frametype, int offset, char *modulation)
struct mbe_parameters mbe_parms
Definition mbelib.h:38
Definition p25p1_heuristics.h:17
Definition dsd.h:62
SNDFILE * wav_out_f
Definition dsd.h:100
int playfiles
Definition dsd.h:122
int unmute_encrypted_p25
Definition dsd.h:125
SF_INFO * audio_out_file_info
Definition dsd.h:86
int frame_x2tdma
Definition dsd.h:107
int mod_threshold
Definition dsd.h:119
int scoperate
Definition dsd.h:74
int inverted_dmr
Definition dsd.h:118
int symboltiming
Definition dsd.h:68
FILE * mbe_out_f
Definition dsd.h:96
int audio_out_type
Definition dsd.h:90
int split
Definition dsd.h:91
char mbe_out_file[1024]
Definition dsd.h:94
SF_INFO * audio_in_file_info
Definition dsd.h:78
int mod_qpsk
Definition dsd.h:114
int frame_dstar
Definition dsd.h:106
int playoffset
Definition dsd.h:92
int resume
Definition dsd.h:105
int serial_fd
Definition dsd.h:104
int onesymbol
Definition dsd.h:63
int p25status
Definition dsd.h:72
char wav_out_file[1024]
Definition dsd.h:99
int p25enc
Definition dsd.h:70
int errorbars
Definition dsd.h:66
int msize
Definition dsd.h:121
int audio_in_fd
Definition dsd.h:76
FILE * mbe_in_f
Definition dsd.h:65
int ssize
Definition dsd.h:120
int frame_nxdn48
Definition dsd.h:109
int frame_p25p1
Definition dsd.h:108
char audio_out_dev[1024]
Definition dsd.h:83
int uvquality
Definition dsd.h:116
int audio_out
Definition dsd.h:98
int use_cosine_filter
Definition dsd.h:124
int p25lc
Definition dsd.h:71
int audio_out_fd
Definition dsd.h:84
int frame_nxdn96
Definition dsd.h:110
char mbe_out_dir[1024]
Definition dsd.h:93
int mod_gfsk
Definition dsd.h:115
float audio_gain
Definition dsd.h:97
int datascope
Definition dsd.h:67
char serial_dev[1024]
Definition dsd.h:103
int mod_c4fm
Definition dsd.h:113
int delay
Definition dsd.h:123
int audio_in_type
Definition dsd.h:82
SNDFILE * audio_out_file
Definition dsd.h:85
int frame_provoice
Definition dsd.h:112
SNDFILE * audio_in_file
Definition dsd.h:77
int p25tg
Definition dsd.h:73
int verbose
Definition dsd.h:69
int serial_baud
Definition dsd.h:102
int frame_dmr
Definition dsd.h:111
char audio_in_dev[1024]
Definition dsd.h:75
int inverted_x2tdma
Definition dsd.h:117
char mbe_out_path[1024]
Definition dsd.h:95
char mbe_in_file[1024]
Definition dsd.h:64
Definition dsd.h:129
int audio_out_idx2
Definition dsd.h:140
float * audio_out_float_buf
Definition dsd.h:135
int minref
Definition dsd.h:149
int lastsynctype
Definition dsd.h:163
int lastsrc
Definition dsd.h:170
int rf_mod
Definition dsd.h:161
int currentslot
Definition dsd.h:188
int maxbuf[1024]
Definition dsd.h:154
int errs2
Definition dsd.h:173
int jitter
Definition dsd.h:143
int maxref
Definition dsd.h:150
int input_offset
Definition dsd.h:222
int input_length
Definition dsd.h:221
int aout_max_buf_idx
Definition dsd.h:183
char slot1light[8]
Definition dsd.h:179
int output_num_samples
Definition dsd.h:229
const float * input_samples
Definition dsd.h:220
int umid
Definition dsd.h:148
mbe_parms * cur_mp
Definition dsd.h:189
char algid[9]
Definition dsd.h:186
int output_offset
Definition dsd.h:227
int firstframe
Definition dsd.h:177
int lastp25type
Definition dsd.h:164
int tgcount
Definition dsd.h:168
int lmid
Definition dsd.h:147
int max
Definition dsd.h:146
int center
Definition dsd.h:142
float * aout_max_buf_p
Definition dsd.h:182
char ftype[16]
Definition dsd.h:159
pthread_mutex_t output_mutex
Definition dsd.h:224
short * output_buffer
Definition dsd.h:226
int repeat
Definition dsd.h:132
float aout_gain
Definition dsd.h:180
char fsubtype[16]
Definition dsd.h:158
P25Heuristics inv_p25_heuristics
Definition dsd.h:205
float audio_out_temp_buf[160]
Definition dsd.h:137
int sidx
Definition dsd.h:153
mbe_parms * prev_mp
Definition dsd.h:190
int sbuf[128]
Definition dsd.h:152
short * audio_out_buf
Definition dsd.h:133
pthread_mutex_t input_mutex
Definition dsd.h:218
int p25kid
Definition dsd.h:192
int offset
Definition dsd.h:165
int numflips
Definition dsd.h:162
int carrier
Definition dsd.h:166
P25Heuristics p25_heuristics
Definition dsd.h:202
int errs
Definition dsd.h:172
int numtdulc
Definition dsd.h:176
char keyid[17]
Definition dsd.h:187
int audio_out_idx
Definition dsd.h:139
int nac
Definition dsd.h:171
char slot0light[8]
Definition dsd.h:178
float * output_samples
Definition dsd.h:228
int midx
Definition dsd.h:156
int optind
Definition dsd.h:175
int min
Definition dsd.h:145
char tg[25][16]
Definition dsd.h:167
int samplesPerSymbol
Definition dsd.h:184
float * audio_out_float_buf_p
Definition dsd.h:136
pthread_cond_t output_ready
Definition dsd.h:225
float * audio_out_temp_buf_p
Definition dsd.h:138
int last_dibit
Definition dsd.h:199
int output_finished
Definition dsd.h:231
short * audio_out_buf_p
Definition dsd.h:134
pthread_cond_t input_ready
Definition dsd.h:219
mbe_parms * prev_mp_enhanced
Definition dsd.h:191
int mbe_file_type
Definition dsd.h:174
int lastsample
Definition dsd.h:151
int synctype
Definition dsd.h:144
int output_length
Definition dsd.h:230
int minbuf[1024]
Definition dsd.h:155
float aout_max_buf[200]
Definition dsd.h:181
int * dibit_buf
Definition dsd.h:130
int lasttg
Definition dsd.h:169
int symbolcnt
Definition dsd.h:160
char err_str[64]
Definition dsd.h:157
int * dibit_buf_p
Definition dsd.h:131
int symbolCenter
Definition dsd.h:185