Пример #1
0
        def align(self) -> Alignment:
            """
            align and return (alignment)
            """
            eprint(f"Aligning {self.name}")
            aligner = ASMAligner(self.pscore, self.rscore, self.postalignthres)
            alignment = aligner.get_alignment()

            return alignment
    def get_alignment(self) -> Alignment:
        """
        Gets the optimal alignment
        """
        self._set_up()
        self._solve()

        al = self._get_alignment_helper(
            1 + len(self._P),
            1 + len(self._S),
        )
        if al is None:
            raise ValueError("Cannot get alignment")

        if self.postalignthres >= 0:
            eprint(f"Running PostAlign with threshold {self.postalignthres}")
            pa = PostAlign(al, self.postalignthres)
            al = pa.postalign()

        return al
def print_alignment(alignment: Alignment):
    stdout, stderr = alignment_repr(alignment)
    print(stdout)
    eprint(stderr)
Пример #4
0
def bwv846():
    import os
    from midi import process_midi
    from utils.repr import noteinfos_repr, alignment_repr
    from utils.processfile import process_score_file
    from align import ASMAligner
    from utils.eprint import eprint

    BWV846_PATH = os.path.join(DATA_PATH, "bwv846")
    OUTPUT_PATH = os.path.join(REPRO_RESULTS_PATH, "bwv846")

    pieces = ["prelude", "fugue"]
    postalignthreses = [-1, 0, 500, 1000]

    for piece in pieces:
        eprint(f"=== Processing: {piece} ===")
        piece_path = os.path.join(BWV846_PATH, piece)
        piece_output_path = os.path.join(OUTPUT_PATH, piece)
        os.makedirs(piece_output_path, exist_ok=True)

        for postalignthres in postalignthreses:
            eprint(f"Post align thres: {postalignthres}")

            # with postalignthres
            actual_output_path = os.path.join(piece_output_path,
                                              str(postalignthres))
            os.makedirs(actual_output_path, exist_ok=True)

            # convert midi to score format
            for midi_type in ["r", "p"]:
                # r: reference (score)
                # p: performance

                mid_path = os.path.join(piece_path, f"{piece}.{midi_type}.mid")
                mid_notes = process_midi(mid_path)
                output_path = os.path.join(actual_output_path,
                                           f"{piece}.{midi_type}score.txt")
                mid_str = noteinfos_repr(mid_notes)

                of = open(output_path, "w")
                of.write(mid_str)
                of.close()

            pscore_path = os.path.join(actual_output_path,
                                       f"{piece}.pscore.txt")
            rscore_path = os.path.join(actual_output_path,
                                       f"{piece}.rscore.txt")

            P = process_score_file(pscore_path)
            S = process_score_file(rscore_path)

            aligner = ASMAligner(P, S, postalignthres)
            alignment = aligner.get_alignment()

            stdout, stderr = alignment_repr(alignment)

            align_out_path = os.path.join(actual_output_path,
                                          f"{piece}.align.txt")
            stat_file_path = os.path.join(actual_output_path,
                                          f"{piece}.align.stat.txt")

            of = open(align_out_path, "w")
            of.write(stdout)
            of.close()

            sf = open(stat_file_path, "w")
            sf.write(stderr)
            sf.close()

        eprint(f"Finished processing {piece}")
        eprint(f"View output in {piece_output_path}")
        eprint()
Пример #5
0
        print("======================================")
        print(f"Finished processing: {piece_basename}")
        print("======================================")
    __write_overall_results(overall_results, OUTPUT_PATH)
"""

func_map = {
    "bwv846": bwv846,
    "bach10": bach10,
    # "bach10_oracle": bach10_oracle,
}
if __name__ == "__main__":
    repro_args = sys.argv[1:]
    if len(repro_args) == 0:
        eprint("No repro arg given--running everything!")
        for name, f in func_map.items():
            print("++++++++++++++++++++++++++++++++++++")
            print(f"Starting: {name}")
            print("++++++++++++++++++++++++++++++++++++")
            f()
            print("++++++++++++++++++++++++++++++++++++")
            print(f"Finished: {name}")
            print("++++++++++++++++++++++++++++++++++++")
        sys.exit(0)
    elif len(repro_args) != 1:
        eprint(f"Unknown repro args: {repro_args}. Please see README.md")
        sys.exit(1)
    repro_arg = repro_args[0]
    if repro_arg in func_map:
        func_map[repro_arg]()
        "--mode",
        type=str,
        choices=["midi", "score"],
        help="Output type",
        default="score",
    )
    parser.add_argument("--output", type=str, help="Output file path for MIDI")

    args = parser.parse_args()

    inp = args.input
    oup = args.output
    mode = args.mode

    if mode == "midi" and len(oup) == 0:
        eprint("Output file path required for midi mode.")
        sys.exit(1)

    s = converter.parse(inp)

    # first convert to MIDI
    tmp_path = s.write("midi")

    if mode == "midi":
        shutil.move(tmp_path, oup)
    elif mode == "score":
        res = process_midi(tmp_path)

        out = noteinfos_repr(res)

        print(out)