Skip to content

Commit 0328a46

Browse files
authored
Add CPU Usage to the main panel in new layout (#339)
With a simple timer over available block calculation
1 parent 2ad4550 commit 0328a46

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/synth/synth.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ void Synth::setSampleRate(double sampleRate)
234234

235235
template <bool multiOut> void Synth::processInternal(const clap_output_events_t *outq)
236236
{
237+
auto start = std::chrono::high_resolution_clock::now();
237238
if (!SinTable::staticsInitialized)
238239
SinTable::initializeStatics();
239240

@@ -465,6 +466,10 @@ template <bool multiOut> void Synth::processInternal(const clap_output_events_t
465466

466467
AudioToUIMsg msg2{AudioToUIMsg::UPDATE_VOICE_COUNT, (uint32_t)voiceCount};
467468
audioToUi.push(msg2);
469+
470+
AudioToUIMsg msg3{AudioToUIMsg::UPDATE_CPU_USAGE, 0, (float)(cpuUsage * 100)};
471+
audioToUi.push(msg3);
472+
468473
lastVuUpdate = 0;
469474
}
470475
else
@@ -522,6 +527,18 @@ template <bool multiOut> void Synth::processInternal(const clap_output_events_t
522527
resampler[0]->renormalizePhases();
523528
}
524529
}
530+
531+
// Finish CPU calculation
532+
if (isEditorAttached)
533+
{
534+
auto end = std::chrono::high_resolution_clock::now();
535+
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
536+
auto micros = duration.count();
537+
auto availmicrosinv = hostSampleRate * 1e-9 / blockSize;
538+
auto pct = micros * availmicrosinv;
539+
auto cpuFac = 0.995;
540+
cpuUsage = cpuUsage * cpuFac + pct * (1 - cpuFac);
541+
}
525542
}
526543

527544
void Synth::process(const clap_output_events_t *o)

src/synth/synth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ struct Synth
336336
UPDATE_PARAM,
337337
UPDATE_VU,
338338
UPDATE_VOICE_COUNT,
339+
UPDATE_CPU_USAGE,
339340
SET_PATCH_NAME,
340341
SET_PATCH_DIRTY_STATE,
341342
DO_PARAM_RESCAN,
@@ -422,6 +423,7 @@ struct Synth
422423

423424
sst::basic_blocks::dsp::VUPeak vuPeak;
424425
std::array<sst::basic_blocks::dsp::VUPeak, numOps> opVuPeak;
426+
double cpuUsage{0};
425427
int32_t updateVuEvery{(int32_t)(48000 * 2.5 / 60 / blockSize)}; // approx
426428
int32_t lastVuUpdate{updateVuEvery};
427429

src/ui/settings-panel.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <sst/jucegui/components/NamedPanel.h>
2121
#include <sst/jucegui/components/ToggleButton.h>
2222
#include <sst/jucegui/component-adapters/DiscreteToReference.h>
23+
#include <fmt/core.h>
2324
#include "six-sines-editor.h"
2425

2526
namespace baconpaul::six_sines::ui
@@ -34,9 +35,19 @@ struct SettingsPanel : jcmp::NamedPanel, HasEditor
3435
void clearHighlight();
3536

3637
void setVoiceCount(int vc) { voiceCount->setText("Voices: " + std::to_string(vc)); }
38+
void setCpuUsage(double cpu)
39+
{
40+
if (std::round(cpu) != std::round(lastCpu))
41+
{
42+
cpuLabel->setText(fmt::format("CPU: {} %", std::round(cpu)));
43+
repaint();
44+
lastCpu = cpu;
45+
}
46+
}
3747

3848
std::unique_ptr<jcmp::Label> voiceCount;
3949
std::unique_ptr<jcmp::Label> cpuLabel;
50+
double lastCpu{-2000};
4051

4152
bool isPlayScreenShowing{false};
4253
bool suppressPowerOff{false};

src/ui/six-sines-editor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ void SixSinesEditor::idle()
287287
applyDawExtraStateFromAudio();
288288
}
289289
}
290+
else if (aum->action == Synth::AudioToUIMsg::UPDATE_CPU_USAGE)
291+
{
292+
settingsPanel->setCpuUsage(aum->value);
293+
}
290294
else
291295
{
292296
SXSNLOG("Ignored patch message " << aum->action);

0 commit comments

Comments
 (0)