Skip to content

Commit 0bdff17

Browse files
committed
WIP
1 parent b2b13a7 commit 0bdff17

File tree

11 files changed

+127
-53
lines changed

11 files changed

+127
-53
lines changed

app/demo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ void demo_cleanup()
215215
*/
216216

217217
spCanvas.reset();
218+
219+
spOsc.reset();
218220
}
219221
/*
220222

app/nodes/node_oscillator.cpp

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <zest/logger/logger.h>
99

10+
#include <zing/audio/audio.h>
11+
1012
#include <earlevel/el_wavetable.h>
1113
#include <earlevel/el_wavetable_utils.h>
1214

@@ -30,38 +32,9 @@ using namespace Zest;
3032
namespace {
3133
const int NumWaves = 4;
3234

33-
struct SetterWave : public ISliderCB
34-
{
35-
SliderValue myVal;
36-
virtual void UpdateSlider(Slider* pSlider, SliderOp op, SliderValue& val)
37-
{
38-
myVal.type = SliderType::Mark;
39-
myVal.step = 0.33f;
40-
if (op == SliderOp::Get)
41-
{
42-
myVal.name = pSlider->GetLabel();
43-
myVal.valueText = fmt::format("{:1.2f}", myVal.value);
44-
myVal.units = "dB";
45-
myVal.valueFlags = WidgetValueFlags::NoQuantization;
46-
val = myVal;
47-
}
48-
else
49-
{
50-
myVal = val;
51-
}
52-
}
53-
};
5435

5536
} // Namespace
5637

57-
struct AudioSettings
58-
{
59-
uint32_t sampleRate = 22000;
60-
sp_data* pSP = nullptr;
61-
};
62-
63-
AudioSettings maud;
64-
6538
Oscillator::~Oscillator()
6639
{
6740
// CleanUp();
@@ -81,18 +54,21 @@ void Oscillator::BuildNode(Canvas& canvas)
8154
sliderVal.step = 0.333f;
8255
sliderVal.type = SliderType::Mark;
8356

84-
auto spWaveSlider = std::make_shared<WaveSlider>("Wave", sliderVal);
85-
spWaveSlider->SetRect(NRectf(0.0f, 0.0f, 0.0f, 50.0f));
86-
spWaveSlider->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
57+
m_spWaveSlider = std::make_shared<WaveSlider>("Wave", sliderVal);
58+
m_spWaveSlider->SetRect(NRectf(0.0f, 0.0f, 0.0f, 50.0f));
59+
m_spWaveSlider->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
60+
m_spWaveSlider->AddValueUpdatedCB([=]() {
61+
UpdateWave();
62+
});
8763

88-
spRootLayout->AddChild(spWaveSlider);
64+
spRootLayout->AddChild(m_spWaveSlider);
8965

9066
// Keep same height, expand the width
9167
auto spCustom = std::make_shared<Widget>("Custom");
9268
spCustom->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
9369
spCustom->SetRect(NRectf(0.0f, 0.0f, 0.0f, 50.0f));
9470
spCustom->AddPostDrawCB([=](Canvas& canvas, const NRectf& rect) {
95-
spWaveSlider->DrawGeneratedWave(canvas, rect);
71+
m_spWaveSlider->DrawGeneratedWave(canvas, rect);
9672
});
9773
spRootLayout->AddChild(spCustom);
9874

@@ -128,6 +104,47 @@ void Oscillator::BuildNode(Canvas& canvas)
128104
spSocket->SetRect(NRectf(0.0f, 0.0f, 30.0f, 30.0f));
129105
spSocket->SetConstraints(glm::uvec2(LayoutConstraint::Preferred, LayoutConstraint::Expanding));
130106
spHorzLayout->AddChild(spSocket);
107+
108+
Reset();
109+
110+
UpdateWave();
111+
}
112+
113+
void Oscillator::UpdateWave()
114+
{
115+
auto& ctx = Zing::GetAudioContext();
116+
117+
sp_oscmorph2d* pOsc = nullptr;
118+
sp_oscmorph2d_create(&pOsc);
119+
120+
// Setup the oscillator
121+
sp_oscmorph2d_init(ctx.pSP, pOsc, &m_vecTables[0], NumWaves, m_numBandLimitedTables, &m_vecTableFrequencies[0], 0);
122+
pOsc->freq = 0;
123+
pOsc->amp = 0;
124+
pOsc->wtpos = 0;
125+
pOsc->enableBandlimit = 1;
126+
pOsc->bandlimitIndexOverride = -1;
127+
128+
SliderValue val;
129+
m_spWaveSlider->GetCB()->UpdateSlider(m_spWaveSlider.get(), SliderOp::Get, val);
130+
131+
pOsc->wtpos = val.value;
132+
133+
pOsc->amp = 1.0;
134+
pOsc->iphs = 0;
135+
136+
pOsc->freq = 100;
137+
138+
std::vector<float> wave;
139+
wave.resize(1000);
140+
for (size_t i = 0; i < wave.size(); i++)
141+
{
142+
sp_oscmorph2d_compute(ctx.pSP, pOsc, nullptr, &wave[i]);
143+
}
144+
145+
sp_oscmorph2d_destroy(&pOsc);
146+
147+
m_spWaveSlider->SetWave(wave);
131148
}
132149

133150
void Oscillator::CleanUp()
@@ -248,6 +265,8 @@ void Oscillator::Reset()
248265
{
249266
CleanUp();
250267

268+
auto& ctx = Zing::GetAudioContext();
269+
251270
int tableLen = 2048;
252271

253272
// The wave table to use
@@ -287,7 +306,7 @@ void Oscillator::Reset()
287306
m_vecTableFrequencies.resize(m_numBandLimitedTables);
288307
for (int table = 0; table < m_numBandLimitedTables; table++)
289308
{
290-
m_vecTableFrequencies[table] = float(osc->GetTables()[table].topFreq * maud.sampleRate);
309+
m_vecTableFrequencies[table] = float(osc->GetTables()[table].topFreq * ctx.outputState.sampleRate);
291310
}
292311
}
293312
// Ensure all have the same table size
@@ -306,7 +325,7 @@ void Oscillator::Reset()
306325

307326
// Create the wave table and copy in the data
308327
sp_ftbl* pTable = nullptr;
309-
sp_ftbl_create(maud.pSP, &pTable, sourceTable.waveTableLen);
328+
sp_ftbl_create(ctx.pSP, &pTable, sourceTable.waveTableLen);
310329

311330
// Create our wavetable for the oscillator
312331
for (size_t i = 0; i < sourceTable.waveTableLen; i++)

app/nodes/node_oscillator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace NodeGraph
1717
{
1818
class Node;
1919
class Canvas;
20+
class WaveSlider;
2021
}
2122

2223
class Oscillator
@@ -25,6 +26,7 @@ class Oscillator
2526
Oscillator(const std::string& strName, AudioUtils::WaveTableType type, float frequency = 440.0f, float phase = 0.0f);
2627
virtual ~Oscillator();
2728

29+
void UpdateWave();
2830
void Reset();
2931
void CleanUp();
3032
/*
@@ -69,5 +71,6 @@ class Oscillator
6971
int m_numBandLimitedTables = 0;
7072

7173
std::shared_ptr<NodeGraph::Node> m_spNode;
74+
std::shared_ptr<NodeGraph::WaveSlider> m_spWaveSlider;
7275
};
7376

include/nodegraph/theme.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,9 @@ DECLARE_THEME_SETTING_VALUE(b_debugShowLayout);
8383
DECLARE_THEME_SETTING_VALUE(c_socketColor);
8484
DECLARE_THEME_SETTING_VALUE(c_socketShadowColor);
8585

86+
// Wave slider
87+
DECLARE_THEME_SETTING_VALUE(c_waveSliderCenterColor);
88+
DECLARE_THEME_SETTING_VALUE(c_waveSliderBorderColor);
89+
8690
} // namespace Nodegraph
8791

include/nodegraph/widgets/widget.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ class TipTimer
8484

8585
~TipTimer()
8686
{
87-
ActiveTips.erase(m_pOwner);
87+
if (ActiveTips.find(m_pOwner) != ActiveTips.end())
88+
{
89+
ActiveTips.erase(m_pOwner);
90+
}
8891
}
8992
TipState Update()
9093
{
@@ -217,6 +220,7 @@ class Widget;
217220
using WidgetList = std::vector<std::shared_ptr<Widget>>;
218221

219222
using fnPostDraw = std::function<void(Canvas& canvas, const Zest::NRectf& hintRect)>;
223+
using fnValueUpdated = std::function<void()>;
220224
class Widget
221225
{
222226
public:
@@ -230,6 +234,7 @@ class Widget
230234

231235
// Post Draw
232236
virtual void AddPostDrawCB(const fnPostDraw& fnCB);
237+
virtual void AddValueUpdatedCB(const fnValueUpdated& fnCB);
233238

234239
virtual const Zest::NRectf& GetRect() const;
235240
virtual void SetRect(const Zest::NRectf& sz);
@@ -280,6 +285,8 @@ class Widget
280285

281286
void Visit(const std::function<void(Widget*)>& fnVisit);
282287

288+
void SendValueUpdated();
289+
283290
protected:
284291
Zest::NRectf m_rect;
285292
Widget* m_pParent = nullptr;
@@ -290,6 +297,7 @@ class Widget
290297
uint64_t m_flags = 0;
291298
glm::vec2 m_sizeHint = glm::vec2(0.0f);
292299
fnPostDraw m_postDrawCB;
300+
fnValueUpdated m_valueUpdatedCB;
293301
TipTimer m_tipTimer;
294302
};
295303

include/nodegraph/widgets/widget_slider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Slider : public Widget
5454

5555
virtual const NRectf& GetSliderRangeArea() const;
5656

57+
ISliderCB* GetCB() const { return m_pCB.get(); }
5758
protected:
5859
std::shared_ptr<ISliderCB> m_pCB;
5960
NRectf m_sliderRangeArea;

include/nodegraph/widgets/widget_waveslider.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class WaveSlider : public Slider
1818
WaveSlider(const std::string& label, const SliderValue& value);
1919
virtual void PostDraw(Canvas& canvas, const NRectf& rc);
2020
virtual void DrawGeneratedWave(Canvas& canvas, const NRectf& rc);
21+
22+
void SetWave(const std::vector<float>& vals);
23+
24+
private:
25+
std::vector<float> m_wave;
2126
};
2227

2328
}

settings.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ c_nodeTitleShadowColor = [ 0.19596540927886963, 0.19596540927886963, 0.195965409
1919
c_sliderBorderColor = [ 0.29971182346343994, 0.29971182346343994, 0.29971182346343994, 1.0 ]
2020
c_sliderCenterColor = [ 0.10662823915481567, 0.0845036432147026, 0.0845036432147026, 1.0 ]
2121
c_sliderShadowColor = [ 0.33717578649520874, 0.3264872133731842, 0.3264872133731842, 1.0 ]
22-
c_sliderThumbColor = [ 0.8443803787231445, 0.2920047342777252, 0.2920047342777252, 1.0 ]
22+
c_sliderThumbColor = [ 0.7608069181442261, 0.13374416530132294, 0.13374416530132294, 1.0 ]
2323
c_sliderThumbShadowColor = [ 0.5446685552597046, 0.4096786677837372, 0.4096786677837372, 1.0 ]
2424
c_sliderTipBorderColor = [ 0.7665705680847168, 0.7665705680847168, 0.7665705680847168, 1.0 ]
2525
c_sliderTipCenterColor = [ 0.23054754734039307, 0.23054754734039307, 0.23054754734039307, 1.0 ]
2626
c_sliderTipFontColor = [ 0.6791103482246399, 0.7655288577079773, 0.8386167287826538, 1.0 ]
2727
c_sliderTipShadowColor = [ 0.10951006412506104, 0.10951006412506104, 0.10951006412506104, 1.0 ]
2828
c_socketColor = [ 0.0, 0.0, 0.0, 0.0 ]
29+
c_waveSliderBorderColor = [ 1.0, 1.0, 1.0, 1.0 ]
30+
c_waveSliderCenterColor = [ 1.0, 1.0, 1.0, 0.0 ]
2931
s_gridLineSize = 2.0
3032
s_knobChannelGap = 3.0
3133
s_knobChannelWidth = 8.0
@@ -54,4 +56,4 @@ s_sliderTipBorderSize = 0.0
5456
s_sliderTipFontPad = 7.0
5557
s_sliderTipFontSize = 36.0
5658
s_sliderTipShadowSize = 4.0
57-
s_windowSize = [ 3840.0, 2224.0 ]
59+
s_windowSize = [ 3840.0, 2036.0 ]

src/widgets/widget.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ void Widget::AddPostDrawCB(const fnPostDraw& fnCB)
2424
m_postDrawCB = fnCB;
2525
}
2626

27+
void Widget::AddValueUpdatedCB(const fnValueUpdated& fnCB)
28+
{
29+
m_valueUpdatedCB = fnCB;
30+
}
31+
2732
Widget* Widget::GetParent() const
2833
{
2934
return m_pParent;
@@ -378,5 +383,13 @@ void Widget::DrawTip(Canvas& canvas, const glm::vec2& widgetTopCenter, const Wid
378383
fontSize);
379384
}
380385
}
386+
387+
void Widget::SendValueUpdated()
388+
{
389+
if (m_valueUpdatedCB)
390+
{
391+
m_valueUpdatedCB();
392+
}
393+
}
381394

382395
} // Nodegraph

src/widgets/widget_slider.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void DefaultSliderCB::UpdateSlider(Slider* pSlider, SliderOp op, SliderValue& va
3232
else
3333
{
3434
myVal = val;
35+
pSlider->SendValueUpdated();
3536
}
3637
}
3738

0 commit comments

Comments
 (0)