Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Created November 2, 2024 21:09
Show Gist options
  • Save Xenakios/390993913b01148600d7e85fbc1a59c4 to your computer and use it in GitHub Desktop.
Save Xenakios/390993913b01148600d7e85fbc1a59c4 to your computer and use it in GitHub Desktop.
#pragma once
#include "libMTSClient.h"
#include <cmath>
namespace mtsesp_wrapper
{
class MIDIKeyTuner
{
public:
MIDIKeyTuner() { client = MTS_RegisterClient(); }
~MIDIKeyTuner()
{
if (client)
MTS_DeregisterClient(client);
}
bool MTS_ESP_available() const noexcept { return client != nullptr; }
double noteToFrequency(char note, char channel = -1)
{
if (client && mtsEnabled)
return MTS_NoteToFrequency(client, note, channel);
return fallbackReferenceFrequency * std::pow(2.0, (note - 69) * (1.0 / 12.0));
}
void setMTSESP_Enabled(bool b) { mtsEnabled = b; }
void setFallbackA4Frequency(double hz) { fallbackReferenceFrequency = hz; }
// for access to the raw API if needed, obviously check for nullptr before using the result
MTSClient *getClient() { return client; }
// no copies or moves
MIDIKeyTuner(const MIDIKeyTuner &) = delete;
MIDIKeyTuner(MIDIKeyTuner &&) = delete;
MIDIKeyTuner &operator=(MIDIKeyTuner &other) = delete;
MIDIKeyTuner operator=(MIDIKeyTuner &&other) = delete;
private:
MTSClient *client = nullptr;
double fallbackReferenceFrequency = 440.0;
bool mtsEnabled = true;
};
} // namespace mtsesp_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment