Exemple #1
0
def chord_translate(chord_str: str,
                    scale: int,
                    base_oct: int = 3) -> List[str]:
    chord_str = chord_str.replace("(", "").replace(")", "")
    try:
        chord = pychord.Chord(chord_str)
        addomit = dict()
    except ValueError as e:
        # add omit だけ抜き取りたい
        chord_without_quality = re.sub(f"^{note}", "", chord_str)
        valid_qualities = list(pychord.quality.QUALITY_DICT.keys())
        valid_qualities.remove("")
        valid_qualities.sort(key=len, reverse=True)

        found = False
        addomit_str = ""
        for quality in valid_qualities:
            if chord_without_quality.find(quality) == 0:  # 先頭のコードqualityを削除する
                found = True
                addomit_str = re.sub(f"^{re.escape(quality)}", "",
                                     chord_without_quality)
                chord = pychord.Chord(
                    re.sub(f"{re.escape(addomit_str)}$", "", chord_str))
                break
        if not found:
            raise e

        # TODO: Fix this
        # Should raise error if removed addomit_str is not empty
        addomit = {
            "adds":
            [r.groups()[-2:] for r in re.finditer(add_regex, addomit_str)],
            "omits":
            [r.groups()[-1] for r in re.finditer(omit_regex, addomit_str)],
        }

    if len(addomit) > 0:
        s = musthe.Scale(f"{chord.root}3", "major")
        adds = addomit.get("adds")
        omits = addomit.get("omits")
        for add in adds:
            align = (add[0].count("#") + add[0].count("+") -
                     add[0].count("b") - add[0].count("-"))

            adder_note_dict = parse_note_str(
                transpose(s[int(add[1]) - 1].scientific_notation(), align))
            chord.quality.append_note(adder_note_dict["note"], chord._root,
                                      int(adder_note_dict["oct"]) - 3)

        for omit in omits:
            for o in range(int(omit) - 2, int(omit) + 1):
                if o in chord.quality.components:
                    chord.quality.components.remove(o)

    chord.transpose(scale)
    notes = chord.components_with_pitch(base_oct)

    return notes
Exemple #2
0
def generate_harmony(matches, next_matches, initial):
    global track_hypothesis, pos_hypothesis
    # print(f"Current hypothesis: {track_hypothesis}:{pos_hypothesis}")

    track_matches = matches[matches.track == track_hypothesis]
    if not track_matches.empty:
        pos_matches = track_matches[track_matches.position == pos_hypothesis]
        if pos_matches.empty:
            prev_match = track_matches.iloc[0]
        else:
            prev_match = pos_matches.iloc[0]
        result = next_matches[next_matches.track.isin([prev_match.track]) &
            next_matches.position.isin([prev_match.position +
            c.N_NGRAM])].iloc[0]
        pos_hypothesis = result.position
    else:
        prev_match = matches.iloc[0]
        result = next_matches.iloc[0]
        track_hypothesis = result.track
        pos_hypothesis = result.position

    duration = result.duration.split()
    harmony = [pychord.Chord(h) for h in result.harmony.split() if h != 'None']
    delta = (initial - prev_match.initial) % 12
    for i in range(len(harmony)):
        harmony[i].transpose(delta)

    return harmony, duration
Exemple #3
0
def parse_song(text):
    chords = []
    for token in text.split():
        try:
            chords.append(pychord.Chord(token))
        except ValueError:
            pass
    return chords
Exemple #4
0
    def get_user_chord_progression(self) -> List:
        """Calls for user input and returns a list of chord objects.
        """
        userInput = input()

        while userInput[-1] != '.':
            if userInput == 'q':
                print('~ Goodbye! ~')
                sys.exit()
            print('Oops! Make sure to include a period (.) at the end.')
            sys.stdout.flush()
            userInput = input()
        # remove the trailing period
        userInput = userInput[:-1]
        # remove all whitespace for each entry
        chordProgression = [
            pc.Chord(''.join(s.strip())) for s in userInput.split(',')
        ]

        return chordProgression
Exemple #5
0
txt = open(args.txt)
time.sleep(1)

