1

我试图在给定的旋律中获得音符音高(只是名称,没有八度)之间的转换率。例如,如果我的旋律音高是(按顺序)CDEDFCBC,我应该得到 CD 转换以 0.5 的速率发生,BC 的速率为 1,等等。

我应该能够在 Python 中编写一个函数来做到这一点(可能使用很多elifs......)但看起来 music21 也必须能够轻松地做到这一点。我在这里查看了文档、谷歌和其他问题......我找不到方法,但我怀疑我错过了一个可能对我真正有用的工具包。

4

2 回答 2

1

您可能正在寻找的是一种二元表示,我通常用字典来处理。这可能有点草率,但您可以轻松地整理它:

note_list = ...a list containing all notes in order
bigram_dict = {}
for note in range(1, len(note_list)):
    bigram = (note -1, note)
    if bigram not in bigram_dict:
        bigram_dict[bigram] = 1 / len(note_list)
    else:
        bigram_dict[bigram] += 1 / len(note_list)

这将为您提供每个二元组的百分比。如果使用 Python 2.x,则必须bigram_dict[bigram += float(1 / len(note_list))避免整数/浮点问题。此外,如果字典给您带来麻烦,您可以尝试使用 defaultdict。

于 2016-06-05T04:38:01.773 回答
0

我建议做类似的事情:

from music21.ext.more_itertools import windowed
from collections import Counter
# assuming s is your Stream
nameTuples = []
for n1, n2 in windowed(s.recurse().notes, 2):
    nameTuples.append((n1.name, n2.name))
c = Counter(nameTuples)
totalNotes = len(s.recurse().notes) # py2 cast to float
{k : v / totalNotes for k, v in c.items()}

窗口化的好处是很容易创建三元组等。

于 2017-08-31T01:47:51.690 回答