// generates audio using a mass-spring-damper system approximation // you'll notice that this approximation is simply an IIR filter // where we calculate the coefficients based on the spring system properties class MSDUGen extends UGen { // the variables of the system private float mass, springConstant, damping; // coefficients of the approximation private float coeff1, coeff2; // previous values private float[] Y; MSDUGen(float m, float k, float d) { mass = m; springConstant = k; damping = d; Y = new float[3]; // normalized initial condition Y[0] = 0.f; Y[1] = 1.0f; Y[2] = 0.f; // coefficients are calculated when sample rate is set } void sampleRateChanged() { float T = 1.f / sampleRate(); float denom = (mass + (T*damping) + (T*T*springConstant)); coeff1 = ((2.0f*mass) + (T*damping)) / denom; coeff2 = -mass / denom; } void uGenerate(float[] channels) { Y[0] = (Y[1] * coeff1) + (Y[2] * coeff2); Y[2] = Y[1]; Y[1] = Y[0]; float samp = Y[1] - Y[2]; for(int i = 0; i < channels.length; i++) { channels[i] = samp; } } }