-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmastering_example.py
More file actions
297 lines (257 loc) · 11.9 KB
/
mastering_example.py
File metadata and controls
297 lines (257 loc) · 11.9 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
"""
Example demonstrating how to use the RoEx client for audio mastering.
This example covers:
- Mastering a single audio track.
- Mastering multiple tracks together as an album for consistent loudness and tone.
Workflow:
1. Initialize client securely using environment variables.
2. Upload audio file(s) to RoEx's secure storage.
3. For single track mastering:
a. Create a mastering request.
b. Retrieve preview and final mastered tracks.
c. Download the final master.
4. For album mastering:
a. Upload all tracks for the album.
b. Create an album mastering request with individual settings per track.
c. Process the album (downloads handled internally by the client method).
Before running:
1. Set your API key in the environment:
export ROEX_API_KEY='your_api_key_here'
2. Have WAV or FLAC file(s) ready for mastering.
- Supported sample rates: 44.1kHz, 48kHz
- Supported bit depths: 16-bit, 24-bit
- Maximum duration: 10 minutes per track
File Security:
- Files are uploaded using secure, signed URLs.
- All processing happens in RoEx's secure environment.
- Files are automatically removed after processing.
- Download URLs are temporary and expire.
Example Usage:
# Master a single track
export ROEX_API_KEY='your_api_key_here'
python mastering_example.py /path/to/your/track.wav
# Master multiple tracks as an album (provide paths as arguments)
export ROEX_API_KEY='your_api_key_here'
python mastering_example.py /path/to/track1.wav /path/to/track2.wav /path/to/track3.wav
"""
import os
import sys
from pathlib import Path
from typing import List
from roex_python.client import RoExClient
from roex_python.models import (
MasteringRequest, AlbumMasteringRequest,
MusicalStyle, DesiredLoudness
)
from roex_python.utils import upload_file
from common import (
get_api_key,
validate_audio_file,
ensure_output_dir,
setup_logger,
validate_audio_properties, # Import new validation function
AudioValidationError # Import base custom exception
)
from soundfile import SoundFileError # Import directly from soundfile
# Constants
MASTERING_MAX_DURATION_SECS = 600 # 10 minutes for mastering
# Set up logger for this module
logger = setup_logger(__name__)
def master_single_track(client: RoExClient, file_path: Path, output_dir: Path):
"""Handles the workflow for mastering a single track."""
logger.info(f"\n=== Mastering Single Track: {file_path.name} ===")
# 1. Upload file
logger.info(f"Uploading {file_path.name}...")
try:
track_url = upload_file(client, str(file_path))
logger.info("File uploaded to secure storage.")
except Exception as e:
logger.error(f"Error uploading {file_path.name}: {e}")
return False
# 2. Create mastering request
logger.info("Creating mastering request...")
mastering_request = MasteringRequest(
track_url=track_url,
musical_style=MusicalStyle.ACOUSTIC,
desired_loudness=DesiredLoudness.MEDIUM,
sample_rate="44100",
webhook_url="https://webhook-test-786984745538.europe-west1.run.app"
)
# 3. Get preview master
mastering_task_id = None
try:
logger.info("Creating mastering preview...")
try:
mastering_response = client.mastering.create_mastering_preview(mastering_request)
if not mastering_response or not mastering_response.mastering_task_id:
logger.error("Failed to create mastering preview task: No task ID received.")
return False
mastering_task_id = mastering_response.mastering_task_id
logger.info(f"Mastering Task ID: {mastering_task_id}")
except Exception as e:
logger.error(f"Error during preview generation: {e}")
return False
logger.info("Retrieving preview master (this may take some time)...")
preview_results = client.mastering.retrieve_preview_master(mastering_task_id)
preview_url = preview_results.get('download_url_mastered_preview')
if preview_url:
logger.info("Preview master ready.")
# Optional: Download preview
# preview_path = output_dir / f"preview_{file_path.stem}.wav"
# client.api_provider.download_file(preview_url, str(preview_path))
# logger.info(f"Preview saved to {preview_path}")
else:
logger.info("Could not retrieve preview master URL.")
except Exception as e:
logger.error(f"Error during preview generation: {e}")
# Continue to final master retrieval if task ID exists
if not mastering_task_id:
return False
# 4. Get final master URL
try:
logger.info("Retrieving final master URL (this may take some time)...")
# This retrieves the download URL for the final mastered file
final_url_response = client.mastering.retrieve_final_master(mastering_task_id)
final_url = final_url_response.get('download_url_mastered')
if not final_url:
error_msg = final_url_response.get('message', 'No URL found')
logger.error(f"Could not retrieve final master URL. Error: {error_msg}")
return False
logger.info("Final master URL ready.")
except Exception as e:
logger.error(f"Error retrieving final master URL: {e}")
return False
# 5. Download the final master file using the obtained URL
logger.info("Downloading final master...")
final_filename = output_dir / f"mastered_{file_path.stem}.wav"
try:
if client.api_provider.download_file(final_url, str(final_filename)):
logger.info(f"Downloaded final master to {final_filename}")
return True
else:
logger.error("Failed to download final master.")
return False
except Exception as e:
logger.error(f"Error downloading final master: {e}")
return False
def master_album(client: RoExClient, file_paths: List[Path], output_dir: Path):
"""Handles the workflow for mastering multiple tracks as an album."""
logger.info("\n=== Mastering Album ===")
if not file_paths:
logger.warning("No files provided for album mastering.")
return False
album_mastering_requests = []
track_urls = {}
# 1. Upload all tracks
logger.info("Uploading album tracks...")
for file_path in file_paths:
try:
logger.info(f" Uploading {file_path.name}...")
url = upload_file(client, str(file_path))
track_urls[file_path.name] = url # Store URL mapped to original name
logger.info(f" Uploaded {file_path.name}")
# Define mastering settings for this track (customize as needed)
req = MasteringRequest(
track_url=url,
musical_style=MusicalStyle.ACOUSTIC, # Apply style per track or use a common one
desired_loudness=DesiredLoudness.MEDIUM,
webhook_url="https://webhook-test-786984745538.europe-west1.run.app"
)
album_mastering_requests.append(req)
except Exception as e:
logger.error(f"Error uploading {file_path.name}: {e}. Skipping track.")
# Decide whether to stop or continue album processing
# return False # Option: Stop if any track fails upload
if not album_mastering_requests:
logger.error("No tracks successfully uploaded for album mastering.")
return False
# 2. Create album mastering request
logger.info("\nCreating album mastering request...")
album_request = AlbumMasteringRequest(tracks=album_mastering_requests)
# 3. Process album (downloads handled by the method)
logger.info("Processing album (this may take some time)...")
try:
# The process_album method handles polling and downloading
# It requires the output directory where files will be saved
album_results = client.mastering.process_album(album_request, str(output_dir))
# The result is typically a list of dicts or similar, indicating success/failure per track
# We can add more detailed result checking here if needed based on client implementation
logger.info("\n=== Album Mastering Results ===")
# Basic check - assumes process_album returns something meaningful or raises exceptions
if album_results is not None: # Adjust based on actual return type
logger.info(f"Album mastering process initiated/completed. Check logs or webhook if configured.")
logger.info(f"Mastered files saved to: {output_dir}")
# You might want to check the contents of output_dir or parse album_results here
# Example: Check if expected files exist
all_files_exist = True
for req in album_mastering_requests:
# Try to reconstruct expected output filename (this is an assumption)
# The actual naming might depend on the `process_album` implementation
original_path = next((p for p, u in track_urls.items() if u == req.track_url), None)
if original_path:
expected_name = f"mastered_{Path(original_path).stem}.wav"
if not (output_dir / expected_name).exists():
logger.warning(f"Warning: Expected output file not found: {expected_name}")
all_files_exist = False
else:
logger.warning("Warning: Could not determine original filename for a track URL.")
all_files_exist = False
if all_files_exist:
logger.info("All expected output files appear to be present.")
return True
else:
logger.error("Album mastering process returned unexpected result.")
return False
except Exception as e:
logger.error(f"Error during album processing: {e}")
return False
def mastering_workflow(input_files: List[str]):
"""Main workflow for mastering single or multiple tracks.
Args:
input_files: List of input file paths provided as arguments.
"""
# Setup
setup_logger() # Use the logger from common.py
api_key = get_api_key()
if not api_key:
return # Error logged in get_api_key
client = RoExClient(
api_key=api_key,
base_url="https://tonn.roexaudio.com"
)
validated_files = []
for f in input_files:
try:
validated_path = validate_audio_file(f)
validate_audio_properties(validated_path, MASTERING_MAX_DURATION_SECS)
validated_files.append(validated_path)
except (FileNotFoundError, ValueError, AudioValidationError, SoundFileError) as e:
logger.error(f"Error: Input file not found or invalid - {e}. Aborting mastering workflow.")
return # Stop if any file fails validation
if len(validated_files) == 1:
# Single track mastering
output_dir = ensure_output_dir("single_masters")
success = master_single_track(client, validated_files[0], output_dir)
else:
# Album mastering
output_dir = ensure_output_dir("album_masters")
success = master_album(client, validated_files, output_dir)
if success:
logger.info("\n=== Mastering Workflow Completed Successfully ===")
else:
logger.error("\n=== Mastering Workflow Failed ===")
if __name__ == "__main__":
if len(sys.argv) < 2:
logger.info("Usage:")
logger.info(" Master single track: python mastering_example.py <path_to_track>")
logger.info(" Master album: python mastering_example.py <path_to_track1> <path_to_track2> ...")
sys.exit(1)
input_file_args = sys.argv[1:]
try:
mastering_workflow(input_file_args)
except ValueError as e:
logger.error(f"\nError: {e}")
except KeyboardInterrupt:
logger.info("\nMastering cancelled by user.")
except Exception as e:
logger.error(f"\nAn unexpected error occurred: {e}")