1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/**
bespoke synth, a software modular synthesizer
Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
//
// Scale.h
// modularSynth
//
// Created by Ryan Challinor on 11/24/12.
//
//
#pragma once
#include "IDrawableModule.h"
#include "DropdownList.h"
#include "LFO.h"
#include "Chord.h"
#include "ChordDatabase.h"
#include <atomic>
class IScaleListener
{
public:
virtual ~IScaleListener() {}
virtual void OnScaleChanged() = 0;
};
struct Accidental
{
Accidental(int pitch, int direction)
: mPitch(pitch)
, mDirection(direction)
{}
int mPitch;
int mDirection;
};
inline bool operator==(const Accidental& lhs, const Accidental& rhs)
{
return lhs.mPitch == rhs.mPitch && lhs.mDirection == rhs.mDirection;
}
struct ScalePitches
{
int mScaleRoot{ 0 };
std::string mScaleType;
std::vector<int> mScalePitches[2]; //double-buffered to avoid thread safety issues when modifying
std::atomic<int> mScalePitchesFlip{ 0 };
std::vector<Accidental> mAccidentals;
void SetRoot(int root);
void SetScaleType(std::string type);
void SetAccidentals(const std::vector<Accidental>& accidentals);
const std::vector<int>& GetPitches() const { return mScalePitches[mScalePitchesFlip]; }
int ScaleRoot() const { return mScaleRoot; }
std::string GetType() const { return mScaleType; }
void GetChordDegreeAndAccidentals(const Chord& chord, int& degree, std::vector<Accidental>& accidentals) const;
int GetScalePitch(int index) const;
bool IsRoot(int pitch) const;
bool IsInPentatonic(int pitch) const;
bool IsInScale(int pitch) const;
int GetPitchFromTone(int n) const;
int GetToneFromPitch(int pitch) const;
int NumTonesInScale() const;
};
class MTSClient;
class Scale : public IDrawableModule, public IDropdownListener, public IFloatSliderListener, public IIntSliderListener, public ITextEntryListener, public IButtonListener
{
public:
Scale();
~Scale();
void Init() override;
void CreateUIControls() override;
bool IsSingleton() const override { return true; }
int MakeDiatonic(int pitch);
bool IsRoot(int pitch);
bool IsInPentatonic(int pitch);
bool IsInScale(int pitch);
int GetPitchFromTone(int n);
int GetToneFromPitch(int pitch);
void SetScale(int root, std::string type);
int ScaleRoot() { return mScale.mScaleRoot; }
std::string GetType() { return mScale.mScaleType; }
void SetRoot(int root, bool force = true);
void SetScaleType(std::string type, bool force = true);
void AddListener(IScaleListener* listener);
void RemoveListener(IScaleListener* listener);
void ClearListeners();
void Poll() override;
void SetScaleDegree(int degree);
int GetScaleDegree() { return mScaleDegree; }
void SetAccidentals(const std::vector<Accidental>& accidentals);
void GetChordDegreeAndAccidentals(const Chord& chord, int& degree, std::vector<Accidental>& accidentals);
ScalePitches& GetScalePitches() { return mScale; }
std::vector<int> GetPitchesForScale(std::string type);
void SetRandomSeptatonicScale();
int GetNumScaleTypes() { return (int)mScales.size(); }
std::string GetScaleName(int index) { return mScales[index].mName; }
int NumTonesInScale() const { return mScale.NumTonesInScale(); }
int GetPitchesPerOctave() const { return MAX(1, mPitchesPerOctave); }
float PitchToFreq(float pitch);
float FreqToPitch(float freq);
const ChordDatabase& GetChordDatabase() const { return mChordDatabase; }
void DropdownUpdated(DropdownList* list, int oldVal, double time) override;
void FloatSliderUpdated(FloatSlider* slider, float oldVal, double time) override;
void IntSliderUpdated(IntSlider* slider, int oldVal, double time) override;
void CheckboxUpdated(Checkbox* checkbox, double time) override;
void TextEntryComplete(TextEntry* entry) override;
void ButtonClicked(ClickButton* button, double time) override;
void LoadLayout(const ofxJSONElement& moduleInfo) override;
void SetUpFromSaveData() override;
void SaveState(FileStreamOut& out) override;
void LoadState(FileStreamIn& in, int rev) override;
int GetModuleSaveStateRev() const override { return 1; }
bool IsEnabled() const override { return true; }
private:
struct ScaleInfo
{
ScaleInfo() {}
ScaleInfo(std::string name, std::vector<int> pitches)
: mName(name)
, mPitches(pitches)
{}
std::string mName;
std::vector<int> mPitches;
};
//IDrawableModule
void DrawModule() override;
void GetModuleDimensions(float& width, float& height) override;
void NotifyListeners();
void SetUpRootList();
float RationalizeNumber(float input);
void UpdateTuningTable();
float GetTuningTableRatio(int semitonesFromCenter);
void SetRandomRootAndScale();
enum IntonationMode
{
kIntonation_Equal,
kIntonation_Just,
kIntonation_Pythagorean,
kIntonation_Meantone,
kIntonation_Rational,
kIntonation_SclFile,
kIntonation_Oddsound
};
ScalePitches mScale;
std::list<IScaleListener*> mListeners;
DropdownList* mRootSelector{ nullptr };
DropdownList* mScaleSelector{ nullptr };
IntSlider* mScaleDegreeSlider{ nullptr };
int mScaleDegree{ 0 };
ClickButton* mLoadSCLButton{ nullptr };
ClickButton* mLoadKBMButton{ nullptr };
ClickButton* mQueuedButtonPress{ nullptr };
std::vector<ScaleInfo> mScales;
int mNumSeptatonicScales{ 0 };
int mScaleIndex{ 0 };
int mPitchesPerOctave{ 12 };
float mReferenceFreq{ 440 };
float mReferencePitch{ 69 };
TextEntry* mPitchesPerOctaveEntry{ nullptr };
TextEntry* mReferenceFreqEntry{ nullptr };
TextEntry* mReferencePitchEntry{ nullptr };
IntonationMode mIntonation{ IntonationMode::kIntonation_Equal };
DropdownList* mIntonationSelector{ nullptr };
std::array<float, 256> mTuningTable{};
ChordDatabase mChordDatabase;
MTSClient* mOddsoundMTSClient{ nullptr };
std::string mSclContents;
std::string mKbmContents;
std::string mCustomScaleDescription;
bool mWantSetRandomRootAndScale{ false };
};
extern Scale* TheScale;
|