Exemple #1
0
    def major_triad(note):
        """Get the major triad of a Note

        :param note: Note
        :returns: Chord
        """
        mingus_chord = _MingusChord.major_triad(note.letter)
        return Chord.mingusChord_to_chord(mingus_chord, note)
Exemple #2
0
 def getChordOrArpeggio(self, input_bar):
     notes = input_bar.get_note_names()
     random.seed()
     chord = chords.major_triad(notes[random.randint(0,len(notes) - 1)])
     new_bar = Bar()
     #nc = NoteContainer(chord)
     #new_bar.place_notes(nc, 1)
     #return new_bar
     if(len(notes) % 2 != 0):
         #this will be a chord
         nc = NoteContainer(chord)
         new_bar.place_notes(nc, 1)
         return new_bar
     else:
         #this will be an arpeggio
         duration = 0
         print(str(len(chord)))
         for note in chord:
             if(duration == 0):
                 duration = 2
             else:
                 duration = 4
             new_bar.place_notes(note, duration)
         return new_bar
 def test_major_triad(self):
     self.assertEqual(["C", "E", "G"], chords.major_triad("C"))
     self.assertEqual(["E", "G#", "B"], chords.major_triad("E"))
     self.assertEqual(["Eb", "G", "Bb"], chords.major_triad("Eb"))
Exemple #4
0
def generate_accompaniment(net_output_chords):
    generated_chords = []
    chords = []
    for chord in net_output_chords:
        root, key = (chord.split(":"))
        print(root, key)
        if key == 'maj':
            chords.append(ch.major_triad(root))
        if key == 'min':
            chords.append(ch.minor_triad(root))      

    print(chords)  

    key = chords[0][0]
    print('key', key)
    if not fluidsynth.init(SF2):
        print("Couldn't load soundfont", SF2)
        sys.exit(1)

    print(dir(fluidsynth.midi))
    # fluidsynth.midi.start_audio_output()
    fluidsynth.midi.start_recording()
    phrase = 0
    while phrase == 0:
        i = 0
        for chord in chords:
            print("chord", chord)
            c = NoteContainer(chords[i])
            generated_chords.append([{'note':cc.name.replace("B#","B").replace("E#","E").replace("##","#"), 'octave':cc.octave} for cc in c])
            l = Note(c[0].name)
            p = c[1]
            l.octave_down()
            print(ch.determine(chords[i])[0])

            # Play chord and lowered first note
            # fluidsynth.midi.MidiFileOut.write_NoteContainer("test.mid", c)
            print("NEW CHORD = ", c)

            if PLAY_ENABLED:

                fluidsynth.play_NoteContainer(c)
                fluidsynth.play_Note(l)
                time.sleep(1.0)

                # Play highest note in chord
                fluidsynth.play_Note(c[-1])

                # 50% chance on a bass note

                if random() > 0.50:
                    p = Note(c[1].name)
                    p.octave_down()
                    fluidsynth.play_Note(p)
                time.sleep(0.50)

                # 50% chance on a ninth

                if random() > 0.50:
                    l = Note(intervals.second(c[0].name, key))
                    l.octave_up()
                    fluidsynth.play_Note(l)
                time.sleep(0.25)

                # 50% chance on the second highest note

                if random() > 0.50:
                    fluidsynth.play_Note(c[-2])
                time.sleep(0.25)
                fluidsynth.stop_NoteContainer(c)
                fluidsynth.stop_Note(l)
                fluidsynth.stop_Note(p)
            i += 1
        print("-" * 20)
        phrase = 1
        return generated_chords
