Esempio n. 1
0
def load(filename):
    f = open(filename)
    try:
        json_object = json.load(f)
    except:
        raise InvalidFileFormat, "Need JSON file!"
    f.close()

    if not "file_type" in json_object or not json_object[
            "file_type"] == FILE_TYPE:
        raise InvalidFileFormat, "Invalid JSON file."

    if not json_object["format_version"] == FILE_FORMAT_VERSION:
        raise InvalidFileFormat, "Got file format version %s, need version %s" % (
            json_object["format_version"], FILE_FORMAT_VERSION)

    # collect strings from tabulature
    strings = [i["tuning"] for i in json_object["tabulature"]]

    project = Project(json_object["audiofile"], strings, filename)

    for json_marker_object in json_object["markers"]:
        marker = Timeline.Marker(project.timeline,
                                 json_marker_object["start"],
                                 json_marker_object["duration"],
                                 json_marker_object["text"],
                                 x=json_marker_object["x"])
        project.timeline.markers.append(marker)

    for i in xrange(len(json_object["tabulature"])):
        json_string_object = json_object["tabulature"][i]

        for json_marker_object in json_string_object["markers"]:
            string = project.timeline.tabulature.strings[i]
            marker = Timeline.TabMarker(string, json_marker_object["start"],
                                        json_marker_object["duration"],
                                        json_marker_object["fret"])
            string.markers.append(marker)

    for json_annotation_object in json_object["annotations"]:
        marker = Timeline.Annotation(project.timeline,
                                     x=json_annotation_object["x"],
                                     time=json_annotation_object["time"],
                                     text=json_annotation_object["text"])
        project.timeline.annotations.append(marker)

    for json_tap_object in json_object["rhythm"]:
        time = json_tap_object["time"]
        tap = Timeline.Tap(project.timeline.rhythm,
                           json_tap_object["weight"],
                           time=time)
        project.timeline.rhythm.taps.append(tap)

    project.touched = False

    return project