-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudacity_automation_external.py
More file actions
339 lines (273 loc) · 9.73 KB
/
audacity_automation_external.py
File metadata and controls
339 lines (273 loc) · 9.73 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
"""
External Audacity Automation
Uses AppleScript (macOS) or other automation methods to control Audacity.
"""
import os
import sys
import subprocess
import time
from pathlib import Path
def create_applescript_automation(input_file, output_file, effects=None):
"""
Create an AppleScript to automate Audacity on macOS.
"""
if effects is None:
effects = [
{"name": "Echo", "delay": 0.3, "decay": 0.1},
]
# Convert effects to AppleScript commands
effect_commands = []
for effect in effects:
if effect["name"] == "Echo":
effect_commands.append(f'''
-- Apply Echo effect
tell application "System Events"
tell process "Audacity"
click menu item "Echo..." of menu "Effect" of menu bar 1
delay 1
set value of slider 1 of window 1 to {int(effect.get("delay", 0.3) * 100)}
set value of slider 2 of window 1 to {int(effect.get("decay", 0.1) * 100)}
click button "OK" of window 1
delay 2
end tell
end tell''')
applescript = f'''
tell application "Audacity"
activate
delay 2
end tell
tell application "System Events"
tell process "Audacity"
-- Import raw data
click menu item "Raw Data..." of menu "Import" of menu "File" of menu bar 1
delay 1
-- Set file path (you'll need to manually select the file)
-- This is a limitation of AppleScript with file dialogs
-- Select all audio
keystroke "a" using command down
delay 1
{chr(10).join(effect_commands)}
-- Export as raw
click menu item "Export Audio..." of menu "Export" of menu "File" of menu bar 1
delay 1
-- Set export format to RAW
click pop up button 1 of window 1
click menu item "RAW (header-less)" of menu 1 of pop up button 1 of window 1
-- Set encoding to U-Law
click pop up button 2 of window 1
click menu item "U-Law" of menu 1 of pop up button 2 of window 1
-- Set output path (you'll need to manually set this)
-- click button "Save" of window 1
end tell
end tell
'''
script_file = "audacity_automation.scpt"
with open(script_file, 'w') as f:
f.write(applescript)
print(f"Created AppleScript: {script_file}")
print("Note: AppleScript has limitations with file dialogs.")
print("You may need to manually select files in some cases.")
return script_file
def create_python_automation_script(input_dir="input-bitmap", output_dir="output-raw", effects=None):
"""
Create a Python script that uses pyautogui for automation.
"""
if effects is None:
effects = [
{"name": "Echo", "delay": 0.3, "decay": 0.1},
]
script_content = f'''
#!/usr/bin/env python3
"""
Audacity Automation using pyautogui
Requires: pip install pyautogui pillow
"""
import pyautogui
import time
import os
from pathlib import Path
# Configure pyautogui
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.5
def open_audacity():
"""Open Audacity application"""
pyautogui.hotkey('cmd', 'space') # Spotlight
time.sleep(1)
pyautogui.write('Audacity')
time.sleep(1)
pyautogui.press('enter')
time.sleep(3) # Wait for Audacity to open
def import_raw_data(file_path):
"""Import raw data file"""
pyautogui.hotkey('cmd', 'shift', 'i') # Import Raw Data
time.sleep(1)
# Type the file path
pyautogui.write(str(file_path))
time.sleep(1)
pyautogui.press('enter')
time.sleep(2)
def apply_echo_effect(delay=0.3, decay=0.1):
"""Apply echo effect"""
pyautogui.hotkey('cmd', 'shift', 'e') # Echo effect
time.sleep(1)
# Set delay (assuming it's the first slider)
pyautogui.click(x=400, y=300) # Adjust coordinates as needed
pyautogui.write(str(delay))
# Set decay (assuming it's the second slider)
pyautogui.click(x=400, y=350) # Adjust coordinates as needed
pyautogui.write(str(decay))
# Click OK
pyautogui.press('enter')
time.sleep(2)
def export_raw_audio(output_path):
"""Export as raw audio"""
pyautogui.hotkey('cmd', 'shift', 'e') # Export Audio
time.sleep(1)
# Set format to RAW
pyautogui.click(x=300, y=200) # Format dropdown
time.sleep(1)
pyautogui.press('down') # Navigate to RAW
pyautogui.press('enter')
# Set encoding to U-Law
pyautogui.click(x=300, y=250) # Encoding dropdown
time.sleep(1)
pyautogui.press('down') # Navigate to U-Law
pyautogui.press('enter')
# Set output path
pyautogui.write(str(output_path))
time.sleep(1)
pyautogui.press('enter')
time.sleep(2)
def process_file(input_file, output_file, effects):
"""Process a single file"""
print(f"Processing {{input_file}}...")
# Open Audacity
open_audacity()
# Import the file
import_raw_data(input_file)
# Select all
pyautogui.hotkey('cmd', 'a')
time.sleep(1)
# Apply effects
for effect in effects:
if effect["name"] == "Echo":
apply_echo_effect(
delay=effect.get("delay", 0.3),
decay=effect.get("decay", 0.1)
)
# Export
export_raw_audio(output_file)
# Close project
pyautogui.hotkey('cmd', 'w')
time.sleep(1)
# Main processing
input_dir = "{input_dir}"
output_dir = "{output_dir}"
effects = {effects}
input_path = Path(input_dir)
output_path = Path(output_dir)
output_path.mkdir(exist_ok=True)
for bitmap_file in input_path.glob("*.bmp"):
output_file = output_path / f"{{bitmap_file.stem}}.raw"
process_file(str(bitmap_file), str(output_file), effects)
print("Processing complete!")
'''
script_file = "audacity_pyautogui.py"
with open(script_file, 'w') as f:
f.write(script_content)
print(f"Created pyautogui automation script: {script_file}")
print("To use this script:")
print("1. Install pyautogui: pip install pyautogui")
print("2. Adjust the click coordinates for your screen resolution")
print("3. Run: python audacity_pyautogui.py")
return script_file
def create_sox_alternative(input_dir="input-bitmap", output_dir="output-raw", effects=None):
"""
Create a script using SoX (Sound eXchange) as an alternative to Audacity.
SoX can apply audio effects programmatically without GUI.
"""
if effects is None:
effects = [
{"name": "echo", "gain_in": 0.8, "gain_out": 0.5, "delays": "0.3", "decays": "0.1"},
]
script_content = f'''
#!/usr/bin/env python3
"""
SoX-based audio processing as alternative to Audacity
Requires: brew install sox (on macOS) or apt-get install sox (on Linux)
"""
import subprocess
import os
from pathlib import Path
def process_with_sox(input_file, output_file, effects):
"""Process audio file with SoX effects"""
# Build SoX command
cmd = ["sox", input_file, output_file]
# Add effects
for effect in effects:
if effect["name"] == "echo":
cmd.extend([
"echo",
str(effect.get("gain_in", 0.8)),
str(effect.get("gain_out", 0.5)),
str(effect.get("delays", "0.3")),
str(effect.get("decays", "0.1"))
])
elif effect["name"] == "reverb":
cmd.extend([
"reverb",
str(effect.get("room_scale", 0.5)),
str(effect.get("h_freq", 0.5)),
str(effect.get("reverberance", 0.5)),
str(effect.get("damping", 0.5))
])
print(f"Running: {{' '.join(cmd)}}")
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
print(f"Successfully processed {{input_file}}")
return True
except subprocess.CalledProcessError as e:
print(f"Error processing {{input_file}}: {{e.stderr}}")
return False
# Main processing
input_dir = "{input_dir}"
output_dir = "{output_dir}"
effects = {effects}
input_path = Path(input_dir)
output_path = Path(output_dir)
output_path.mkdir(exist_ok=True)
# Check if SoX is installed
try:
subprocess.run(["sox", "--version"], capture_output=True, check=True)
print("SoX is installed and ready to use!")
except FileNotFoundError:
print("SoX is not installed. Install it with:")
print(" macOS: brew install sox")
print(" Linux: sudo apt-get install sox")
print(" Windows: Download from https://sox.sourceforge.net/")
exit(1)
for bitmap_file in input_path.glob("*.bmp"):
output_file = output_path / f"{{bitmap_file.stem}}.raw"
process_with_sox(str(bitmap_file), str(output_file), effects)
print("SoX processing complete!")
'''
script_file = "sox_processing.py"
with open(script_file, 'w') as f:
f.write(script_content)
print(f"Created SoX processing script: {script_file}")
print("SoX is a command-line alternative to Audacity for audio processing.")
return script_file
if __name__ == "__main__":
effects = [
{"name": "Echo", "delay": 0.3, "decay": 0.1},
{"name": "Reverb", "room_size": 0.5, "damping": 0.5},
]
print("Creating automation scripts...")
create_applescript_automation("test.bmp", "test.raw", effects)
create_python_automation_script("input-bitmap", "output-raw", effects)
create_sox_alternative("input-bitmap", "output-raw", effects)
print("\\nAutomation options created:")
print("1. audacity_automation.scpt - AppleScript (macOS)")
print("2. audacity_pyautogui.py - Python with pyautogui")
print("3. sox_processing.py - SoX command-line alternative")