Skip to content

harmonics

tones_to_harmonic_segment(tones, sub=False)

Converts a series of tones to a harmonic or sub-harmonic segment.

Parameters:

Name Type Description Default
tones List[JustInterval]

A series of tones.

required
sub bool

When True, returns a sub-harmonic segment. Else, a harmonic (overtone) segment.

False

Returns:

Type Description
List[int]

The harmonic, or sub-harmonic, segment

Examples:

>>> from .intervals import JustInterval
>>> tones = []
>>> tones.append(JustInterval(16, 15))
>>> tones.append(JustInterval(4, 3))
>>> tones.append(JustInterval(8, 5))
>>> tones_to_harmonic_segment(tones)
[4, 5, 6]
>>> tones = []
>>> tones.append(JustInterval(9, 5))
>>> tones.append(JustInterval(9, 8))
>>> tones.append(JustInterval(27, 20))
>>> tones.append(JustInterval(63, 40))
>>> tones_to_harmonic_segment(tones)
[4, 5, 6, 7]
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/jintonic/harmonics.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def tones_to_harmonic_segment(
    tones: List[JustInterval],
    sub: bool = False,
) -> List[int]:
    """Converts a series of tones to a harmonic or sub-harmonic segment.

    Parameters:
        tones: A series of tones.
        sub: When True, returns a sub-harmonic segment. Else, a harmonic
             (overtone) segment.

    Returns:
        The harmonic, or sub-harmonic, segment

    Examples:
        >>> from .intervals import JustInterval
        >>> tones = []
        >>> tones.append(JustInterval(16, 15))
        >>> tones.append(JustInterval(4, 3))
        >>> tones.append(JustInterval(8, 5))
        >>> tones_to_harmonic_segment(tones)
        [4, 5, 6]

        >>> tones = []
        >>> tones.append(JustInterval(9, 5))
        >>> tones.append(JustInterval(9, 8))
        >>> tones.append(JustInterval(27, 20))
        >>> tones.append(JustInterval(63, 40))
        >>> tones_to_harmonic_segment(tones)
        [4, 5, 6, 7]
    """
    if sub:
        _tones = [(tone.denominator, tone.numerator) for tone in tones]
    else:
        _tones = [(tone.numerator, tone.denominator) for tone in tones]

    lcm_tones = lcm([tone[1] for tone in _tones])
    # print(lcm_tones)
    segment = [tone[0] * (lcm_tones // tone[1]) for tone in _tones]
    # print(segment)
    gcd_segment = reduce(gcd, segment)
    segment = [harmonic // gcd_segment for harmonic in segment]
    if sub:
        segment.reverse()
    for i, harmonic in enumerate(segment):
        try:
            if harmonic / 2 == segment[i + 1] - 1:
                segment[i] = harmonic // 2
        except IndexError:
            continue
    if sub:
        segment.reverse()
    return segment

harmonic_to_identity(harmonic)

Converts a harmonic number to its tone identity.

Paramters

harmonic: A harmonic number

Returns:

Type Description
int

The harmonic's tone identity

Examples:

>>> harmonic_to_identity(6)
3
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/jintonic/harmonics.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def harmonic_to_identity(harmonic: int) -> int:
    """Converts a harmonic number to its tone identity.

    Paramters:
        harmonic: A harmonic number

    Returns:
        The harmonic's tone identity

    Examples:
        >>> harmonic_to_identity(6)
        3
    """
    return harmonic // (2 ** (prime_factors(harmonic).count(2)))

harmonic_segment_to_identities(segment)

Converts a harmonic or sub-harmonic segment to its tone identities.

Parameters:

Name Type Description Default
segment List[int]

A harmonic, or sub-harmonic, segment

required

Returns:

Type Description
List[int]

The tone identities of each harmonic in a segment

Examples:

>>> harmonic_segment_to_identities([4, 5, 6])
[1, 5, 3]
>>> harmonic_segment_to_identities([10, 12, 15])
[5, 3, 15]
>>> harmonic_segment_to_identities([4, 5, 6, 7, 9])
[1, 5, 3, 7, 9]
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/jintonic/harmonics.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def harmonic_segment_to_identities(segment: List[int]) -> List[int]:
    """Converts a harmonic or sub-harmonic segment to its tone identities.

    Parameters:
        segment: A harmonic, or sub-harmonic, segment

    Returns:
        The tone identities of each harmonic in a segment

    Examples:
        >>> harmonic_segment_to_identities([4, 5, 6])
        [1, 5, 3]

        >>> harmonic_segment_to_identities([10, 12, 15])
        [5, 3, 15]

        >>> harmonic_segment_to_identities([4, 5, 6, 7, 9])
        [1, 5, 3, 7, 9]
    """
    return [harmonic_to_identity(harmonic) for harmonic in segment]