Exemple #5
0
def get_diatonic_upper_chord_extension(chord,
                                       extension,
                                       key=None,
                                       mode='major'):
    '''
    params:
        - chord is a (parsed) shorthand symbol e.g. 'Dm7'
        - extension is an integer representing the target extension (e.g. 3 to give the 3rd (major or minor))
    '''

    root, chord_type = chord_note_and_family(chord)

    extension_index = {
        1: 0,
        3: 1,
        5: 2,
        7: 3,
        9: 4,
        2: 4,
        11: 5,
        4: 5,
        13: 6,
        6: 6
    }

    # we consider the thirteenth chords which arise from diatonic chord extensions only,
    # we then check if our chord is a subchord of any of these diatonic thirteenths;
    # if it is, then we assume that it plays the role of the chord (roman numeral) which is most likely to generate that thirteenth.
    # e.g. if we find a G13 then we assume it comes from G7 (V7 in C)

    # More details:
    # if key is not specified, then default to the most conventional extensions for a given chord type (https://en.wikipedia.org/wiki/Extended_chord#Chord_structure
    # Note: Would this be different in a different mode/tonality (e.g. extend me a IV chord while playing a locrian tune...)? Should probably factor this in... (TO-DO)
    # See also https://www.reddit.com/r/musictheory/comments/8becrf/when_to_apply_which_chord_extension/

    # M7 -> M13 (M7, 9, #11*, 13) *by convention
    # m7 -> m13 (m7, 9, 11, 13)
    # 7 -> 13 (7, 9, 11, 13)
    # m7b5 -> m7b5b9b13 (7, b9, 11, b13) (e.g. B D F A -> B D F A C E G)

    # if key is specified then we can work out the degree of our chord (e.g. an FM7 in C is a IV chord) and be more clever with extensions

    # IM7 -> M13#11
    # IIm7 -> m13 (e.g. DFACEGB) - note that this has a major 13th (https://music.stackexchange.com/questions/16932/why-does-a-cm13-chord-use-a-instead-of-a)
    # IIIm7 -> m13b9
    # IVM7 -> M13#11
    # V7 -> 13
    # VIm7 -> m7b13 (e.g. ACEGBDF)
    # VIIm7b5 -> m7b5b9b13
    def assume_key(root, chord_type, mode):

        if mode == 'harmonic_minor':
            if chord_type in ['7b9']:
                # assume V (C7b9 implies F harmonic minor, i.e. 7b9 is phyrgian dominant (V mode of HM)) TODO: refactor?
                return notes.int_to_note(notes.note_to_int(root) - 7)

        if chord_type in ['M7', 'M9', 'M13', 'M6']:
            return root
        elif chord_type in ['m7', 'm9', 'm11', 'm13']:
            # assume II, e.g. Dm7 -> return C
            return notes.int_to_note(notes.note_to_int(root) - 2)
        elif chord_type in ['m7b9', 'm11b9', 'm13b9']:
            # assume III
            return notes.int_to_note(notes.note_to_int(root) - 4)
        elif '#11' in chord_type:
            # assume IV
            return notes.int_to_note(notes.note_to_int(root) - 5)
        elif chord_type in ['7', '9', '11', '13']:
            # assume V
            return notes.int_to_note(notes.note_to_int(root) - 7)
        elif chord_type in ['m7b13']:
            # assume VI
            return notes.int_to_note(notes.note_to_int(root) - 9)
        elif ('b5' in chord_type) or ('dim' in chord_type):
            # assume VII
            return notes.int_to_note(notes.note_to_int(root) - 11)
        elif chord_type in [
                '7b9'
        ]:  # TODO: refactor so that this is not an ad hoc exception (given 7b9 is covered below) but instea maybe automatically check all hminor modes etc
            pass
        else:
            print(
                f'\nWarning: utilities.assume_key() does not know how to handle chord_type {chord_type}'
            )
            pass

    if key is None:
        key = assume_key(root, chord_type, mode)

        # TODO: handle modes / alterations (also see above)
        # assume_key() is assuming major tonality (more precisely, diatonic_thirteen() is assuming major tonality and assume_key() provides accordingly)
        if key is None:  # if assume_key didn't work e.g. for a 7b9 which does not arise on any 7th chord built from the major scale
            if chord_type == '7b9':
                diatonic_extended_chord = chords.dominant_flat_ninth(
                    root) + chords.major_triad(
                        intervals.minor_second(root))[1:]  # 1 3 5 b7 b9 11 b13
            return diatonic_extended_chord[extension_index[extension]]

    try:
        diatonic_extended_chord = diatonic_thirteenth(root, key)
    except NoteFormatError:
        try:
            diatonic_extended_chord = diatonic_thirteenth(root, synonyms[key])
        except KeyError:
            print(f'Problem fetching diatonic_thirteenth({root},{key})')

    return diatonic_extended_chord[extension_index[extension]]
Exemple #6
0
	def test_major_triad(self):
		self.assertEqual(["C", "E", "G"], chords.major_triad("C"))
		self.assertEqual(["E", "G#", "B"], chords.major_triad("E"))
		self.assertEqual(["Eb", "G", "Bb"], chords.major_triad("Eb"))
Exemple #7
0
 def test_major_triad(self):
     self.assertEqual(['C', 'E', 'G'], chords.major_triad('C'))
     self.assertEqual(['E', 'G#', 'B'], chords.major_triad('E'))
     self.assertEqual(['Eb', 'G', 'Bb'], chords.major_triad('Eb'))
 def test_major_triad(self):
     self.assertEqual(['C', 'E', 'G'], chords.major_triad('C'))
     self.assertEqual(['E', 'G#', 'B'], chords.major_triad('E'))
     self.assertEqual(['Eb', 'G', 'Bb'], chords.major_triad('Eb'))