1
0
mirror of https://github.com/JvanKatwijk/dab-cmdline synced 2025-10-05 23:52:50 +02:00
Files
SDR-DAB_dab-cmdline/example-2/main.cpp
2025-08-16 18:52:13 +02:00

804 lines
22 KiB
C++

#
/*
* 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
* This program might (or might not) be used to mould the interface to
* your wishes. Do not take it as a definitive and "ready" program
* for the DAB-library
*/
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include <cstdio>
#include <iostream>
#include <complex>
#include <vector>
#include "audiosink.h"
#include "filesink.h"
#include "dab-api.h"
#include "includes/support/band-handler.h"
#ifdef HAVE_SDRPLAY
#include "sdrplay-handler.h"
#elif HAVE_SDRPLAY_V3
#include "sdrplay-handler-v3.h"
#elif HAVE_AIRSPY
#include "airspy-handler.h"
#elif HAVE_RTLSDR
#include "rtlsdr-handler.h"
#elif HAVE_WAVFILES
#include "wavfiles.h"
#elif HAVE_RAWFILES
#include "rawfiles.h"
#elif HAVE_XMLFILES
#include "xml-filereader.h"
#elif HAVE_RTL_TCP
#include "rtl_tcp-client.h"
#elif HAVE_HACKRF
#include "hackrf-handler.h"
#elif HAVE_LIME
#include "lime-handler.h"
#elif HAVE_PLUTO
#include "pluto-handler.h"
#endif
#include <locale>
#include <codecvt>
#include <atomic>
#ifdef DATA_STREAMER
#include "tcp-server.h"
#endif
#include <string>
using std::cerr;
using std::endl;
void printOptions (); // forward declaration
// we deal with callbacks from different threads. So, if you extend
// the functions, take care and add locking whenever needed
static
std::atomic<bool> run;
static
void *theRadio = nullptr;
static
std::atomic<bool>timeSynced;
static
std::atomic<bool>timesyncSet;
static
std::atomic<bool>ensembleRecognized;
static
audioBase *soundOut = nullptr;
#ifdef DATA_STREAMER
tcpServer tdcServer (8888);
#endif
std::string programName = "Sky Radio";
//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 name_of_ensemble (const std::string &name, int Id, void *userData) {
fprintf (stderr, "ensemble %s is (%X) recognized\n",
name. c_str (), (uint32_t)Id);
ensembleRecognized. store (true);
}
std::vector<std::string> programNames;
std::vector<int> programSIds;
#include <bits/stdc++.h>
std::unordered_map <int, std::string> ensembleContents;
static
void serviceName (const std::string &s, int SId,
uint16_t subChId, void *userdata) {
for (auto it : programNames)
if (it == s)
return;
ensembleContents. insert (pair <int, std::string> (SId, s));
programNames. push_back (std::string (s));
programSIds . push_back (SId);
std::cerr << "program " << s << " is part of the ensemble\n";
}
static
void programdata_Handler (audiodata *d, void *ctx) {
(void)ctx;
// std::cerr << "\tstartaddress\t= " << d -> startAddr << "\n";
// std::cerr << "\tlength\t\t= " << d -> length << "\n";
// std::cerr << "\tsubChId\t\t= " << d -> subchId << "\n";
// std::cerr << "\tprotection\t= " << d -> protLevel << "\n";
// std::cerr << "\tbitrate\t\t= " << d -> bitRate << "\n";
}
//
// The function is called from within the library with
// a string, the so-called dynamic label
static
void dataOut_Handler (const char * dynamicLabel, void *ctx) {
(void)ctx;
// std::cerr << std::string (dynamicLabel) << "\r";
}
//
// The function is called from the MOT handler, with
// as parameters the filename where the picture is stored
// d denotes the subtype of the picture
// typedef void (*motdata_t)(std::string, int, void *);
void motdata_Handler (uint8_t *data, int size,
const char *s, int d, void *ctx) {
(void)s; (void)d; (void)ctx;
// fprintf (stderr, "plaatje %s met lengte %d\n", s, size);
}
void tii_data_Handler (tiiData *theData, void *ctx) {
(void)theData;
(void)ctx;
}
void timeHandler (int hours, int minutes, void *ctx) {
// fprintf (stderr, "%2d:%2d\n", hours, minutes);
(void)ctx;
}
//
// 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;
}
//
// This function is overloaded. In the normal form it
// handles a buffer full of PCM samples. We pass them on to the
// audiohandler, based on portaudio. Feel free to modify this
// and send the samples elsewhere
//
// However, in the "special mode", the aac frames are send out
// Obviously, the parameters "rate" and "isStereo" are meaningless
// then.
static
void pcmHandler (int16_t *buffer, int size, int rate,
bool isStereo, void *ctx) {
static bool isStarted = false;
(void)isStereo;
if (!isStarted) {
soundOut -> restart ();
isStarted = true;
}
soundOut -> audioOut (buffer, size, rate);
}
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) {
// Default values
uint8_t theMode = 1;
std::string theChannel = "11C";
uint8_t theBand = BAND_III;
#ifdef HAVE_HACKRF
int lnaGain = 40;
int vgaGain = 40;
int ppmOffset = 0;
const char *optionsString = "t:T:D:M:B:P:O:A:C:G:g:p:";
#elif HAVE_LIME
int16_t gain = 70;
std::string antenna = "Auto";
const char *optionsString = "t:T:D:M:B:P:O:A:C:G:g:X:";
#elif HAVE_PLUTO
int16_t gain = 60;
bool autogain = true;
const char *optionsString = "t:T:D:M:B:P:O:A:C:G:Q";
#elif HAVE_SDRPLAY
int16_t GRdB = 30;
int16_t lnaState = 4;
bool autogain = true;
int16_t ppmOffset = 0;
bool X_dump = false;
const char *optionsString = "t:T:D:M:B:P:O:A:C:G:L:Qp:X";
#elif HAVE_SDRPLAY_V3
int16_t GRdB = 30;
int16_t lnaState = 3;
bool autogain = false;
int16_t ppmOffset = 0;
const char *optionsString = "t:T:D:M:B:P:O:A:C:G:L:Qp:X";
bool X_dump = false;
#elif HAVE_AIRSPY
int16_t gain = 20;
bool autogain = false;
bool rf_bias = false;
int16_t ppmOffset = 0;
const char *optionsString = "t:T:D:M:B:P:O:A:C:G:p:b";
#elif HAVE_RTLSDR
int16_t gain = 50;
bool autogain = false;
int16_t ppmOffset = 0;
const char *optionsString = "t:T:D:M:B:P:O:A:C:G:p:Q";
#elif HAVE_WAVFILES
std::string fileName;
bool repeater = true;
const char *optionsString = "t:T:D:M:B:P:O:A:F:R:";
#elif HAVE_RAWFILES
std::string fileName;
bool repeater = true;
const char *optionsString = "t:T:D:M:B:P:O:A:F:R:";
#elif HAVE_XMLFILES
std::string fileName;
bool repeater = true;
const char *optionsString = "t:T:D:M:B:P:O:A:F:R:";
#elif HAVE_RTL_TCP
int gain = 50;
bool autogain = false;
int ppmOffset = 0;
std::string hostname = "127.0.0.1"; // default
int32_t basePort = 1234; // default
const char *optionsString = "t:T:D:M:B:P:O:A:C:G:Qp:H:I";
#endif
std::string soundChannel = "default";
int16_t latency = 10;
int16_t timeSyncTime = 10;
int16_t freqSyncTime = 5;
int opt;
struct sigaction sigact;
bandHandler dabBand;
deviceHandler *theDevice = nullptr;
int16_t thresholdValue = 6; // default;
bool err;
int theDuration = -1; // no limit
std::cerr << "dab_cmdline example II,\n \
Copyright 2017 J van Katwijk, Lazy Chair Computing\n";
timeSynced. store (false);
timesyncSet. store (false);
run. store (false);
std::wcout.imbue(std::locale("en_US.utf8"));
if (argc == 1) {
printOptions ();
exit (1);
}
std::setlocale (LC_ALL, "en-US.utf8");
fprintf (stderr, "options are %s\n", optionsString);
while ((opt = getopt (argc, argv, optionsString)) != -1) {
switch (opt) {
case 't':
thresholdValue = atoi (optarg);
break;
case 'T':
theDuration = 60 * atoi (optarg); // minutes
break;
case 'D':
freqSyncTime = atoi (optarg);
break;
case 'd':
timeSyncTime = atoi (optarg);
break;
case 'M':
theMode = atoi (optarg);
if (!((theMode == 1) || (theMode == 2) || (theMode == 4)))
theMode = 1;
break;
case 'B':
theBand = std::string (optarg) == std::string ("L_BAND") ?
L_BAND : BAND_III;
break;
case 'P':
programName = optarg;
break;
case 'O':
soundOut = new fileSink (std::string (optarg), &err);
if (!err) {
std::cerr << "sorry, could not open file\n";
exit (32);
}
break;
case 'A':
soundChannel = optarg;
break;
#ifdef HAVE_WAVFILES
case 'F':
fileName = std::string (optarg);
break;
case 'R':
repeater = false;
break;
#elif HAVE_RAWFILES
case 'F':
fileName = std::string (optarg);
break;
case 'R': repeater = false;
break;
#elif HAVE_XMLFILES
case 'F':
fileName = std::string (optarg);
break;
case 'R': repeater = false;
break;
#elif HAVE_HACKRF
case 'G':
lnaGain = atoi (optarg);
break;
case 'g':
vgaGain = atoi (optarg);
break;
case 'C':
theChannel = std::string (optarg);
break;
case 'p':
ppmOffset = 0;
break;
#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_PLUTO
case 'G':
gain = atoi (optarg);
break;
case 'Q':
autogain = true;
break;
case 'C':
theChannel = std::string (optarg);
fprintf (stderr, "%s \n", optarg);
break;
#elif HAVE_SDRPLAY
case 'G':
GRdB = atoi (optarg);
break;
case 'L':
lnaState = atoi (optarg);
break;
case 'Q':
autogain = true;
break;
case 'C':
theChannel = std::string (optarg);
break;
case 'p':
ppmOffset = atoi (optarg);
break;
case 'X':
X_dump = true;
break;
#elif HAVE_SDRPLAY_V3
case 'G':
GRdB = atoi (optarg);
break;
case 'L':
lnaState = atoi (optarg);
break;
case 'Q':
autogain = true;
break;
case 'C':
theChannel = std::string (optarg);
break;
case 'p':
ppmOffset = atoi (optarg);
break;
case 'X':
X_dump = true;
break;
#elif HAVE_AIRSPY
case 'G':
gain = atoi (optarg);
break;
case 'Q':
autogain = true;
break;
case 'b':
rf_bias = true;
break;
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;
case 'H':
hostname = std::string (optarg);
break;
case 'I':
basePort = atoi (optarg);
break;
case 'G':
gain = atoi (optarg);
break;
case 'Q':
autogain = true;
break;
case 'p':
ppmOffset = atoi (optarg);
break;
#endif
default:
fprintf (stderr, "Option %c not understood\n", opt);
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,
ppmOffset,
GRdB,
lnaState,
autogain,
0,
0,
X_dump);
#elif HAVE_SDRPLAY_V3
theDevice = new sdrplayHandler_v3 (frequency,
ppmOffset,
GRdB,
lnaState,
autogain,
0,
0,
X_dump);
#elif HAVE_AIRSPY
theDevice = new airspyHandler (frequency,
ppmOffset,
gain, rf_bias);
#elif HAVE_RTLSDR
theDevice = new rtlsdrHandler (frequency,
ppmOffset,
gain,
autogain);
#elif HAVE_HACKRF
theDevice = new hackrfHandler (frequency,
ppmOffset,
lnaGain,
vgaGain);
#elif HAVE_LIME
theDevice = new limeHandler (frequency, gain, antenna);
#elif HAVE_PLUTO
theDevice = new plutoHandler (frequency, gain, autogain);
#elif HAVE_WAVFILES
theDevice = new wavFiles (fileName, repeater);
#elif defined (HAVE_RAWFILES)
theDevice = new rawFiles (fileName, repeater);
#elif defined (HAVE_XMLFILES)
theDevice = new xml_fileReader (fileName, repeater);
#elif HAVE_RTL_TCP
theDevice = new rtl_tcp_client (hostname,
basePort,
frequency,
gain,
autogain,
ppmOffset);
#endif
}
catch (std::exception& ex) {
std::cerr << "allocating device failed, fatal: ";
fprintf (stderr, "%s\n", ex.what());
exit (1);
}
if (theDevice == nullptr) {
fprintf (stderr, "no device selected, fatal\n");
exit (33);
}
//
if (soundOut == NULL) { // not bound to a file?
soundOut = new audioSink (latency, soundChannel, &err);
if (err) {
std::cerr << "no valid sound channel, fatal\n";
exit (33);
}
}
//
// and with a sound device we now can create a "backend"
API_struct interface;
interface. dabMode = theMode;
interface. thresholdValue = thresholdValue;
interface. syncsignal_Handler = syncsignalHandler;
interface. systemdata_Handler = systemData;
interface. name_of_ensemble = name_of_ensemble;
interface. serviceName = serviceName;
interface. fib_quality_Handler = fibQuality;
interface. audioOut_Handler = pcmHandler;
interface. dataOut_Handler = dataOut_Handler;
interface. bytesOut_Handler = bytesOut_Handler;
interface. programdata_Handler = programdata_Handler;
interface. program_quality_Handler = mscQuality;
interface. motdata_Handler = motdata_Handler;
interface. tii_data_Handler = tii_data_Handler;
interface. timeHandler = timeHandler;
theRadio = dabInit (theDevice,
&interface,
nullptr, // no spectrum shown
nullptr, // no constellations
nullptr // Ctx
);
if (theRadio == NULL) {
std::cerr << "sorry, no radio available, fatal\n";
exit (4);
}
theDevice -> restartReader (frequency);
//
// The device should be working right now
timesyncSet. store (false);
ensembleRecognized. store (false);
dabStartProcessing (theRadio);
while (!timeSynced. load () && (--timeSyncTime >= 0))
sleep (1);
if (!timeSynced. load ()) {
cerr << "There does not seem to be a DAB signal here" << endl;
theDevice -> stopReader ();
sleep (1);
dabStop (theRadio);
dabExit (theRadio);
delete theDevice;
exit (22);
}
else
std::cerr << "there might be a DAB signal here" << endl;
while (!ensembleRecognized. load () &&
(--freqSyncTime >= 0)) {
std::cerr << freqSyncTime + 1 << "\r";
sleep (1);
}
std::cerr << "\n";
if (!ensembleRecognized. load ()) {
std::cerr << "no ensemble data found, fatal\n";
theDevice -> stopReader ();
sleep (1);
dabReset (theRadio);
dabExit (theRadio);
delete theDevice;
exit (22);
}
sleep (4);
run. store (true);
std::cerr << "we try to start program " <<
programName << "\n";
audiodata ad;
if (!is_audioService (theRadio, programName. c_str ())) {
std::cerr << "sorry we cannot handle service " <<
programName << "\n";
run. store (false);
}
else {
audiodata ad;
dataforAudioService (theRadio,
programName. c_str (), ad, 0);
if (ad. defined) {
dabReset_msc (theRadio);
set_audioChannel (theRadio, ad);
}
else {
std::cerr << "sorry we cannot handle service " <<
programName << "\n";
run. store (false);
}
}
while (run. load () && (theDuration != 0)) {
if (theDuration > 0)
theDuration --;
sleep (1);
}
theDevice -> stopReader ();
// dabReset (theRadio);
dabExit (theRadio);
delete theDevice;
delete soundOut;
}
void printOptions (void) {
std::cerr <<
" dab-cmdline options are\n"
" -T Duration\tstop after <Duration> seconds\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"
" -P name\tprogram to be selected in the ensemble\n"
" -A name\t select the audio channel (portaudio)\n"
" -O fileName\t output to file <name>\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";
}