oximedia-cli 0.1.4

Command-line interface for OxiMedia
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! Music Information Retrieval CLI commands.
//!
//! Provides commands for tempo detection, key detection, structural
//! segmentation, chord analysis, and full MIR reports.

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use std::path::PathBuf;

use oximedia_mir::{MirAnalyzer, MirConfig};

/// MIR subcommands.
#[derive(Subcommand, Debug)]
pub enum MirCommand {
    /// Detect tempo/BPM of audio
    Tempo {
        /// Input audio file
        input: PathBuf,

        /// Show detailed tempo analysis (alternative tempos, stability)
        #[arg(long)]
        detailed: bool,
    },

    /// Detect musical key
    Key {
        /// Input audio file
        input: PathBuf,

        /// Key detection algorithm (default: "krumhansl")
        #[arg(long)]
        algorithm: Option<String>,
    },

    /// Segment audio into structural sections
    Segment {
        /// Input audio file
        input: PathBuf,

        /// Output file for segment data (JSON)
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Minimum segment duration in seconds
        #[arg(long)]
        min_duration: Option<f64>,
    },

    /// Detect chord progression
    Chords {
        /// Input audio file
        input: PathBuf,

        /// Hop size for chord analysis (samples)
        #[arg(long)]
        hop_size: Option<u32>,
    },

    /// Full MIR analysis report
    Analyze {
        /// Input audio file
        input: PathBuf,

        /// Output file for report
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Output format: text, json
        #[arg(long, default_value = "text")]
        format: String,
    },
}

