def elem_to_dict(elem):
    """ Used to convert the 'attributes' element to a dict for easy value access during beat slicing.
    This is a dict of str:list. Each list contains more dicts like this.
    The reason we have a list of dicts (as opposed to a single dict) is because we index by tag name,
    but we can have multiple of the same tag as children (e.g. multiple <clef>s in <attributes>)

    :param elem: The element to convert.
    :type elem: ET.Element
    :return: A nested dict representation of the element.
                    The key (tag) maps to a list of elements with that tag (each with its own dictionary).
    :rtype: dict[str, list[dict]]
    """
    d = {'text': elem.text, 'tail': elem.tail, 'attrib': elem.attrib}
    if elem:
        for child in elem:
            if child.tag not in d:
                d[child.tag] = []
            d[child.tag].append(elem_to_dict(child))
    return d