current_chord = None
for line in txt:
    note, duration, chord = line.split()
    duration = float(fractions.Fraction(duration))

    if args.c and chord != 'None' and chord != current_chord:
        if args.x:
            print(f'CHORD {chord} {datetime.now().time()}')
        current_chord = chord
        if args.s:
            threading.Thread(target=play.play_chord_async,
                             args=(pychord.Chord(current_chord), 3)).start()

    if note[0] == 'R':
        time.sleep(util.duration_to_sec(duration, bpm))
    else:
        if args.x:
            print(f'NOTE {util.canonical_note(note)} {datetime.now().time()}')
        if args.n:
            keyboard.press(c.note_to_key[note])
        time.sleep(util.duration_to_sec(duration, bpm))
        if args.n:
            keyboard.release(c.note_to_key[note])

with mido.open_output() as outport:
    outport.send(mido.Message('stop'))
    def toPychord(self):
        """
        Convert this Chord object to a pychord.Chord.

        If this Chord is key irrespective, it is put into the key of C
        """
        chord_root = self.chord[:self.chord.find(":")]
        find_bass = self.chord.find("/")
        if find_bass == -1:
            chord_quality = self.chord[self.chord.find(":") + 1:]
            chord_bass = ''
        else:
            chord_quality = self.chord[self.chord.find(":") +
                                       1:self.chord.find("/")]
            chord_bass = self.chord[self.chord.find("/") + 1:]

        # Create notes list to reference later. Only sharps are used. Any flats can look to one index before their base.
        notes = [
            "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"
        ]
        # Record intervals for a generic scale (2 = whole step, 1 = half step)
        intervals = [2, 2, 1, 2, 2, 2, 1]

        # If key_irrespective, convert to key of C
        if self.key_irrespective:
            # Check for 'x:__' chords and return None
            if chord_root == 'x':
                return None

            # Check for 'b' in chord_root
            offset = 0
            if chord_root.find('b') != -1:
                offset = -1
                chord_root = chord_root[0]

            chord_root = int(chord_root) - 1  # index from 0 instead of 1
            chord_index = 3  # Begin at C
            for i in range(chord_root):
                chord_index += intervals[i]
            chord_index += offset
            chord_index %= len(notes)

            chord_root = notes[chord_index]

        # Definining quality conversions based on http://ismir2005.ismir.net/proceedings/1080.pdf
        #  and https://github.com/yuma-m/pychord/blob/master/pychord/constants/qualities.py
        #  reveals that 'maj' => 'M' if not alone and 'min' => 'm' if not alone are the only
        #  required conversions
        new_quality = chord_quality
        """if new_quality.find('maj') != -1 and new_quality != 'maj':
            new_quality = new_quality.replace('maj', 'M')
        if new_quality.find('min') != -1 and new_quality != 'min':
            new_quality = new_quality.replace('min', 'm')
        if new_quality.find('hdim7') != -1:
            new_quality = new_quality.replace('hdim7', 'm7-5')
        if new_quality.find('(9)') != -1:
            new_quality = new_quality.replace('(9)', '9')
        if new_quality.find('(11)') != -1:
            new_quality = new_quality.replace('(11)', '')
        if new_quality == 'M':
            new_quality = new_quality.replace('M', 'maj')
        if new_quality == 'M6':
            new_quality = new_quality.replace('M6', '6')
        if new_quality.find('6'):
            new_quality = new_quality.replace('6', '')
        if new_quality == 'M(b7)':
            new_quality = new_quality.replace('M(b7)', 'm7')"""
        if 'maj' in new_quality and new_quality != 'maj':
            # Replace 'maj' with 'M' for 7th chords
            new_quality = new_quality.replace('maj', 'M')
        if 'min' in new_quality and new_quality != 'min':
            # Replace 'min' with 'm' for 7th chords
            new_quality = new_quality.replace('min', 'm')
        if 'hdim7' in new_quality:
            new_quality = new_quality.replace('hdim7', 'm7-5')
        # Remove parenthesis from qualities
        if '(' in new_quality:
            new_quality = new_quality.replace('(', '')
        if ')' in new_quality:
            new_quality = new_quality.replace(')', '')

        # Create pychord.Chord with converted values (excluding bass)
        try:
            result_chord = pychord.Chord(chord_root + new_quality)
        except:
            if 'maj' in new_quality or 'M' in new_quality:
                new_quality = 'maj'
            elif 'min' in new_quality or 'm' in new_quality:
                new_quality = 'min'
            try:
                result_chord = pychord.Chord(chord_root + new_quality)
            except:
                # Something is funky with the formatting
                return None

        # If no bass or unreadable bass, return un-inverted chord
        try:
            int(chord_bass)
        except:
            return result_chord

        # Configure bass
        chord_notes = list(result_chord.components())
        try:
            result_chord = pychord.Chord(chord_root + new_quality + "/" +
                                         chord_notes[int(chord_bass) - 3])
        except:
            pass

        return result_chord
