예제 #1
0
def generate_hypercube_progression(chrd):
    bin_note_ints = chord_to_binary(chrd)
    note_ints = bin_to_note_ints(bin_note_ints)
    path = get_hypercube_neighbor_path(note_ints)
    if len(path):
        return ChordProgression(path)
    else:
        return ChordProgression([])
예제 #2
0
def get_chord_prog(bs, transpose=0):
    """コード進行取得、移調する(オプション)"""

    cp = ChordProgression([
        elem.text.replace('♭', 'b').replace('maj', 'M')
        for elem in bs.select('span ruby rt')
        if elem.text not in ('N.C', 'N.C.')
    ])
    cp.transpose(transpose)

    return ' '.join([str(c) for c in cp])
예제 #3
0
 def test_self_add(self):
     cp1 = ChordProgression(["C", "F", "G"])
     cp2 = ChordProgression(["Am", "Em"])
     cp1 += cp2
     self.assertEqual(len(cp1), 5)
     self.assertEqual(
         cp1.chords,
         [Chord("C"),
          Chord("F"),
          Chord("G"),
          Chord("Am"),
          Chord("Em")])
예제 #4
0
def chord_transpose(chords, step):
    """Function that tranposes the chords by specified step amount

    Args:
        chords (list): List of chords to transpose
        step (int): The number of steps to transpose up (positive integer) or down (negative integer)

    Returns:
        list: tuple containing list of original chords and list of transposed chords
    """
    original_chords = ChordProgression(chords)
    transposed_chords = ChordProgression(chords)
    transposed_chords.transpose(step)
    return original_chords, transposed_chords
예제 #5
0
 def test_set_item(self):
     cp = ChordProgression(["C", "F", "G"])
     cp[1] = Chord("E")
     self.assertEqual(cp[0], Chord("C"))
     self.assertEqual(cp[1], Chord("E"))
     self.assertEqual(cp[2], Chord("G"))
     self.assertEqual(len(cp), 3)
예제 #6
0
def generate_chord_progression(mdl,
                               starting_chord,
                               length,
                               max_memory=4,
                               random_neighbors=False):
    progression = ChordProgression(Chord(starting_chord))
    current_chord = starting_chord
    memory = []
    top_k = 10
    if max_memory > 0:
        memory = [current_chord]

    for i in range(0, length):
        next_chord = current_chord
        # print("current_chord: ", current_chord)
        neighbors = mdl.get_nearest_neighbors(next_chord, top_k)
        neighbor_chords = [n[1] for n in neighbors]
        # print("\tneighbors:", neighbor_chords)
        # choose random neighbor
        if random_neighbors:
            processed_neighbors = np.random.choice(neighbor_chords, top_k)
        else:
            processed_neighbors = neighbor_chords

        if len(processed_neighbors):
            # choose sequential neighbors
            for neighbor in processed_neighbors:
                if neighbor != next_chord and neighbor not in memory:
                    next_chord = neighbor
                    memory.append(next_chord)
                    if len(memory) >= max_memory:
                        memory.pop(0)  # remove first element

            # random_neighbors = np.random.choice(neighbor_chords, 1)
            # print("\trandom_neighbors: ", random_neighbors)
            # next_chord = random_neighbors[0]

            current_chord = next_chord
            if current_chord:
                chrd = Chord(current_chord)
                progression.append(chrd)
    return progression
예제 #7
0
def main():
    args = parse_args()
    chords = ChordProgression(args.chord)

    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.triangle,
                              osc1_volume=1.0,
                              use_osc2=False)

    for chord in chords:
        notes = chord.components_with_pitch(root_pitch=3)
        print("Play {}".format(chord))
        player.play_wave(synthesizer.generate_chord(notes, 1.0))
예제 #8
0
def get_chord_progression_from_file(f_name, debug=False):
    """
    Takes a file name returns a chord progression

    :return ChordProgression
    """

    if debug:
        print("processing: {}".format(f_name))
    mlt = Multitrack()
    try:
        mlt.parse_midi(filename=f_name, binarized=True)
        merged_mlt = mlt.get_merged_pianoroll(mode='max')
    except:
        print("unable to parse. skipping: {}".format(f_name))
        return ChordProgression()  # empty progression

    chord_progression = ChordProgression([])
    last_chord_change = 0
    for i, x in enumerate(merged_mlt):
        # print(x)
        notes = np.where(x)
        num_notes = np.sum(x)
        if num_notes:
            if num_notes > 1:
                chord_notes = INT_TO_NOTE[notes[0] % 12]
                chord_name = note_to_chord(chord_notes.tolist())
                if chord_name:
                    chord = chord_name[0]
                    if len(chord_progression.chords
                           ) == 0 or chord_progression.chords[-1] != chord:
                        # print("chord: ", notes[0], chord_notes, chord_name)
                        chord_progression.append(chord)
            # else:
            #     print("note: ", notes[0])
    # print(chord_progression)
    return chord_progression
