selcal/src/main.cpp

108 lines
3.2 KiB
C++

#include "synth.h"
#include "code.h"
#include <iostream>
#include <map>
#include <magic_enum.hpp>
#include "midi.h"
#include <thread>
#include <chrono>
int main(int argc, char** argv) {
Synth synth;
for(int i = 1; i < argc; ++i) {
synth.loadSoundfont(argv[i]);
}
for (const Synth::Program& program : synth.getPrograms()) {
std::cout << program.name << " " << program.soundfontId
<< ":(" << program.bankNumber << ", " << program.programNumber << ")"
<< std::endl;
if (program.name == "Tuba") {
std::cout << "Loading program!" << std::endl;
synth.loadProgram(program);
}
}
std::map<SELCAL::Key, double> keyFrequencies = {
{SELCAL::Key::KEY_1, 680.0},
{SELCAL::Key::KEY_2, 754.2},
{SELCAL::Key::KEY_3, 836.6},
{SELCAL::Key::KEY_4, 927.9},
{SELCAL::Key::KEY_5, 1029.2},
{SELCAL::Key::KEY_6, 1141.6},
{SELCAL::Key::KEY_7, 1266.2},
{SELCAL::Key::KEY_8, 1404.4},
{SELCAL::Key::KEY_9, 1557.8},
{SELCAL::Key::KEY_A, 312.6},
{SELCAL::Key::KEY_B, 346.7},
{SELCAL::Key::KEY_C, 384.6},
{SELCAL::Key::KEY_D, 426.6},
{SELCAL::Key::KEY_E, 473.2},
{SELCAL::Key::KEY_F, 524.8},
{SELCAL::Key::KEY_G, 582.1},
{SELCAL::Key::KEY_H, 645.7},
{SELCAL::Key::KEY_J, 716.1},
{SELCAL::Key::KEY_K, 794.3},
{SELCAL::Key::KEY_L, 881.0},
{SELCAL::Key::KEY_M, 977.2},
{SELCAL::Key::KEY_P, 1083.9},
{SELCAL::Key::KEY_Q, 1202.3},
{SELCAL::Key::KEY_R, 1333.5},
{SELCAL::Key::KEY_S, 1479.1},
{SELCAL::Key::KEY_T, 329.2},
{SELCAL::Key::KEY_U, 365.2},
{SELCAL::Key::KEY_V, 405.0},
{SELCAL::Key::KEY_W, 449.3},
{SELCAL::Key::KEY_X, 498.3},
{SELCAL::Key::KEY_Y, 552.7},
{SELCAL::Key::KEY_Z, 613.1},
};
std::array<double, NUM_MIDI_NOTES> tunings;
tunings.fill(0);
for (auto pair : keyFrequencies) {
std::cout
<< magic_enum::enum_name(pair.first) << " = "
<< pair.second << " Hz "
<< " or MIDI key " << frequencyToMidi(pair.second)
<< std::endl;
// fluidsynth wants tunings in MIDI cents, which effectively means MIDI key * 100.
// As we only care about the SELCAL 32 tones, only tune those specific keys. We don't
// need to tune the full 128-key keyboard.
constexpr double MIDI_CENT_SCALE = 100.0;
const int keyIndex = static_cast<int>(pair.first);
tunings[keyIndex] = frequencyToMidi(pair.second * MIDI_CENT_SCALE);
}
bool success = synth.setTuning(tunings);
std::cout << "Set tuning: " << (success ? "success" : "failed") << std::endl;
fluid_synth_t* raw_synth = synth.getSynth();
using namespace std::chrono_literals;
int i, key;
for(i = 0; i < 12; i++)
{
/* Generate a random key */
key = 30 + (int)(12.0f * rand() / (float) RAND_MAX);
/* Play a note */
fluid_synth_noteon(raw_synth, 0, key, 100);
/* Sleep for 1 second */
std::this_thread::sleep_for(1s);
/* Stop the note */
fluid_synth_noteoff(raw_synth, 0, key);
}
}