Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
structure builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ahankinson committed Dec 15, 2010
1 parent f52dbc2 commit b5758fb
Showing 1 changed file with 79 additions and 14 deletions.
93 changes: 79 additions & 14 deletions converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from music21 import tie
from music21 import clef

import types
import logging
import pprint
import copy
Expand All @@ -36,11 +37,14 @@ class ConverterMei(object):
def __init__(self):
"""docstring for __init__"""
self._meiDoc = None
self._sections = None
self._score = stream.Score()
self._staff_registry = {}
self._voice_registry = {}
self._registry = [] # {id: <m21_obj>}


# these objects have direct mappings from MEI to M21 elements.
# Thus than can easily be converted and stored for later.
self._objects_to_convert = {
'note': self._create_note,
'rest': self._create_rest,
Expand All @@ -63,43 +67,81 @@ def parseFile(self, filename):
self._create_registries()
#self._parse_children(self._meiDoc.gettoplevel())

lg.debug(self._voice_registry)
lg.debug(self._registry)

# ===============================
# register objects by ID.
def _create_registries(self):
scoredefs = self._meiDoc.search('scoredef')
for scoredef in scoredefs:
if not scoredef.ancestor_by_name('section'):
self._parse_children(scoredef)

sections = self._meiDoc.search('section')
for section in sections:
if section.has_child('scoredef'):
# parse the new scoredef.
pass
if section.has_child('measure'):
for measure in section.children_by_name('measure'):
self._sections = self._meiDoc.search('section')
for section in self._sections:
if section.descendents_by_name('measure'):
for measure in section.descendents_by_name('measure'):
self._parse_children(measure)

def _structure_work(self):
pass

def _parse_children(self, element):
if element.name in self._objects_to_convert.keys():
n = self._objects_to_convert[element.name](element)
self._registry.append((element.id, n))
if not isinstance(n, types.NoneType):
self._registry.append((element.id, n))
if element.children:
map(self._parse_children, element.children)

# start building the M21 structure
def _structure_work(self):
for section in self._sections:
if section.has_child('scoredef'):
#set the new scoredefs.
pass
self._parse_structure(section)

def _parse_structure(self, element):
pass
if element.name == "measure":
lg.debug("Whoops! found a measure!")
else:
lg.debug("We don't know how to deal with {0}.".format(element.name))

if element.children:
map(self._parse_structure, element.children)

def _create_staffgrp(self, element):
lg.debug("Creating staffgrp from {0}".format(element.id))
# a staff group will just be a spanner element.
m_staffgrp = spanner.StaffGroup()
return m_staffgrp

def _create_measure(self, element):
# a bit of an anomaly, this one. Since we essentially want to create
# (part * voice) number of measures, there is not a direct one-to-one
# mapping of mei measure to m21 measure. Nevertheless, there are certain
# tasks that we can condense into the measure creation here.
lg.debug("Creating a measure, for what it's worth, from {0}".format(element.id))
m_measure = stream.Measure()
if element.has_attribute('n') and element.measure_number.isdigit():
m_measure.number = int(element.measure_number)

if element.has_barline:
if not element.is_repeat:
m_barline = bar.Barline()
m_barline.style = self._barline_converter(element.barline)
else:
m_barline = bar.Repeat()
m_barline.style = self._barline_converter(element.barline)
if element.barline is "rptstart":
m_barline.direction = "start"
elif element.barline is "rptend":
m_barline.direction = "end"
else:
raise ConverterMeiError("Could not determine barline type")
m_measure.rightBarline = m_barline

return m_measure


def _create_staffdef(self, element):
staffnum = element.attribute_by_name('n').value
if staffnum not in self._staff_registry.keys():
Expand Down Expand Up @@ -144,6 +186,29 @@ def _create_chord(self, element):
m_chord = chord.Chord()

return m_chord

def _barline_converter(self, barline):
"""
Converts a MEI barline representation into a Music21 and thus a
MusicXML barline representation.
"""
barline_dict = {
'dashed': 'dashed',
'dotted': 'dotted',
'dbl': 'light-light',
'dbldashed': '',
'dbldotted': '',
'end': 'light-heavy',
'invis': 'none',
'rptstart': 'heavy-light',
'rptboth': '',
'rptend': 'light-heavy',
'single': ''
}
if barline in barline_dict.keys():
return barline_dict[barline]
else:
raise ConverterMeiError("Could not convert barline.")

if __name__ == "__main__":
from optparse import OptionParser
Expand Down

0 comments on commit b5758fb

Please sign in to comment.