diff --git a/source/device.cpp b/source/device.cpp index 469fcb1..6cdec13 100644 --- a/source/device.cpp +++ b/source/device.cpp @@ -9,6 +9,16 @@ * If not, see . */ +/** + * 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 drawSamplesBuf; -//static std::vector drawSamplesBuf2; +static std::vector drawSamplesBuf2; static std::ofstream logSamplesFile; static wav::clip wavOutput; static std::deque 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 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; diff --git a/source/stmdsp/stmdsp.cpp b/source/stmdsp/stmdsp.cpp index bb417ca..93c52dd 100644 --- a/source/stmdsp/stmdsp.cpp +++ b/source/stmdsp/stmdsp.cpp @@ -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() { diff --git a/source/stmdsp/stmdsp.hpp b/source/stmdsp/stmdsp.hpp index 8da98f2..0b9398e 100644 --- a/source/stmdsp/stmdsp.hpp +++ b/source/stmdsp/stmdsp.hpp @@ -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; };