Exemple #7
0
    def handle(self, argv=None):
        """
        Main function.

        Parses command, load settings and dispatches accordingly.

        """
        help_message = "Please supply chord progression!. See --help for more options."
        parser = argparse.ArgumentParser(
            description=
            'chords2midi - Create MIDI files from written chord progressions.\n'
        )
        parser.add_argument('progression',
                            metavar='U',
                            type=str,
                            nargs='*',
                            help=help_message)
        parser.add_argument('-B',
                            '--bassline',
                            action='store_true',
                            default=False,
                            help='Throw an extra bassline on the pattern')
        parser.add_argument('-b',
                            '--bpm',
                            type=int,
                            default=80,
                            help='Set the BPM (default 80)')
        parser.add_argument('-t',
                            '--octave',
                            type=str,
                            default='4',
                            help='Set the octave(s) (ex: 3,4) (default 4)')
        parser.add_argument('-i',
                            '--input',
                            type=str,
                            default=None,
                            help='Read from an input file.')
        parser.add_argument('-k',
                            '--key',
                            type=str,
                            default='C',
                            help='Set the key (default C)')
        parser.add_argument('-n',
                            '--notes',
                            type=int,
                            default=99,
                            help='Notes in each chord (default all)')
        parser.add_argument('-d',
                            '--duration',
                            type=float,
                            default=1.0,
                            help='Set the chord duraction (default 1)')
        parser.add_argument(
            '-D',
            '--directory',
            action='store_true',
            default=False,
            help=
            'Output the contents to the directory of the input progression.')
        parser.add_argument(
            '-H',
            '--humanize',
            type=float,
            default=0.0,
            help=
            'Set the amount to "humanize" (strum) a chord, in ticks - try .11 (default 0.0)'
        )
        parser.add_argument(
            '-o',
            '--output',
            type=str,
            help=
            'Set the output file path. Default is the current key and progression in the current location.'
        )
        parser.add_argument(
            '-O',
            '--offset',
            type=float,
            default=0.0,
            help='Set the amount to offset each chord, in ticks. (default 0.0)'
        )
        parser.add_argument('-p',
                            '--pattern',
                            type=str,
                            default=None,
                            help='Set the pattern. Available patterns: ' +
                            (', '.join(patterns.keys())))
        parser.add_argument(
            '-r',
            '--reverse',
            action='store_true',
            default=False,
            help='Reverse a progression from C-D-E format into I-II-III format'
        )
        parser.add_argument('-v',
                            '--version',
                            action='store_true',
                            default=False,
                            help='Display the current version of chords2midi')

        args = parser.parse_args(argv)
        self.vargs = vars(args)

        if self.vargs['version']:
            version = pkg_resources.require("chords2midi")[0].version
            print(version)
            return

        # Support `c2m I III V and `c2m I,III,V` formats.
        if not self.vargs['input']:
            if len(self.vargs['progression']) < 1:
                print("You need to supply a progression! (ex I V vi IV)")
                return
            if len(self.vargs['progression']) < 2:
                progression = self.vargs['progression'][0].split(',')
            else:
                progression = self.vargs['progression']
        else:
            with open(self.vargs['input']) as fn:
                content = ''.join(fn.readlines()).strip()
                content = content.replace('\n', ' ').replace(',', '  ')
                progression = content.split(' ')
        og_progression = progression

        # If we're reversing, we don't need any of the MIDI stuff.
        if self.vargs['reverse']:
            result = ""
            key = self.vargs['key']
            for item in progression:
                comps = pychord.Chord(item).components()
                position = determine(comps, key, True)[0]
                if 'M' in position:
                    position = position.upper()
                    position = position.replace('M', '')
                if 'm' in position:
                    position = position.lower()
                    position = position.replace('m', '')
                if 'B' in position:
                    position = position + "b"
                    position = position.replace('B', '')

                result = result + position + " "
            print result
            return

        track = 0
        channel = 0
        ttime = 0
        duration = self.vargs['duration']  # In beats
        tempo = self.vargs['bpm']  # In BPM
        volume = 100  # 0-127, as per the MIDI standard
        bar = 0
        humanize_interval = self.vargs['humanize']
        directory = self.vargs['directory']
        num_notes = self.vargs['notes']
        offset = self.vargs['offset']
        key = self.vargs['key']
        octaves = self.vargs['octave'].split(',')
        root_lowest = self.vargs.get('root_lowest', False)
        bassline = self.vargs['bassline']
        pattern = self.vargs['pattern']

        # Could be interesting to do multiple parts at once.
        midi = MIDIFile(1)
        midi.addTempo(track, ttime, tempo)

        ##
        # Main generator
        ##
        has_number = False
        progression_chords = []

        # Apply patterns
        if pattern:
            if pattern not in patterns.keys():
                print("Invalid pattern! Must be one of: " +
                      (', '.join(patterns.keys())))
                return

            new_progression = []
            input_progression = progression[:]  # 2.7 copy
            pattern_mask = patterns[pattern]
            pattern_mask_index = 0
            current_chord = None

            while True:
                pattern_instruction = pattern_mask[pattern_mask_index]

                if pattern_instruction == "N":
                    if len(input_progression) == 0:
                        break
                    current_chord = input_progression.pop(0)
                    new_progression.append(current_chord)
                elif pattern_instruction == "S":
                    new_progression.append(current_chord)
                elif pattern_instruction == "X":
                    new_progression.append("X")

                if pattern_mask_index == len(pattern_mask) - 1:
                    pattern_mask_index = 0
                else:
                    pattern_mask_index = pattern_mask_index + 1
            progression = new_progression

        # We do this to allow blank spaces
        for chord in progression:

            # This is for # 'I', 'VI', etc
            progression_chord = to_chords(chord, key)
            if progression_chord != []:
                has_number = True

            # This is for 'C', 'Am', etc.
            if progression_chord == []:
                try:
                    progression_chord = [pychord.Chord(chord).components()]
                except Exception:
                    # This is an 'X' input
                    progression_chord = [None]

            chord_info = {}
            chord_info['notes'] = progression_chord[0]
            if has_number:
                chord_info['number'] = chord
            else:
                chord_info['name'] = chord

            if progression_chord[0]:
                chord_info['root'] = progression_chord[0][0]
            else:
                chord_info['root'] = None
            progression_chords.append(chord_info)

        # For each input..
        previous_pitches = []
        for chord_index, chord_info in enumerate(progression_chords):

            # Unpack object
            chord = chord_info['notes']
            # NO_OP
            if chord == None:
                bar = bar + 1
                continue
            root = chord_info['root']
            root_pitch = pychord.utils.note_to_val(
                notes.int_to_note(notes.note_to_int(root)))

            # Reset internals
            humanize_amount = humanize_interval
            pitches = []
            all_new_pitches = []

            # Turns out this algorithm was already written in the 1800s!
            # https://en.wikipedia.org/wiki/Voice_leading#Common-practice_conventions_and_pedagogy

            # a) When a chord contains one or more notes that will be reused in the chords immediately following, then these notes should remain, that is retained in the respective parts.
            # b) The parts which do not remain, follow the law of the shortest way (Gesetze des nachsten Weges), that is that each such part names the note of the following chord closest to itself if no forbidden succession XXX GOOD NAME FOR A BAND XXX arises from this.
            # c) If no note at all is present in a chord which can be reused in the chord immediately following, one must apply contrary motion according to the law of the shortest way, that is, if the root progresses upwards, the accompanying parts must move downwards, or inversely, if the root progresses downwards, the other parts move upwards and, in both cases, to the note of the following chord closest to them.
            root = None
            for i, note in enumerate(chord):

                # Sanitize notes
                sanitized_notes = notes.int_to_note(notes.note_to_int(note))
                pitch = pychord.utils.note_to_val(sanitized_notes)

                if i == 0:
                    root = pitch

                if root:
                    if root_lowest and pitch < root:  # or chord_index is 0:
                        pitch = pitch + 12  # Start with the root lowest

                all_new_pitches.append(pitch)

                # Reuse notes
                if pitch in previous_pitches:
                    pitches.append(pitch)

            no_melodic_fluency = False  # XXX: vargify
            if previous_pitches == [] or all_new_pitches == [] or pitches == [] or no_melodic_fluency:
                pitches = all_new_pitches
            else:
                # Detect the root direction
                root_upwards = None
                if pitches[0] >= all_new_pitches[0]:
                    root_upwards = True
                else:
                    root_upwards = False

                # Move the shortest distance
                if pitches != []:
                    new_remaining_pitches = list(all_new_pitches)
                    old_remaining_pitches = list(previous_pitches)
                    for i, new_pitch in enumerate(all_new_pitches):
                        # We're already there
                        if new_pitch in pitches:
                            new_remaining_pitches.remove(new_pitch)
                            old_remaining_pitches.remove(new_pitch)
                            continue

                    # Okay, so need to find the overall shortest distance from the remaining pitches - including their permutations!
                    while len(new_remaining_pitches) > 0:
                        nearest_distance = 9999
                        previous_index = None
                        new_index = None
                        pitch_to_add = None
                        for i, pitch in enumerate(new_remaining_pitches):
                            # XXX: DRY

                            # The Pitch
                            pitch_to_test = pitch
                            nearest = min(old_remaining_pitches,
                                          key=lambda x: abs(x - pitch_to_test))
                            old_nearest_index = old_remaining_pitches.index(
                                nearest)
                            if nearest < nearest_distance:
                                nearest_distance = nearest
                                previous_index = old_nearest_index
                                new_index = i
                                pitch_to_add = pitch_to_test

                            # +12
                            pitch_to_test = pitch + 12
                            nearest = min(old_remaining_pitches,
                                          key=lambda x: abs(x - pitch_to_test))
                            old_nearest_index = old_remaining_pitches.index(
                                nearest)
                            if nearest < nearest_distance:
                                nearest_distance = nearest
                                previous_index = old_nearest_index
                                new_index = i
                                pitch_to_add = pitch_to_test

                            # -12
                            pitch_to_test = pitch - 12
                            nearest = min(old_remaining_pitches,
                                          key=lambda x: abs(x - pitch_to_test))
                            old_nearest_index = old_remaining_pitches.index(
                                nearest)
                            if nearest < nearest_distance:
                                nearest_distance = nearest
                                previous_index = old_nearest_index
                                new_index = i
                                pitch_to_add = pitch_to_test

                        # Before we add it - just make sure that there isn't a better place for it.
                        pitches.append(pitch_to_add)
                        del old_remaining_pitches[previous_index]
                        del new_remaining_pitches[new_index]

                        # This is for the C E7 type scenario
                        if len(old_remaining_pitches) == 0:
                            for x, extra_pitch in enumerate(
                                    new_remaining_pitches):
                                pitches.append(extra_pitch)
                                del new_remaining_pitches[x]

                    # Final check - can the highest and lowest be safely folded inside?
                    max_pitch = max(pitches)
                    min_pitch = min(pitches)
                    index_max = pitches.index(max_pitch)
                    folded_max = max_pitch - 12
                    if (folded_max > min_pitch) and (folded_max
                                                     not in pitches):
                        pitches[index_max] = folded_max

                    max_pitch = max(pitches)
                    min_pitch = min(pitches)
                    index_min = pitches.index(min_pitch)

                    folded_min = min_pitch + 12
                    if (folded_min < max_pitch) and (folded_min
                                                     not in pitches):
                        pitches[index_min] = folded_min

                    # Make sure the average can't be improved
                    # XXX: DRY
                    if len(previous_pitches) != 0:
                        previous_average = sum(previous_pitches) / len(
                            previous_pitches)

                        # Max
                        max_pitch = max(pitches)
                        min_pitch = min(pitches)
                        index_max = pitches.index(max_pitch)
                        folded_max = max_pitch - 12

                        current_average = sum(pitches) / len(pitches)
                        hypothetical_pitches = list(pitches)
                        hypothetical_pitches[index_max] = folded_max
                        hypothetical_average = sum(hypothetical_pitches) / len(
                            hypothetical_pitches)
                        if abs(previous_average -
                               hypothetical_average) <= abs(previous_average -
                                                            current_average):
                            pitches[index_max] = folded_max
                        # Min
                        max_pitch = max(pitches)
                        min_pitch = min(pitches)
                        index_min = pitches.index(min_pitch)
                        folded_min = min_pitch + 12

                        current_average = sum(pitches) / len(pitches)
                        hypothetical_pitches = list(pitches)
                        hypothetical_pitches[index_min] = folded_min
                        hypothetical_average = sum(hypothetical_pitches) / len(
                            hypothetical_pitches)
                        if abs(previous_average -
                               hypothetical_average) <= abs(previous_average -
                                                            current_average):
                            pitches[index_min] = folded_min

                # Apply contrary motion
                else:
                    print("Applying contrary motion!")
                    for i, new_pitch in enumerate(all_new_pitches):
                        if i == 0:
                            pitches.append(new_pitch)
                            continue

                        # Root upwards, the rest move down.
                        if root_upwards:
                            if new_pitch < previous_pitches[i]:
                                pitches.append(new_pitch)
                            else:
                                pitches.append(new_pitch - 12)
                        else:
                            if new_pitch > previous_pitches[i]:
                                pitches.append(new_pitch)
                            else:
                                pitches.append(new_pitch + 12)

            # Bassline
            if bassline:
                pitches.append(root_pitch - 24)

            # Melody

            # Octave is a simple MIDI offset counter
            for octave in octaves:
                for note in pitches:
                    pitch = int(note) + (int(octave.strip()) * 12)

                    # Don't humanize bassline note
                    if bassline and (pitches.index(note) == len(pitches) - 1):
                        midi_time = offset + bar
                    else:
                        midi_time = offset + bar + humanize_amount

                    # Write the note
                    midi.addNote(track=track,
                                 channel=channel,
                                 pitch=pitch,
                                 time=midi_time,
                                 duration=duration,
                                 volume=volume)

                humanize_amount = humanize_amount + humanize_interval
                if i + 1 >= num_notes:
                    break
            bar = bar + 1
            previous_pitches = pitches

        ##
        # Output
        ##

        if self.vargs['output']:
            filename = self.vargs['output']
        elif self.vargs['input']:
            filename = self.vargs['input'].replace('.txt', '.mid')
        else:
            if has_number:
                key_prefix = key + '-'
            else:
                key_prefix = ''

            filename = key_prefix + '-'.join(og_progression) + '-' + str(tempo)
            if bassline:
                filename = filename + "-bassline"
            if pattern:
                filename = filename + "-" + pattern
            if os.path.exists(filename):
                filename = key_prefix + '-'.join(og_progression) + '-' + str(
                    tempo) + '-' + str(int(time.time()))
            filename = filename + '.mid'

            if directory:
                directory_to_create = '-'.join(og_progression)
                try:
                    os.makedirs(directory_to_create)
                except OSError as exc:  # Python >2.5
                    if exc.errno == errno.EEXIST and os.path.isdir(
                            directory_to_create):
                        pass
                    else:
                        raise
                filename = directory_to_create + '/' + filename

        with open(filename, "wb") as output_file:
            midi.writeFile(output_file)
