From e27847c8c314b982b0585e578bb96097e16396de Mon Sep 17 00:00:00 2001
From: Marek Blok <Marek.Blok@pg.edu.pl>
Date: Fri, 10 Nov 2023 22:37:33 +0100
Subject: [PATCH] AdjustableDelay fixes and AdjustableDelay example in echo.cpp

---
 CHANGELOG               |  5 ++---
 examples/echo.cpp       | 33 +++++++++++++++++++++++----------
 src/cpp/DSP_modules.cpp |  6 +++---
 src/include/DSP_lib.h   |  2 +-
 4 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index af14309..75eda99 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,4 @@
 TODO::
-- Add example with test of DSP::u::AdjustableDelay
-  + output difference between standard and cyclic buffer to check if both variants work the same
 - DSP::u::Zeroinserter => add variant with possibility to define number of inputs
 
   SOUND_support_t::get_wave_in_raw_buffer - zastąpić metodą zawierającą konwersję typu, ew. rozbudować konwersję w DSP::u::AudioInput
@@ -12,8 +10,9 @@ TODO::
 LAST DONE:
 
 CHANGES:
-- ver. 0.20.030 <b>2023.11.10</b> Added:
+- ver. 0.20.031 <b>2023.11.10</b> Added:
   - DSP::u::AdjustableDelay - delay block with programaticly controlable delay (betwee 0 and user defined max_delay).
+  - examples: echo.cpp - added adjustable delay block use.
 - ver. 0.20.029 <b>2023.11.10</b> Added:
   - DSP::Block::DefineStandardInputs
   - DSP::Component::DefineStandardOutputs
diff --git a/examples/echo.cpp b/examples/echo.cpp
index f4c6342..3f959a9 100644
--- a/examples/echo.cpp
+++ b/examples/echo.cpp
@@ -1,14 +1,14 @@
 /*! Simple Digital Signal Processing Engine echo example.
  * \author Marek Blok
  * \date 2010.04.26
- * \date updated 2021.01.18
+ * \date updated 2023.11.10
  */
 #include <DSP_lib.h>
 
 int main(void)
 {
   DSP::Clock_ptr MasterClock;
-  int temp;
+  int loop_counter, final_counter;
   long int Fp;
 
   DSP::log.SetLogState(DSP::e::LogState::console | DSP::e::LogState::file);
@@ -24,8 +24,10 @@ int main(void)
   Fp = AudioIn.GetSamplingRate();
 
   DSP::u::Addition Add(2U);
-  DSP::u::LoopDelay Delay(MasterClock, Fp/2);
-  Delay.SetName("0.5s");
+  DSP::u::AdjustableDelay AdjustableDelay(1*Fp, 0); // delay up to 1 second starting from 0 seconds
+  AdjustableDelay.SetName("max 1 sec");
+  DSP::u::LoopDelay Delay(MasterClock, Fp/80); // basic delay - block needed for digital feedback loop
+  Delay.SetName("1/80 sec");
   DSP::u::Amplifier Scale(0.7);
   Scale.SetName("0.7");
 
@@ -33,7 +35,8 @@ int main(void)
 
   // Examples of connections
   AudioIn.Output("out") >> Add.Input("in1");
-  Add.Output("out") >> Delay.Input("in");
+  Add.Output("out") >> AdjustableDelay.Input("in");
+  AdjustableDelay.Output("out") >> Delay.Input("in");
   Delay.Output("out") >> Scale.Input("in");
   Scale.Output("out") >> Add.Input("in2");
   // Note the reversed connection below !!!
@@ -42,17 +45,27 @@ int main(void)
   DSP::Component::CheckInputsOfAllComponents();
   DSP::Clock::SchemeToDOTfile(MasterClock, "echo.dot");
 
-  temp=0;
+  bool use_const_delay = false;  // set to true for constant feedback lop delay
+  unsigned int actual_delay = 0;
+  loop_counter=0;
+  final_counter=0;
   do
   {
-    DSP::Clock::Execute(MasterClock, Fp/8);
+    DSP::Clock::Execute(MasterClock, Fp/16);
+    if (use_const_delay == true) {
+      actual_delay = AdjustableDelay.SetDelay(Fp/2); // set new delay - will not get larger then the max_delay setup on block construction
+    } 
+    else {
+      actual_delay = AdjustableDelay.SetDelay(loop_counter*Fp/100); // set new delay - will not get larger then the max_delay setup on block construction
+    }
 
-    DSP::log << "MAIN" << DSP::e::LogMode::second << temp << std::endl;
+    DSP::log << "MAIN" << DSP::e::LogMode::second << final_counter << ": actual_delay=" << actual_delay << " samples." << std::endl;
 
     if (AudioIn.GetBytesRead() == 0)
-      temp++;
+      final_counter++;
+    loop_counter++;
   }
-  while (temp < 30);
+  while (final_counter < 30);
 
   DSP::Clock::FreeClocks();
   DSP::log << "MAIN" << DSP::e::LogMode::second << "end" << std::endl << DSP::e::LogMode::Error << std::endl;
diff --git a/src/cpp/DSP_modules.cpp b/src/cpp/DSP_modules.cpp
index 08e097f..4666772 100644
--- a/src/cpp/DSP_modules.cpp
+++ b/src/cpp/DSP_modules.cpp
@@ -3631,9 +3631,9 @@ DSP::u::AdjustableDelay::AdjustableDelay(unsigned int max_delay_in, unsigned int
   std::vector<unsigned int> indexes;
 
   if (IsBufferCyclic == true)
-    SetName("Delay(cyclic)", false);
+    SetName("AdjustableDelay(cyclic)", false);
   else
-    SetName("Delay", false);
+    SetName("AdjustableDelay", false);
 
   if (InputsNo <= 0)
     InputsNo = 1;
@@ -3877,7 +3877,7 @@ void DSP::u::AdjustableDelay::InputExecute_with_cyclic_buffer(INPUT_EXECUTE_ARGS
 
     THIS->OutputBlocks[0]->EXECUTE_PTR(
         THIS->OutputBlocks[0], THIS->OutputBlocks_InputNo[0],
-        THIS->State[0][THIS->index[current_index]], block);
+        THIS->State[0][current_index], block);
   }
 
   // update buffer
diff --git a/src/include/DSP_lib.h b/src/include/DSP_lib.h
index 572213c..52cf42b 100644
--- a/src/include/DSP_lib.h
+++ b/src/include/DSP_lib.h
@@ -11,7 +11,7 @@
 
 #define DSP_VER_MAJOR 0
 #define DSP_VER_MINOR 20
-#define DSP_VER_BUILD 30 // !!! without zeroes before, else this will be treated as octal number
+#define DSP_VER_BUILD 31 // !!! without zeroes before, else this will be treated as octal number
 #define DSP_VER_YEAR  2023
 #define DSP_VER       DSP_VER_MAJOR.DSP_VER_MINOR.DSP_VER_BUILD
 
-- 
GitLab