2022-09-16 15:38:52 +00:00
|
|
|
#ifndef YAW_SCALE_INCLUDED
|
|
|
|
#define YAW_SCALE_INCLUDED
|
|
|
|
|
|
|
|
#include <vector>
|
2022-09-18 17:50:35 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
struct Note
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
double hz;
|
|
|
|
unsigned int number;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<std::string> defaultNoteNames = {
|
|
|
|
"A",
|
|
|
|
"A#",
|
|
|
|
"B",
|
|
|
|
"C",
|
|
|
|
"C#",
|
|
|
|
"D",
|
|
|
|
"D#",
|
|
|
|
"E",
|
|
|
|
"F",
|
|
|
|
"F#",
|
|
|
|
"G",
|
|
|
|
"G#"};
|
2022-09-16 15:38:52 +00:00
|
|
|
|
|
|
|
class Scale
|
|
|
|
{
|
|
|
|
double sampleRate = 48000.0;
|
|
|
|
|
|
|
|
// freqs in hz, periods in samples
|
|
|
|
std::vector<double> frequencies;
|
|
|
|
std::vector<double> periods;
|
2022-09-18 17:50:35 +00:00
|
|
|
std::vector<std::string> names;
|
2022-09-16 15:38:52 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void newSampleRate(double rate)
|
|
|
|
{
|
|
|
|
double ratio = rate / sampleRate;
|
|
|
|
sampleRate = rate;
|
|
|
|
for (double ¬e : periods)
|
|
|
|
note *= ratio;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Default ctor: 12TET @ 48kHz
|
|
|
|
Scale(double hz = 440.0)
|
|
|
|
{
|
2022-09-18 17:50:35 +00:00
|
|
|
hz /= 64.0;
|
2022-09-16 15:38:52 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 12 * 8; ++i)
|
|
|
|
{
|
|
|
|
frequencies.push_back(hz);
|
|
|
|
periods.push_back(sampleRate / hz);
|
2022-09-18 17:50:35 +00:00
|
|
|
names.push_back(defaultNoteNames[i % 12]);
|
2022-09-16 15:38:52 +00:00
|
|
|
hz *= exp2(1.0 / 12.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:50:35 +00:00
|
|
|
|
2022-09-16 15:38:52 +00:00
|
|
|
double getNearestPeriod(double period)
|
|
|
|
{
|
|
|
|
for (auto note : periods)
|
|
|
|
{
|
|
|
|
if (period > note)
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should NOT happen.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:50:35 +00:00
|
|
|
unsigned int getNearestNoteNumber(double hz)
|
|
|
|
{
|
|
|
|
for ( unsigned int i = 0; i < frequencies.size(); ++i )
|
|
|
|
{
|
|
|
|
if (hz < frequencies[i]) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double getNearestFrequency(double hz)
|
|
|
|
{
|
|
|
|
for (auto note : frequencies)
|
|
|
|
{
|
|
|
|
if (hz < note) return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-16 15:38:52 +00:00
|
|
|
// TODO: parse scala files. new ctor that parses arbitrary scales
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|