Skip to content

scales.py

The file math/src/tonnetz/scales.py. 92 lines, 5 definitions.

Makam scales: the pitch set, the chords it contains and its shape on the network.

Code, comments and printed output are in Turkish. The explanations are translated.

Top of file: imports

"""Makam dizileri: perde kümesi, içerdiği akorlar, ağ üzerindeki şekil."""from __future__ import annotationsfrom .chain import chain_indexfrom .core import Tonnetz

INTERVALS_53

constant
# Aralık simgeleri (53-TET'te koma): brif 3.1. A: artık ikili.INTERVALS_53 = {"F": 1, "B": 4, "S": 5, "K": 8, "T": 9, "A": 12}

MAKAMS_53

constant
# [VARSAYIM] V2: kaynaktan doğrulanacak. Brif 4.4.MAKAMS_53 = {    "cargah": {"name": "Çargâh", "steps": "T T B T T T B"},    "buselik": {"name": "Bûselik", "steps": "T B T T B T T"},    "kurdi": {"name": "Kürdî", "steps": "B T T T B T T"},    "rast": {"name": "Rast", "steps": "T K S T T K S"},    "ussak": {"name": "Uşşak", "steps": "K S T T B T T"},    "hicaz": {"name": "Hicaz", "steps": "S A S T B T T"},}

parse_steps

function

Turns interval symbols into step counts: "T T B" -> [9, 9, 4].

def parse_steps(symbols: str, table: dict[str, int] = INTERVALS_53) -> list[int]:    """Aralık simgelerini adım sayılarına çevirir: "T T B" -> [9, 9, 4]."""    return [table[s] for s in symbols.split()]

scale

function

Gives the pitches of the scale by adding up the steps from the tonic.

The steps must add up to N (the scale closes at the octave). The octave pitch is not repeated.

def scale(t: Tonnetz, tonic: int, steps: list[int]) -> list[int]:    """Karar perdesinden başlayıp adımları toplayarak dizinin perdelerini verir.    Adımların toplamı N olmalıdır (dizi oktavda kapanır). Oktav perdesi yinelenmez.    """    if sum(steps) != t.N:        raise ValueError(f"Adımların toplamı {sum(steps)}, N={t.N} olmalı.")    out, x = [], tonic    for s in steps:        out.append(x % t.N)        x += s    return out

canonical_shape

function

The canonical form of a set under translation (x -> x + c) and reflection (x -> -x).

Both are symmetries of the Tonnetz graph. Reflection swaps major and minor. Two sets with the same canonical form give the same shape on the network.

def canonical_shape(t: Tonnetz, pitch_set) -> tuple[int, ...]:    """Kümenin öteleme (x -> x + c) ve yansıma (x -> -x) altındaki kanonik biçimi.    İkisi de Tonnetz grafının simetrisidir. Yansıma majör ile minörü yer değiştirir.    Aynı kanonik biçime sahip iki küme ağ üzerinde aynı şekli verir.    """    N = t.N    s = {p % N for p in pitch_set}    best = None    for sign in (1, -1):        for p in s:            cand = tuple(sorted((sign * (q - p)) % N for q in s))            if best is None or cand < best:                best = cand    return best

is_translate

function

Is the set s2 a translate of the set s1?

def is_translate(t: Tonnetz, s1, s2) -> bool:    """s2 kümesi, s1 kümesinin bir ötelemesi mi?"""    N = t.N    a, b = {p % N for p in s1}, {p % N for p in s2}    return any({(p + c) % N for p in a} == b for c in range(N))

analyze

function

The analysis of a scale on the network.

Gives its pitches, the chords and edges it contains, the connected components, the length of the arc it covers in the chain of fifths and its canonical form.

def analyze(t: Tonnetz, tonic: int, steps: list[int]) -> dict:    """Bir dizinin ağ üzerindeki çözümlemesi.    Perdeleri, içerdiği akorları ve kenarları, bağlantılı bileşenleri, beşli zincirinde    kapladığı yayın uzunluğunu ve kanonik biçimini verir.    """    pitches = scale(t, tonic, steps)    chords = t.chords_within(pitches)    ks = [chain_index(t, p) for p in pitches]    # Zincirde kapladığı en kısa yay (dairesel): N - en büyük boşluk + 1 perde.    sk = sorted(ks)    gaps = [(sk[(i + 1) % len(sk)] - sk[i]) % t.N for i in range(len(sk))]    return {        "tonic": tonic % t.N,        "steps": steps,        "pitches": pitches,        "chain_indices": ks,        "chain_arc_length": t.N - max(gaps) + 1,        "chords": [{"kind": c.kind, "root": c.root} for c in chords],        "major_count": sum(c.kind == "M" for c in chords),        "minor_count": sum(c.kind == "m" for c in chords),        "edges": [list(e) for e in t.edges_within(pitches)],        "node_components": t.node_components(pitches),        "chord_components": [            [{"kind": c.kind, "root": c.root} for c in comp] for comp in t.chord_components(chords)        ],        "shape": list(canonical_shape(t, pitches)),    }

Every number on this site comes from data produced by the Python core and is checked by tests.

TÜBİTAK 2204-A mathematics project