Common file for handling arbitrary tunings.
This commit is contained in:
parent
846bdedf13
commit
1a6edd85a0
|
@ -0,0 +1,53 @@
|
|||
#ifndef YAW_SCALE_INCLUDED
|
||||
#define YAW_SCALE_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Scale
|
||||
{
|
||||
double sampleRate = 48000.0;
|
||||
|
||||
// freqs in hz, periods in samples
|
||||
std::vector<double> frequencies;
|
||||
std::vector<double> periods;
|
||||
|
||||
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)
|
||||
{
|
||||
hz /= 32.0;
|
||||
|
||||
for (int i = 0; i < 12 * 8; ++i)
|
||||
{
|
||||
frequencies.push_back(hz);
|
||||
periods.push_back(sampleRate / hz);
|
||||
hz *= exp2(1.0 / 12.0);
|
||||
}
|
||||
}
|
||||
|
||||
double getNearestPeriod(double period)
|
||||
{
|
||||
for (auto note : periods)
|
||||
{
|
||||
if (period > note)
|
||||
return note;
|
||||
}
|
||||
|
||||
// This should NOT happen.
|
||||
return 0;
|
||||
}
|
||||
|
||||
// TODO: parse scala files. new ctor that parses arbitrary scales
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue