After three weeks of silence, there are now two, very helpful reviews of RatioKey on the App Store.

My thanks to the authors, especially with regard to the suggestion to use a Diamond Marimba interface (see also tonality diamond). I wish I'd known about that six months ago!

I can't talk about plans for future versions, if any, but such suggestions are sure to find their way onto my todo list, or, if they're already there, increase their priority. I have a lot of ideas for RatioKey or related applications, which are waiting on time, focus, and clarity (a clear notion of the larger context, which is largely about data formats). Given clarity, I can find focus and make time. Of course, anything I come up with must pass muster with Apple's reviewers, so I can't promise anything until it's actually available for download.

Let me say that I welcome competition. I'd hate to think that the potential of the iPad (and touchscreen devices in general) to help liberate us all from the equal tempered scale depended on the spare-time application of my own modest programming skills. RatioKey as it currently exists is, as much as anything, a vector for getting the idea of specifying ratios by adjusting their prime power components out into the noosphere. I hope to see and hear that come back in the form of new apps written by others.

Meanwhile, I'll be struggling with that clarity issue and whittling away at my todo list.

Please note that there's a lag between the completion of a new version and its availability on the App Store, even assuming that it's approved promptly, once reviewed, so if a new version were to appear within the next few days, it wouldn't reflect what's been discussed here. That will have to wait for later.

## Saturday, September 25, 2010

## 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.

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.

Subscribe to:
Posts (Atom)