예제 #9
0
 def test_transpose(self):
     cp = ChordProgression(["C", "F", "G"])
     cp.transpose(3)
     self.assertEqual(cp.chords, [Chord("Eb"), Chord("Ab"), Chord("Bb")])
예제 #10
0
 def test_pop(self):
     cp = ChordProgression(["C", "D", "E"])
     c = cp.pop()
     self.assertEqual(len(cp), 2)
     self.assertEqual(c, Chord("E"))
예제 #11
0
 def test_insert(self):
     cp = ChordProgression(["C", "D", "E"])
     cp.insert(0, "F")
     self.assertEqual(len(cp), 4)
     self.assertEqual(cp.chords[0], Chord("F"))
예제 #12
0
 def test_append(self):
     cp = ChordProgression(["C", "D", "E"])
     cp.append("F")
     self.assertEqual(len(cp), 4)
     self.assertEqual(cp.chords[-1], Chord("F"))
예제 #13
0
 def test_multiple_chords_str(self):
     c1 = "C"
     c2 = "D"
     cp = ChordProgression([c1, c2])
     self.assertEqual(cp.chords, [Chord(c1), Chord(c2)])
예제 #14
0
 def test_multiple_chords(self):
     c1 = Chord("C")
     c2 = Chord("D")
     cp = ChordProgression([c1, c2])
     self.assertEqual(cp.chords, [c1, c2])
예제 #15
0
 def test_one_chord_list_str(self):
     c = "C"
     cp = ChordProgression([c])
     self.assertEqual(cp.chords, [Chord(c)])
예제 #16
0
 def test_get_item(self):
     cp = ChordProgression(["C", "F", "G"])
     self.assertEqual(cp[0], Chord("C"))
     self.assertEqual(cp[1], Chord("F"))
     self.assertEqual(cp[-1], Chord("G"))
예제 #17
0
 def test_one_chord_invalid_type(self):
     c = 1
     with self.assertRaises(TypeError):
         ChordProgression(c)
예제 #18
0
 def test_invalid_eq(self):
     cp = ChordProgression(["C", "F", "G"])
     with self.assertRaises(TypeError):
         print(cp == 0)
예제 #19
0
 def test_eq(self):
     cp1 = ChordProgression(["C", "F", "G"])
     cp2 = ChordProgression(["C", "F", "G"])
     self.assertEqual(cp1, cp2)
     self.assertIsNot(cp1, cp2)
예제 #20
0
 def test_slice(self):
     cp = ChordProgression(["C", "F", "G"])
     self.assertEqual(cp[0:1], [Chord("C")])
     self.assertEqual(cp[1:], [Chord("F"), Chord("G")])
     self.assertEqual(cp[0::2], [Chord("C"), Chord("G")])
예제 #21
0
 def test_none(self):
     cp = ChordProgression()
     self.assertEqual(cp.chords, [])
예제 #22
0
    "G": 7,
    "G#": 8,
    "Ab": 8,
    "A": 9,
    "A#": 10,
    "Bb": 10,
    "B": 11
}
intervals = [
    "unison", "m2", "maj2", "m3", "maj3", "p4", "tritone"
    "p5", "m6", "maj6", "m7", "maj7"
]  # to index

# sample chord progression
testprog = ["Am7", "Cmaj7", "Fmaj7", "Em7"]
prog = ChordProgression(testprog)
chordnotes = [p.components_with_pitch(4) for p in prog]

tur = turtle.Turtle()
turtle.hideturtle()  # hide turtle arrow

for i, chord in enumerate(chordnotes):
    indices = []
    for note in chord:
        index = chromatic[
            note[:-1]]  # get the note and its index for interval purposes
        indices.append(index)
    ints = []
    for j, ind in enumerate(indices):
        if j == 0:
            pass
예제 #23
0
 def test_one_chord_list(self):
     c = Chord("C")
     cp = ChordProgression([c])
     self.assertEqual(cp.chords, [c])