summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVasile Vilvoiu <vasi@vilvoiu.ro>2023-05-06 20:40:52 +0300
committerVasile Vilvoiu <vasi@vilvoiu.ro>2023-05-06 20:40:52 +0300
commitf2f4792f824cd64e679cdc859fb008769541257b (patch)
treed2ee924bd8945dbf4112afa365aaa822dfea34bc /src
parent2ebb152342283a2237bc7ca2dd96baf3cac0d56e (diff)
Add support for waiting for input.
Introduce -S/--sleep_for_input command line argument. Closes #17.
Diffstat (limited to 'src')
-rw-r--r--src/configuration.cpp11
-rw-r--r--src/configuration.hpp2
-rw-r--r--src/specgram.cpp6
3 files changed, 19 insertions, 0 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp
index 7fdcdb5..88f3355 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -26,6 +26,7 @@ Configuration::Configuration()
this->datatype_ = DataType::kSignedInt16;
this->has_complex_input_ = false;
this->prescale_factor_ = 1.0f;
+ this->sleep_for_input_ = 0;
this->fft_width_ = 1024;
this->fft_stride_ = 1024;
@@ -169,6 +170,8 @@ Configuration::Build(int argc, const char **argv)
prescale(input_opts, "float", "Prescaling factor (default: 1.0)", {'p', "prescale"});
args::ValueFlag<int>
block_size(input_opts, "integer", "Block size when reading input, in data types (default: 256)", {'b', "block_size"});
+ args::ValueFlag<int>
+ sleep_for_input(input_opts, "integer", "Duration in milliseconds to sleep for when input is not available (default: 0, busywaits)", {'S', "sleep_for_input"});
args::Group fft_opts(parser, "FFT options:", args::Group::Validators::DontCare);
args::ValueFlag<int>
@@ -269,6 +272,14 @@ Configuration::Build(int argc, const char **argv)
conf.block_size_ = args::get(block_size);
}
}
+ if (sleep_for_input) {
+ if (args::get(sleep_for_input) < 0) {
+ std::cerr << "'sleep_for_input' must be zero or positive." << std::endl;
+ return std::make_tuple(conf, 1, true);
+ } else {
+ conf.sleep_for_input_ = args::get(sleep_for_input);
+ }
+ }
if (rate) {
if (args::get(rate) <= 0) {
std::cerr << "'rate' must be positive." << std::endl;
diff --git a/src/configuration.hpp b/src/configuration.hpp
index dec0e0c..42776ad 100644
--- a/src/configuration.hpp
+++ b/src/configuration.hpp
@@ -32,6 +32,7 @@ private:
DataType datatype_; /* input data type (does not cover complex/real discrimination) */
bool has_complex_input_; /* true if input is complex */
double prescale_factor_; /* value to scale input with before applying other transformations */
+ std::size_t sleep_for_input_; /* number of milliseconds to sleep when input is not ready */
std::size_t fft_width_; /* size of FFT window, in values */
std::size_t fft_stride_; /* stride of FFT window, in values */
@@ -110,6 +111,7 @@ public:
auto GetDataType() const { return datatype_; }
auto HasComplexInput() const { return has_complex_input_; }
auto GetPrescaleFactor() const { return prescale_factor_; }
+ auto GetSleepForInput() const { return sleep_for_input_; }
/* FFT getters */
auto GetFFTWidth() const { return fft_width_; }
diff --git a/src/specgram.cpp b/src/specgram.cpp
index 0cd9635..5e5e30c 100644
--- a/src/specgram.cpp
+++ b/src/specgram.cpp
@@ -21,6 +21,8 @@
#include <random>
#include <cstdio>
#include <cassert>
+#include <thread>
+#include <chrono>
/* main loop exit condition */
volatile bool main_loop_running = true;
@@ -237,6 +239,10 @@ main(int argc, char** argv)
auto block = reader->GetBlock();
if (!block) {
/* block not finished yet */
+ if (auto sleep = conf.GetSleepForInput()) {
+ /* sleep for a bit so we don't busywait on sparse input */
+ std::this_thread::sleep_for(std::chrono::duration<size_t, std::milli>(sleep));
+ }
continue;
}