-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmasterpiece.py
150 lines (133 loc) · 5.73 KB
/
masterpiece.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
import json
from midiutil.MidiFile import MIDIFile
from randomnote import RandomNote
class Masterpiece(object):
def __init__(self, rules_path="rules.json", length=4, tempo=90):
self.rules_path = rules_path
self.length = length
self.tempo = tempo
rules_file = open(rules_path, "r")
rules = json.load(rules_file)
rules_file.close()
self.rhythm = rules["rhythm"]
self.seq_chord = rules["seq_chord"]
self.seq_perc = rules["seq_perc"]
self.velocity = rules["velocity"]
self.rn = RandomNote(rules["notes"], rules["interval_upper"], rules["interval_lower"])
self.MyMIDI = MIDIFile(3)
self.current_track_number = 0
def create_melody_sequence(self):
seq_melody = []
for i in range(self.length):
for phrase in self.rhythm:
self.rn.reset()
for duration in phrase:
seq_melody.append((self.rn.random_note(), duration))
return seq_melody
def create_melody_track(self):
seq_melody = self.create_melody_sequence()
self.MyMIDI.addTrackName(
track=self.current_track_number,
time=0, trackName="piano")
self.MyMIDI.addTempo(
track=self.current_track_number,
time=0, tempo=self.tempo)
self.MyMIDI.addProgramChange(
tracknum=self.current_track_number,
channel=0, time=0, program=0)
pos = 0
for pitch, duration in seq_melody:
relative_pos = pos - int(pos / 4) * 4
if 0 <= relative_pos < 1:
vol = self.velocity["strong"]
elif 2 <= relative_pos < 3:
vol = self.velocity["intermediate"]
else:
vol = self.velocity["weak"]
self.MyMIDI.addNote(
track=self.current_track_number,
channel=0, pitch=pitch, time=pos, duration=duration, volume=vol)
if relative_pos in [0, 2]:
self.MyMIDI.addControllerEvent(
track=self.current_track_number,
channel=0, time=pos, controller_number=64, parameter=127)
self.MyMIDI.addControllerEvent(
track=self.current_track_number,
channel=0, time=pos + 1.96875, controller_number=64, parameter=0)
pos += duration
self.current_track_number += 1
def create_chord_track(self):
self.MyMIDI.addTrackName(
track=self.current_track_number,
time=0, trackName="chords")
self.MyMIDI.addTempo(
track=self.current_track_number,
time=0, tempo=self.tempo)
self.MyMIDI.addProgramChange(
tracknum=self.current_track_number,
channel=0, time=0, program=0)
# C D E F G A B | C D E F G A B | C
# 48 50 52 53 55 57 59 | 60 62 64 65 67 69 71 | 72
pos = 0
while pos < self.length * 16:
for item in self.seq_chord:
for pitch in item:
self.MyMIDI.addControllerEvent(
track=self.current_track_number,
channel=0, time=pos, controller_number=64, parameter=127)
self.MyMIDI.addControllerEvent(
track=self.current_track_number,
channel=0, time=pos + 1.96875, controller_number=64, parameter=0)
self.MyMIDI.addNote(
track=self.current_track_number,
channel=0, pitch=pitch, time=pos, duration=2, volume=76)
self.MyMIDI.addControllerEvent(
track=self.current_track_number,
channel=0, time=pos + 2, controller_number=64, parameter=127)
self.MyMIDI.addControllerEvent(
track=self.current_track_number,
channel=0, time=pos + 3.96875, controller_number=64, parameter=0)
self.MyMIDI.addNote(
track=self.current_track_number,
channel=0, pitch=pitch, time=pos + 2, duration=2, volume=68)
pos += 4
self.current_track_number += 1
def create_perc_track(self):
self.MyMIDI.addTrackName(
track=self.current_track_number,
time=0, trackName="perc")
self.MyMIDI.addTempo(
track=self.current_track_number,
time=0, tempo=self.tempo)
self.MyMIDI.addProgramChange(
tracknum=self.current_track_number,
channel=9, time=0, program=0)
pos = 0
while pos < self.length * 16:
if pos != 0:
self.MyMIDI.addNote(
track=self.current_track_number,
channel=9, pitch=49, time=pos, duration=0.5, volume=102)
for pitch, duration in self.seq_perc:
relative_pos = pos - int(pos / 4) * 4
if 0 <= relative_pos < 1:
vol = 102
elif 2 <= relative_pos < 3:
vol = 96
else:
vol = 92
self.MyMIDI.addNote(
track=self.current_track_number,
channel=9, pitch=pitch, time=pos, duration=duration, volume=vol)
pos += duration
self.current_track_number += 1
def create_midi_file(self, filename, melody=True, chord=True, perc=True):
if melody:
self.create_melody_track()
if chord:
self.create_chord_track()
if perc:
self.create_perc_track()
with open(filename, "wb") as midi_file:
self.MyMIDI.writeFile(midi_file)