-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathex-add-notes.py
executable file
·61 lines (50 loc) · 2.16 KB
/
ex-add-notes.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
#!/usr/bin/env python3
#------------------------------------------------------------------------
# pylive: ex-add-notes
#
# Add 16 randomly-generated notes to a clip.
# The first track must be a MIDI track.
#------------------------------------------------------------------------
import live
import time
import random
def main():
#--------------------------------------------------------------------------------
# Connect to the Live set.
#--------------------------------------------------------------------------------
set = live.Set(scan=True)
#--------------------------------------------------------------------------------
# Connect to the Live set.
#--------------------------------------------------------------------------------
track = set.tracks[0]
if not track.is_midi_track:
raise ValueError("First track must be a MIDI track")
clip = set.tracks[0].clips[0]
if clip is None:
clip = track.create_clip(0, 4)
if not clip.is_midi_clip:
raise ValueError("Clip at [0, 0] must be a MIDI clip")
print("Populating clip [0, 0] with random notes")
for n in range(32):
note = generate_random_note(clip)
print(" - Adding note %d at time %.2f" % (note[0], note[1]))
clip.add_note(*note)
def generate_random_note(clip: live.Clip):
#--------------------------------------------------------------------------------
# Generate a random note in a minor scale.
#--------------------------------------------------------------------------------
scale = (0, 2, 3, 5, 7, 8, 10)
degree = random.choice(scale)
octave = random.randrange(3)
fundamental = 60
pitch = fundamental + (octave * 12) + degree
#--------------------------------------------------------------------------------
# Generate a random start time, rounded to the nearest note
#--------------------------------------------------------------------------------
start_time = int(random.uniform(0, clip.length - 0.5) * 4) / 4
duration = 0.5
velocity = random.randrange(0, 127)
mute = False
return pitch, start_time, duration, velocity, mute
if __name__ == "__main__":
main()