-
Notifications
You must be signed in to change notification settings - Fork 39
/
ableton.py
276 lines (231 loc) · 7.31 KB
/
ableton.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
#!/usr/bin/env python3
# Stemgen for Ableton Live
# Installation:
# `python3 -m pip install opencv-python`
# Only on macOS: `python3 -m pip install pyobjc-core`
# Only on macOS: `python3 -m pip install pyobjc`
# `python3 -m pip install pyautogui`
# `python3 -m pip install pylive`
# Also install https://github.com/ideoforms/AbletonOSC as a Remote Script
# Only on Windows: you can install https://github.com/p-groarke/wsay/releases to get audio feedback
# Usage:
# Open Ableton Live
# Open the project you want to export
# Check your export settings and make sure that the export folder is set to "stemgen/input"
# Solo the tracks you want to export as stems
# Run `python3 ableton.py`
# Enter the name of the file
# Don't touch your computer until it's done
# Enjoy your stems!
import os
from pathlib import Path
import platform
import sys
import subprocess
import live
import pyautogui
import time
import logging
from metadata import create_metadata_json, ableton_color_index_to_hex
from shutil import which
# Settings
NAME = "track"
IS_RETINA = False
OS = "windows" if platform.system() == "Windows" else "macos"
PYTHON_EXEC = sys.executable if not None else "python3"
INSTALL_DIR = Path(__file__).parent.absolute()
STEMS = []
# https://github.com/asweigart/pyautogui/issues/790
if OS == "macos":
import pyscreeze
import PIL
__PIL_TUPLE_VERSION = tuple(int(x) for x in PIL.__version__.split("."))
pyscreeze.PIL__version__ = __PIL_TUPLE_VERSION
def say(text):
if OS == "windows":
if which("wsay") is not None:
os.system("wsay " + text)
else:
os.system("say " + text)
return
# Switch to Ableton Live
def switch_to_ableton():
print("Looking for Ableton Live...")
if OS == "windows":
ableton = pyautogui.getWindowsWithTitle("Ableton Live")[0]
if ableton != None:
print("Found it!")
ableton.activate()
ableton.maximize()
return
pyautogui.keyDown("command")
pyautogui.press("tab")
time.sleep(1)
x, y = pyautogui.locateCenterOnScreen(
os.path.join(INSTALL_DIR, "screenshots", OS, "logo.png"),
confidence=0.9,
grayscale=True,
)
print("Found it!")
if IS_RETINA == True:
x = x / 2
y = y / 2
pyautogui.moveTo(x, y)
pyautogui.keyUp("command")
return
# Export a track based on a solo location
def export(track, position):
# Solo the track (if not exporting master)
if position != 0:
track.solo = True
# Get the track name and color
print(track.name)
name = track.name
color = ableton_color_index_to_hex[track.color_index]
STEMS.append({"color": color, "name": name})
# Export the track
if OS == "windows":
pyautogui.hotkey("ctrl", "shift", "r")
else:
pyautogui.hotkey("command", "shift", "r")
pyautogui.press("enter")
time.sleep(1)
pyautogui.typewrite(NAME + "." + str(position) + ".aif")
pyautogui.press("enter")
print("Exporting: " + NAME + "." + str(position) + ".aif")
# Wait for the export to finish
time.sleep(1)
while True:
try:
location = pyautogui.locateOnScreen(
os.path.join(INSTALL_DIR, "screenshots", OS, "export.png"),
confidence=0.9,
grayscale=True,
)
if location != None:
print("Exporting...")
else:
print("Exported: " + NAME + "." + str(position) + ".aif")
break
except:
print("Exported: " + NAME + "." + str(position) + ".aif")
break
# Unsolo the track (if not exporting master)
if position != 0:
track.solo = False
return
def main():
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s.%(msecs)03d: %(message)s",
datefmt="%H:%M:%S",
)
print("Welcome to Stemgen for Ableton Live!")
# Get file name
global NAME
NAME = pyautogui.prompt(
text="Enter the name of the file", title="Stemgen for Ableton Live", default=""
)
print("File name: " + NAME)
# Check Retina Display
if OS == "macos":
global IS_RETINA
if (
subprocess.call(
"system_profiler SPDisplaysDataType | grep 'Retina'", shell=True
)
== 0
):
IS_RETINA = True
else:
IS_RETINA = False
print("Retina Display: " + str(IS_RETINA))
# Get Ableton Live set
set = live.Set(scan=True)
switch_to_ableton()
time.sleep(1)
# Get the solo-ed tracks
soloed_tracks = []
for track in set.tracks:
if track.solo:
soloed_tracks.append(track)
if len(soloed_tracks) == 0:
print("You need to solo the tracks or groups you want to export as stems.")
say("Oops")
exit()
if len(soloed_tracks) < 4:
print("You need to solo at least 4 tracks or groups.")
say("Oops")
exit()
if len(soloed_tracks) > 8:
print("You can't create stems with more than 8 tracks or groups.")
say("Oops")
exit()
print("Found " + str(len(soloed_tracks)) + " solo-ed tracks.")
# Delete old files using the same file name in `stemgen/input` folder
for file in os.listdir(os.path.join(INSTALL_DIR, "input")):
if file.startswith(NAME):
os.remove(os.path.join(INSTALL_DIR, "input", file))
# Unsolo the tracks
for track in set.tracks:
if track.solo:
track.solo = False
# Export master
export(soloed_tracks, 0)
# Check if master was exported in `stemgen/input` folder
if not os.path.exists(os.path.join(INSTALL_DIR, "input", NAME + ".0.aif")):
print("You need to set `stemgen/input` as the output folder.")
say("Oops")
exit()
# Export stems
i = 1
for soloed_track in soloed_tracks:
export(soloed_track, i)
i += 1
# Switch to Terminal
if OS == "windows":
cmd = pyautogui.getWindowsWithTitle(
"Command Prompt"
) or pyautogui.getWindowsWithTitle("Windows PowerShell")
if cmd[0] != None:
cmd[0].activate()
else:
pyautogui.keyDown("command")
pyautogui.press("tab")
pyautogui.keyUp("command")
# Create metadata.part1.json and metadata.part2.json if double stems
if len(soloed_tracks) == 8:
create_metadata_json(
STEMS[:4], os.path.join(INSTALL_DIR, "metadata.part1.json")
)
create_metadata_json(
STEMS[4:], os.path.join(INSTALL_DIR, "metadata.part2.json")
)
print("Created or updated metadata files.")
# Create the stem file(s)
if OS == "windows":
subprocess.run(
[
PYTHON_EXEC,
os.path.join(INSTALL_DIR, "stem.py"),
"-i",
os.path.join(INSTALL_DIR, "input", NAME + ".0.aif"),
"-f",
"aac",
]
)
else:
subprocess.run(
[
PYTHON_EXEC,
os.path.join(INSTALL_DIR, "stem.py"),
"-i",
os.path.join(INSTALL_DIR, "input", NAME + ".0.aif"),
]
)
print("Done! Enjoy :)")
say("Done")
return
if __name__ == "__main__":
os.chdir(INSTALL_DIR)
main()