]> code.bitgloo.com Git - clyne/stmdspgui.git/commitdiff
draw samples: set time frame
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 5 Oct 2021 23:59:20 +0000 (19:59 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 5 Oct 2021 23:59:20 +0000 (19:59 -0400)
source/device.cpp
source/stmdsp/stmdsp.cpp
source/stmdsp/stmdsp.hpp

index 469fcb1f82444c3163467a6cd06b42daacfc5f0b..6cdec13e50f243fc70e62030c01c3ee6e6211f52 100644 (file)
@@ -9,6 +9,16 @@
  * If not, see <https://www.gnu.org/licenses/>.
  */
 
+/**
+ * TODO list:
+ * - Test loading the signal generator with a formula.
+ * - Improve signal generator audio streaming.
+ * - Decide how to handle drawing samples at 96kS/s.
+ *    - May not be possible: USB streaming maxing out at ~80kS/s (1.25MB/s)
+ * - Draw input samples
+ * - Log samples
+ */
+
 #include "stmdsp.hpp"
 
 #include "imgui.h"
@@ -46,14 +56,6 @@ static const unsigned int sampleRateInts[6] = {
     48'000,
     96'000
 };
-static const unsigned int sampleRateBSizes[6] = {
-    8000,
-    16000,
-    20000,
-    32000,
-    48000,
-    96000,
-};
 
 static bool measureCodeTime = false;
 static bool drawSamples = false;
@@ -68,12 +70,13 @@ static bool popupRequestLog = false;
 
 static std::mutex mutexDrawSamples;
 //static std::vector<stmdsp::dacsample_t> drawSamplesBuf;
-//static std::vector<stmdsp::dacsample_t> drawSamplesBuf2;
+static std::vector<stmdsp::dacsample_t> drawSamplesBuf2;
 static std::ofstream logSamplesFile;
 static wav::clip wavOutput;
 
 static std::deque<stmdsp::dacsample_t> drawSamplesQueue;
-static unsigned int drawSamplesBufferSize = 4096;
+static double drawSamplesTimeframe = 1.0; // seconds
+static unsigned int drawSamplesBufferSize = 1;
 
 static void measureCodeTask(stmdsp::device *device)
 {
@@ -345,6 +348,22 @@ void deviceRenderDraw()
     if (popupRequestDraw) {
         ImGui::Begin("draw", &popupRequestDraw);
         ImGui::Checkbox("Draw input", &drawSamplesInput);
+        ImGui::SameLine();
+        ImGui::Text("|  time: %0.3f sec", drawSamplesTimeframe);
+        ImGui::SameLine();
+        if (ImGui::Button("-", {30, 0})) {
+            drawSamplesTimeframe = std::max(drawSamplesTimeframe - 0.25, 0.5);
+            auto sr = sampleRateInts[m_device->get_sample_rate()];
+            auto tf = drawSamplesTimeframe;
+            drawSamplesBufferSize = std::round(sr * tf);
+        }
+        ImGui::SameLine();
+        if (ImGui::Button("+", {30, 0})) {
+            drawSamplesTimeframe = std::min(drawSamplesTimeframe + 0.25, 30.);
+            auto sr = sampleRateInts[m_device->get_sample_rate()];
+            auto tf = drawSamplesTimeframe;
+            drawSamplesBufferSize = std::round(sr * tf);
+        }
 
         static std::vector<stmdsp::dacsample_t> buffer;
         static decltype(buffer.begin()) bufferCursor;
@@ -423,11 +442,6 @@ void deviceRenderMenu()
             deviceStart();
         }
 
-/**
-TODO test siggen formula
-TODO improve siggen audio streaming
-TODO draw: smoothly chain captures for 96kHz
- */
         if (ImGui::MenuItem("Upload algorithm", nullptr, false, isConnected))
             deviceAlgorithmUpload();
         if (ImGui::MenuItem("Unload algorithm", nullptr, false, isConnected))
@@ -503,7 +517,7 @@ void deviceRenderToolbar()
                         std::this_thread::sleep_for(std::chrono::milliseconds(10));
                     } while (m_device->get_sample_rate() != i);
 
-                    drawSamplesBufferSize = sampleRateBSizes[i];
+                    drawSamplesBufferSize = std::round(sampleRateInts[i] * drawSamplesTimeframe);
                 }
             }
         }
@@ -520,7 +534,7 @@ void deviceConnect()
             if (m_device->connected()) {
                 auto sri = m_device->get_sample_rate();
                 sampleRatePreview = sampleRateList[sri];
-                drawSamplesBufferSize = sampleRateBSizes[sri];
+                drawSamplesBufferSize = std::round(sampleRateInts[sri] * drawSamplesTimeframe);
                 log("Connected!");
             } else {
                 delete m_device;
index bb417ca459887b3951b13701c1a4ae46e59c4deb..93c52dd22ba986ba6bbcbf1eb020e8666e536ad1 100644 (file)
@@ -30,18 +30,18 @@ namespace stmdsp
         m_serial(file, 8'000'000/*230400*/, serial::Timeout::simpleTimeout(50))
     {
         if (m_serial.isOpen()) {
-                  m_serial.flush();
-           m_serial.write("i");
-           if (auto id = m_serial.read(7); id.starts_with("stmdsp")) {
+            m_serial.flush();
+            m_serial.write("i");
+            if (auto id = m_serial.read(7); id.starts_with("stmdsp")) {
                 if (id.back() == 'h')
                     m_platform = platform::H7;
                 else if (id.back() == 'l')
                     m_platform = platform::L4;
                 else
                     m_serial.close();
-           } else {
-               m_serial.close();
-           }
+            } else {
+                m_serial.close();
+            }
         }
     }
 
@@ -69,17 +69,18 @@ namespace stmdsp
     }
 
     unsigned int device::get_sample_rate() {
-        unsigned char result = 0xFF;
-
-        if (connected()) {
+        if (connected() && !is_running()) {
             uint8_t request[2] = {
                 'r', 0xFF
             };
             m_serial.write(request, 2);
+
+            unsigned char result = 0xFF;
             m_serial.read(&result, 1);
+            m_sample_rate = result;
         }
 
-        return result;
+        return m_sample_rate;
     }
 
     void device::continuous_start() {
index 8da98f246ec1e1f5931751d7589a2702592d3372..0b9398e59479f803f7d30e5685d648d96198321d 100644 (file)
@@ -90,6 +90,7 @@ namespace stmdsp
         serial::Serial m_serial;
         platform m_platform = platform::Unknown;
         unsigned int m_buffer_size = SAMPLES_MAX;
+        unsigned int m_sample_rate = 0;
         bool m_is_siggening = false;
         bool m_is_running = false;
     };