From 50312941efa993160f860d241327e714a3692885 Mon Sep 17 00:00:00 2001 From: pedroramonedafranco Date: Fri, 28 May 2021 13:55:12 +0200 Subject: [PATCH] fix chords --- pianoplayer/core.py | 12 ++++++------ pianoplayer/scorereader.py | 13 ++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pianoplayer/core.py b/pianoplayer/core.py index 601962a..af399ef 100644 --- a/pianoplayer/core.py +++ b/pianoplayer/core.py @@ -102,14 +102,14 @@ def annotate_PIG(hand, is_right=True): for n in hand.noteseq: onset_time = "{:.4f}".format(n.time) offset_time = "{:.4f}".format(n.time + n.duration) - spelled_pitch = n.name + spelled_pitch = n.pitch onset_velocity = str(None) offset_velocity = str(None) channel = '0' if is_right else '1' finger_number = n.fingering if is_right else -n.fingering cost = n.cost ans.append((onset_time, offset_time, spelled_pitch, onset_velocity, offset_velocity, channel, - finger_number, cost)) + finger_number, cost, n.noteID)) return ans @@ -195,14 +195,14 @@ def annotate(args): pig_notes.extend(annotate_PIG(rh)) if not args.right_only: - pig_notes.extend(annotate_PIG(lh)) + pig_notes.extend(annotate_PIG(lh, is_right=False)) with open(args.outputfile, 'wt') as out_file: tsv_writer = csv.writer(out_file, delimiter='\t') for idx, (onset_time, offset_time, spelled_pitch, onset_velocity, offset_velocity, channel, - finger_number, cost) in enumerate(sorted(pig_notes)): + finger_number, cost, id_n) in enumerate(sorted(pig_notes, key=lambda tup: (float(tup[0]), int(tup[5]), int(tup[2])))): tsv_writer.writerow([idx, onset_time, offset_time, spelled_pitch, onset_velocity, offset_velocity, - channel, finger_number, cost]) + channel, finger_number, cost, id_n]) else: ext = os.path.splitext(args.filename)[1] if ext in ['mid', 'midi']: @@ -258,4 +258,4 @@ def annotate(args): if __name__ == '__main__': - run_annotate('../scores/test_chords.xml', outputfile="test_chords_annotated.txt", musescore=True, right_only=True, n_measures=800, depth=9) \ No newline at end of file + run_annotate('../scores/test_chord.xml', outputfile="test_chord_annotate.xml", right_only=True, musescore=True, n_measures=800, depth=0) \ No newline at end of file diff --git a/pianoplayer/scorereader.py b/pianoplayer/scorereader.py index 6056039..561f541 100644 --- a/pianoplayer/scorereader.py +++ b/pianoplayer/scorereader.py @@ -60,8 +60,9 @@ def reader(sf, beam=0): print('Reading beam', beam, 'with', len(strm), 'objects in stream.') chordID = 0 - + noteID = 0 for n in strm.getElementsByClass("GeneralNote"): + if n.duration.quarterLength==0: continue if hasattr(n, 'tie'): # address bug https://github.com/marcomusy/pianoplayer/issues/29 @@ -72,7 +73,7 @@ def reader(sf, beam=0): # print "doppia nota", n.name continue an = INote() - an.noteID += 1 + an.noteID = noteID an.note21 = n an.isChord= False an.name = n.name @@ -92,6 +93,7 @@ def reader(sf, beam=0): an.fingering = get_finger_music21(n) noteseq.append(an) + noteID += 1 elif n.isChord: @@ -101,7 +103,7 @@ def reader(sf, beam=0): for j, cn in enumerate(n.pitches): an = INote() an.chordID = chordID - an.noteID += 1 + an.noteID = noteID an.isChord = True an.pitch = cn.midi an.note21 = cn @@ -111,7 +113,7 @@ def reader(sf, beam=0): an.octave = cn.octave an.measure = n.measureNumber an.x = keypos(cn) - an.time = n.offset-sfasam*j + an.time = n.offset-sfasam*(len(n.pitches)-j-1) an.duration= n.duration.quarterLength+sfasam*(an.NinChord-1) if hasattr(cn, 'pitch'): pc = cn.pitch.pitchClass @@ -122,10 +124,11 @@ def reader(sf, beam=0): else: an.isBlack = False an.fingering = get_finger_music21(n, j) + noteID += 1 noteseq.append(an) chordID += 1 - if len(noteseq)<2: + if len(noteseq) < 2: print("Beam is empty.") return [] return noteseq