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;
3032namespace {
3133const 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-
6538Oscillator::~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
133150void 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++)
0 commit comments