forked from Jobsecond/random-midi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomnote.py
29 lines (24 loc) · 862 Bytes
/
randomnote.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
# -*- coding: utf-8 -*-
import random
class RandomNote(object):
def __init__(self, choose_from, interval_upper, interval_lower):
self.last_played = 0
self.notes = choose_from
self.interval_upper = interval_upper
self.interval_lower = interval_lower
def random_note(self):
while True:
note = random.choice(range(1, len(self.notes) + 1))
if not self.last_played:
break
else:
if random.choice(self.interval_upper) \
>= abs(note - self.last_played) \
>= random.choice(self.interval_lower):
break
else:
continue
self.last_played = note
return self.notes[self.last_played - 1]
def reset(self):
self.last_played = 0