1
0
mirror of https://github.com/JvanKatwijk/dab-cmdline synced 2025-10-06 16:12:54 +02:00
Files
SDR-DAB_dab-cmdline/example-4/main.cpp

629 lines
17 KiB
C++
Raw Normal View History

2017-09-27 12:56:43 +02:00
#
/*
* Copyright (C) 2015, 2016, 2017
* Jan van Katwijk (J.vanKatwijk@gmail.com)
* Lazy Chair Computing
*
* This file is part of the DAB-library
*
* DAB-library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* DAB-library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DAB-library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* E X A M P L E P R O G R A M
* for the DAB-library
*/
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include <cstdio>
#include <iostream>
2018-08-12 12:25:20 +02:00
#include <complex>
#include <vector>
#include "dab-api.h"
#include "includes/support/band-handler.h"
2017-09-27 12:56:43 +02:00
#ifdef HAVE_SDRPLAY
#include "sdrplay-handler.h"
#elif HAVE_AIRSPY
#include "airspy-handler.h"
#elif HAVE_RTLSDR
#include "rtlsdr-handler.h"
#elif HAVE_WAVFILES
#include "wavfiles.h"
2017-12-18 23:21:56 +02:00
#elif HAVE_RAWFILES
#include "rawfiles.h"
2017-09-27 12:56:43 +02:00
#elif HAVE_RTL_TCP
#include "rtl_tcp-client.h"
2019-10-30 12:41:59 +01:00
elif HAVE_HACKRF
#include "hackrf-handler.h"
#elif HAVE_LIME
#include "lime-handler.h"
2017-09-27 12:56:43 +02:00
#endif
#include <atomic>
#ifdef DATA_STREAMER
#include "tcp-server.h"
#endif
using std::cerr;
using std::endl;
void printOptions (void); // forward declaration
// we deal with some callbacks, so we have some data that needs
// to be accessed from global contexts
static
std::atomic<bool> run;
static
2018-08-12 12:25:20 +02:00
void *theRadio = NULL;
2017-09-27 12:56:43 +02:00
static
std::atomic<bool>timeSynced;
static
std::atomic<bool>timesyncSet;
static
std::atomic<bool>ensembleRecognized;
#ifdef DATA_STREAMER
tcpServer tdcServer (8888);
#endif
std::string programName = "Classic FM";
int32_t serviceIdentifier = -1;
static void sighandler (int signum) {
fprintf (stderr, "Signal caught, terminating!\n");
run. store (false);
}
static
void syncsignalHandler (bool b, void *userData) {
timeSynced. store (b);
timesyncSet. store (true);
(void)userData;
}
//
// This function is called whenever the dab engine has taken
// some time to gather information from the FIC bloks
// the Boolean b tells whether or not an ensemble has been
// recognized, the names of the programs are in the
// ensemble
static
void ensemblenameHandler (std::string name, int Id, void *userData) {
fprintf (stderr, "ensemble %s is (%X) recognized\n",
name. c_str (), (uint32_t)Id);
ensembleRecognized. store (true);
}
static
void programnameHandler (std::string s, int SId, void * userdata) {
fprintf (stderr, "%s (%X) is part of the ensemble\n", s. c_str (), SId);
}
static
void programdataHandler (audiodata *d, void *ctx) {
(void)ctx;
fprintf (stderr, "\tstartaddress\t= %d\n", d -> startAddr);
fprintf (stderr, "\tlength\t\t= %d\n", d -> length);
fprintf (stderr, "\tsubChId\t\t= %d\n", d -> subchId);
fprintf (stderr, "\tprotection\t= %d\n", d -> protLevel);
fprintf (stderr, "\tbitrate\t\t= %d\n", d -> bitRate);
}
//
// The function is called from within the library with
// a string, the so-called dynamic label
static
void dataOut_Handler (std::string dynamicLabel, void *ctx) {
(void)ctx;
fprintf (stderr, "%s\r", dynamicLabel. c_str ());
}
//
// Note: the function is called from the tdcHandler with a
// frame, either frame 0 or frame 1.
// The frames are packed bytes, here an additional header
// is added, a header of 8 bytes:
// the first 4 bytes for a pattern 0xFF 0x00 0xFF 0x00 0xFF
// the length of the contents, i.e. framelength without header
// is stored in bytes 5 (high byte) and byte 6.
// byte 7 contains 0x00, byte 8 contains 0x00 for frametype 0
// and 0xFF for frametype 1
// Note that the callback function is executed in the thread
// that executes the tdcHandler code.
static
void bytesOut_Handler (uint8_t *data, int16_t amount,
uint8_t type, void *ctx) {
#ifdef DATA_STREAMER
uint8_t localBuf [amount + 8];
int16_t i;
localBuf [0] = 0xFF;
localBuf [1] = 0x00;
localBuf [2] = 0xFF;
localBuf [3] = 0x00;
localBuf [4] = (amount >> 8) & 0xFF;
localBuf [5] = amount & 0xFF;
localBuf [6] = 0x00;
localBuf [7] = type == 0 ? 0 : 0xFF;
for (i = 0; i < amount; i ++)
localBuf [8 + i] = data;
tdcServer. sendData (localBuf, amount + 8);
#else
(void)data;
(void)amount;
#endif
(void)ctx;
}
//
// However, in this "special mode", the mp2 and aac frames are send out
// through the same function that is used otherwise for the PCM
// samples
static
void frameHandler (int16_t *buffer, int size, int rate,
bool isStereo, void *ctx) {
//
// Now we know that we have been cheating, the int16_t * buffer
// is actually an uint8_t * buffer, however, the size
// gives the correct amount of elements
fwrite ((void *)buffer, size, 1, stdout);
}
static
void systemData (bool flag, int16_t snr, int32_t freqOff, void *ctx) {
// fprintf (stderr, "synced = %s, snr = %d, offset = %d\n",
// flag? "on":"off", snr, freqOff);
}
static
void fibQuality (int16_t q, void *ctx) {
// fprintf (stderr, "fic quality = %d\n", q);
}
static
void mscQuality (int16_t fe, int16_t rsE, int16_t aacE, void *ctx) {
// fprintf (stderr, "msc quality = %d %d %d\n", fe, rsE, aacE);
}
int main (int argc, char **argv) {
2019-10-29 19:16:18 +01:00
// Default values
2017-09-27 12:56:43 +02:00
uint8_t theMode = 1;
std::string theChannel = "11C";
uint8_t theBand = BAND_III;
2019-10-29 19:16:18 +01:00
#ifdef HAVE_HACKRF
int lnaGain = 40;
int vgaGain = 40;
int ppmOffset = 0;
const char *optionsString = "T:D:d:M:B:P:O:A:C:G:g:p:";
2019-10-29 19:16:18 +01:00
#elif HAVE_LIME
int16_t gain = 70;
std::string antenna = "Auto";
const char *optionsString = "T:D:d:M:B:P:O:A:C:G:g:X:";
2019-10-29 19:16:18 +01:00
#elif HAVE_SDRPLAY
2018-11-05 19:52:41 +01:00
int16_t GRdB = 30;
int16_t lnaState = 2;
2017-09-27 12:56:43 +02:00
bool autogain = false;
2019-10-29 19:16:18 +01:00
int16_t ppmOffset = 0;
const char *optionsString = "T:D:d:M:B:P:O:A:C:G:L:Qp:";
2019-10-29 19:16:18 +01:00
#elif HAVE_AIRSPY
int16_t gain = 20;
bool autogain = false;
int ppmOffset = 0;
const char *optionsString = "T:D:d:M:B:P:O:A:C:G:p:";
2019-10-29 19:16:18 +01:00
#elif HAVE_RTLSDR
int16_t gain = 50;
bool autogain = false;
int16_t ppmOffset = 0;
const char *optionsString = "T:D:d:M:B:P:O:A:C:G:p:Q";
2019-10-29 19:16:18 +01:00
#elif HAVE_WAVFILES
2017-09-27 12:56:43 +02:00
std::string fileName;
2019-10-29 19:16:18 +01:00
bool repeater = true;
const char *optionsString = "D:d:M:B:P:O:A:F:R:";
2017-12-18 23:21:56 +02:00
#elif HAVE_RAWFILES
std::string fileName;
2019-10-29 19:16:18 +01:00
bool repeater = true;
const char *optionsString = "D:d:M:B:P:O:A:F:R:";
#elif
// HAVE_RTL_TCP
int gain = 50;
bool autogain = false;
int ppmOffset = 0;
2017-09-27 12:56:43 +02:00
std::string hostname = "127.0.0.1"; // default
int32_t basePort = 1234; // default
const char *optionsString = "T:D:d:M:B:P:O:A:C:G:Qp:H:I";
2017-09-27 12:56:43 +02:00
#endif
2019-10-29 19:16:18 +01:00
std::string soundChannel = "default";
int16_t timeSyncTime = 5;
int16_t freqSyncTime = 5;
int opt;
struct sigaction sigact;
bandHandler dabBand;
deviceHandler *theDevice;
int theDuration = -1; // infinite
2017-09-27 12:56:43 +02:00
std::cerr << "dab_cmdline example IV,\n \
2019-10-29 19:16:18 +01:00
Copyright 2017 J van Katwijk, Lazy Chair Computing\n";
2017-09-27 12:56:43 +02:00
timeSynced. store (false);
timesyncSet. store (false);
run. store (false);
2019-10-29 19:16:18 +01:00
std::wcout.imbue(std::locale("en_US.utf8"));
2017-09-27 12:56:43 +02:00
if (argc == 1) {
printOptions ();
exit (1);
}
2019-10-29 19:16:18 +01:00
std::setlocale (LC_ALL, "en-US.utf8");
fprintf (stderr, "options are %s\n", optionsString);
while ((opt = getopt (argc, argv, optionsString)) != -1) {
2017-09-27 12:56:43 +02:00
switch (opt) {
case 'T':
theDuration = 60 * atoi (optarg);
break;
2019-10-29 19:16:18 +01:00
case 'D':
freqSyncTime = atoi (optarg);
break;
2017-09-27 12:56:43 +02:00
2019-10-29 19:16:18 +01:00
case 'd':
timeSyncTime = atoi (optarg);
2017-09-27 12:56:43 +02:00
break;
case 'M':
theMode = atoi (optarg);
if (!((theMode == 1) || (theMode == 2) || (theMode == 4)))
2017-09-27 12:56:43 +02:00
theMode = 1;
break;
case 'B':
theBand = std::string (optarg) == std::string ("L_BAND") ?
2019-10-29 19:16:18 +01:00
L_BAND : BAND_III;
2017-09-27 12:56:43 +02:00
break;
case 'P':
programName = optarg;
break;
2019-10-29 19:16:18 +01:00
#ifdef HAVE_WAVFILES
case 'F':
fileName = std::string (optarg);
2018-11-05 19:52:41 +01:00
2019-10-29 19:16:18 +01:00
case 'R':
repeater = false;
break;
#elif HAVE_RAWFILES
2017-09-27 12:56:43 +02:00
case 'F':
fileName = std::string (optarg);
break;
2019-10-29 19:16:18 +01:00
case 'R': repeater = false;
break;
#elif HAVE_HACKRF
case 'G':
lnaGain = atoi (optarg);
break;
case 'g':
vgaGain = atoi (optarg);
break;
2017-09-27 12:56:43 +02:00
case 'C':
theChannel = std::string (optarg);
break;
2019-10-29 19:16:18 +01:00
case 'p':
ppmOffset = 0;
break;
2017-09-27 12:56:43 +02:00
2019-10-29 19:16:18 +01:00
#elif HAVE_LIME
case 'G':
case 'g':
gain = atoi (optarg);
break;
case 'X':
antenna = std::string (optarg);
break;
case 'C':
theChannel = std::string (optarg);
fprintf (stderr, "%s \n", optarg);
break;
#elif HAVE_SDRPLAY
2017-09-27 12:56:43 +02:00
case 'G':
2018-11-05 19:52:41 +01:00
GRdB = atoi (optarg);
2017-09-27 12:56:43 +02:00
break;
2018-11-05 19:52:41 +01:00
case 'L':
lnaState = atoi (optarg);
break;
2019-10-29 19:16:18 +01:00
case 'Q':
autogain = true;
break;
case 'C':
theChannel = std::string (optarg);
break;
case 'p':
ppmOffset = atoi (optarg);
break;
#elif HAVE_AIRSPY
2018-11-05 19:52:41 +01:00
case 'G':
2019-10-29 19:16:18 +01:00
gain = atoi (optarg);
2018-11-05 19:52:41 +01:00
break;
2019-10-29 19:16:18 +01:00
2017-09-27 12:56:43 +02:00
case 'Q':
autogain = true;
break;
2019-10-29 19:16:18 +01:00
case 'p':
ppmOffset = atoi (optarg);
break;
case 'C':
theChannel = std::string (optarg);
break;
#elif HAVE_RTLSDR
case 'G':
gain = atoi (optarg);
break;
case 'Q':
autogain = true;
break;
case 'p':
ppmOffset = atoi (optarg);
break;
case 'C':
theChannel = std::string (optarg);
break;
#elif HAVE_RTL_TCP
case 'C':
theChannel = std::string (optarg);
break;
2017-09-27 12:56:43 +02:00
case 'H':
hostname = std::string (optarg);
break;
case 'I':
basePort = atoi (optarg);
break;
2019-10-29 19:16:18 +01:00
case "G":
gain = atoi (optarg);
break;
2017-09-27 12:56:43 +02:00
2019-10-29 19:16:18 +01:00
case 'Q':
autogain = true;
break;
case 'p':
ppmOffset = atoi (optarg);
break;
2017-09-27 12:56:43 +02:00
2019-10-29 19:16:18 +01:00
#endif
2017-09-27 12:56:43 +02:00
default:
2019-10-29 19:16:18 +01:00
fprintf (stderr, "Option %c not understood\n", opt);
2017-09-27 12:56:43 +02:00
printOptions ();
exit (1);
}
}
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
int32_t frequency = dabBand. Frequency (theBand, theChannel);
try {
#ifdef HAVE_SDRPLAY
theDevice = new sdrplayHandler (frequency,
2019-10-29 19:16:18 +01:00
ppmOffset,
2018-11-05 19:52:41 +01:00
GRdB,
lnaState,
2017-09-27 12:56:43 +02:00
autogain,
0,
0);
#elif HAVE_AIRSPY
theDevice = new airspyHandler (frequency,
2019-10-29 19:16:18 +01:00
ppmOffset,
gain, false);
2017-09-27 12:56:43 +02:00
#elif HAVE_RTLSDR
theDevice = new rtlsdrHandler (frequency,
2019-10-29 19:16:18 +01:00
ppmOffset,
gain,
2017-09-27 12:56:43 +02:00
autogain);
2019-10-29 19:16:18 +01:00
#elif HAVE_HACKRF
theDevice = new hackrfHandler (frequency,
ppmOffset,
lnaGain,
vgaGain);
#elif HAVE_LIME
theDevice = new limeHandler (frequency, gain, antenna);
2017-09-27 12:56:43 +02:00
#elif HAVE_WAVFILES
2019-10-29 19:16:18 +01:00
theDevice = new wavFiles (fileName, repeater);
#elif defined (HAVE_RAWFILES)
theDevice = new rawFiles (fileName, repeater);
2017-09-27 12:56:43 +02:00
#elif HAVE_RTL_TCP
theDevice = new rtl_tcp_client (hostname,
basePort,
frequency,
2019-10-29 19:16:18 +01:00
gain,
2017-09-27 12:56:43 +02:00
autogain,
2019-10-29 19:16:18 +01:00
ppmOffset);
2017-09-27 12:56:43 +02:00
#endif
}
catch (int e) {
2019-10-29 19:16:18 +01:00
std::cerr << "allocating device failed (" << e << "), fatal\n";
2017-09-27 12:56:43 +02:00
exit (32);
}
// and with a sound device we can create a "backend"
2018-08-12 12:25:20 +02:00
theRadio = dabInit (theDevice,
theMode,
syncsignalHandler,
systemData,
ensemblenameHandler,
programnameHandler,
fibQuality,
frameHandler,
dataOut_Handler,
bytesOut_Handler,
programdataHandler,
mscQuality,
NULL, // no mot slides
NULL, // no spectrum shown
NULL, // no constellations
NULL
);
2017-09-27 12:56:43 +02:00
if (theRadio == NULL) {
fprintf (stderr, "sorry, no radio available, fatal\n");
exit (4);
}
2019-10-29 19:16:18 +01:00
theDevice -> restartReader (frequency);
2017-09-27 12:56:43 +02:00
//
// The device should be working right now
timesyncSet. store (false);
ensembleRecognized. store (false);
2018-08-12 12:25:20 +02:00
dabStartProcessing (theRadio);
2017-09-27 12:56:43 +02:00
2019-10-29 19:16:18 +01:00
while (!timeSynced. load () && (--timeSyncTime >= 0))
2017-09-27 12:56:43 +02:00
sleep (1);
if (!timeSynced. load ()) {
cerr << "There does not seem to be a DAB signal here" << endl;
theDevice -> stopReader ();
sleep (1);
2018-08-12 12:25:20 +02:00
dabStop (theRadio);
dabExit (theRadio);
2017-09-27 12:56:43 +02:00
delete theDevice;
exit (22);
}
else
cerr << "there might be a DAB signal here" << endl;
2019-10-29 19:16:18 +01:00
int timeOut = 0;
int waitingTime = 15;
2017-09-27 12:56:43 +02:00
if (!ensembleRecognized. load ())
while (!ensembleRecognized. load () && (++timeOut < waitingTime)) {
fprintf (stderr, "%d\r", waitingTime - timeOut);
sleep (1);
}
fprintf (stderr, "\n");
if (!ensembleRecognized. load ()) {
fprintf (stderr, "no ensemble data found, fatal\n");
theDevice -> stopReader ();
sleep (1);
2018-08-12 12:25:20 +02:00
dabStop (theRadio);
dabExit (theRadio);
2017-09-27 12:56:43 +02:00
delete theDevice;
exit (22);
}
run. store (true);
2020-06-18 13:32:07 +02:00
if (serviceIdentifier != -1) {
char temp [255];
dab_getserviceName (theRadio, serviceIdentifier, temp);
programName = std::string (temp);
}
2018-08-12 12:25:20 +02:00
std::cerr << "we try to start program " <<
programName << "\n";
if (!is_audioService (theRadio, programName. c_str ())) {
std::cerr << "sorry we cannot handle service " <<
programName << "\n";
run. store (false);
exit (22);
}
audiodata ad;
dataforAudioService (theRadio, programName. c_str (), &ad, 0);
if (!ad. defined) {
std::cerr << "sorry we cannot handle service " <<
programName << "\n";
run. store (false);
}
dabReset_msc (theRadio);
set_audioChannel (theRadio, &ad);
2017-09-27 12:56:43 +02:00
while (run. load () && (theDuration != 0)) {
if (theDuration > 0)
theDuration --;
2017-09-27 12:56:43 +02:00
sleep (1);
}
2017-09-27 12:56:43 +02:00
theDevice -> stopReader ();
2018-08-12 12:25:20 +02:00
dabStop (theRadio);
dabExit (theRadio);
2017-09-27 12:56:43 +02:00
delete theDevice;
}
void printOptions (void) {
2019-10-30 12:41:59 +01:00
std::cerr <<
" dab-cmdline options are\n"
" -T duration\t stop after <duration> minutes\n"
" -M Mode\tMode is 1, 2 or 4. Default is Mode 1\n"
" -D number\tamount of time to look for an ensemble\n"
" -d number\tseconds to reach time sync\n"
" -P name\tprogram to be selected in the ensemble\n"
" for file input:\n"
" -F filename\tin case the input is from file\n"
" -R switch off automatic continuation after eof\n"
" for hackrf:\n"
" -B Band\tBand is either L_BAND or BAND_III (default)\n"
" -C Channel\n"
" -v vgaGain\n"
" -l lnaGain\n"
" -a amp enable (default off)\n"
" -c number\tppm offset\n"
" for SDRplay:\n"
" -B Band\tBand is either L_BAND or BAND_III (default)\n"
" -C Channel\n"
" -G Gain reduction in dB (range 20 .. 59)\n"
" -L lnaState (depends on model chosen)\n"
" -Q autogain (default off)\n"
" -c number\t ppm offset\n"
" for rtlsdr:\n"
" -B Band\tBand is either L_BAND or BAND_III (default)\n"
" -C Channel\n"
" -G number\t gain, range 0 .. 100\n"
" -Q autogain (default off)\n"
" -c number\tppm offset\n"
" for airspy:\n"
" -B Band\tBand is either L_BAND or BAND_III (default)\n"
" -C Channel\n"
" -G number\t gain, range 1 .. 21\n"
" -b set rf bias\n"
" -c number\t ppm Correction\n"
" for rtl_tcp:\n"
" -H url\t hostname for connection\n"
" -I number\t baseport for connection\n"
" -G number\t gain setting\n"
" -Q autogain (default off)\n"
" -c number\t ppm Correction\n"
" for limesdr:\n"
" -G number\t gain\n"
" -X antenna selection\n"
" -C channel\n";
2017-09-27 12:56:43 +02:00
}