Tone Tram

2023-10-02  (Updated 2024-07-10)    7 minute read

Tangentially related to aliqot, but not necessarily a sequel or anything.

Image of an ondes Martenot, a keyboard with a wire over the keybed, and a small

The ondes Martenot is an instrument made by some French guy. I like it because it is essentially a more tactile version of the theremin, where you can still make large sweeping slides in pitch and volume, but you get to feel how you're doing it. Some versions of the instrument even come with registrations bumps that let you feel where the pitches are.

I want to make a digital one.

The pitch wire

The main feature of the ondes Martenot is that the pitch is controlled by pulling a wire back and forth using a ring. I am far from the first to make a digital version, and as far as I can tell, the go-to approach is to use the wire to spin a wheel attached to an encoder or potentiometer:

I don't like this approach because it requires either a multi-turn potentiometer or a calibration sequence whenever the instrument is powered on to know exactly where the ring is.

My solution is to construct an absolute linear encoder using magnets and hall effect sensors.

de Bruijn sequence

The original idea for this project actually came from me, on a complete whim, wanting to know whether there was a shift equivalent to the Gray code. That is, in the same way that the Gray code is a series of unique values in a sequence that differ from their neighbors by one bit, was there an equivalent code where the values were shifted versions of their neighbors? The answer, of course, is yes, and it's called the de Bruijn sequence. The main benefit of such an encoding is that where I need N*M magnets in Gray Code, where N is the number of bits and M is the number of positions, I could just use one channel of M magnets. This saves a lot of money in magnets and a lot of labor in putting things together, plus its much easier to design and produce around one row than N rows.

It turns out that the 2 bit case is the same for both Gray code and de Bruijn sequence, so as an example, lets consider the three bit case:

Gray code:
a ⬜ ⬜ ⬜
b ⬜ ⬜ 🟫
c ⬜ 🟫 🟫
d ⬜ 🟫 ⬜
e 🟫 🟫 ⬜
f 🟫 🟫 🟫
g 🟫 ⬜ 🟫
h 🟫 ⬜ ⬜
a ⬜ ⬜ ⬜
Each row differs by the next row by one bit
de Bruijn sequence:
a ⬜ ⬜ ⬜
b ⬜ ⬜ 🟫
c ⬜ 🟫 🟫
d 🟫 🟫 🟫
e 🟫 🟫 ⬜
f 🟫 ⬜ 🟫
g ⬜ 🟫 ⬜
h 🟫 ⬜ ⬜
a ⬜ ⬜ ⬜
The last two bits of each row is the same as the first two bits of the next

One way to visualize the de Bruijn sequence to make the shifting relationship clearer is to visualize the value in the context of the rest of the sequence (similar to what is done on the Wikipedia page):

a [⬜ ⬜ ⬜]🟫 🟫 🟫 ⬜ 🟫
b [⬜ ⬜ 🟫]🟫 🟫 ⬜ 🟫 ⬜
c [⬜ 🟫 🟫]🟫 ⬜ 🟫 ⬜ ⬜
d [🟫 🟫 🟫]⬜ 🟫 ⬜ ⬜ ⬜
e [🟫 🟫 ⬜]🟫 ⬜ ⬜ ⬜ 🟫
f [🟫 ⬜ 🟫]⬜ ⬜ ⬜ 🟫 🟫
g [⬜ 🟫 ⬜]⬜ ⬜ 🟫 🟫 🟫
h [🟫 ⬜ ⬜]⬜ 🟫 🟫 🟫 ⬜
a [⬜ ⬜ ⬜]🟫 🟫 🟫 ⬜ 🟫 (repeats)

Hopefully this makes it clearer that each row is a (cyclically) left-shifted version of the previous row. Given the sequence ⬜ ⬜ ⬜ 🟫 🟫 🟫 ⬜ 🟫 , we could use a row of magnets arranged with one pole representing zero and the other representing one, and read any three adjacent magnets to know exactly where along the row we are, and which three magnets we are reading.

If the hall effect sensors are analog, then we could potentially get an additional 12 bits of precision between magnets (depending on the ADC, minus noise, and assuming magnets have exactly equal magnetic flux).

Optional technical caveat: I say "potentially" because I am assuming that the in-between values are unique. For example, an analog read halfway between the first two values ⬜ ⬜ ⬜ and ⬜ ⬜ 🟫 would read ⬜ ⬜ 🔴 , where 🔴 is the analog value halfway between and 🟫 . If we turn on the instrument in that position and it knows nothing about the previous context, we can only know exactly where we are if ⬜ ⬜ 🔴 is a unique value. One way this could not be true is for the value ⬜ 🔴 🔴 , if there existed adjacent values ⬜ ⬜ 🟫 and ⬜ 🟫 ⬜ as well as ⬜ 🟫 🟫 and ⬜ ⬜ ⬜ . In this case, we know this isn't possible since the shifting property does not allow ⬜ 🟫 🟫 to be next to ⬜ ⬜ ⬜ . I believe it is the case for every situation like this that there is only one valid pair of adjacent values, but I'm no mathematician.

Even with all the inaccuracies, I'm not aware of any linear position sensing mechanisms with that level of precision. In addition, because none of the sensing components contact each other, the wear over time is greatly lower than, for example, a linear potentiometer, where a wiper has to slide back and forth across a thin carbon strip.

According to my limited research, applying this concept to sensing is not an uncommon practice, but much of the literature discussing it is paywalled.

construction

TODO

  • hall effect sensors
  • gluing magnets is a pain
  • modular connector that works with my cowbell pcb
  • de bruijn sequences
    • aren't unique
    • how to decode position?
    • if there's one thing I've learned in the last thousand revisions of this section, it's that these pattern explanations are in dire need of interactive visualization
    • implement footnotes so I can mention that you can extend the sequence to: ⬜ ⬜ ⬜ 🟫 🟫 🟫 ⬜ 🟫 ⬜ ⬜

Further reading