Most music models store pitch as one of 12 piano keys. A Thai scale, described as 7 equal steps, lands between those keys. The model moves 6 of its 7 notes, two of them by 43 cents.
Switch between the scale as played and the scale snapped to 12 keys, then press Play and listen. Then we build, piece by piece, the ruler for pitch and what a grid loses. Then the model that keeps it, and the choices about data, audience and release.
You need a little Python and what a probability is. No music theory. We build the rest from zero.
Based on Stepping Towards Transcultural Machine Learning in Music by Lamtharn (Hanoi) Hantrakul, Magenta, January 2021. The tuning, rhythm and dataset facts come from the sources named in each chapter.
The scale is the textbook model of Thai tuning, 7 equal steps of 171.4 cents each (Morton 1976). Measured instruments vary from it (Garzoli 2015), so treat these exact offsets as illustrative. The 12 keys are equal temperament.
Chapter 0
Run three melodies through a 12-key encoder and measure what each one loses
You hand a music model a recording of a Thai melody. It returns a clean transcription. Every note sits on a piano key. The transcription is wrong, and nothing in the output tells you so.
Lamtharn (Hanoi) Hantrakul names the cause in one line: "new technologies are often applied within the cultural boundaries of their inventors." He came to machine learning through wood. He apprenticed with luthiers in Thailand. Later he designed Fidular, a modular fiddle built from resonators and strings of Asia-Pacific and Middle Eastern fiddles. A tool built inside one tradition assumes that tradition, and the assumption is invisible to its makers.
Here is the assumption at its smallest. A 12-key encoder is a step that turns a pitch into the nearest piano key. Feed it three melodies. Count how often the encoder is within 10 cents of what was sung. A cent is one hundredth of a piano key's step; Chapter 1 builds it properly.
Teal is the pitch as sung. Warm is what the encoder stores. Red marks every moment more than 10 cents off. Pick a melody.
All three melodies are toys made for this page. The seven-step scale is the idealized model of Thai tuning; the sliding phrase is an invented glide, not a transcription of a real performance.
The worked number for the scale: its 8 notes (one octave, up) move by 0, 28.6, 42.9, 14.3, 14.3, 42.9, 28.6 and 0 cents. The mean is 171.4 / 8 = 21.4 cents. Only 2 of 8 notes, the first and the last, stay within 10 cents. The piano tune scores 8 of 8. Same encoder, same code, a different culture.
The failure. Symptom: a model that reports high accuracy on its test set and sounds wrong to players of another tradition. Measurement: snap that tradition's pitch to the model's grid and report the share of time within 10 cents. Fix: change the representation, which is the subject of this lesson.
Chapter 1
Turn any two frequencies into one distance that does not care which tradition you measure
Play A at 440 Hz. Play the A one octave up: 880 Hz. Now go up another octave: 1,760 Hz. Each octave sounds like the same step, but the gap in hertz doubles every time. The ear hears ratios, not differences. So a fair ruler for pitch must measure ratios.
The ruler music research uses is the cent. One octave is 1,200 cents. A piano key's step is 100 cents. Because it measures a ratio, it works for any tuning, piano or not.
How far apart are two pitches? Call the lower frequency f1 and the higher one f2, both in hertz. Call the distance between them in cents c.
c equals twelve hundred times log base two of f-two over f-one.
Divide f-two by f-one. Ask how many octaves that ratio is: that is the log base two. Multiply by 1,200 to get c in cents.
f-one is 440 Hz and f-two is 466.16 Hz, the next piano key up. The ratio is 1.0595, which is 1/12 of an octave, so c = 1200 × 0.0833 = 100.0 cents. Now try f-two = 660 Hz: the ratio is 1.5 and c = 702.0 cents, 2 cents above the key 700.
The lower note stays at 440 Hz. Drag the upper note and read where it lands on the octave. Warm ticks are the 12 keys.
The arithmetic is exact. 485.80 Hz is one step of the idealized seven-step scale above 440 Hz (440 × 2 to the 1/7).
The second chip lands at 171.4 cents. No key sits there: the nearest keys are 100 and 200. That gap is the topic of Chapter 2.
The failure. Symptom: two tunings compared in hertz look closer at low notes than at high notes. Measurement: convert both to cents against the same reference. Fix: store pitch in cents (or in log frequency) from the start, never in raw hertz differences.
Chapter 2
Build a scale of N equal steps and measure how far each note moves when you snap it to the piano
Every tradition picks a set of pitches inside the octave. Western music uses 12 equal steps. The essay puts the contrast plainly: "In Thai classical music, the same octave is divided into seven pitches."
How equal are the seven? The textbook account (Morton, 1976) describes 7 roughly equal steps. Garzoli (2015) measured instruments and argues they are not equidistant. So we use the 7-equal model as a toy. Either way, the seven are not a subset of the twelve.
What frequency is step k of a scale with N equal steps? Call the starting pitch f0, the step number k and the number of steps per octave N. Call the result fk.
f-k equals f-zero times two to the power k over N.
Each step multiplies the frequency by the same ratio, the N-th root of 2. Do it k times starting from f-zero. In cents, each step is 1,200 / N: 100 cents for N = 12, 171.4 cents for N = 7.
f-zero is 261.63 Hz and N is 7. Step k = 1 is 261.63 × 1.1041 = 288.86 Hz. Step k = 3 is 352.13 Hz. The piano's F (k = 5 of 12) is 349.23 Hz, so the two notes differ by 14.3 cents.
Now snap. Write a note's height above the start in cents as c. The 12-key encoder replaces c by the nearest multiple of 100. Call the amount it moves the note the snap error, e.
e equals c minus one hundred times round of c over one hundred.
Count c in piano steps. Round to the nearest key. Multiply back by 100 to get the key's height. The snap error e is the true height c minus that key's height.
Step 2 of the seven-step scale has c = 342.9. c / 100 = 3.429, which rounds to 3, so the key is 300 and e = +42.9 cents. Across the 6 steps between the octaves the errors are −28.6, +42.9, +14.3, −14.3, −42.9 and +28.6: a mean size of 28.6 cents.
The ring is one octave. Warm ticks are the 12 keys. Teal dots are the scale you pick. Turn on the snap to see each dot pulled to a key.
Equal-step scales here are idealized models. The 5-step option shows another equal division for contrast; it is not a model of any one tradition.
The failure. Symptom: a piano-roll melody model plays a Thai tune back with sour notes. Measurement: compute e for each scale step. Fix: store pitch in cents relative to the tonic, so the tuning is data, not code.
Chapter 3
Draw a sliding ornament and measure what a note-by-note model throws away
A Carnatic singer moves from one note to the next. The voice does not jump. It slides up, swings around the target and settles. The essay calls these slides meend, "melodic twists and turns". In Carnatic music the wider family of ornaments is called gamaka, and many gamakas are slides and oscillations between notes. To the singer they are part of the note, not decoration on top of it.
A note-by-note model stores "this key, then that key". To it a slide is noise. Draw one and watch.
Drag across the screen to draw a pitch curve from left to right. Purple is your curve. Turn on Quantize to see the staircase a key-based model stores.
The starting phrase is an invented slide with one swing, not a transcription of a real gamaka. Play sounds your curve, or its staircase when Quantize is on.
One number sums up the damage. Track pitch at T moments, called frames (a pitch tracker often uses 100 frames per second). At frame t the snap error is et, as in Chapter 2. Call the mean size of the error Ē.
E-bar equals one over T, times the sum over t of the absolute value of e-t.
At every frame, find how far the staircase is from the curve, e-t, and drop the sign. Add these over all T frames. Divide by T. That is E-bar.
A straight slide from 0 to 200 cents over T = 200 frames rises 1 cent per frame. The errors climb from 0 to 50 and fall back, twice. Their mean is E-bar = 25 cents, and only 20% of frames sit within 10 cents of a key.
The fix is in the representation. Store pitch as a continuous curve, one value in cents per frame. Tone Transfer does this. Its showcase, in the essay's words: it can "re-render a Western concert flute" to follow the meend of a Carnatic singer. The flute has no such tradition, but the model follows the curve the singer produced. Chapter 6 shows the model.
The failure. Symptom: a transcription of a Carnatic phrase shows extra short notes, or a flat line where the singer swung. Measurement: E-bar and the share of frames within 10 cents. Fix: keep a frame-level pitch curve and treat note labels as a later, optional view.
Chapter 4
Count where a melody spends its time, then change the bin width and watch the scale appear or vanish
Give a classifier a whole recording and ask "which scale is this?". It cannot read every frame. So engineers summarize: count how much time the pitch spends at each height. That count is a pitch histogram. Folding all octaves into one gives a pitch-class histogram.
The most common version has 12 bins, one per piano key. It is called a chroma feature. It is a good summary for piano-based music. For other music it bakes in the grid at the first step, before any learning starts.
Which bin does a pitch fall in? Call the pitch's height above the tonic c cents, the bin width w cents, and the number of bins per octave M = 1,200 / w. Call the bin number b.
b equals round of c over w, mod M.
Count c in units of w and round to the nearest bin. Take the remainder after dividing by M, so the same note in any octave lands in the same bin b. Add one frame's time to bin b.
A seven-step note at c = 342.9 and a piano note at c = 300. With w = 100 (M = 12) both land in bin 3: the histogram cannot tell them apart. With w = 10 (M = 120) they land in bins 34 and 30. A note at c = 1,250 with w = 10 goes to round(125) mod 120 = bin 5.
Bars show where a seven-step melody with slides spends its time, folded into one octave. Warm ticks are the 12 keys, teal ticks the 7 steps. Pick a bin width.
The page generates the melody: the idealized seven steps held with a little wobble, plus slides between them. Real pitch histograms of Thai or Carnatic recordings have different shapes.
At 100 cents the seven steps blur into piano bins. At 10 cents seven clear peaks stand at 171.4-cent spacing. Research on Indian art music uses fine histograms of this kind, built on the tonic, for tasks like raga recognition.
The failure. Symptom: a scale classifier confuses two ragas or two tunings that a musician tells apart at once. Measurement: plot the fine histogram of both and check whether their peaks share a 100-cent bin. Fix: use 10 to 20 cent bins relative to the tonic, not chroma.
Chapter 5
Lay a 4-beat bar over tala cycles of 8, 7 and 16 beats and count how long until they line up again
Pitch is one grid. Time is another. Much Western popular music counts in bars of 4 beats, and many beat trackers learn that from their data. Indian art music organizes time in a tala: a cycle of beats with its own grouping, repeated through a piece. The first beat of the cycle (sam in Hindustani music) is where phrases land.
| Cycle | Tradition | Beats | Grouping |
|---|---|---|---|
| Adi tala | Carnatic | 8 | 4 + 2 + 2 |
| Misra chapu | Carnatic | 7 | 3 + 2 + 2 |
| Teentaal | Hindustani | 16 | 4 + 4 + 4 + 4 |
When does a cycle's first beat fall on a bar's first beat again? Call the cycle length L beats and the bar length B beats. Call the number of cycles until they line up again R.
R equals the least common multiple of L and B, over L.
Find the first beat count that is a whole number of cycles and a whole number of bars. That is the least common multiple of L and B. Divide by L to get R, the cycles you wait.
Adi tala: L = 8, B = 4, lcm = 8, so R = 1. Every cycle starts on a bar line. Misra chapu: L = 7, lcm(7, 4) = 28, so R = 4. A 4-beat model puts the cycle's first beat on a bar line only once every 4 cycles.
Teal dots are the cycle's beats; the big one is the first beat. Warm rings mark where the 4-beat model puts a bar line. Press Play and watch the bar line drift.
The beat counts and groupings are the standard ones. Real performances also change tempo and subdivide beats, which this toy leaves out. Play sounds a click per beat, accented on the first beat and on each group start.
The failure. Symptom: a downbeat tracker marks "bar 1" in the middle of a Misra chapu group, and the error repeats with period 4 cycles. Measurement: compare predicted bar lines with annotated cycle starts. Fix: make the cycle length a label the model can learn, using data annotated with tala, such as the Saraga collections (Chapter 8).
Chapter 6
See why a model with a synthesizer inside can learn an instrument from 15 minutes of audio
Two walls stand in front of transcultural work. The representation must hold curves, which Chapters 2 to 5 showed. And the data is small: there are far fewer clean recordings of a Sarangi than of a pop guitar. A model that needs thousands of hours cannot serve a tradition that has a few.
DDSP (Differentiable Digital Signal Processing, Engel et al., 2020) answers both. Its input is a pitch curve and a loudness curve, frame by frame, with no key grid. A small network turns them into settings for a classic synthesizer. The synthesizer already knows what a periodic sound is, so the network learns only how one instrument colours it. The essay: "With as little as 15 minutes of data, you too can train your own model of lesser-known instruments." The DDSP paper's violin model used about 13 minutes of solo violin.
What does the synthesizer compute? Call the pitch f0 in hertz and the loudness A. Call the share of energy in harmonic k ck; the network predicts these. Call the sample rate fs, the sample number n, the number of harmonics K, and the output sample x[n].
x of n equals A times the sum over k from one to K of c-k times sine of two pi k f-zero n over f-s.
For each harmonic k, make a sine wave k times faster than f-zero. Weight it by c-k. Add the K waves and scale by A. Keep only harmonics below half of f-s, since the samples cannot hold higher ones.
f-zero is 440 Hz and f-s is 16,000 samples per second, so half the sample rate is 8,000 Hz. 8,000 / 440 = 18.2, so K = 18 harmonics fit. At f-zero = 220 Hz, K = 36.
Each bar is one harmonic c-k. The pitch slider moves continuously, with no keys. Brightness sets how fast the bars fall. Press Play to hear it.
The harmonic weights here follow a simple falling rule set by Brightness. In DDSP a network predicts them frame by frame from audio, and a filtered-noise part adds breath and bow noise.
Small models bring a second gain. They run in a browser on the phone. The audio need not leave the device.
The failure. Symptom: a large audio model trained on web data produces a generic "Indian flute" for any input. Measurement: count the clean hours you have per instrument before you pick a model family. Fix: if you have minutes, pick a model with a strong built-in structure, such as DDSP, over a model that learns sound from scratch.
Chapter 7
Follow one deployment through three choices and size its model for a phone
On India's 74th Independence Day, in August 2020, Google launched Sounds of India. You sang into a phone. The phone played your melody back as a Bansuri (bamboo flute), a Shehnai (double-reed pipe) or a Sarangi (bowed string instrument). The essay says the models reached "millions of mobile phones simultaneously." Three choices made it transcultural.
The engineering had to fit a phone. The TensorFlow write-up (Ramesh, Zats, Yu and Hantrakul, 2020) says the models ran in the browser with TensorFlow.js. No microphone audio went to a server. Compression cut one model download from 10 MB to 300 KB.
How long does a download take? Call the model size S in bytes and the connection rate r in bits per second. Call the time t.
t equals eight times S over r.
Turn the size S into bits by multiplying by 8. Divide by the rate r. The result is the wait t before the model can run.
With a made-up rate r of 1 megabit per second: S = 10 MB gives t = 8 × 10,000,000 / 1,000,000 = 80 seconds. S = 300 KB gives t = 2.4 seconds. The 33-fold cut turns a wait most people abandon into a pause.
Blue bars are download times for the two model sizes. Drag the connection speed down to a weak mobile signal.
The two sizes, 10 MB and 300 KB, are from the TensorFlow blog post on Sounds of India (August 2020). The connection speeds are illustrative.
The failure. Symptom: a tool for a tradition launches, and its own community finds it sounds wrong or cannot load it. Measurement: who set the quality bar, and how long the first load takes on a typical local phone. Fix: commission data from players, test with listeners who know the sound, and size the model for local devices.
Chapter 8
Turn the representation and the data mix separately and see that either one can cap the result
The essay borrows a split from Jesse Engel of Magenta. Model bias is the set of choices a researcher makes that encode properties of the data: a 12-key grid, a 4-beat bar, a chroma feature. Dataset bias comes from how the data was collected and what it contains: a corpus that is mostly Western pop. These choices are essential to make a model work, and they "bake in limitations by definition."
So no model is neutral. The job is to choose the bias on purpose and say what it is. The essay points to model cards, short documents that state what a model was built for and what it was trained on.
Pick how the model stores pitch, then drag the share of training audio from the target tradition. The weaker bar sets the ceiling.
An illustrative toy. "Can represent" is the share of frames within 10 cents of the grid, for the Chapter 0 scale and sliding phrase. "Ceiling equals the lower bar" is a teaching simplification, not a law.
The worked number: on the toy melodies, 33% of frames stay within 10 cents of a 12-key grid. A 24-step grid keeps 50%. A continuous curve keeps all. Raise the target data to 100% and the 12-key model still tops out at 33%. Switch to continuous with 5% data and the data caps it. You must fix both.
Some groups work on the data side. The CompMusic project (Music Technology Group, Universitat Pompeu Fabra, led by Xavier Serra) built research corpora for several non-Western traditions. Its successor Saraga (Srinivasamurthy et al., 2021) releases Carnatic and Hindustani recordings under Creative Commons licences, with time-aligned melody, rhythm and structure annotations.
The failure. Symptom: a team adds more Thai audio and accuracy on Thai melodies does not move. Measurement: test the representation alone, by snapping clean ground-truth pitch to the model's grid. Fix: if that already loses the music, change the model, not the data.
Chapter 9
Walk the release decision for a model that worked, and see why the team held it back
The guqin is a seven-string Chinese zither. UNESCO lists guqin music as Intangible Cultural Heritage and traces the instrument back more than 3,000 years. The team trained a DDSP model of it. Technically, the model worked.
Then Hantrakul played it for a Chinese colleague from a partner design team. In his words, "his facial reaction, a priceless mix of skepticism and aversion, made me reevaluate my position entirely." The worry: "non-Chinese listeners hearing the Guqin for the first time through our technology could form incorrect impressions" of the instrument and of Chinese classical music.
A model is a lossy copy. If it is many people's first guqin, its flaws become their picture of the art form. So the team decided not to open-source the Guqin models in Tone Transfer or in the DDSP Colab notebook. The essay's reason: "models are cultural distillations that should be treated with care and respect", like anthems, fabrics and symbols. Released without care, the same power could enable "a new form of AI-powered cultural appropriation."
Set the three choices the team faced. The meter shows the risk that a listener forms a wrong first impression of the tradition.
First listeners
Faithfulness, judged by players
Release
An illustrative toy, not a scoring method from the essay. It encodes the essay's argument: risk grows with first-time listeners, rough fidelity and wide release.
The worked case is the team's own. First-time listeners, a model its tradition's members found wrong, an open release: the meter sits at its top. They chose "keep in the lab". The same DDSP method shipped for the Bansuri, where the audience knew the sound and judged it.
The failure. Symptom: a released model becomes the default sound of a tradition for people outside it. Measurement: before release, play it to people from the tradition and record their verdict. Fix: if they object, add context, limit the release, or do not release.
Chapter 10
Keep the numbers, the checklist and the code in one place
The essay's goal: "Transcultural technologies empower cultural pluralism at every phase of engineering and design." That means the representation, the data, the audience and the release, not a sound borrowed at the end.
| Term | Meaning | Number to remember |
|---|---|---|
| Cent | A ratio unit of pitch distance | 1,200 per octave; 100 per piano key |
| Seven-step scale | Textbook model of Thai tuning | 171.4-cent steps; snap errors up to 42.9 cents |
| Snap error | How far a grid moves a note | Between −50 and +50 cents on 12 keys |
| Meend, gamaka | Slides and swings that are part of the note | A straight slide loses 25 cents on average |
| Chroma | 12-bin pitch-class histogram | Use 10 to 20 cent bins for other tunings |
| Tala | A repeating beat cycle | Adi 8, Misra chapu 7, Teentaal 16 |
| DDSP | Network plus built-in synthesizer | About 15 minutes of audio per instrument |
| Sounds of India | Bansuri, Shehnai, Sarangi in the browser | Download cut from 10 MB to 300 KB |
| Model bias, dataset bias | The model's choices, the data's mix | Either one caps the result |
pythonimport numpy as np from math import lcm def cents(f2, f1): # distance in cents, any tuning return 1200 * np.log2(f2 / f1) def snap_error(c, step=100): # c: array of cents above the tonic return c - step * np.round(c / step) def pitch_class_hist(c, w=10): # w=100 gives chroma M = 1200 // w b = np.round(c / w).astype(int) % M return np.bincount(b, minlength=M) / len(c) def cycles_to_realign(L, B=4): # tala of L beats vs bars of B return lcm(L, B) // L seven = 1200 * np.arange(8) / 7 # idealized seven-step scale e = snap_error(seven) # [0, -28.6, 42.9, 14.3, -14.3, -42.9, 28.6, 0] print(np.abs(e).mean(), (np.abs(e) <= 10).mean()) # 21.4 cents, 0.25 print(cycles_to_realign(7)) # 4 for Misra chapu
Now press Present or Teach and explain four things from memory. Why 7 equal steps do not fit 12 keys. What a slide loses on a grid. The two kinds of bias. Why the Guqin model stayed in the lab. Then go back to the hero and say which chapter fixes each moved note.