19 Feb

clavichord 4: electrostatic charge

To simulate air molecules, I’ve added particles to the simulation. These are tiny light molecules that will bounce off the clavichord (once that’s simulated) so I can measure the vibration of the air molecules and extract a sound simulation from that.

simulation of 2000 particles

To make sure that the particles actually bounce and don’t just pass right through each other, it was necessary to add another force to the simulation, the electrostatic force. With this, I can give all particles a “charge” which will either attract or repel other particles.

To start with, I’ve set all particles to have a positive charge, which causes them all to try to stay away from each other.

To keep the particles within a defined area (so they dfon’t just fall down off-screen forever) I’ve also added a “boundary”, which simulates an invisible box which keeps everything inside it. The particles bounce off the walls, losing a little bit of velocity as they do so.

Doing this in JavaScript is tricky – with only 2,000 particles, the simulation slows down to a crawl. I may need to look into how WebASM works, or whether it would be quicker to use WebWorkers to run the physics in the background.

Demo with only 200 particles.

I think the next step is to move onto 3 dimensions.

14 Feb

clavichord 3: 2D mass-spring thing completed

So as part of my clavichord build, I’m trying to simulate the vibrations that happen within the instrument when a string is struck.

To do that, I’m building up a physical simulation from scratch. Over the last two days, I’ve gone from a 1-dimensional mass-spring simulation to a 2D one.

Yesterday’s work was to extend the math of the 1D version to handle 2D. Today’s work was to make sure that multiple nodes could be handled in a realistic fashion.

the demo

The hardest part for me so far was to figure out how to apply forces in defined directions in a realistically proportionate way. The example I read first (here) did not bother doing this, so the physics was incorrect.

Luckily, I remembered some trigonometry and was able to use Pythagoras’s theorem and some SOHCAHTOA usage to figure it out.

Springs in real life don’t just bounce for ever, so I applied some damping to the springs based on some example math I found based on Hooke’s spring law:

Relative velocity (va-vb in the above) was found by measuring the distance between the end points, measuring the distance between the end points after velocity in all dimensions are added, then simply subtracting one from the other

The end result is pretty.

Interesting aside – Pythagoras’s theorem works in not just 2 dimensions!

13 Feb

Clavichord 2: simple 2d mass-spring system

Today, I had hoped to have a 2D mass-spring system worked out, but it turns out the tutorial I was working from was flawed and overly simplistic, so I had to work out some math from first principles.

A 1-dimensional spring is simple – there are 2 points, the distance is simple to figure out, and when you figure out the forces (spring force, damping, gravity), it’s very obvious how to apply them in the single dimension available to you.

So, a 1-D mass-spring system is easy. Example here.

In 2 dimensions, things get a lot more tricky. Firstly, the distance between two points is no longer a simple subtraction ( 1d: z2-z1, 2d: sqrt((x2-x1)^2+(z2-z1)^2) ), but even worse, the forces being applied are proportional to the angles between the points.

So, today, I spent the most of the last few hours just making yesterday’s work easier to manage, and creating some “helper functions” to apply forces on nodes.

The example: 2d mass spring.

Tomorrow I’ll try extend the system a bit more so you can have a few nodes all linked together – like a chain or net. At the moment, the “spring” physics in the system only applies the spring force to one side of the link, so I’ll need to adjust that so the spring is applied to both. It will be interesting to see how two nodes hanging side-by side on a line behave.

12 Feb

Clavichord 1: 1d Mass Spring system

So today I’ve started on the clavichord project. But I haven’t touched a piece of wood yet.

The last time I built a clavichord, I got to the point where I was trying to build the sound-box and the bridge. I could not figure out the right shape for the bridge, and could not find any documentation online that explained why any particular shape was chosen.

The nearest I could come to it was descriptions of ribbing in guitars and how they affect tone.

But, because I like to know what I’m doing before I do it, I’ve decided to build a simulator that can simulate a clavichord’s sound-generation abilities without me needing to actually build it.

And if I build it right, I’ll be able to adjust parameters in the simulation (bridge positions, soundbox sizes, etc) dynamically to figure out the best possible parameters for what I want.

After thinking about it for a day or so, I think the right way to do this is with a mass-spring system, where the clavichord is represented as a massively dense system of nodes and springs, which allow me to apply virtual forces to things like strings and keys and see what happens.

What I hope is that the simulation will result in vibrations that I can translate into actual sound, so I can hear the simulated clavichord.

I’ve never done this before (has anyone?) so I’m starting from first principles.

The first principles in this case is a simple 1-dimensional mass-spring system, which I’ve built here. It simply simulates a weight hanging from an anchor, bobbing up and down on a spring until the bouncing is damped and the weight hangs still.

Tomorrow, I’ll work on building that into a 2D system. There is a very simply tutorial of 1D mass-spring systems here. Near the end, the tutor expands it to be 2D, but I don’t think it’s done correctly – doesn’t take into account diagonal stresses that trigonometry is required to solve.

I could not find a simple 2D tutorial, so will need to build that from first-principals tomorrow.