commit e784bd6972beeb64f19dea0edc8ed3c04cc6eb72
parent b9d0ef05a903b1089d2137e7033e7d1758492393
Author: Matt Demanett <matt@demanett.net>
Date: Sun, 30 Dec 2018 23:34:12 -0500
ANALYZER*: get rid of mutex synchronization with graphics thread.
Diffstat:
2 files changed, 20 insertions(+), 18 deletions(-)
diff --git a/src/analyzer_base.cpp b/src/analyzer_base.cpp
@@ -11,21 +11,13 @@ ChannelAnalyzer::~ChannelAnalyzer() {
_worker.join();
delete[] _workerBuf;
delete[] _stepBuf;
- if (_bins) {
- delete[] _bins;
- }
+ delete[] _bins0;
+ delete[] _bins1;
if (_averagedBins) {
delete _averagedBins;
}
}
-const float* ChannelAnalyzer::getBins() {
- if (_bins) {
- return _bins;
- }
- return _averagedBins->getAverages();
-}
-
float ChannelAnalyzer::getPeak() {
float max = 0.0;
float sum = 0.0;
@@ -75,14 +67,21 @@ void ChannelAnalyzer::work() {
_analyzer.process();
_analyzer.postProcess();
- if (_bins) {
- _analyzer.getMagnitudes(_bins, _binsN);
+ float* bins = _bins0;
+ if (_currentBins == _bins0) {
+ bins = _bins1;
}
- else {
+ if (_averagedBins) {
float* frame = _averagedBins->getInputFrame();
_analyzer.getMagnitudes(frame, _binsN);
_averagedBins->commitInputFrame();
+ const float* averages = _averagedBins->getAverages();
+ std::copy(averages, averages + _binsN, bins);
+ }
+ else {
+ _analyzer.getMagnitudes(bins, _binsN);
}
+ _currentBins = bins;
}
while (_workerBufReadI != _workerBufWriteI) {
@@ -201,8 +200,6 @@ void AnalyzerCore::stepChannel(int channelIndex, Input& input) {
void AnalyzerDisplay::draw(NVGcontext* vg) {
- std::lock_guard<std::mutex> lock(_module->_core._channelsMutex);
-
drawBackground(vg);
float strokeWidth = std::max(1.0f, 3 - gRackScene->zoomWidget->zoom);
_xAxisLogFactor = (_module->_rangeMaxHz - _module->_rangeMinHz) / _module->_rangeMaxHz;
diff --git a/src/analyzer_base.hpp b/src/analyzer_base.hpp
@@ -4,6 +4,7 @@
#include <thread>
#include <mutex>
#include <condition_variable>
+#include <atomic>
#include "bogaudio.hpp"
#include "dsp/analyzer.hpp"
@@ -15,7 +16,9 @@ namespace bogaudio {
struct ChannelAnalyzer {
SpectrumAnalyzer _analyzer;
int _binsN;
- float* _bins;
+ float* _bins0;
+ float* _bins1;
+ std::atomic<const float*> _currentBins;
AveragingBuffer<float>* _averagedBins;
const int _stepBufN;
float* _stepBuf;
@@ -39,7 +42,9 @@ struct ChannelAnalyzer {
)
: _analyzer(size, overlap, windowType, sampleRate, false)
, _binsN(size / binSize)
- , _bins(averageN == 1 ? new float[_binsN] {} : NULL)
+ , _bins0(new float[_binsN] {})
+ , _bins1(new float[_binsN] {})
+ , _currentBins(_bins0)
, _averagedBins(averageN == 1 ? NULL : new AveragingBuffer<float>(_binsN, averageN))
, _stepBufN(size / overlap)
, _stepBuf(new float[_stepBufN] {})
@@ -52,7 +57,7 @@ struct ChannelAnalyzer {
}
virtual ~ChannelAnalyzer();
- const float* getBins();
+ inline const float* getBins() { return _currentBins; }
float getPeak();
void step(float sample);
void work();