-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
183 lines (135 loc) · 5.23 KB
/
Copy pathprocessor.py
File metadata and controls
183 lines (135 loc) · 5.23 KB
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
import json
import os
import sys
from time import sleep
from subprocess import run
from tkinter import messagebox
from utils import do_command
import pipe_client
from constants import *
class Processor:
def __init__(self, master):
self.master = master
# Check every {self.wait_time} seconds for a response
self.wait_time = 0.1
# Call self.timeout after {self.timeout_time} seconds with no response
self.timeout_time = 5.0
# Path to write files to
self.path = os.path.expanduser('~') + "/Pictures/glitch/"
self.client = pipe_client.PipeClient()
def write(self, command, timeout=True):
# custom write function for debugging
self.client.write(command, timer=True)
response = ''
time_elapsed = 0
while response == '':
if timeout == True:
if time_elapsed > 5.0:
self.timeout(command)
sleep(self.wait_time)
time_elapsed += self.wait_time
response = self.client.read()
def process(self, params, processes):
self.counter_start = int(params["counter_start"])
self.num_iterations = int(params["num_iterations"])
if self.num_iterations < 1:
return
if params["import"] == True:
command = "ImportRaw:"
# (do_command(command))
self.write(command, timeout=False)
# Get audio info
command = "GetInfo: Type=Tracks"
info = (do_command(command))
# Trim info
info = info[1:-26]
# Load track data
track_data = json.loads(info)
first_track_end = track_data[0]["end"]
# Set start and end
start = 0.2 # 0.2 secs is enough to avoid image header
end = first_track_end
# Create file path if does not already exist
folder_name = params["folder_name"]
filepath = self.path + folder_name
if not os.path.exists(filepath):
os.mkdir(filepath)
for i in range(self.counter_start,
self.num_iterations + self.counter_start):
if params["fill_gaps"] == True:
if i not in params["gaps"]:
continue
# Copy audio
command = "SelectAll:"
# (do_command(command))
self.write(command)
command = "Copy:"
# (do_command(command))
self.write(command)
# Select portion to process
command = f"SelectTime: Start={start} End={end}"
# (do_command(command))
self.write(command)
for effect in processes:
command = effect["name"] + ": "
for param in effect["params"]:
start_val = float(param["start_val"])
end_val = float(param["end_val"])
value = self.get_value(start_val, end_val, i)
command += param["name"] + "=" + str(value) + " "
print(command)
# (do_command(command))
self.write(command)
# Select all for export
command = "SelectAll:"
# (do_command(command))
self.write(command)
# Export
filename = f"{filepath}/{i}.raw"
command = f"Export2: Filename={filename}"
# (do_command(command))
self.write(command)
# Select all, delete, and paste original audio
# (do_command("SelectAll:"))
# (do_command("Delete:"))
# (do_command("Paste:"))
self.write("SelectAll:")
self.write("Delete:")
self.write("Paste:")
if TEST_TIMEOUT:
self.timeout(command)
else:
self.success(params)
def get_value(self, start_val, end_val, idx):
if idx == 0:
return start_val
if idx == self.num_iterations:
return end_val
inc = (end_val - start_val) / self.num_iterations
return start_val + (inc * (idx - self.counter_start))
# This function runs when a process times out
# (usually means the application froze).
# This is unfortunately quite common.
# The only solution I currently have is to display an error,
# shut down Audacity, and shut down the application.
def timeout(self, command):
messagebox.showerror("command timed out",
f"The current command timed out: {command}\nExiting...")
# Kill Audacity
if PLATFORM == "win32":
# not tested on Windows yet
run(["taskkill","/IM","Audacity","/F"],shell=True)
else:
run(["killall Audacity"],shell=True)
# Kill AGK
sys.exit()
# This function is called when the process completes.
# It can easily be edited to display different info
# at the end of the process.
def success(self, params):
num_iterations = params["num_iterations"]
if params["fill_gaps"] == True:
num_iterations = len(params["gaps"])
full_path = self.path + params["folder_name"]
messagebox.showinfo("Success",
f"Done - wrote {num_iterations} files to {full_path}.")