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

@@ -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