Skip to content

Commit c8cbde0

Browse files
committed
Make scale and tuning frequency global.
1 parent 10956d9 commit c8cbde0

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

Source/aeolus/engine.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class PrepareRankwaveJob : ThreadPoolJob
3333
: ThreadPoolJob(rw->getStopName())
3434
, _rankwave{rw}
3535
, _sampleRate{sampleRate}
36-
{
36+
37+
{
3738
}
3839

3940
JobStatus runJob() override
@@ -52,6 +53,8 @@ class PrepareRankwaveJob : ThreadPoolJob
5253

5354
EngineGlobal::EngineGlobal()
5455
: _rankwaves{}
56+
, _scale(Scale::EqualTemp)
57+
, _tuningFrequency(440.0f)
5558
{
5659
loadRankwaves();
5760
loadIRs();
@@ -108,6 +111,8 @@ void EngineGlobal::loadRankwaves()
108111
jassert(synth);
109112

110113
auto rankwave = std::make_unique<Rankwave>(*synth);
114+
rankwave->createPipes(_scale, _tuningFrequency);
115+
111116
auto* ptr = rankwave.get();
112117
_rankwaves.add(rankwave.release());
113118
_rankwavesByName.set(ptr->getStopName(), ptr);

Source/aeolus/engine.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "aeolus/globals.h"
2323
#include "aeolus/ringbuffer.h"
24+
#include "aeolus/scale.h"
2425
#include "aeolus/voice.h"
2526
#include "aeolus/addsynth.h"
2627
#include "aeolus/rankwave.h"
@@ -70,6 +71,12 @@ class EngineGlobal : public juce::DeletedAtShutdown
7071

7172
void updateStops(float sampleRate);
7273

74+
float getTuningFrequency() const noexcept { return _tuningFrequency; }
75+
void setTuningFrequenct(float f) noexcept { _tuningFrequency = f; }
76+
77+
const Scale& getScale() const noexcept { return _scale; }
78+
void setScaleType(Scale::Type type) noexcept { _scale.setType(type); }
79+
7380
JUCE_DECLARE_SINGLETON (EngineGlobal, false)
7481

7582
private:
@@ -84,6 +91,9 @@ class EngineGlobal : public juce::DeletedAtShutdown
8491

8592
std::vector<IR> _irs;
8693
int _longestIRLength; ///< Longest IR length in samples
94+
95+
Scale _scale;
96+
float _tuningFrequency;
8797
};
8898

8999
//==============================================================================

Source/aeolus/rankwave.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// ----------------------------------------------------------------------------
2020

2121
#include "rankwave.h"
22+
#include "engine.h"
2223

2324
using 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+
413431
void 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
}

Source/aeolus/rankwave.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class Pipewave
7474

7575
const Addsynth& getModel() const noexcept { return _model; }
7676

77+
// After changing the frequency of the pipe, the wavetable must be regenerated
78+
// by calling prepareToPlay() method.
79+
void setFrequency(float f) noexcept { _freq = f; }
80+
7781
int getNote() const noexcept { return _note + _model.getNoteMin(); }
7882
float getFreqency() const noexcept { return _freq; }
7983
float getPipeFrequency() const noexcept;
@@ -122,7 +126,12 @@ class Pipewave
122126
class Rankwave
123127
{
124128
public:
125-
Rankwave(Addsynth& model, const Scale& scale = Scale(Scale::EqualTemp));
129+
Rankwave(Addsynth& model);
130+
131+
void createPipes(const Scale& scale, float tuningFreq);
132+
133+
// Recalculate pipes tuning based on the current global scale and A4 frequency.
134+
void retunePipes(const Scale& scale, float tuningFreq);
126135

127136
juce::String getStopName() const { return _model.getStopName(); }
128137
bool isForNote(int note) const noexcept { return note >= _noteMin && note <= _noteMax; }
@@ -137,7 +146,6 @@ class Rankwave
137146
Addsynth& _model;
138147
int _noteMin;
139148
int _noteMax;
140-
Scale _scale;
141149

142150
juce::OwnedArray<Pipewave> _pipes;
143151
};

Source/aeolus/scale.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Scale
4848
using Map = std::map<Type, Table>;
4949

5050
Scale(Type type = EqualTemp);
51+
Type getType() const noexcept { return _type; }
52+
void setType(Type t) noexcept { _type = t; }
5153

5254
const Table& getTable() const;
5355

Source/ui/StopButton.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ void StopButton::paintButton (Graphics& g, bool shouldDrawButtonAsHighlighted, b
6565
}
6666

6767
g.setColour(color);
68-
g.fillEllipse(bounds.getX() + offset, bounds.getY() + offset,
69-
bounds.getWidth() - 8, bounds.getHeight() - 8);
68+
g.fillEllipse(float(bounds.getX() + offset), float(bounds.getY() + offset),
69+
float(bounds.getWidth() - 8), float(bounds.getHeight() - 8));
7070

7171
Colour textColour = _stop.getType() == aeolus::Stop::Type::Reed
7272
? (getToggleState() ? Colour(240, 40, 0) : Colour(128, 20, 0))

0 commit comments

Comments
 (0)