How to use pychord - 8 common examples

To help you get started, we’ve selected a few pychord examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Miserlou / chords2midi / chords2midi / c2m.py View on Github external
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
github erm / guitarlette / src / parser.py View on Github external
from typing import Optional, List
from dataclasses import dataclass, field


from pychord import Chord


@dataclass
class SongToken:
    content: str
    html_class: Optional[str] = None
    chord: Optional[Chord] = None

    def __post_init__(self) -> None:
        if self.content == "":
            self.html_class = "delimiter"
        else:
            try:
                self.chord = Chord(self.content.strip())
            except ValueError:
                pass
            else:
                self.html_class = "chord"

    def transpose(self, degree: int) -> None:
        if self.chord is None:
            raise ValueError("Transpose may only be called on chord tokens")
        assert self.chord is not None
github erm / guitarlette / src / song.py View on Github external
def render(self) -> str:
        s = self.content.strip().decode()
        if s == "":
            return f"<span class="delimiter"></span>"
        if s == "{$s}":
            return " "
        if s == "{$n}" and self.is_new_row:
            self.is_new_row = False
            return "<div class="row">"
        if s == "{$n}" and not self.is_new_row:
            self.is_new_row = True
            return "</div>"

        try:
            chord = Chord(s)
        except ValueError:
            return f"<span class="lyric">{s}</span>"

        if self.degree:
            chord.transpose(self.degree)
            s = str(chord)

        return f"<span class="chord">{s}</span>"
github Miserlou / chords2midi / chords2midi / c2m.py View on Github external
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)
github erm / guitarlette / backend / project / parser.py View on Github external
def parse_token(self, token: str) -> Token:
        try:
            chord = Chord(token)
        except ValueError:
            return Token(content=token)
        else:
            if token not in self.chords:
                self.chords.append(token)
        return Token(content=token, chord=chord)
github erm / guitarlette / src / parser.py View on Github external
def __post_init__(self) -> None:
        if self.content == "":
            self.html_class = "delimiter"
        else:
            try:
                self.chord = Chord(self.content.strip())
            except ValueError:
                pass
            else:
                self.html_class = "chord"
github Miserlou / chords2midi / chords2midi / c2m.py View on Github external
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
github Miserlou / chords2midi / chords2midi / c2m.py View on Github external
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 &lt; 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:

pychord

A library to handle musical chords in python.

MIT
Latest version published 12 months ago

Package Health Score

56 / 100
Full package analysis

Popular pychord functions