dynamic string tuning

This commit is contained in:
2026-06-22 21:31:53 -05:00
parent b4df8657dd
commit 3e94d64f4a
7 changed files with 191 additions and 44 deletions

View File

@@ -13,6 +13,12 @@ PianoString::PianoString(ConfigService* config, LoggerService* logger) : Instrum
void PianoString::noteOn(float frequency, float velocity) {
logger_->log("Piano", LogFlag::Debug, "Note On");
active_ = true;
frequency_ = frequency;
damping_ = 0.5f;
rms_ = 5.0f;
recalculateConstants();
// resize the state vectors so that they are stable at the specified frequency
stringY_current_.resize(segmentCount_ + 1);
@@ -36,48 +42,31 @@ void PianoString::noteOn(float frequency, float velocity) {
stringY_current_[i] = stringY_current_[i] + dt_ * v0;
}
damping_ = 0.5f;
rms_ = 0.5f;
// recalculate based on change in damping
a1_ = 2.0f - 2.0f * damping_ * dt_;
a2_ = 2.0f * damping_ * dt_ - 1.0f;
}
void PianoString::noteOff() {
logger_->log("Piano", LogFlag::Debug, "Note Off");
damping_ = 10.0f;
// recalculate based on change in damping
a1_ = 2.0f - 2.0f * damping_ * dt_;
a2_ = 2.0f * damping_ * dt_ - 1.0f;
recalculateConstants();
}
bool PianoString::isActive() {
return (std::abs(rms_) > 0.001f);
if(active_ && (std::abs(rms_) < 0.0000001f)) {
active_ = false;
// reset state to stationary
for(size_t i = 0; i < segmentCount_ + 1; i++) {
stringY_current_[i] = 0.0f;
stringY_previous_[i] = 0.0f;
stringY_next_[i] = 0.0f;
}
}
return active_;
}
float PianoString::process(bool& scopeTrigger) {
/*
for n in range(2, N-2):
# stiff wave equation
y_xx = y_current[n+1] - 2*y_current[n] + y_current[n-1]
y_xxxx = y_current[n-2] - 4*y_current[n-1] + 6*y_current[n] - 4*y_current[n+1] + y_current[n+2]
y_next[n] = a1 * y_current[n] + a2 * y_last[n] + r2 * y_xx - s2 * y_xxxx
y_next[0] = 0
y_next[1] = 0
y_next[N-1] = 0
y_next[N-2] = 0
# y_sample[i] = math.tanh(y_next[n_sample])
y_sample[i] = y_next[n_sample]
y_last = y_current.copy()
y_current = y_next.copy()
*/
// simulate over string
for(size_t i = 2; i < segmentCount_ - 2; i++) {
float y_xx = stringY_current_[i-1] - 2.0f*stringY_current_[i] + stringY_current_[i+1];
@@ -92,10 +81,27 @@ float PianoString::process(bool& scopeTrigger) {
stringY_previous_ = stringY_current_;
stringY_current_ = stringY_next_;
float sampleOut = stringY_next_[static_cast<size_t>(samplePosition_*stringLength_*segmentCount_)];
float sampleOut = stringY_next_[static_cast<size_t>(samplePosition_*L_*segmentCount_)];
rms_ = 0.99f * rms_ + 0.01f * sampleOut*sampleOut;
return sampleOut;
return 5.0f * sampleOut;
}
}
void PianoString::recalculateConstants() {
L_ = stringLength_ * std::pow(1.005f, -frequency_ + 20.0f) + 0.5f;
waveVelocity_ = 1.0f * frequency_ * L_;
dx_ = L_ / static_cast<float>(segmentCount_);
dt_ = 1.0f / sampleRate_;
r1_ = waveVelocity_ * dt_/dx_;
r2_ = std::pow(waveVelocity_ * dt_/dx_, 2.0f);
s1_ = stiffness_ * dt_/std::pow(dx_, 2.0f);
s2_ = std::pow(stiffness_ * dt_/std::pow(dx_, 2.0f), 2.0f);
a1_ = 2.0f - 2.0f * damping_ * dt_;
a2_ = 2.0f * damping_ * dt_ - 1.0f;
}

View File

@@ -22,22 +22,26 @@ public:
private:
void recalculateConstants();
// states
float frequency_ = 0.0f;
std::vector<float> stringY_current_;
std::vector<float> stringY_previous_;
std::vector<float> stringY_next_;
std::vector<float> stringX_;
bool active_ = false;
// constants
// string parameters
size_t segmentCount_ = 30;
size_t segmentCount_ = 50;
static constexpr float rho_ = 8000.0f; // density, steel, kg/m^3
static constexpr float radius_ = 0.001f; // meters
static constexpr float stringTension_ = 1200.0f; // string tension, N
static constexpr float stiffness_ = 0.001f; // stiffness coefficient
static constexpr float stiffness_ = 0.000f; // stiffness coefficient
float damping_ = 0.5f; // damping coefficient
static constexpr float stringLength_ = 1.0f; // length of string
static constexpr float stringLength_ = 5.0f; // length of string at lowest frequency (20 hz)
static constexpr float strikePosition_ = 0.2f; // x of impulse location
static constexpr float impulseWidth_ = 0.02f; // x of impulse width
static constexpr float impulseVelocity_ = 10000.0f; // x/t of impulse magnitude
@@ -45,13 +49,12 @@ private:
float crossSectionalArea_ = pi * std::pow(radius_, 2.0f); // string cross sectional area, assuming circular
float mu_ = crossSectionalArea_ * rho_; // linear mass density
float waveVelocity_ = std::sqrt(stringTension_ / mu_); // transverse wave velocity
float L_ = 1.0f; // length of string at a given frequency
// eventually we'll have to dynamically tune our string according to the note that comes in
// an alternative is a fully built piano and then it calls voices under the instrument instead of how we do it currently
float f0_ = waveVelocity_ / (2.0f * stringLength_); // fundamental frequency of a non-stiff string
float f1_ = f0_ * std::sqrt(1.0f + stiffness_); // fundamental frequency of the stiff string
// f0_ = waveVelocity_ / (2.0f * stringLength_); // fundamental frequency of a non-stiff string
// f1_ = f0_ * std::sqrt(1.0f + stiffness_); // fundamental frequency of the stiff string
float dx_ = stringLength_ / static_cast<float>(segmentCount_);
float dx_ = L_ / static_cast<float>(segmentCount_);
float dt_ = 1.0f / sampleRate_;
// derived constants