Skip to content

Commit 486d501

Browse files
committed
Fix stereo enhancement crash issue; add missing mutex lock
1 parent c504e53 commit 486d501

File tree

4 files changed

+13
-58
lines changed

4 files changed

+13
-58
lines changed

libjamesdsp/subtree/Main/libjamesdsp/jni/jamesdsp/jdsp/Effects/stereoEnhancement.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ void analysisWarpedPFBStereo(WarpedPFB *pfb1, WarpedPFB *pfb2, float *x1, float
99
void synthesisWarpedPFBStereo(WarpedPFB *pfb1, WarpedPFB *pfb2, float *y1, float *y2);
1010
void StereoEnhancementSetParam(JamesDSPLib *jdsp, float mix)
1111
{
12+
jdsp_lock(jdsp);
1213
jdsp->sterEnh.mix = mix;
1314
jdsp->sterEnh.minusMix = 1.0f - jdsp->sterEnh.mix;
1415
if (jdsp->sterEnh.mix > 0.5f)
@@ -36,17 +37,22 @@ void StereoEnhancementSetParam(JamesDSPLib *jdsp, float mix)
3637
float ms = 0.75f; // 0.75 ms
3738
for (unsigned int i = 0; i < 5; i++)
3839
jdsp->sterEnh.emaAlpha[i] = 1.0f - powf(10.0f, (log10f(0.5f) / (ms / 1000.0f) / (jdsp->fs / (float)Sk[i])));
40+
jdsp_unlock(jdsp);
3941
}
4042
void StereoEnhancementConstructor(JamesDSPLib *jdsp)
4143
{
44+
jdsp_lock(jdsp);
4245
jdsp->sterEnh.subband[1] = jdsp->sterEnh.subband[0] = 0;
46+
jdsp_unlock(jdsp);
4347
}
4448
void StereoEnhancementDestructor(JamesDSPLib *jdsp)
4549
{
50+
jdsp_lock(jdsp);
4651
if (jdsp->sterEnh.subband[0])
4752
free(jdsp->sterEnh.subband[0]);
4853
if (jdsp->sterEnh.subband[1])
4954
free(jdsp->sterEnh.subband[1]);
55+
jdsp_unlock(jdsp);
5056
}
5157
void StereoEnhancementEnable(JamesDSPLib *jdsp)
5258
{
@@ -88,4 +94,4 @@ void StereoEnhancementProcess(JamesDSPLib *jdsp, size_t n)
8894
jdsp->tmpBuffer[0][i] = y1 * snh->gain;
8995
jdsp->tmpBuffer[1][i] = y2 * snh->gain;
9096
}
91-
}
97+
}

src/audio/base/DspHost.cpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ inline void* GetStringForIndex(eel_string_context_state *st, float val, int32_t
5252
return (void*)&st->m_literal_strings[idx];
5353
}
5454

55-
DspHost::DspHost(void* dspPtr, MessageHandlerFunc&& extraHandler) : _extraFunc(std::move(extraHandler)), _stereoWideDebounce(new QTimer())
55+
DspHost::DspHost(void* dspPtr, MessageHandlerFunc&& extraHandler) : _extraFunc(std::move(extraHandler))
5656
{
5757
auto dsp = static_cast<JamesDSPLib*>(dspPtr);
5858
if(!dsp)
@@ -70,23 +70,10 @@ DspHost::DspHost(void* dspPtr, MessageHandlerFunc&& extraHandler) : _extraFunc(s
7070

7171
_dsp = dsp;
7272
_cache = new DspConfig();
73-
74-
75-
/* Workaround: calling StereoEnhancementSetParam while processing has a small chance of causing a crash inside analysisWarpedPFBStereo
76-
* due to the possiblity that snh->subband[x] is accessed before initWarpedPFB or assignPtrWarpedPFB is called by StereoEnhancementSetParam
77-
-> pausing processing not easily doable (in time); reduce strain on StereoEnhancementSetParam instead */
78-
/* TODO: replace workaround with proper patch */
79-
_stereoWideDebounce->setInterval(750);
80-
_stereoWideDebounce->setSingleShot(true);
81-
QObject::connect(_stereoWideDebounce, &QTimer::timeout, [this]{
82-
util::debug("-> Timer finished!");
83-
StereoEnhancementSetParam(cast(this->_dsp), _cache->get<float>(DspConfig::stereowide_level) / 100.0f);
84-
});
8573
}
8674

8775
DspHost::~DspHost()
8876
{
89-
_stereoWideDebounce->deleteLater();
9077
setStdOutHandler(NULL, NULL);
9178
}
9279

@@ -597,28 +584,14 @@ bool DspHost::update(DspConfig *config, bool ignoreCache)
597584
else
598585
StereoEnhancementDisable(cast(this->_dsp));
599586
break;
600-
case DspConfig::stereowide_level: {
601-
_stereoWideClock.tock();
602-
auto duration = _stereoWideClock.duration();
603-
_stereoWideClock.tick();
604-
util::debug(std::to_string(duration.count()));
605-
606-
if(duration.count() > 750 && !_stereoWideDebounce->isActive())
607-
{
608-
util::debug("StereoEnhancementSetParam");
609-
StereoEnhancementSetParam(cast(this->_dsp), _cache->get<float>(DspConfig::stereowide_level) / 100.0f);
610-
}
611-
else if(!_stereoWideDebounce->isActive())
587+
case DspConfig::stereowide_level:
588+
StereoEnhancementDisable(cast(this->_dsp));
589+
StereoEnhancementSetParam(cast(this->_dsp), _cache->get<float>(DspConfig::stereowide_level) / 100.0f);
590+
if(config->get<bool>(DspConfig::stereowide_enable))
612591
{
613-
util::debug("Timer start!");
614-
_stereoWideDebounce->start();
615-
}
616-
else
617-
{
618-
util::debug("Timer already running!");
592+
StereoEnhancementEnable(cast(this->_dsp));
619593
}
620594
break;
621-
}
622595
case DspConfig::tone_enable:
623596
if(current.toBool())
624597
FIREqualizerEnable(cast(this->_dsp));

src/audio/base/DspHost.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <any>
66
#include <list>
77
#include <memory>
8-
#include <chrono>
98

109
#include "EelVariable.h"
1110
#include "Utils.h"
@@ -46,8 +45,6 @@ class DspHost
4645
due to #defines and other global definitons that may conflict */
4746
void* _dsp; // JamesDSPLib*
4847
DspConfig* _cache;
49-
ClockMeasure<std::chrono::milliseconds, std::chrono::steady_clock> _stereoWideClock;
50-
QTimer* _stereoWideDebounce;
5148

5249
void updateLimiter(DspConfig *config);
5350
void updateFirEqualizer(DspConfig *config);

src/audio/base/Utils.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,6 @@
77
#include <map>
88
#include <chrono>
99

10-
template <class DT = std::chrono::milliseconds,
11-
class ClockT = std::chrono::steady_clock>
12-
class ClockMeasure
13-
{
14-
using timep_t = typename ClockT::time_point;
15-
timep_t _start = {}, _end = {};
16-
17-
public:
18-
void tick() {
19-
_end = timep_t{};
20-
_start = ClockT::now();
21-
}
22-
23-
void tock() { _end = ClockT::now(); }
24-
25-
template <class T = DT>
26-
auto duration() const {
27-
assert(_end != timep_t{} && "toc before reporting");
28-
return std::chrono::duration_cast<T>(_end - _start);
29-
}
30-
};
3110
namespace util {
3211

3312
const float minimum_db_level = -100.0F;

0 commit comments

Comments
 (0)