Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuning #253

Merged
merged 15 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions src/sfizz/Tuning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Tuning.h"
#include "Debug.h"
#include "absl/types/optional.h"
#include "Tunings.h" // Surge tuning library
#include <fstream>
#include <sstream>
Expand All @@ -25,10 +26,11 @@ struct Tuning::Impl {
int rootKey() const { return rootKey_; }
float tuningFrequency() const { return tuningFrequency_; }

void updateScale(const Tunings::Scale& scale);
void updateScale(const Tunings::Scale& scale, absl::optional<fs::path> sourceFile = {});
bool shouldReloadScala();
void updateRootKey(int rootKey);
void updateTuningFrequency(float tuningFrequency);

void reset();
private:
void updateKeysFractional12TET();
static Tunings::KeyboardMapping mappingFromParameters(int rootKey, float tuningFrequency);
Expand All @@ -44,25 +46,59 @@ struct Tuning::Impl {
mappingFromParameters(defaultRootKey, defaultTuningFrequency)
};

absl::optional<fs::path> scalaFile_;
fs::file_time_type modificationTime_ {};

static constexpr int numKeys = Tunings::Tuning::N;
static constexpr int keyOffset = 256; // Surge tuning has key range ±256
std::array<float, numKeys> keysFractional12TET_;
};

///
constexpr int Tuning::Impl::defaultRootKey;
constexpr float Tuning::Impl::defaultTuningFrequency;
constexpr int Tuning::Impl::numKeys;
void Tuning::Impl::reset()
{
rootKey_ = defaultRootKey;
tuningFrequency_ = defaultTuningFrequency;
tuning_ = Tunings::Tuning(
Tunings::evenTemperament12NoteScale(),
mappingFromParameters(defaultRootKey, defaultTuningFrequency)
);
scalaFile_.reset();
modificationTime_ = fs::file_time_type::min();
updateKeysFractional12TET();
}

float Tuning::Impl::getKeyFractional12TET(int midiKey) const
{
return keysFractional12TET_[std::max(0, std::min(numKeys - 1, midiKey + keyOffset))];
}

void Tuning::Impl::updateScale(const Tunings::Scale& scale)
void Tuning::Impl::updateScale(const Tunings::Scale& scale, absl::optional<fs::path> sourceFile)
{
tuning_ = Tunings::Tuning(scale, tuning_.keyboardMapping);
updateKeysFractional12TET();

if (sourceFile) {
std::error_code ec;
modificationTime_ = fs::last_write_time(*sourceFile, ec);
scalaFile_ = sourceFile;
paulfd marked this conversation as resolved.
Show resolved Hide resolved
}
}

bool Tuning::Impl::shouldReloadScala()
{
DBG("Should reload scala called");
if (!scalaFile_)
return false;

std::error_code ec;
const auto newTime = fs::last_write_time(*scalaFile_, ec);
if (newTime > modificationTime_) {
DBG("File changed!");
modificationTime_ = newTime;
return true;
}

return false;
}

void Tuning::Impl::updateRootKey(int rootKey)
Expand Down Expand Up @@ -142,7 +178,7 @@ bool Tuning::loadScalaFile(const fs::path& path)
return false;
}

impl_->updateScale(scl);
impl_->updateScale(scl, path);
return true;
}

Expand Down Expand Up @@ -200,4 +236,10 @@ float Tuning::getKeyFractional12TET(int midiKey)
return impl_->getKeyFractional12TET(midiKey);
}

bool Tuning::shouldReloadScala()
{
return impl_->shouldReloadScala();
}


} // namespace sfz
6 changes: 6 additions & 0 deletions src/sfizz/Tuning.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class Tuning {
*/
float getKeyFractional12TET(int midiKey);

/**
* @brief Check whether the underlying scala file has changed.
*
*/
bool shouldReloadScala();

private:
struct Impl;
std::unique_ptr<Impl> impl_;
Expand Down