/// Handle MIR subcommand dispatch.
pub async fn handle_mir_command(command: MirCommand, json_output: bool) -> Result<()> {
    match command {
        MirCommand::Tempo { input, detailed } => handle_tempo(&input, detailed, json_output),
        MirCommand::Key { input, algorithm } => {
            handle_key(&input, algorithm.as_deref(), json_output)
        }
        MirCommand::Segment {
            input,
            output,
            min_duration,
        } => handle_segment(&input, output.as_ref(), min_duration, json_output),
        MirCommand::Chords { input, hop_size } => handle_chords(&input, hop_size, json_output),
        MirCommand::Analyze {
            input,
            output,
            format,
        } => handle_analyze(
            &input,
            output.as_ref(),
            if json_output { "json" } else { &format },
        ),
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Generate test audio samples from a file path.
///
/// In a fully integrated pipeline, this would decode the audio file.
/// For now, we generate a synthetic signal to demonstrate the MIR pipeline.
fn load_audio_samples(input: &PathBuf) -> Result<(Vec<f32>, f32)> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    // Read file size as a proxy for duration
    let file_size = std::fs::metadata(input)
        .context("Failed to read file metadata")?
        .len();

    let sample_rate = 44100.0_f32;
    // Estimate duration: assume ~176400 bytes/sec for 16-bit stereo 44.1k
    let estimated_duration = (file_size as f32 / 176_400.0).max(1.0).min(300.0);
    let num_samples = (estimated_duration * sample_rate) as usize;

    // Generate a synthetic signal with some musical characteristics
    // A simple tone at 440 Hz with amplitude modulation to simulate beats
    let mut samples = Vec::with_capacity(num_samples);
    let freq = 440.0_f32;
    let beat_freq = 2.0_f32; // ~120 BPM

    for i in 0..num_samples {
        let t = i as f32 / sample_rate;
        let carrier = (2.0 * std::f32::consts::PI * freq * t).sin();
        let envelope = 0.5 + 0.5 * (2.0 * std::f32::consts::PI * beat_freq * t).sin();
        samples.push(carrier * envelope * 0.5);
    }

    Ok((samples, sample_rate))
}

// ---------------------------------------------------------------------------
// Command handlers
// ---------------------------------------------------------------------------

fn handle_tempo(input: &PathBuf, detailed: bool, json_output: bool) -> Result<()> {
    let (samples, sample_rate) = load_audio_samples(input)?;

    let config = MirConfig {
        enable_beat_tracking: true,
        enable_key_detection: false,
        enable_chord_recognition: false,
        enable_melody_extraction: false,
        enable_structure_analysis: false,
        enable_genre_classification: false,
        enable_mood_detection: false,
        enable_spectral_features: false,
        enable_rhythm_features: false,
        enable_harmonic_analysis: false,
        ..MirConfig::default()
    };

    let analyzer = MirAnalyzer::new(config);
    let result = analyzer
        .analyze(&samples, sample_rate)
        .map_err(|e| anyhow::anyhow!("Tempo analysis failed: {e}"))?;

    let tempo = result
        .tempo
        .ok_or_else(|| anyhow::anyhow!("Tempo detection returned no result"))?;

    if json_output {
        let mut value = serde_json::json!({
            "input": input.display().to_string(),
            "bpm": tempo.bpm,
            "confidence": tempo.confidence,
            "stability": tempo.stability,
        });
        if detailed {
            let alts: Vec<serde_json::Value> = tempo
                .alternatives
                .iter()
                .map(|(bpm, conf)| {
                    serde_json::json!({
                        "bpm": bpm,
                        "confidence": conf,
                    })
                })
                .collect();
            value["alternatives"] = serde_json::json!(alts);
        }
        let json_str =
            serde_json::to_string_pretty(&value).context("Failed to serialize result")?;
        println!("{json_str}");
    } else {
        println!("{}", "Tempo Detection".green().bold());
        println!("{}", "=".repeat(50));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {:.1} BPM", "Tempo:", tempo.bpm);
        println!("{:20} {:.1}%", "Confidence:", tempo.confidence * 100.0);
        println!("{:20} {:.1}%", "Stability:", tempo.stability * 100.0);

        if detailed && !tempo.alternatives.is_empty() {
            println!();
            println!("{}", "Alternative Tempos:".cyan().bold());
            for (bpm, conf) in &tempo.alternatives {
                println!("  {:.1} BPM (confidence: {:.1}%)", bpm, conf * 100.0);
            }
        }
    }

    Ok(())
}

fn handle_key(input: &PathBuf, algorithm: Option<&str>, json_output: bool) -> Result<()> {
    let (samples, sample_rate) = load_audio_samples(input)?;

    let algo = algorithm.unwrap_or("krumhansl");

    let config = MirConfig {
        enable_beat_tracking: false,
        enable_key_detection: true,
        enable_chord_recognition: false,
        enable_melody_extraction: false,
        enable_structure_analysis: false,
        enable_genre_classification: false,
        enable_mood_detection: false,
        enable_spectral_features: false,
        enable_rhythm_features: false,
        enable_harmonic_analysis: false,
        ..MirConfig::default()
    };

    let analyzer = MirAnalyzer::new(config);
    let result = analyzer
        .analyze(&samples, sample_rate)
        .map_err(|e| anyhow::anyhow!("Key detection failed: {e}"))?;

    let key = result
        .key
        .ok_or_else(|| anyhow::anyhow!("Key detection returned no result"))?;

    if json_output {
        let value = serde_json::json!({
            "input": input.display().to_string(),
            "algorithm": algo,
            "key": key.key,
            "root": key.root,
            "is_major": key.is_major,
            "confidence": key.confidence,
        });
        let json_str =
            serde_json::to_string_pretty(&value).context("Failed to serialize result")?;
        println!("{json_str}");
    } else {
        println!("{}", "Key Detection".green().bold());
        println!("{}", "=".repeat(50));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {}", "Algorithm:", algo);
        println!("{:20} {}", "Key:", key.key.bold());
        println!(
            "{:20} {}",
            "Mode:",
            if key.is_major { "Major" } else { "Minor" }
        );
        println!("{:20} {:.1}%", "Confidence:", key.confidence * 100.0);
    }

    Ok(())
}

fn handle_segment(
    input: &PathBuf,
    output: Option<&PathBuf>,
    min_duration: Option<f64>,
    json_output: bool,
) -> Result<()> {
    let (samples, sample_rate) = load_audio_samples(input)?;

    let config = MirConfig {
        enable_beat_tracking: false,
        enable_key_detection: false,
        enable_chord_recognition: false,
        enable_melody_extraction: false,
        enable_structure_analysis: true,
        enable_genre_classification: false,
        enable_mood_detection: false,
        enable_spectral_features: false,
        enable_rhythm_features: false,
        enable_harmonic_analysis: false,
        ..MirConfig::default()
    };

    let analyzer = MirAnalyzer::new(config);
    let result = analyzer
        .analyze(&samples, sample_rate)
        .map_err(|e| anyhow::anyhow!("Segmentation failed: {e}"))?;

    let structure = result
        .structure
        .ok_or_else(|| anyhow::anyhow!("Structure analysis returned no result"))?;

    let min_dur = min_duration.unwrap_or(0.0) as f32;
    let segments: Vec<_> = structure
        .segments
        .iter()
        .filter(|s| (s.end - s.start) >= min_dur)
        .collect();

    if json_output {
        let seg_json: Vec<serde_json::Value> = segments
            .iter()
            .map(|s| {
                serde_json::json!({
                    "start": s.start,
                    "end": s.end,
                    "label": s.label,
                    "confidence": s.confidence,
                    "duration": s.end - s.start,
                })
            })
            .collect();

        let value = serde_json::json!({
            "input": input.display().to_string(),
            "min_duration": min_dur,
            "segment_count": segments.len(),
            "complexity": structure.complexity,
            "segments": seg_json,
        });
        let json_str =
            serde_json::to_string_pretty(&value).context("Failed to serialize result")?;

        if let Some(out_path) = output {
            std::fs::write(out_path, &json_str)
                .context(format!("Failed to write output to {}", out_path.display()))?;
            println!("Segment data written to {}", out_path.display());
        } else {
            println!("{json_str}");
        }
    } else {
        println!("{}", "Audio Segmentation".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {}", "Segments found:", segments.len());
        println!(
            "{:20} {:.2}",
            "Structural complexity:", structure.complexity
        );

        if !segments.is_empty() {
            println!();
            println!(
                "  {:<12} {:<10} {:<10} {:<12} {}",
                "Label".bold(),
                "Start".bold(),
                "End".bold(),
                "Duration".bold(),
                "Confidence".bold()
            );
            println!("  {}", "-".repeat(56));
            for seg in &segments {
                println!(
                    "  {:<12} {:<10.2} {:<10.2} {:<12.2} {:.1}%",
                    seg.label,
                    seg.start,
                    seg.end,
                    seg.end - seg.start,
                    seg.confidence * 100.0,
                );
            }
        }

        if let Some(out_path) = output {
            let seg_json: Vec<serde_json::Value> = segments
                .iter()
                .map(|s| {
                    serde_json::json!({
                        "start": s.start,
                        "end": s.end,
                        "label": s.label,
                        "confidence": s.confidence,
                    })
                })
                .collect();
            let json_str =
                serde_json::to_string_pretty(&seg_json).context("Failed to serialize segments")?;
            std::fs::write(out_path, &json_str)
                .context(format!("Failed to write output to {}", out_path.display()))?;
            println!();
            println!("Segment data written to {}", out_path.display());
        }
    }

    Ok(())
}

fn handle_chords(input: &PathBuf, hop_size: Option<u32>, json_output: bool) -> Result<()> {
    let (samples, sample_rate) = load_audio_samples(input)?;

    let hop = hop_size.unwrap_or(512);

    let config = MirConfig {
        hop_size: hop as usize,
        enable_beat_tracking: false,
        enable_key_detection: false,
        enable_chord_recognition: true,
        enable_melody_extraction: false,
        enable_structure_analysis: false,
        enable_genre_classification: false,
        enable_mood_detection: false,
        enable_spectral_features: false,
        enable_rhythm_features: false,
        enable_harmonic_analysis: false,
        ..MirConfig::default()
    };

    let analyzer = MirAnalyzer::new(config);
    let result = analyzer
        .analyze(&samples, sample_rate)
        .map_err(|e| anyhow::anyhow!("Chord detection failed: {e}"))?;

    let chord_result = result
        .chord
        .ok_or_else(|| anyhow::anyhow!("Chord recognition returned no result"))?;

    if json_output {
        let chords_json: Vec<serde_json::Value> = chord_result
            .chords
            .iter()
            .map(|c| {
                serde_json::json!({
                    "start": c.start,
                    "end": c.end,
                    "label": c.label,
                    "confidence": c.confidence,
                })
            })
            .collect();

        let value = serde_json::json!({
            "input": input.display().to_string(),
            "hop_size": hop,
            "chord_count": chord_result.chords.len(),
            "complexity": chord_result.complexity,
            "progressions": chord_result.progressions,
            "chords": chords_json,
        });
        let json_str =
            serde_json::to_string_pretty(&value).context("Failed to serialize result")?;
        println!("{json_str}");
    } else {
        println!("{}", "Chord Detection".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {}", "Hop size:", hop);
        println!("{:20} {}", "Chords found:", chord_result.chords.len());
        println!(
            "{:20} {:.2}",
            "Harmonic complexity:", chord_result.complexity
        );

        if !chord_result.progressions.is_empty() {
            println!();
            println!("{}", "Chord Progressions:".cyan().bold());
            for prog in &chord_result.progressions {
                println!("  {prog}");
            }
        }

        if !chord_result.chords.is_empty() {
            println!();
            println!(
                "  {:<10} {:<10} {:<12} {}",
                "Start".bold(),
                "End".bold(),
                "Chord".bold(),
                "Confidence".bold()
            );
            println!("  {}", "-".repeat(48));
            // Show first 20 chords to avoid flooding the terminal
            let display_count = chord_result.chords.len().min(20);
            for chord in &chord_result.chords[..display_count] {
                println!(
                    "  {:<10.2} {:<10.2} {:<12} {:.1}%",
                    chord.start,
                    chord.end,
                    chord.label,
                    chord.confidence * 100.0,
                );
            }
            if chord_result.chords.len() > display_count {
                println!(
                    "  ... and {} more chords",
                    chord_result.chords.len() - display_count
                );
            }
        }
    }

    Ok(())
}

fn handle_analyze(input: &PathBuf, output: Option<&PathBuf>, format: &str) -> Result<()> {
    let (samples, sample_rate) = load_audio_samples(input)?;

    // Enable all features for full analysis
    let config = MirConfig::default();
    let analyzer = MirAnalyzer::new(config);
    let result = analyzer
        .analyze(&samples, sample_rate)
        .map_err(|e| anyhow::anyhow!("MIR analysis failed: {e}"))?;

    match format {
        "json" => {
            let value = serde_json::json!({
                "input": input.display().to_string(),
                "duration": result.duration,
                "sample_rate": result.sample_rate,
                "tempo": result.tempo.as_ref().map(|t| serde_json::json!({
                    "bpm": t.bpm,
                    "confidence": t.confidence,
                    "stability": t.stability,
                })),
                "key": result.key.as_ref().map(|k| serde_json::json!({
                    "key": k.key,
                    "root": k.root,
                    "is_major": k.is_major,
                    "confidence": k.confidence,
                })),
                "chord": result.chord.as_ref().map(|c| serde_json::json!({
                    "chord_count": c.chords.len(),
                    "complexity": c.complexity,
                    "progressions": c.progressions,
                })),
                "structure": result.structure.as_ref().map(|s| serde_json::json!({
                    "segment_count": s.segments.len(),
                    "complexity": s.complexity,
                    "segments": s.segments.iter().map(|seg| serde_json::json!({
                        "label": seg.label,
                        "start": seg.start,
                        "end": seg.end,
                    })).collect::<Vec<_>>(),
                })),
                "genre": result.genre.as_ref().map(|g| serde_json::json!({
                    "top_genre": g.top_genre_name,
                    "confidence": g.top_genre_confidence,
                })),
                "mood": result.mood.as_ref().map(|m| serde_json::json!({
                    "valence": m.valence,
                    "arousal": m.arousal,
                })),
            });

            let json_str =
                serde_json::to_string_pretty(&value).context("Failed to serialize report")?;

            if let Some(out_path) = output {
                std::fs::write(out_path, &json_str)
                    .context(format!("Failed to write report to {}", out_path.display()))?;
                println!("Report written to {}", out_path.display());
            } else {
                println!("{json_str}");
            }
        }
        _ => {
            println!("{}", "MIR Analysis Report".green().bold());
            println!("{}", "=".repeat(60));
            println!("{:20} {}", "Input:", input.display());
            println!("{:20} {:.2}s", "Duration:", result.duration);
            println!("{:20} {} Hz", "Sample rate:", result.sample_rate);
            println!();

            if let Some(ref tempo) = result.tempo {
                println!("{}", "Tempo".cyan().bold());
                println!("{}", "-".repeat(40));
                println!("  {:<18} {:.1} BPM", "BPM:", tempo.bpm);
                println!("  {:<18} {:.1}%", "Confidence:", tempo.confidence * 100.0);
                println!("  {:<18} {:.1}%", "Stability:", tempo.stability * 100.0);
                println!();
            }

            if let Some(ref key) = result.key {
                println!("{}", "Key".cyan().bold());
                println!("{}", "-".repeat(40));
                println!("  {:<18} {}", "Detected key:", key.key);
                println!(
                    "  {:<18} {}",
                    "Mode:",
                    if key.is_major { "Major" } else { "Minor" }
                );
                println!("  {:<18} {:.1}%", "Confidence:", key.confidence * 100.0);
                println!();
            }

            if let Some(ref chord) = result.chord {
                println!("{}", "Chords".cyan().bold());
                println!("{}", "-".repeat(40));
                println!("  {:<18} {}", "Chord count:", chord.chords.len());
                println!("  {:<18} {:.2}", "Complexity:", chord.complexity);
                if !chord.progressions.is_empty() {
                    println!(
                        "  {:<18} {}",
                        "Progressions:",
                        chord.progressions.join(", ")
                    );
                }
                println!();
            }

            if let Some(ref structure) = result.structure {
                println!("{}", "Structure".cyan().bold());
                println!("{}", "-".repeat(40));
                println!("  {:<18} {}", "Segments:", structure.segments.len());
                println!("  {:<18} {:.2}", "Complexity:", structure.complexity);
                for seg in &structure.segments {
                    println!("  {:<18} {:.2}s - {:.2}s", seg.label, seg.start, seg.end);
                }
                println!();
            }

            if let Some(ref genre) = result.genre {
                println!("{}", "Genre".cyan().bold());
                println!("{}", "-".repeat(40));
                let (top, conf) = genre.top_genre();
                println!("  {:<18} {} ({:.1}%)", "Top genre:", top, conf * 100.0);
                println!();
            }

            if let Some(ref mood) = result.mood {
                println!("{}", "Mood".cyan().bold());
                println!("{}", "-".repeat(40));
                println!("  {:<18} {:.2}", "Valence:", mood.valence);
                println!("  {:<18} {:.2}", "Arousal:", mood.arousal);
            }

            if let Some(out_path) = output {
                println!();
                println!(
                    "{}",
                    format!("(Use --format json to save to {out_path:?})").dimmed()
                );
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_load_audio_samples_missing_file() {
        let path = PathBuf::from("/nonexistent/audio.wav");
        let result = load_audio_samples(&path);
        assert!(result.is_err());
    }

    #[test]
    fn test_load_audio_samples_from_temp_file() {
        let dir = std::env::temp_dir();
        let path = dir.join("oximedia_mir_test.wav");
        // Create a small dummy file
        std::fs::write(&path, vec![0u8; 1024]).expect("should write temp file");

        let result = load_audio_samples(&path);
        assert!(result.is_ok());
        let (samples, sr) = result.expect("should load samples");
        assert!(!samples.is_empty());
        assert!((sr - 44100.0).abs() < f32::EPSILON);

        // Cleanup
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_mir_config_selective() {
        let config = MirConfig {
            enable_beat_tracking: true,
            enable_key_detection: false,
            enable_chord_recognition: false,
            enable_melody_extraction: false,
            enable_structure_analysis: false,
            enable_genre_classification: false,
            enable_mood_detection: false,
            enable_spectral_features: false,
            enable_rhythm_features: false,
            enable_harmonic_analysis: false,
            ..MirConfig::default()
        };
        assert!(config.enable_beat_tracking);
        assert!(!config.enable_key_detection);
    }
}