Decent Volume Envelope Triggering & Generation
A lot of time and effort went into this but it's working about as well as I had hoped (sorry about the whining):
Measuring velocity by sampling the volume position number with a difference in time of 48kHz (one PCM period). Subtracting the two numbers as they pass the equivalent volume threshold of -48dB full scale gives the velocity, and we take the positive difference so it only triggers in one direction. Then some gain, and I found a rough square root following this helps to tame things and gives better control over the variable velocity. This is mixed back into the volume number and presented to a peak detector. The detector obviously replaces any smaller input numbers with larger ones, and this is used to retain the attack velocity amplitude while the following accumulator works its way up to it by successively adding the attack number. Once there, it successively subtracts the decay number until it reaches the input value, while also telling the peak detector to now follow its input. Retriggering is automatic because the peak detector also compares its value to the accumulator value.
It took me a while to set the right volume threshold at which to measure the velocity. -48dB is where all 4 LEDs on the bar graph are extinguished, and also very near the edge of audibility, which seems just about right. If you snap back just a tad after triggering it then you don't hear any constant tone, and if you do want constant tone after a trigger it's right there with very little additional movement needed. I added the square root about an hour ago and it seems to really help - there's probably a kinematics physics reason for it but I can't think of one right off.
Here's a quick video. The koto sound is obviously just a percussive female vocal sound. The chime sound is just a click (fast attack and decay of a sine wave) fed through the female formants set to really high Q. The drum sound is a sine wave. I should have included a reverse envelope, it does those pretty well too:
[UPDATE] Just improved the velocity detector. It takes the peak positive velocity over the -60dB to -48dB range and spits it out for a single cycle at the -48dB boundary. Negative velocities and positions outside this 30mm or so window (the way I currently have the volume sensitivity set, it's entirely adjustable) clear the peak and output 0. Seems more responsive and reliable now, lower velocities are easier to do with higher trigger gains, which means less arm movement is required.
In case it isn't clear from the above discussions, the volume processing pipe is rather passive and really messes with the general response times, and the envelope is generated by the velocity spike going down it. Unlike the pitch side, the volume side can take more of a hit in terms of absolute response time without too much trouble. At any rate, setting the trigger gain to 0 and the attack and decay values rather high makes it all transparent.
[EDIT] All my dB figures in this post were off by a factor of 2! They're fixed now. The volume LED bargraph on the prototype has 12dB per LED (which is a 4:1 ratio).