7

我是编程和 Python 的新手,但我目前的很多研究都涉及从 musicxml 文件中提取数据。我有一首乐曲,想提取乐曲中出现的不属于调号一部分的临时记号的数量。我不知道该怎么做,请有人帮忙吗?这是我正在查看的 musicxml 文件中的一项度量的示例:

<measure number='19'>
        <print new-system='no'/>
        <note>
            <rest/>
            <duration>768</duration>
            <voice>1</voice>
            <type>quarter</type>
            <staff>1</staff>
        </note>
        <backup>
            <duration>768</duration>
        </backup>
        <note>
            <pitch>
                <step>E</step>
                <octave>4</octave>
            </pitch>
            <duration>2304</duration>
            <tie type='start'/>
            <voice>2</voice>
            <type>half</type>
            <dot/>
            <staff>1</staff>
            <notations>
                <tied type='start'/>
                <slur type='stop' number='1'/>
            </notations>
        </note>
        <backup>
            <duration>1536</duration>
        </backup>
        <note>
            <pitch>
                <step>E</step>
                <alter>3</alter>
                <octave>3</octave>
            </pitch>
            <duration>1536</duration>
            <voice>1</voice>
            <type>half</type>
            <staff>1</staff>
        </note>
        <note>
            <chord/>
            <pitch>
                <step>G</step>
                <alter>4</alter>
                <octave>3</octave>
            </pitch>
            <duration>1536</duration>
            <voice>1</voice>
            <type>half</type>
            <staff>1</staff>
        </note>
        <backup>
            <duration>2304</duration>
        </backup>
        <note>
            <pitch>
                <step>E</step>
                <octave>2</octave>
            </pitch>
            <duration>2304</duration>
            <voice>5</voice>
            <type>half</type>
            <dot/>
            <staff>2</staff>
        </note>
    </measure>

问题转化为搜索musicxml文件并计算次数

<pitch>
   <step>*</step>
   <alter>**</alter>
       ...

发生在 * 不是(F 或 C)的情况下,并且还找出 * 是 F 或 C 并且后面没有<alter>标签的次数。

任何帮助或建议将不胜感激!

4

2 回答 2

8

我对 Python 细节无能为力,但我有两个与 MusicXML 相关的建议:

1)您的问题是用临时记号表达的,但您的代码侧重于 alter 元素。alter 元素用于改变音高;意外元素是用于书面意外的元素。你在找哪一个?在 MusicXML 中,有多少声音和它在符号中的出现方式之间的二元性很常见,并且对于使用 MusicXML 文件进行研究很重要。

2) 如果您不熟悉编程和 Python,我建议您使用专为音乐学设计的高级工具包,并具有良好的 MusicXML 支持。您会将问题域提升到更高的级别,这应该让您更快地取得进展。显而易见的选择是 music21 工具包,它也是用 Python 编写的。在http://web.mit.edu/music21/上有更多信息。

祝你的研究好运!

于 2013-02-01T07:17:26.887 回答
3

python 有一个xml.dom模块,可让您快速浏览 xml 文件。如果您有任何 Web 开发经验,它与 javascript 的文档对象模型非常相似。

from xml.dom.minidom import parse, parseString

def get_step(note):
    stepNode = note.getElementsByTagName("step")[0]
    #get the text from the Text Node within the <step>,
    #and convert it from unicode to ascii
    return str(stepNode.childNodes[0].nodeValue)

def get_alter(note):
    alters = note.getElementsByTagName("alter")
    if len(alters) == 0:
        return None
    return alters[0]

def is_rest(note):
    return len(note.getElementsByTagName("rest")) > 0

def is_accidental(note):
    return get_alter(note) != None

dom = parse("data.xml")

notes = dom.getElementsByTagName("note")
#rests don't have steps or alters, so we don't care about them. Filter them out.
notes = filter(lambda note: not is_rest(note), notes)

#compile a list of notes of all accidentals (notes with <alter> tags)
accidentals = filter(is_accidental, notes)
#remove notes that are F or C
accidentals_that_are_not_f_or_c = filter(lambda note: get_step(note) not in ["F", "C"], accidentals)

#compile a list of notes that don't contain the alter tag
non_accidentals = filter(lambda note: not is_accidental(note), notes)
#remove notes that are not F or C
non_accidentals_that_are_f_or_c = filter(lambda note: get_step(note) in ["F", "C"], non_accidentals)

print "Accidental notes that are not F or C:"
if len(accidentals_that_are_not_f_or_c) == 0:
    print "(None found)"
else:
    for note in accidentals_that_are_not_f_or_c:
        print get_step(note)

print "Non-accidental notes that are F or C:"
if len(non_accidentals_that_are_f_or_c) == 0:
    print "(None found)"
else:
    for note in non_accidentals_that_are_f_or_c:
        print get_step(note), get_step(note) in ["F", "C"]

输出:

Accidental notes that are not F or C:
E
G
Non-accidental notes that are F or C:
(None found)
于 2013-01-30T13:09:03.097 回答