-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01_mix_analysis.js
More file actions
159 lines (130 loc) · 4.33 KB
/
01_mix_analysis.js
File metadata and controls
159 lines (130 loc) · 4.33 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
#!/usr/bin/env node
/**
* 01_mix_analysis.js - Analyze Your Mix with the Tonn API
*
* This example demonstrates how to analyze a mixed or mastered audio track
* using the Tonn API's mix diagnosis feature (powered by Mix Check Studio).
*
* Usage:
* node 01_mix_analysis.js <audio_file> <musical_style> [--is-master]
*
* Example:
* node 01_mix_analysis.js my_track.wav POP --is-master
* node 01_mix_analysis.js demo.mp3 ROCK
*/
import { existsSync } from 'fs';
import { TonnClient } from '../shared/client.js';
/**
* Print diagnosis results in a readable format.
*/
function printDiagnosisResults(diagnosis) {
if (!diagnosis || typeof diagnosis !== 'object') {
console.log('No valid diagnosis results found.');
return;
}
console.log('\n' + '='.repeat(60));
console.log('MIX DIAGNOSIS RESULTS');
console.log('='.repeat(60));
console.log(`\nCompletion Time: ${diagnosis.completion_time || 'N/A'}`);
if (diagnosis.error) {
console.log(`⚠️ Error: ${diagnosis.info || 'Unknown error'}`);
return;
}
const payload = diagnosis.payload || {};
if (Object.keys(payload).length > 0) {
console.log('\n--- Technical Details ---');
const metrics = [
['Bit Depth', payload.bit_depth],
['Sample Rate', payload.sample_rate],
['Integrated Loudness (LUFS)', payload.integrated_loudness_lufs?.toFixed(1)],
['Peak Loudness (dBFS)', payload.peak_loudness_dbfs],
['Clipping', payload.clipping],
['Stereo Field', payload.stereo_field],
['Mono Compatible', payload.mono_compatible],
['Phase Issues', payload.phase_issues]
];
for (const [name, value] of metrics) {
if (value !== undefined && value !== null) {
console.log(` ${name}: ${value}`);
}
}
if (payload.if_master_drc) {
console.log(`\n Master DRC Evaluation: ${payload.if_master_drc}`);
}
if (payload.if_master_loudness) {
console.log(` Master Loudness Evaluation: ${payload.if_master_loudness}`);
}
}
const summary = payload.summary || {};
if (Object.keys(summary).length > 0) {
console.log('\n--- Recommendations ---');
let idx = 1;
for (const value of Object.values(summary)) {
console.log(` ${idx}. ${value}`);
idx++;
}
}
console.log('\n' + '='.repeat(60));
}
async function main() {
const args = process.argv.slice(2);
if (args.length < 2 || args.includes('--help') || args.includes('-h')) {
console.log(`
Usage: node 01_mix_analysis.js <audio_file> <musical_style> [--is-master]
Arguments:
audio_file Path to audio file (.mp3, .wav, .flac)
musical_style Musical style (e.g., ROCK, POP, ELECTRONIC)
--is-master Flag if the input is a mastered track
Examples:
node 01_mix_analysis.js track.wav POP
node 01_mix_analysis.js master.mp3 ROCK --is-master
Musical Styles:
ROCK, POP, ELECTRONIC, HIPHOP_GRIME, ACOUSTIC, ROCK_INDIE,
SINGER_SONGWRITER, JAZZ, CLASSICAL, and more.
`);
process.exit(args.includes('--help') ? 0 : 1);
}
const inputFile = args[0];
const musicalStyle = args[1].toUpperCase();
const isMaster = args.includes('--is-master');
// Validate input file
if (!existsSync(inputFile)) {
console.error(`Error: File not found: ${inputFile}`);
process.exit(1);
}
const client = new TonnClient();
// Upload the file
console.log(`\n📤 Uploading ${inputFile}...`);
const readableUrl = await client.uploadLocalFile(inputFile);
if (!readableUrl) {
console.error('Failed to upload file. Exiting.');
process.exit(1);
}
// Build the analysis payload
const payload = {
mixDiagnosisData: {
audioFileLocation: readableUrl,
musicalStyle,
isMaster
}
};
// Run the analysis
console.log(`\n🔍 Analyzing your ${isMaster ? 'master' : 'mix'}...`);
const response = await client.post('/mixanalysis', payload);
if (!response) {
console.error('Analysis failed. Check your API key and try again.');
process.exit(1);
}
if (response.error) {
console.error(`❌ Error: ${response.message || 'Unknown error'}`);
process.exit(1);
}
console.log(`✅ ${response.message || 'Analysis complete'}`);
const diagnosisResults = response.mixDiagnosisResults;
if (diagnosisResults) {
printDiagnosisResults(diagnosisResults);
} else {
console.log('No diagnosis results in response.');
}
}
main().catch(console.error);