Xenharmlib(音乐理论库)添加了对纯律的支持
摘要
Xenharmlib 0.4.0 重构了其核心以支持纯律和规整调律,从而能够准确分析从拜占庭到实验性美国音乐等多种音乐传统。
暂无内容
查看缓存全文
缓存时间: 2026/07/28 18:28
# xenharmlib 0.4.0 有什么新变化?
来源:https://xenharmlib.readthedocs.io/en/latest/whats_new_0_4_0.html
就新功能而言,0.4.0 版本并非一小步,而是一次飞跃。xenharmlib 的核心已被重构,不仅支持等分律,还支持具有多个生成音程的调律。这意味着它现在支持整个正则调律的范围,包括纯律。
除了不规则调律(如“Werckmeister 调律”)之外,xenharmlib 终于能够以数学形式表示整个音乐传统宇宙。现在它可以用于精确分析拜占庭帝国、欧洲文艺复兴、印度古典音乐、印尼甘美兰传统、阿拉伯 Maqam 以及 20 世纪美国实验音乐(如 Harry Partch 或 Ben Johnston 的作品)的音乐。
秉持其整体性方法,xenharmlib 允许对所有正则调律使用现有的分析工具,只要它们在数学上与这些调律兼容。例如,素数形式和标准形式计算也可以在(多维)正则调律上执行。
此外,文档已完全翻新:功能示例按调律和记谱法组织,并为所有和声基元提供了全面的专用文档。以下我们将简要概述上述(及其他)功能。(完整的变更日志请见此处 (https://xenharmlib.readthedocs.io/en/latest/changelog.html#changelog-0-4-0))
目录
- [纯律 / 素数限制调律](#just-intonation-prime-limit-tunings)
- [自定义正则调律](#custom-regular-temperaments)
- [完整音程运算](#full-interval-arithmetic)
- [额外的和声基元](#additional-harmonic-primitives)
- [音程类向量](#interval-class-vectors)
- [最近频率近似](#closest-frequency-approximation)
## 纯律 / 素数限制调律¶ (https://xenharmlib.readthedocs.io/en/latest/whats_new_0_4_0.html#just-intonation-prime-limit-tunings)
纯律指的是基于“纯音程”的调律。纯音程由两个整数的分数表征,例如 \\\(\\frac{3}{2}\\\)。历史上,大多数调律系统都基于纯律。直到 18 和 19 世纪,等分律系统才开始在实践中获得广泛接受。xenharmlib 现在通过 `PrimeLimitTuning` (https://xenharmlib.readthedocs.io/en/latest/primelimit_tunings.html) 结构引入对纯律的支持:
```
from xenharmlib import PrimeLimitTuning
limit11 = PrimeLimitTuning(11)
```
例如,现在可以通过从根音向上堆叠 5 个纯五度、向下堆叠 6 个纯五度来生成一个毕达哥拉斯 12 音半音阶:
```
from xenharmlib import PrimeLimitTuning
from xenharmlib import periodic
pythagorean = PrimeLimitTuning(3)
C0 = pythagorean.rs_pitch('1/1')
P5 = pythagorean.rs_interval('3/2')
iseq_a = pythagorean.interval_seq([P5] * 5)
iseq_b = pythagorean.interval_seq([-P5] * 6)
chromatic_scale = (
C0.scale(iseq_a) | C0.scale(iseq_b)
).pcs_normalized()
print(chromatic_scale)
```
```
PrimeLimitPitchScale([1, 256/243, 9/8, 32/27, 81/64, 4/3, 1024/729, 3/2, 128/81, 27/16, 16/9, 243/128], 3-Limit)
```
大多数软件程序将纯律功能实现为标量移调系统,而非完整的算术系统。例如,在五度移调的情况下,通常使用半音阶的定义(如上所述)作为基础,移调被理解为 7 个音阶级的移位:
```
for i in range(0, 12):
pitch = chromatic_scale[i]
result = periodic.scalar_transpose(chromatic_scale, pitch, 7)
interval = pitch.interval(result)
print(f'{pitch.short_repr} --> {result.short_repr} ({interval.short_repr})')
```
```
1 --> 3/2 (3/2)
256/243 --> 128/81 (3/2)
9/8 --> 27/16 (3/2)
32/27 --> 16/9 (3/2)
81/64 --> 243/128 (3/2)
4/3 --> 2 (3/2)
1024/729 --> 512/243 (3/2)
3/2 --> 9/4 (3/2)
128/81 --> 64/27 (3/2)
27/16 --> 81/32 (3/2)
16/9 --> 8/3 (3/2)
243/128 --> 2048/729 (262144/177147)
```
正如输出的最后一个音高(\\\(\\frac{243}{128}\\\))所示,在毕达哥拉斯半音阶中按音阶级移调并不总是导致该音程的*实际*移调。这是调音键盘乐器(如钢琴或羽管键琴)时的一个常见问题,因为这种乐器每八度只有有限数量的音符。从数学上讲,这种按五度移调的定义是不规则的:五度的大小会根据选择哪个音符作为起点而变化。
与键盘乐器不同,无品弦乐器或人声没有音符选择有限的问题。因此,xenharmlib 将其默认移调实现为*正则移调*,而不是标量移调,其效果是,将上述音阶的最后一个音符移调后产生的音高类不再包含在音阶本身中。
```
for i in range(0, 12):
pitch = chromatic_scale[i]
result = pitch.transpose(P5)
interval = pitch.interval(result)
try:
scale_degree = periodic.index(chromatic_scale, result)
except ValueError:
scale_degree = 'not in scale'
print(f'{pitch.short_repr} --> {result.short_repr} ', end='')
print(f'({interval.short_repr}) - degree: {scale_degree}')
```
```
1 --> 3/2 (3/2) - degree: 7
256/243 --> 128/81 (3/2) - degree: 8
9/8 --> 27/16 (3/2) - degree: 9
32/27 --> 16/9 (3/2) - degree: 10
81/64 --> 243/128 (3/2) - degree: 11
4/3 --> 2 (3/2) - degree: 12
1024/729 --> 512/243 (3/2) - degree: 13
3/2 --> 9/4 (3/2) - degree: 14
128/81 --> 64/27 (3/2) - degree: 15
27/16 --> 81/32 (3/2) - degree: 16
16/9 --> 8/3 (3/2) - degree: 17
243/128 --> 729/256 (3/2) - degree: not in scale
```
这一定义不仅在数学上更合理(因为它对五度有一个单一、一致的定义),而且也使我们能够处理那些充分利用可用调律空间(而不仅仅是半音阶选择音符)的作品。
## 自定义正则调律¶ (https://xenharmlib.readthedocs.io/en/latest/whats_new_0_4_0.html#custom-regular-temperaments)
现在可以使用新引入的 `Multi-Generator Tunings` (https://xenharmlib.readthedocs.io/en/latest/multigen_tunings.html) 来设计自定义正则调律。例如,四分音差中全律(Quarter-Comma-Meantone Temperament)使三度音降低一个和谐逗号的四分之一,可以这样构造:
```
from fractions import Fraction
from xenharmlib import MultiGenTuning
from xenharmlib import FrequencyRatio
g3 = FrequencyRatio(3) * FrequencyRatio(80, 81) ** Fraction(1, 4)
qcm = MultiGenTuning(
(FrequencyRatio(2), g3),
eq_diff_vec=(1, 0)
)
```
## 完整音程运算¶ (https://xenharmlib.readthedocs.io/en/latest/whats_new_0_4_0.html#full-interval-arithmetic)
一个在许多理论应用中必要但长期缺失的特性现在在 0.4.0 版本中引入。我们说的是音程运算:
```
from xenharmlib import WesternNotation
western = WesternNotation()
m3 = western.shorthand_interval('m', 3)
M3 = western.shorthand_interval('M', 3)
print(m3 + M3)
print(M3 - m3)
print(3 * M3)
```
```
WesternNoteInterval(P, 5)
WesternNoteInterval(A, 1)
WesternNoteInterval(A, 7)
```
要了解更多信息,请查阅专用的音程页面 (https://xenharmlib.readthedocs.io/en/latest/prim_interval.html)。
## 额外的和声基元¶ (https://xenharmlib.readthedocs.io/en/latest/whats_new_0_4_0.html#additional-harmonic-primitives)
0.4.0 版本为其分析工具箱带来了两个新的和声基元:
序列 (https://xenharmlib.readthedocs.io/en/latest/prim_seq.html) 允许您将“音符的连续”概念化,例如在分段分析中。序列带有典型的变换方法,如逆行和倒影。
```
from xenharmlib import WesternNotation
western = WesternNotation()
# melodic flow of the beginning of "marry had a little lamb"
seq = western.seq(western.note(pcs, 4) for pcs in 'EDCDEEE')
print(seq)
print(seq.retrograde())
print(seq.inversion())
```
```
WesternNoteSeq([E4, D4, C4, D4, E4, E4, E4])
WesternNoteSeq([E4, E4, E4, D4, C4, D4, E4])
WesternNoteSeq([E4, F#4, G#4, F#4, E4, E4, E4])
```
通过音程扇 (https://xenharmlib.readthedocs.io/en/latest/prim_interval_fan.html),引入了一种新类型的抽象音阶对象,它描述了序列或音阶的元素到调性中心的相对距离:
```
from xenharmlib import WesternNotation
western = WesternNotation()
# melodic flow of the beginning of "marry had a little lamb"
seq = western.seq(western.note(pcs, 4) for pcs in 'EDCDEEE')
# calculate relative distances to the tonic of C4
tonic = western.note('C', 4)
ifan = seq.to_interval_fan(tonic)
print(ifan)
```
```
WesternNoteIntervalFan([M3, M2, P1, M2, M3, M3, M3])
```
## 音程类向量¶ (https://xenharmlib.readthedocs.io/en/latest/whats_new_0_4_0.html#interval-class-vectors)
作为音阶分析(尤其是后调性分析)的重要工具,xenharmlib 现在支持计算音程类向量:
```
from xenharmlib import EDOTuning
from xenharmlib.setc import ic_vector
edo31 = EDOTuning(31)
scale = edo31.index_scale([0, 5, 11, 15, 17, 21])
print(ic_vector(scale))
```
```
(0, 1, 0, 2, 1, 3, 0, 0, 0, 3, 1, 1, 0, 1, 2)
```
## 最近频率近似¶ (https://xenharmlib.readthedocs.io/en/latest/whats_new_0_4_0.html#closest-frequency-approximation)
一维调律和记谱法的所有和声基元现在都支持最近频率近似,例如音阶:
```
from xenharmlib import WesternNotation
from xenharmlib import Frequency
western = WesternNotation()
# overtone frequencies measured e.g. by
# a spectrogram plugin or librosa
frequencies = [
Frequency(f) for f in [230, 410, 460, 500, 690]
]
scale = western.closest_scale(frequencies)
print(scale)
```
```
WesternNoteScale([A#3, G#4, A#4, B4, F5])
```
相似文章
和声解析:迈向音乐科学理论的进展(2012)
本文从物理学和计算的基本原理推导出大调音阶、标准和弦词典以及大三和弦与小三和弦之间的区别,提出了一种科学的和声理论,解决了亥姆霍兹(Helmholtz)和特哈特(Terhardt)早期理论中的局限性。
Show HN: XY 音乐记谱法
Show HN: XY 音乐记谱法 —— 一款基于网页的音乐记谱与播放工具,内置钢琴音色库,支持可选的外部 MIDI 连接,并提供播放控制功能。
面向程序员的音乐理论
本文从第一性原理出发,探讨如何用编程理解音乐理论,展示声音如何被表示为数字,并通过代码片段构建音阶与和弦。
Show HN:我做了一个效率高 60 倍的音频转 MIDI 模型
kobimusic 推出了 Dettato,一个最先进的音频转 MIDI 转录模型,参数量仅有 20M(比 MuScriptor 的 1.3B 小约 65 倍),在公开基准测试中击败了开源和闭源对手,完全基于合成数据和 CC 许可录音训练,开放权重并允许商用。
Show HN: Resonate – 低延迟高分辨率频谱分析
Resonate 是一种低延迟、低内存的算法,用于对音频信号进行感知相关的频谱分析,采用带有指数加权移动平均的谐振器模型。