0

I have a matrix (square - nxn) <--Depends on how many chords there are.

------------------------------------------
            Transition Matrix: (FROM)
------------------------------------------
 A     Bm   C#m    D     E    F#m    G#   
------------------------------------------
0.07  0.00  0.00  0.00  0.66  0.14  0.86 | A   | 
0.15  0.17  0.00  0.05  0.07  0.30  0.00 | Bm  | 
0.00  0.00  0.00  0.00  0.00  0.37  0.00 | C#m | 
0.19  0.00  0.39  0.16  0.14  0.00  0.00 | D   | (TO)
0.52  0.83  0.29  0.43  0.00  0.19  0.00 | E   | 
0.07  0.00  0.32  0.36  0.14  0.00  0.14 | F#m | 
0.00  0.00  0.00  0.00  0.00  0.00  0.00 | G#  | 

This matrix gets multiplied by another matrix which is:

1
0
0
0
0
0
0

This means the starting note is an A (1st row corresponds to A).

The next note is determined by the column of the Markov Transition Matrix which says there is a:

7% chance to become A

15% chance to become Bm

0% chance to become C#m, etc. etc.

(Done by matrix multiplication, using numPy).

So the resulting matrix is a 1col x nrows matrix.

This matrix will look like this:

0.07
0.15
0.00
0.19
0.52
0.07
0.00

^ This means that there is a 0.07% that the next note is an A, etc.

^ Using these probabilities can I play back those notes? Or generate a midi file based on it?

I should only get back one note (plus the first one), and based on the probability it should be an E.

LightninBolt74
  • 211
  • 4
  • 11
  • 1
    Which midi library are you using to generate the music from the notes? If all you want to do is generate the markov chain from the transition matrix, [this question](https://stackoverflow.com/questions/59858123/) may help. – CDJB Feb 05 '20 at 11:28
  • @CDJB I don't know, I'm new to python, anything simple is fine. Thanks for the link for the random generator. – LightninBolt74 Feb 05 '20 at 12:18

1 Answers1

1

I wrote a small program to demonstrate Markov generation, including output to a MIDI file using the music21 library.

Note that your transition matrix has two problems:

  1. The probabilities in the 5th column do not sum to 1.00. I replaced 0.66 with 0.65 to make it work.
  2. This is a matrix of chord transition probabilities, not note transitions. So my example involves chords (triads), not individual notes. Your question uses the word "note" many times where I think you meant "chord" instead, because all your examples involve chords such as B minor.

I wrote a program to repeat each chord 4 times, so that each chord lasts an entire measure of 4 beats.

I picked C major as the default instead of your A major. If you want to generate chords in a different key you can change the scale root in the calls to chord_to_names to chord_to_midi, using the "root_midi" parameter. For instance, call it with 57 instead of the default 60 to get A major.

Once you run this program it will generate chords.mid, which you can open in a program such a MuseScore or a midi editor.

Sample output:

Generated chords: [0, 0, 1, 4, 0, 3, 4, 5]
Note names:
['C', 'E', 'G']
['C', 'E', 'G']
['D', 'F', 'A']
['G', 'B', 'D']
['C', 'E', 'G']
['F', 'A', 'C']
['G', 'B', 'D']
['A', 'C', 'E']
MIDI Notes:
[60, 64, 67]
[60, 64, 67]
[62, 65, 69]
[55, 59, 62]
[60, 64, 67]
[53, 57, 60]
[55, 59, 62]
[57, 60, 64]

Which looks like this rendered in MuseScore:

enter image description here

import numpy as np

from music21.chord import Chord
from music21.stream import Part
from music21.midi.translate import streamToMidiFile


CHORD_TRANSITIONS = np.matrix([
  #              Transition Matrix: (FROM)
  # ---------------------------------------------
  #  I     ii     iii    IV      V     vi    viio   
  # ---------------------------------------------
    [0.07,  0.00,  0.00,  0.00,  0.65,  0.14,  0.86],  # | I    | 
    [0.15,  0.17,  0.00,  0.05,  0.07,  0.30,  0.00],  # | ii   | 
    [0.00,  0.00,  0.00,  0.00,  0.00,  0.37,  0.00],  # | iii  | 
    [0.19,  0.00,  0.39,  0.16,  0.14,  0.00,  0.00],  # | IV   | (TO)
    [0.52,  0.83,  0.29,  0.43,  0.00,  0.19,  0.00],  # | V    | 
    [0.07,  0.00,  0.32,  0.36,  0.14,  0.00,  0.14],  # | vi   |  
    [0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  0.00]]) # | viio |  

CHORD_TRANSITIONS = CHORD_TRANSITIONS.T  # swap axes for simpler lookup based on current chord

TRIADS = [
  (0, 4, 7),  # I
  (2, 5, 9),  # ii
  (4, 7, 11),  # iii
  (-7, -3, 0),  # IV
  (-5, -1, 2),  # V
  (-3, 0, 4),  # vi
  (-1, 2, 5),  # viio
] 

PC_TO_NAME = ('C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B')

def midi_to_pc(midi_note):
  return midi_note % 12

def midi_to_name(midi_note):
  return PC_TO_NAME[midi_to_pc(midi_note)]

def chord_to_midi(chord_index, root_midi=60):  # chord_index is from 0 to 6
  return [root_midi + x for x in TRIADS[chord_index]]

def chord_to_names(chord_index, root_midi=60):  # chord_index is from 0 to 6
  return [midi_to_name(note) for note in chord_to_midi(chord_index, root_midi)]

num_chords = len(CHORD_TRANSITIONS)

def generate(start_chord=0, length=8):
  cur = start_chord
  chords = [cur]
  for _ in range(length - 1):
    probs = CHORD_TRANSITIONS[cur,:].tolist()[0]
    chord = np.random.choice(num_chords, p=probs)
    chords.append(chord)
    cur = chord
  return chords

chords = generate()

print(f'Generated chords: {chords}')

print(f'Note names:')
for chord in chords:
  print(chord_to_names(chord))

print(f'MIDI notes:')
for chord in chords:
  print(chord_to_midi(chord))

print('Converting to MIDI')
part = Part()
for chord in chords:
  midis = chord_to_midi(chord)

  # Repeat each chord 4 times.
  for i in range(4):  
    part.append(Chord(midis, quarterLength=1))

mf = streamToMidiFile(part)

mf.open('chords.mid', 'wb')
mf.write()
mf.close()
eraoul
  • 1,072
  • 12
  • 19
  • Hello, I hope my answer/code was useful to you. If your question has been answered, could you please make sure to mark the answer as accepted? – eraoul Mar 05 '20 at 02:04