def test_transpose_slash(self): c = Chord("Am7/G") c.transpose(3) self.assertEqual(c.root, "C") self.assertEqual(c.quality.quality, "m7") self.assertEqual(c.on, "Bb") self.assertEqual(c, Chord("Cm7/Bb"))
def test_minor_chord(self): c = Chord("Am") com = c.components_with_pitch(root_pitch=2) self.assertEqual(com, ["A2", "C3", "E3"])
def test_sixth_chord(self): c = Chord("C6") com0 = c.components(visible=False) self.assertEqual(com0, [0, 4, 7, 9]) com1 = c.components(visible=True) self.assertEqual(com1, ["C", "E", "G", "A"])
def test_sus4_chord(self): c = Chord("Fsus4") com0 = c.components(visible=False) self.assertEqual(com0, [5, 10, 12]) com1 = c.components(visible=True) self.assertEqual(com1, ["F", "Bb", "C"])
def test_aug_chord(self): c = Chord("Eaug") com0 = c.components(visible=False) self.assertEqual(com0, [4, 8, 12]) com1 = c.components(visible=True) self.assertEqual(com1, ["E", "G#", "C"])
def test_minor_chord(self): c = Chord("Am") com0 = c.components(visible=False) self.assertEqual(com0, [9, 12, 16]) com1 = c.components(visible=True) self.assertEqual(com1, ["A", "C", "E"])
def test_transpose_negative(self): c = Chord("Am") c.transpose(-4) self.assertEqual(c.root, "F") self.assertEqual(c.quality.quality, "m") self.assertEqual(c, Chord("Fm"))
def test_m7dim5(self): chords = find_chords_from_notes(["F#", "A", "C", "E"]) self.assertEqual(chords, [Chord("F#m7-5"), Chord("Am6/F#")])
def test_seventh_chord(self): c = Chord("G7") com0 = c.components(visible=False) self.assertEqual(com0, [7, 11, 14, 17]) com1 = c.components(visible=True) self.assertEqual(com1, ["G", "B", "D", "F"])
def test_dim_chord(self): c = Chord("Ddim") com0 = c.components(visible=False) self.assertEqual(com0, [2, 5, 8]) com1 = c.components(visible=True) self.assertEqual(com1, ["D", "F", "G#"])
def test_add9_chord(self): c = Chord("Eadd9") com = c.components_with_pitch(root_pitch=5) self.assertEqual(com, ["E5", "G#5", "B5", "F#6"])
def check_results(results, results_real): def extend(chords, durations): chords_extended = [] durations_extended = [] for i in range(0, len(durations)): n = round(float(durations[i]) / 0.1) j = 0 for j in range(0, n): chords_extended.append(chords[i]) durations_extended.append("0.1") return (chords_extended, durations_extended) # storing chords and durations that program found in the analyzed file my_chords, my_durations = extend(results[0], results[1]) i = 0 # storing actual chords and durations of the analyzed file real_chords, real_durations = extend(results_real[0], results_real[1]) j = 0 # current time is left border of chunk being checked and current_time_r is right current_time = 0 current_time_r = float(real_durations[j]) # variables to store succesful percentage total_percentage = 0 percentage = 0 # running through chords that are found for m in range(0, len(my_chords)): if (current_time < current_time_r): if (my_chords[i] == real_chords[j]): print("{} Matching {}".format(my_chords[i], real_chords[j])) current_time = current_time + float(my_durations[i]) # calculating percentage percentage = round(float(my_durations[i]) / duration * 100, 2) total_percentage = total_percentage + percentage print("Total percentage increased by ", percentage) i = i + 1 else: # If my chord doesnt match actual chords check if some of its tone do print("{} Doesn't match {}".format(my_chords[i], real_chords[j])) try: # if chord isnt none my_chord = Chord(my_chords[i]) real_chord = Chord(real_chords[j]) my_notes = reference_sort(my_chord.components()) real_notes = reference_sort(real_chord.components()) m = 0 for note in my_notes: if (note == real_notes[m]): percentage = round( float(my_durations[i]) / duration * 100 / len(my_notes), 2) total_percentage = total_percentage + percentage print( "Chord isnt right but some notes are, percentage increased by: ", percentage) m = m + 1 except: pass current_time = current_time + float(my_durations[i]) i = i + 1 else: if (j < len(real_durations) - 1): j = j + 1 current_time_r = current_time_r + float(real_durations[j]) if (my_chords[i] == real_chords[j]): print("{} Matching {}".format(my_chords[i], real_chords[j])) current_time = current_time + float(my_durations[i]) # calculating percentage percentage = round( float(my_durations[i]) / duration * 100, 2) total_percentage = total_percentage + percentage print("Total percentage increased by ", percentage) i = i + 1 else: print("{} Doesn't match {}".format(my_chords[i], real_chords[j])) try: my_chord = Chord(my_chords[i]) real_chord = Chord(real_chords[j]) my_notes = reference_sort(my_chord.components()) real_notes = reference_sort(real_chord.components()) m = 0 for note in my_notes: if (note == real_notes[m]): percentage = round( float(my_durations[i]) / duration * 100 / len(my_notes), 2) total_percentage = total_percentage + percentage print( "Chord isnt right but some notes are, percentage increased by: ", percentage) m = m + 1 except: pass current_time = current_time + float(my_durations[i]) i = i + 1 #print(current_time , ' ' , current_time_r) print() return total_percentage
def test_aug(self): chords = find_chords_from_notes(["F", "A", "Db"]) self.assertEqual( chords, [Chord("Faug"), Chord("Aaug/F"), Chord("Dbaug/F")])
def test_overwrite(self): self.quality_manager.set_quality("11", (0, 4, 7, 10, 14, 17)) chord = Chord("C11") self.assertEqual(chord.components(), ['C', 'E', 'G', 'Bb', 'D', 'F'])
def test_major(self): chords = find_chords_from_notes(["C", "E", "G"]) self.assertEqual(chords, [Chord("C")])
def test_find_from_components(self): self.quality_manager.set_quality("13", (0, 4, 7, 10, 14, 17, 21)) chords = find_chords_from_notes(['C', 'E', 'G', 'Bb', 'D', 'F', 'A']) self.assertEqual(chords, [Chord("C13")])
def test_m7dim5(self): chords = note_to_chord(["F#", "A", "C", "E"]) self.assertEqual(chords, [Chord("F#m7-5")])
def test_keep_existing_chord(self): chord = Chord("C11") self.quality_manager.set_quality("11", (0, 4, 7, 10, 14, 17)) self.assertEqual(chord.components(), ['C', 'G', 'Bb', 'D', 'F'])
# chord track chord_track = pretty_midi.Instrument(program=0) chord_start = ( start_measure + 1) * measure_sec if incomplete_start else start_measure * measure_sec for i, chord in enumerate(chords): start = chord_start + i * measure_sec end = start + measure_sec lyric = pretty_midi.Lyric(text=chord, time=start) midi.lyrics.append(lyric) previous = -1 _chords = Chord(chord).components() _chords = [NOTE_VAL_DICT.get(c) for c in _chords] for c in _chords: if c < previous: c += 12 note = pretty_midi.Note(velocity=CHORD_VELOCITY, pitch=int(CHORD_INIT_NOTE + c), start=start, end=end) previous = c chord_track.notes.append(note) midi.instruments.append(melody_track) midi.instruments.append(chord_track) # loal save
def test_slash_chord(self): c = Chord("CM9/D") com0 = c.components(visible=False) self.assertEqual(com0, [-10, 0, 4, 7, 11]) com1 = c.components(visible=True) self.assertEqual(com1, ["D", "C", "E", "G", "B"])
def test_Node__to_chord(): n = Node(vec=np.array([1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0])) assert n.to_chord(), Chord("C")
def test_major(self): chords = note_to_chord(["C", "E", "G"]) self.assertEqual(chords, [Chord("C")])
def test_normal_chord(self): c = Chord("C") com = c.components_with_pitch(root_pitch=1) self.assertEqual(com, ["C1", "E1", "G1"])
def test_major_on_third(self): chords = note_to_chord(["F#", "A", "D"]) self.assertEqual(chords, [Chord("D/F#")])
def test_slash_chord(self): c = Chord("Dm7/G") com = c.components_with_pitch(root_pitch=3) self.assertEqual(com, ["G3", "D4", "F4", "A4", "C5"])
def test_major_on_fifth(self): chords = note_to_chord(["B", "E", "G#"]) self.assertEqual(chords, [Chord("E/B")])
def test_normal_chord(self): c = Chord("C") com0 = c.components(visible=False) self.assertEqual(com0, [0, 4, 7]) com1 = c.components(visible=True) self.assertEqual(com1, ["C", "E", "G"])
def test_dim(self): chords = note_to_chord(["Eb", "Gb", "A"]) self.assertEqual(chords, [Chord("Ebdim")])
def test_add9(self): chords = find_chords_from_notes(["C", "E", "G", "D"]) self.assertEqual(chords, [Chord("Cadd9")])
def test_aug(self): chords = note_to_chord(["F", "A", "Db"]) self.assertEqual( chords, [Chord("Faug"), Chord("Aaug/F"), Chord("Dbaug/F")])
def test_minor_add4(self): chords = find_chords_from_notes(["C", "Eb", "F", "G"]) self.assertEqual(chords, [Chord("Cmadd4")])
def test_add9(self): chords = note_to_chord(["C", "E", "G", "D"]) self.assertEqual(chords, [Chord("Cadd9")])
def test_transpose_positive(self): c = Chord("Am") c.transpose(3) self.assertEqual(c.root, "C") self.assertEqual(c.quality.quality, "m") self.assertEqual(c, Chord("Cm"))
def test_transpose_zero(self): c = Chord("Am") c.transpose(0) self.assertEqual(c.root, "A") self.assertEqual(c.quality.quality, "m") self.assertEqual(c, Chord("Am"))
def test_transpose_eq1(self): c = Chord("C") c.transpose(1) self.assertEqual(c, Chord("C#")) self.assertEqual(c, Chord("Db"))
def test_transpose_eq2(self): c = Chord("C") c.transpose(2) self.assertEqual(c, Chord("D"))
def process( stringToProcess, processed): global songHalfTones #print 'String to process "' + stringToProcess + '".' afterSplit = re.split(" |_|!|\.\.\.|\.\.|: |\*|high|open|bass|riff|palm mute|notes|m6|madd11/|m7add11/|7sus2|8|m7b5|madd13|add13", stringToProcess, 1) # 3rd parameter is maxsplit # Also works with single space, do this to catch faulty txt. #print '* Split by delimiters "' + str(afterSplit) + '".' #print 'songHalfTones:',songHalfTones if len(afterSplit[0]) != 0: chord = Chord(afterSplit[0]) #print '* Extracted "' + chord.chord + '" chord.' chord.transpose( songHalfTones, "C#" ) #print '* Transposed to "' + chord.chord + '" chord.' processed += chord.chord #print '* Processed after chord "' + processed + '".' #else: #print '* No chord to extract.' if len(afterSplit) == 1: return processed delimiterWas = '' if len(afterSplit[1]) == 0: delimiterWas = stringToProcess[len(afterSplit[0]):] else: delimiterWas = stringToProcess[len(afterSplit[0]):-len(afterSplit[1])] #print '* Delimiter was "' + delimiterWas + '".' processed += delimiterWas #print '* Processed after delimiter "' + processed + '".' #print '* Still must process "' + afterSplit[1] + '".' return process( afterSplit[1], processed )