forked from brentelliott/PyReaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyReaper.py
306 lines (227 loc) · 7.23 KB
/
PyReaper.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Forked from brentelliott/PyReaper (PyReaper v0.00002 by Brent Elliott)
# https://github.com/brentelliott/PyReaper
from reaper_python import *
from math import log
from contextlib import contextmanager
@contextmanager
def undoable(message):
""" Call "RPR_Undo_EndBlock()" automatically even when the script crashes. Very useful for testing and debugging.
And using "with" statement keeps the actual script code clean and readable."""
RPR_Undo_BeginBlock2(0)
try:
yield
finally:
RPR_Undo_EndBlock2(0,message,-1)
@contextmanager
def noUIRefresh():
""" Call RPR_PreventUIRefresh(-1) automatically even when the script crashes."""
RPR_PreventUIRefresh(1)
try:
yield
finally:
RPR_PreventUIRefresh(-1)
def msg(m):
RPR_ShowConsoleMsg(m)
RPR_ShowConsoleMsg('\n')
#i hate this
def time_to_beats(f):
beat_tuple = RPR_TimeMap2_timeToBeats(0, f, 0, 0, 0, 0)
bars = int(beat_tuple[3]+1)
beats = int(beat_tuple[0]+1)
decimal = int(round(beat_tuple[0]-(beats-1), 2)*100)
return (bars, beats, decimal)
class ReaperProject(object):
def __init__(self, project_number=0):
#set project 0 as default
#globalize this
self.project_number = project_number
class ReaperMediaItem(object):
def __init__(self, id):
self.id = id
def get_all_takes(self):
takes = []
num_takes = RPR_GetMediaItemNumTakes(self.id)
for take in range(num_takes):
takes.append(RPR_GetMediaItemTake(self.id, take))
return takes
#mute
@property
def mute(self):
if RPR_GetMediaItemInfo_Value(self.id, 'B_MUTE') == 0.0:
return False
else:
return True
@mute.setter
def mute(self, b):
if b:
RPR_SetMediaItemInfo_Value(self.id, 'B_MUTE', 1.0)
else:
RPR_SetMediaItemInfo_Value(self.id, 'B_MUTE', 0.0)
@property
def position(self):
return RPR_GetMediaItemInfo_Value(self.id, 'D_POSITION')
@position.setter
def position(self, l):
RPR_SetMediaItemInfo_Value(self.id, 'D_POSITION', l)
@property
def length(self):
return RPR_GetMediaItemInfo_Value(self.id, 'D_LENGTH')
@length.setter
def length(self, l):
RPR_SetMediaItemInfo_Value(self.id, 'D_LENGTH', l)
class ReaperFX(object):
def __init__(self):
pass
class ReaperTrack(object):
def __init__(self, id):
self.id = id
#self.media_items = []
def get_all_media_items(self):
media_items = []
num_items = RPR_GetTrackNumMediaItems(self.id)
for item in range(num_items):
media_items.append(ReaperMediaItem(RPR_GetTrackMediaItem(self.id, item)))
return media_items
@property
def number(self):
return int(RPR_GetMediaTrackInfo_Value(self.id, 'IP_TRACKNUMBER'))
#volume getter and setter
@property
def volume(self):
return RPR_GetMediaTrackInfo_Value(self.id, 'D_VOL')
@volume.setter
def volume(self, value):
RPR_SetMediaTrackInfo_Value(self.id, 'D_VOL', value)
#db getter and setter
@property
def db(self):
volume = RPR_GetMediaTrackInfo_Value(self.id, 'D_VOL')
return (20 * log(volume)) / log(10)
@db.setter
def db(self, value):
volume = 10 ** (float(value) / 20)
RPR_SetMediaTrackInfo_Value(self.id, 'D_VOL', volume)
#pan getter and setter
@property
def pan(self):
return RPR_GetMediaTrackInfo_Value(self.id, 'D_PAN')
@pan.setter
def pan(self, value):
RPR_SetMediaTrackInfo_Value(self.id, 'D_PAN', value)
#mute
@property
def mute(self):
if RPR_GetMediaTrackInfo_Value(self.id, 'B_MUTE') == 0.0:
return False
else:
return True
@mute.setter
def mute(self, b):
if b:
RPR_SetMediaTrackInfo_Value(self.id, 'B_MUTE', 1.0)
else:
RPR_SetMediaTrackInfo_Value(self.id, 'B_MUTE', 0.0)
#solo property
@property
def solo(self):
#replace with GetMediaTrackInfo_Value
flag = RPR_GetTrackState(self.id, 0)[2]
if flag & 16:
return True
else:
return False
@solo.setter
def solo(self, value):
#solo_type
#0 = off
#1 = solo
#2 = solo in place
RPR_SetMediaTrackInfo_Value(self.id, 'I_SOLO', value)
#phase_invert
@property
def phase_invert(self):
if RPR_GetMediaTrackInfo_Value(self.id, 'B_PHASE') == 0.0:
return False
else:
return True
@phase_invert.setter
def phase_invert(self, value):
if value:
RPR_SetMediaTrackInfo_Value(self.id, 'B_PHASE', 1.0)
else:
RPR_SetMediaTrackInfo_Value(self.id, 'B_PHASE', 0.0)
@property
def record_monitor(self):
return int(RPR_GetMediaTrackInfo_Value(self.id, 'I_RECMON'))
@record_monitor.setter
def record_monitor(self, value):
RPR_SetMediaTrackInfo_Value(self.id, 'I_RECMON', value)
@property
def fx_enabled(self):
if RPR_GetMediaTrackInfo_Value(self.id, 'I_FXEN') == 0.0:
return False
else:
return True
@fx_enabled.setter
def fx_enabled(self, value):
if value:
RPR_SetMediaTrackInfo_Value(self.id, 'I_FXEN', 1.0)
else:
RPR_SetMediaTrackInfo_Value(self.id, 'I_FXEN', 0.0)
@property
def record_arm(self):
if RPR_GetMediaTrackInfo_Value(self.id, 'I_RECARM') == 0.0:
return False
else:
return True
@record_arm.setter
def record_arm(self, value):
if value:
RPR_SetMediaTrackInfo_Value(self.id, 'I_RECARM', 1.0)
else:
RPR_SetMediaTrackInfo_Value(self.id, 'I_RECARM', 0.0)
#name getter and setter
@property
def name(self):
return RPR_GetSetMediaTrackInfo_String(self.id, "P_NAME", '', False)[3]
@name.setter
def name(self, value):
#holy shit GetSetTrackState is a disaster
RPR_GetSetMediaTrackInfo_String(self.id, "P_NAME", value, True)
#color getter and setter
@property
def color(self):
return RPR_GetTrackColor(self.id)
@color.setter
def color(self, value):
if type(value) is str:
if value.startswith('#'):
RPR_SetTrackColor(self.id, int(value.strip('#'), 16))
elif type(value) is tuple:
h = '%02x%02x%02x' % value
RPR_SetTrackColor(self.id, int(h, 16))
else:
raise TypeError
#selection
@property
def selected(self):
if RPR_GetMediaTrackInfo_Value(self.id, 'I_SELECTED') == 1.0:
return True
else:
return False
@selected.setter
def selected(self, value):
if value:
RPR_SetTrackSelected(self.id, 1)
else:
RPR_SetTrackSelected(self.id, 0)
def get_all_tracks():
tracks = []
for i in range(RPR_CountTracks(0)):
tracks.append(ReaperTrack(RPR_GetTrack(0, i)))
return tracks
def get_selected_tracks():
tracks = []
for i in range(RPR_CountSelectedTracks(0)):
tracks.append(ReaperTrack(RPR_GetSelectedTrack(0, i)))
return tracks