Exemple #8
0
def harmonic_random(reffile):
    chords = []
    lf = open(reffile)
    nf = open(reffile)

    for line in lf:
        label, value, time = line.split()
        if random.randint(1,10) <= 3:
            continue
        time = datetime.strptime(time, '%H:%M:%S.%f')
        chord = gen_rand_chord()
        chords.append((pychord.Chord(chord), time))

    chord_groups = [[] for c in chords]
    for line in nf:
        label, value, time = line.split()
        if label != 'NOTE':
            continue
        time = datetime.strptime(time, '%H:%M:%S.%f')

        if time < chords[0][1]:
            continue
        for i in range(len(chords)-1):
            if chords[i][1] <= time < chords[i+1][1]:
                chord_groups[i].append(value[:-1])
                break
        else:
            assert chords[-1][1] <= time
            chord_groups[-1].append(value[:-1])

    bad_notes = 0
    total_notes = 0
    for i in range(len(chords)):
        chord = chords[i][0]
        notes = chord_groups[i]
        quality = chord.quality.quality
        if quality in major_quals:
            quality = 'major'
        elif quality in minor_quals:
            quality = 'natural_minor'
        else:
            quality = None

        if quality:
            scale = musthe.Scale(musthe.Note(chord.root), quality)
            scale_notes = [str(scale[i]) for i in range(len(scale))]
        else:
            scale = []

        for i in range(len(notes)):
            lower = max(0, i-HARMONIC_WINDOW)
            upper = min(len(notes)-1, i+HARMONIC_WINDOW)
            relevant_notes = [notes[j] for j in range(lower, upper+1)]

            for note in relevant_notes:
                if note in chord.components():# or musthe.Note(note) in scale:
                    break
            else:
                bad_notes += 1

        total_notes += len(notes)

    # print('{:>8}'.format(f'{bad_notes}/{total_notes}\t'), end='')
    # print(f'{bad_notes/total_notes*100:.0f}% error')
    return bad_notes / total_notes