-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMiniSequencer.py
139 lines (122 loc) · 4.84 KB
/
MiniSequencer.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
from gi.repository import GLib
import ttcommon.Config as Config
from ttcommon.Util.CSoundClient import new_csound_client
from ttcommon.Util.NoteDB import Note
from ttcommon.Util.NoteDB import PARAMETER
class MiniSequencer:
def __init__(self, recordButtonState, recordOverSensitivity):
self.notesList = []
self.sequencer = []
self.pitchs = []
self.beat = 4
self.volume = 0.5
self.tempo = Config.PLAYER_TEMPO
self.checkOk = 0
self.tick = 0
self.id = 1000
self.csnd = new_csound_client()
self.startLooking = 0
self.recordState = 0
self.startPoint = 0
self.recordButtonState = recordButtonState
self.recordOverSensitivity = recordOverSensitivity
self.playbackTimeout = None
self.playState = 0
def setTempo(self, tempo):
self.tempo = tempo
GLib.source_remove(self.playBackTimeout)
self.playState = 0
def handleRecordButton(self, widget, data=None):
if not self.startLooking:
if widget.get_active() and not self.recordState:
self.button = 1
self.recordOverSensitivity(True)
self.beats = [i * 4 for i in range(self.beat)]
self.upBeats = [i + 2 for i in self.beats]
self.realTick = [i for i in range(self.beat * 4)]
self.clearSequencer()
self.startLooking = 1
self.startPlayback()
def handleOverButton(self, widget, data=None):
if not self.startLooking:
if widget.get_active() and not self.recordState:
self.button = 2
self.startLooking = 1
self.startPlayback()
def clearSequencer(self, widget=None):
for n in self.notesList:
self.csnd.loopDelete(n)
self.notesList = []
def getPlayState(self):
return self.playState
def startPlayback(self):
if not self.playState:
self.playbackTimeout = GLib.timeout_add(
int(60000 / self.tempo / 12), self.handleClock)
self.handleClock()
self.playState = 1
def stopPlayback(self):
if self.playbackTimeout is not None:
GLib.source_remove(self.playbackTimeout)
self.playbackTimeout = None
self.playState = 0
def recording(self, note):
if self.startLooking:
self.sequencer = []
self.pitchs = []
self.recordState = 1
self.startLooking = 0
self.recordButtonState(self.button, True)
self.startPoint = int(self.csnd.loopGetTick())
if self.startPoint == 0:
self.startPoint = self.beat * Config.TICKS_PER_BEAT - 1
if self.recordState:
self.pitchs.append(note.pitch)
self.sequencer.append(note)
def quantize(self, onset):
if (onset % 3) == 0:
return onset
elif (onset % 3) == 1:
return (onset // 3) * 3
elif (onset % 3) == 2:
return ((onset // 3) + 1) * 3
def adjustDuration(self, pitch, onset):
if pitch in self.pitchs:
offset = int(self.csnd.loopGetTick())
for note in self.sequencer:
if note.pitch == pitch and note.onset == onset:
if offset > note.onset:
note.duration = (offset - note.onset) + 4
else:
note.duration = ((offset + (
self.beat * Config.TICKS_PER_BEAT)) -
note.onset) + 4
note.onset = self.quantize(note.onset)
n = Note(0, note.trackId, self.id, note)
self.notesList.append(n)
self.id = self.id + 1
self.csnd.loopPlay(n, 1) # add as active
self.pitchs.remove(pitch)
def adjustSequencerVolume(self, volume):
self.volume = volume
for n in self.notesList:
self.csnd.loopUpdate(n, PARAMETER.AMPLITUDE,
n.cs.amplitude * self.volume, 1)
def handleClock(self):
currentTick = int(self.csnd.loopGetTick())
t = currentTick / 3
if self.tick != t:
self.tick = t
if self.startLooking:
if self.tick in self.beats:
self.recordButtonState(self.button, True)
if self.tick in self.upBeats:
self.recordButtonState(self.button, False)
if self.recordState:
if currentTick < self.startPoint:
self.checkOk = 1
if currentTick >= self.startPoint and self.checkOk:
self.checkOk = 0
self.recordState = 0
self.recordButtonState(self.button, False)
return True