From dc62680d34f55949bd7347ecb9087300f3efa478 Mon Sep 17 00:00:00 2001 From: Jan van Katwijk Date: Wed, 2 Aug 2017 13:13:21 +0200 Subject: [PATCH] test version with file input --- device-handler.h | 2 + devices/device-handler.cpp | 2 + devices/wavfiles/wavfiles.cpp | 150 ++++++++++++++++++++++++++++++++++ devices/wavfiles/wavfiles.h | 53 ++++++++++++ example-2/CMakeLists.txt | 28 +++++++ example-2/main.cpp | 23 +++++- 6 files changed, 255 insertions(+), 3 deletions(-) create mode 100644 devices/wavfiles/wavfiles.cpp create mode 100644 devices/wavfiles/wavfiles.h diff --git a/device-handler.h b/device-handler.h index 77baf13..89476cd 100644 --- a/device-handler.h +++ b/device-handler.h @@ -29,6 +29,7 @@ #include #include +#include using namespace std; class deviceHandler { @@ -53,6 +54,7 @@ virtual void set_autogain (bool); int32_t lastFrequency; int32_t vfoOffset; int theGain; +virtual void run (void); }; #endif diff --git a/devices/device-handler.cpp b/devices/device-handler.cpp index 3745238..aa02d23 100644 --- a/devices/device-handler.cpp +++ b/devices/device-handler.cpp @@ -47,6 +47,8 @@ bool deviceHandler::restartReader (void) { void deviceHandler::stopReader (void) { } +void deviceHandler::run (void) { +} int32_t deviceHandler::getSamples (std::complex *v, int32_t amount) { diff --git a/devices/wavfiles/wavfiles.cpp b/devices/wavfiles/wavfiles.cpp new file mode 100644 index 0000000..9e35887 --- /dev/null +++ b/devices/wavfiles/wavfiles.cpp @@ -0,0 +1,150 @@ +# +/* + * Copyright (C) 2013 .. 2017 + * Jan van Katwijk (J.vanKatwijk@gmail.com) + * Lazy Chair Programming + * + * 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 + */ +#include +#include +#include +#include +#include +#include +#include +#include "wavfiles.h" + +static inline +int64_t getMyTime (void) { +struct timeval tv; + + gettimeofday (&tv, NULL); + return ((int64_t)tv. tv_sec * 1000000 + (int64_t)tv. tv_usec); +} + +#define __BUFFERSIZE 8 * 32768 + + wavFiles::wavFiles (std::string f) { +SF_INFO *sf_info; + + fileName = f; + _I_Buffer = new RingBuffer>(__BUFFERSIZE); + + sf_info = (SF_INFO *)alloca (sizeof (SF_INFO)); + sf_info -> format = 0; + filePointer = sf_open (f. c_str (), SFM_READ, sf_info); + if (filePointer == NULL) { + fprintf (stderr, "file %s no legitimate sound file\n", + f. c_str ()); + throw (24); + } + if ((sf_info -> samplerate != 2048000) || + (sf_info -> channels != 2)) { + fprintf (stderr, "This is not a recorded dab file, sorry\n"); + sf_close (filePointer); + throw (25); + } + currPos = 0; +} + + wavFiles::~wavFiles (void) { + if (running) + workerHandle. join (); + running = false; + sf_close (filePointer); + delete _I_Buffer; +} + +bool wavFiles::restartReader (void) { + workerHandle = std::thread (wavFiles::run, this); + running = true; + return true; +} + +void wavFiles::stopReader (void) { + if (running) + workerHandle. join (); + running = false; +} +// +// size is in I/Q pairs +int32_t wavFiles::getSamples (std::complex *V, int32_t size) { +int32_t amount; + if (filePointer == NULL) + return 0; + + while (_I_Buffer -> GetRingBufferReadAvailable () < (uint32_t)size) + usleep (100); + + amount = _I_Buffer -> getDataFromBuffer (V, size); + return amount; +} + +int32_t wavFiles::Samples (void) { + return _I_Buffer -> GetRingBufferReadAvailable (); +} + +void wavFiles::run (wavFiles *p) { +int32_t t, i; +std::complex *bi; +int32_t bufferSize = 32768; +int64_t period; +int64_t nextStop; + + p -> running = true; + period = (32768 * 1000) / 2048; // full IQś read + fprintf (stderr, "Period = %ld\n", period); + bi = new std::complex [bufferSize]; + nextStop = getMyTime (); + while (p -> running) { + while (p -> _I_Buffer -> WriteSpace () < bufferSize) { + if (!p -> running) + break; + usleep (100); + } + + nextStop += period; + t = p -> readBuffer (bi, bufferSize); + if (t < bufferSize) { + for (i = t; i < bufferSize; i ++) + bi [i] = 0; + t = bufferSize; + } + p -> _I_Buffer -> putDataIntoBuffer (bi, bufferSize); + if (nextStop - getMyTime () > 0) + usleep (nextStop - getMyTime ()); + } + fprintf (stderr, "taak voor replay eindigt hier\n"); + delete [] bi; +} +/* + * length is number of uints that we read. + */ +int32_t wavFiles::readBuffer (std::complex *data, int32_t length) { +int32_t i, n; +float temp [2 * length]; + + n = sf_readf_float (filePointer, temp, length); + if (n < length) { + sf_seek (filePointer, 0, SEEK_SET); + fprintf (stderr, "End of file, restarting\n"); + } + for (i = 0; i < n; i ++) + data [i] = std::complex (temp [2 * i], temp [2 * i + 1]); + return n & ~01; +} + diff --git a/devices/wavfiles/wavfiles.h b/devices/wavfiles/wavfiles.h new file mode 100644 index 0000000..9b77862 --- /dev/null +++ b/devices/wavfiles/wavfiles.h @@ -0,0 +1,53 @@ +# +/* + * Copyright (C) 2013 .. 2017 + * Jan van Katwijk (J.vanKatwijk@gmail.com) + * Lazy Chair Computing + * + * This file is part of the Qt-DAB program + * Qt-DAB 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. + * + * Qt-DAB 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 Qt-DAB; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __WAV_FILES__ +#define __WAV_FILES__ + +#include +#include "ringbuffer.h" +#include "device-handler.h" +#include + +class wavFiles: public deviceHandler { +public: + wavFiles (std::string); + ~wavFiles (void); + int32_t getSamples (std::complex *, int32_t); + uint8_t myIdentity (void); + int32_t Samples (void); + bool restartReader (void); + void stopReader (void); + +private: + std::string fileName; +static void run (wavFiles *); + int32_t readBuffer (std::complex *, int32_t); + RingBuffer> *_I_Buffer; + std::thread workerHandle; + int32_t bufferSize; + SNDFILE *filePointer; + bool running; + int64_t currPos; +}; + +#endif + diff --git a/example-2/CMakeLists.txt b/example-2/CMakeLists.txt index e506646..545b615 100644 --- a/example-2/CMakeLists.txt +++ b/example-2/CMakeLists.txt @@ -42,6 +42,11 @@ if(DEFINED RTLSDR) set(objectName dab-rtlsdr-2) endif () +if(DEFINED WAVFILES) + set(WAVFILES true) + set(objectName dab-files) +endif () + # ######################################################################### find_package (PkgConfig) @@ -161,6 +166,29 @@ endif () add_definitions (-DHAVE_RTLSDR) endif() + + if (WAVFILES) + include_directories ( + ../devices/wavfiles/ + ) + + set (${objectName}_HDRS + ${${objectName}_HDRS} + ../devices/wavfiles/wavfiles.h + ) + + set (${objectName}_SRCS + ${${objectName}_SRCS} + ../devices/wavfiles/wavfiles.cpp + ) + find_package(LibSndFile) + if (NOT LIBSNDFILE_FOUND) + message(FATAL_ERROR "please install libsndfile") + endif () + list(APPEND extraLibs ${LIBSNDFILE_LIBRARY}) + + add_definitions (-DHAVE_WAVFILES) + endif() ####################################################################### # # Here we really start diff --git a/example-2/main.cpp b/example-2/main.cpp index 07b6cad..55dfdb7 100644 --- a/example-2/main.cpp +++ b/example-2/main.cpp @@ -37,6 +37,8 @@ #include "airspy-handler.h" #elif HAVE_RTLSDR #include "rtlsdr-handler.h" +#elif HAVE_WAVFILES +#include "wavfiles.h" #endif #include @@ -160,6 +162,9 @@ int opt; struct sigaction sigact; bandHandler dabBand; deviceHandler *theDevice; +#ifdef HAVE_WAVFILES +std::string fileName; +#endif fprintf (stderr, "dab_cmdline,\n \ Copyright 2017 J van Katwijk, Lazy Chair Computing\n"); @@ -167,7 +172,11 @@ deviceHandler *theDevice; timesyncSet. store (false); run. store (false); +#ifndef HAVE_WAVFILES while ((opt = getopt (argc, argv, "M:B:C:P:G:A:L:S:Q")) != -1) { +#else + while ((opt = getopt (argc, argv, "M:B:P:A:L:S:F:")) != -1) { +#endif fprintf (stderr, "opt = %c\n", opt); switch (opt) { @@ -182,9 +191,6 @@ deviceHandler *theDevice; L_BAND : BAND_III; break; - case 'C': - theChannel = std::string (optarg); - break; case 'P': programName = optarg; @@ -193,6 +199,10 @@ deviceHandler *theDevice; case 'p': ppmCorrection = atoi (optarg); break; +#ifndef HAVE_WAVFILES + case 'C': + theChannel = std::string (optarg); + break; case 'G': theGain = atoi (optarg); @@ -201,6 +211,11 @@ deviceHandler *theDevice; case 'Q': autogain = true; break; +#else + case 'F': + fileName = std::string (optarg); + break; +#endif case 'A': soundChannel = optarg; @@ -248,6 +263,8 @@ deviceHandler *theDevice; ppmCorrection, theGain, autogain); +#elif HAVE_WAVFILES + theDevice = new wavFiles (fileName); #endif } catch (int e) {