draw samples: set time frame

pull/1/head
Clyne 3 years ago
parent 9f48039620
commit a4e2375f04

@ -9,6 +9,16 @@
* If not, see <https://www.gnu.org/licenses/>. * 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 "stmdsp.hpp"
#include "imgui.h" #include "imgui.h"
@ -46,14 +56,6 @@ static const unsigned int sampleRateInts[6] = {
48'000, 48'000,
96'000 96'000
}; };
static const unsigned int sampleRateBSizes[6] = {
8000,
16000,
20000,
32000,
48000,
96000,
};
static bool measureCodeTime = false; static bool measureCodeTime = false;
static bool drawSamples = false; static bool drawSamples = false;
@ -68,12 +70,13 @@ static bool popupRequestLog = false;
static std::mutex mutexDrawSamples; static std::mutex mutexDrawSamples;
//static std::vector<stmdsp::dacsample_t> drawSamplesBuf; //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 std::ofstream logSamplesFile;
static wav::clip wavOutput; static wav::clip wavOutput;
static std::deque<stmdsp::dacsample_t> drawSamplesQueue; 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) static void measureCodeTask(stmdsp::device *device)
{ {
@ -345,6 +348,22 @@ void deviceRenderDraw()
if (popupRequestDraw) { if (popupRequestDraw) {
ImGui::Begin("draw", &popupRequestDraw); ImGui::Begin("draw", &popupRequestDraw);
ImGui::Checkbox("Draw input", &drawSamplesInput); 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 std::vector<stmdsp::dacsample_t> buffer;
static decltype(buffer.begin()) bufferCursor; static decltype(buffer.begin()) bufferCursor;
@ -423,11 +442,6 @@ void deviceRenderMenu()
deviceStart(); 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)) if (ImGui::MenuItem("Upload algorithm", nullptr, false, isConnected))
deviceAlgorithmUpload(); deviceAlgorithmUpload();
if (ImGui::MenuItem("Unload algorithm", nullptr, false, isConnected)) if (ImGui::MenuItem("Unload algorithm", nullptr, false, isConnected))
@ -503,7 +517,7 @@ void deviceRenderToolbar()
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (m_device->get_sample_rate() != i); } 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()) { if (m_device->connected()) {
auto sri = m_device->get_sample_rate(); auto sri = m_device->get_sample_rate();
sampleRatePreview = sampleRateList[sri]; sampleRatePreview = sampleRateList[sri];
drawSamplesBufferSize = sampleRateBSizes[sri]; drawSamplesBufferSize = std::round(sampleRateInts[sri] * drawSamplesTimeframe);
log("Connected!"); log("Connected!");
} else { } else {
delete m_device; delete m_device;

@ -30,18 +30,18 @@ namespace stmdsp
m_serial(file, 8'000'000/*230400*/, serial::Timeout::simpleTimeout(50)) m_serial(file, 8'000'000/*230400*/, serial::Timeout::simpleTimeout(50))
{ {
if (m_serial.isOpen()) { if (m_serial.isOpen()) {
m_serial.flush(); m_serial.flush();
m_serial.write("i"); m_serial.write("i");
if (auto id = m_serial.read(7); id.starts_with("stmdsp")) { if (auto id = m_serial.read(7); id.starts_with("stmdsp")) {
if (id.back() == 'h') if (id.back() == 'h')
m_platform = platform::H7; m_platform = platform::H7;
else if (id.back() == 'l') else if (id.back() == 'l')
m_platform = platform::L4; m_platform = platform::L4;
else else
m_serial.close(); m_serial.close();
} else { } else {
m_serial.close(); m_serial.close();
} }
} }
} }
@ -69,17 +69,18 @@ namespace stmdsp
} }
unsigned int device::get_sample_rate() { unsigned int device::get_sample_rate() {
unsigned char result = 0xFF; if (connected() && !is_running()) {
if (connected()) {
uint8_t request[2] = { uint8_t request[2] = {
'r', 0xFF 'r', 0xFF
}; };
m_serial.write(request, 2); m_serial.write(request, 2);
unsigned char result = 0xFF;
m_serial.read(&result, 1); m_serial.read(&result, 1);
m_sample_rate = result;
} }
return result; return m_sample_rate;
} }
void device::continuous_start() { void device::continuous_start() {

@ -90,6 +90,7 @@ namespace stmdsp
serial::Serial m_serial; serial::Serial m_serial;
platform m_platform = platform::Unknown; platform m_platform = platform::Unknown;
unsigned int m_buffer_size = SAMPLES_MAX; unsigned int m_buffer_size = SAMPLES_MAX;
unsigned int m_sample_rate = 0;
bool m_is_siggening = false; bool m_is_siggening = false;
bool m_is_running = false; bool m_is_running = false;
}; };

Loading…
Cancel
Save