1919// ----------------------------------------------------------------------------
2020
2121#include " rankwave.h"
22+ #include " engine.h"
2223
2324using namespace juce ;
2425
@@ -193,7 +194,6 @@ void Pipewave::genwave()
193194{
194195 thread_local static Random rnd;
195196
196-
197197 const float sampleRate_r = 1 .0f / _sampleRate;
198198
199199 float m = _model.getNoteAttack (_note);
@@ -387,21 +387,24 @@ void Pipewave::attgain(float* att, int n, float p)
387387
388388// ==============================================================================
389389
390- Rankwave::Rankwave (Addsynth& model, const Scale& scale )
390+ Rankwave::Rankwave (Addsynth& model)
391391 : _model(model)
392392 , _noteMin(model.getNoteMin())
393393 , _noteMax(model.getNoteMax())
394- , _scale(scale)
395394 , _pipes{}
396395{
397396 jassert (_noteMax - _noteMin + 1 > 0 );
397+ }
398+
399+ void Rankwave::createPipes (const Scale& scale, float tuningFrequency)
400+ {
401+ _pipes.clear ();
398402
399403 const auto fn = _model.getFn ();
400404 const auto fd = _model.getFd ();
401- const auto & s = scale.getTable ();
402405
403- float fbase = 440 . 0f ;
404- fbase *= fn / (fd * s[9 ]);
406+ const auto & s = scale. getTable () ;
407+ float fbase = tuningFrequency * fn / (fd * s[9 ]);
405408
406409 for (int i = _noteMin; i <= _noteMax; ++i) {
407410 auto pipe = std::make_unique<Pipewave>(_model, i - _noteMin, ldexpf (fbase * s[i % 12 ], i/12 - 5 ));
@@ -410,6 +413,21 @@ Rankwave::Rankwave(Addsynth& model, const Scale& scale)
410413 }
411414}
412415
416+ void Rankwave::retunePipes (const Scale& scale, float tuningFrequency)
417+ {
418+ const auto fn = _model.getFn ();
419+ const auto fd = _model.getFd ();
420+
421+ const auto & s = scale.getTable ();
422+ float fbase = tuningFrequency * fn / (fd * s[9 ]);
423+
424+ for (int i = _noteMin; i <= _noteMax; ++i) {
425+ Pipewave* pipe = _pipes[i - _noteMin];
426+ pipe->setFrequency (ldexpf (fbase * s[i % 12 ], i/12 - 5 ));
427+
428+ }
429+ }
430+
413431void Rankwave::prepareToPlay (float sampleRate)
414432{
415433 for (auto * pipe : _pipes) {
@@ -422,6 +440,9 @@ Pipewave::State Rankwave::trigger(int note)
422440 if (note < _noteMin || note > _noteMax)
423441 return {};
424442
443+ int index = note - _noteMin;
444+ jassert (index >= 0 && index < (int )_pipes.size ());
445+
425446 Pipewave* pipe = _pipes[note - _noteMin];
426447 return pipe->trigger ();
427448}
0 commit comments