Monday, September 06, 2010

how RatioKey works

This is reposted (with edits) from the Tuning mailing list...

The code is divided into the user interface and an audio engine. They communicate via C structures that are passed back and forth between them, with the main item of interest in these structures being a pitch. That pitch could be expressed in cycles per second (Hz), but I have instead chosen to express it in terms of a proportional unit, which plugs directly into the sound generation code without conversion. That proportional unit needs some explanation.

To avoid having to calculate sine values while the audio hardware waits for input, I have set up a table of precalculated sines, which holds values for one complete cycle (2pi radians). The size of this table is one of the components of the proportional unit mentioned above. The other is the sample rate, which is 44100 samples per second, the same as a CD.

((table indices)/cycle) / (samples/second) ~ (table indices)/sample

and that's the proportional unit, (table indices)/sample. Given this quantity, the audio engine progresses from one sample to the next by adding this value to the previous value (resetting whenever it reaches the size of the table) and then uses the result it to look up the sine, which is what's passed to the hardware.

The user interface is composed of buttons, labeled with ratios, and associated with these values that are proportional to pitch. If the "1/1" frequency is changed, then all of the others are also changed.

The selection of ratios is a bit complicated. I begin with a permutation of every combination of powers of 2, 3, 5, 7, and 11, permitting the powers of 2 to vary from 1/32 to 32, the powers of 3 from 1/27 to 27, the powers of 5 from 1/25 to 25, the powers of 7 from 1/7 to 7, and the powers of 11 from 1/11 to 11. Fractional powers, like 1/32 and 1/25, appear as integers in the denominator of a ratio. The numerator and denominator are the products of their prime power components.

I then discard all combinations resulting in a ratio with a value greater than 2 or smaller than 1/2. Further I discard any combinations where either the numerator or the denominator is larger than 32.

There is a third test which may seem a bit arbitrary, but which I decided to use to make sure that I was winnowing out the more complicated ratios. For this test, each prime number is multiplied by the absolute value of its power (1/32 and 32 both yield 5, 5 times 2 is 10), and then these products are added together. If the sum of these is greater than 21, I also discard that ratio.

What's left after passing through these tests is what you find on the keyboard, arranged in ascending order from bottom to top, 105 distinct tones over two octaves.

Setting "1/1" is also a bit complicated. I use an arbitrary base frequency (which defaults to 11 Hz) and a picker to select powers of 2, 3, 5, 7, and 11, with the same limits as before, to produce a ratio which is resolved into a single quotient, which is then multiplied by the base frequency to produce the "1/1" frequency. This allows transposition by simply fiddling the prime powers to generate a different quotient.

The interface arrangement is far from ideal. It has no structure other than higher tones being placed closer to the top, and it's difficult to play. I've put a great deal of thought into how to fix this, and have some ideas that I hope to put into practice, but for the moment it is what it is, and, having published the app in this form, the current keyboard will have to remain as an option in any future version. My apologies to any who see gaping holes in my reasoning.

No comments:

Post a Comment