rumpl 0.3.0

Sample-free General MIDI player and renderer
Documentation
mod cli;

use anyhow::Result;
use clap::Parser;
use rumpl::synth::drums::gm_drum_name;
use rumpl::synth::patches::gm_program_name;
use rumpl::{demo, midi, output};

fn main() -> Result<()> {
  let args = cli::Cli::parse();
  match args.command {
    cli::Cmd::Play {
      file,
      gain,
      tail,
      soundfont,
    } => {
      warn_soundfont(soundfont.as_deref());
      let seq = midi::load(&file)?;
      output::play::play(seq, gain, tail)?;
    }
    cli::Cmd::Info { file } => {
      let info = midi::info(&file)?;
      print_info(&file, &info);
    }
    cli::Cmd::Instruments => {
      for program in 0..128u8 {
        println!("{:>3}  {}", program, gm_program_name(program));
      }
    }
    cli::Cmd::Demo { program, out } => match program {
      cli::DemoTarget::Program(program) => {
        let out = if out.is_dir() {
          out.join(demo::demo_filename(program))
        } else {
          out
        };
        demo::write_demo_midi(program, &out)?;
        eprintln!(
          "wrote demo of {} ({program}) → {}",
          gm_program_name(program),
          out.display()
        );
      }
      cli::DemoTarget::Drums => {
        let out = if out.is_dir() {
          out.join(demo::DRUM_DEMO_FILENAME)
        } else {
          out
        };
        demo::write_drum_demo_midi(&out)?;
        eprintln!("wrote demo of the drum kit → {}", out.display());
      }
      cli::DemoTarget::DrumSound(key) => {
        let out = if out.is_dir() {
          out.join(demo::drum_sound_demo_filename(key))
        } else {
          out
        };
        demo::write_drum_sound_demo_midi(key, &out)?;
        eprintln!(
          "wrote demo of {} (key {key}) → {}",
          gm_drum_name(key).unwrap_or("drum"),
          out.display()
        );
      }
    },
    cli::Cmd::Render {
      file,
      out,
      gain,
      sample_rate,
      tail,
      soundfont,
    } => {
      warn_soundfont(soundfont.as_deref());
      let seq = midi::load(&file)?;
      let out = out.unwrap_or_else(|| file.with_extension("wav"));
      output::render::render_to_wav(&seq, &out, sample_rate, gain, tail)?;
      eprintln!("rendered {} → {}", file.display(), out.display());
    }
  }
  Ok(())
}

fn print_info(file: &std::path::Path, info: &midi::FileInfo) {
  let mins = (info.duration / 60.0).floor() as u64;
  let secs = info.duration - (mins as f64) * 60.0;

  println!("{}", file.display());
  println!("  format:   {}", info.format);
  println!("  tracks:   {}", info.track_count);
  match info.ppq {
    Some(ppq) => println!("  division: {ppq} PPQ"),
    None => println!("  division: SMPTE timecode"),
  }
  println!("  length:   {mins}:{secs:05.2} ({:.2} s)", info.duration);

  let tempos: Vec<String> =
    info.tempos_bpm.iter().map(|b| format!("{b:.1}")).collect();
  let label = if info.tempos_bpm.len() > 1 {
    "tempos"
  } else {
    "tempo"
  };
  println!("  {label}:    {} BPM", tempos.join(", "));
  println!("  notes:    {}", info.note_count);

  println!("  voices:");
  for ch in &info.channels {
    let name = if ch.is_drums {
      "Drum Kit"
    } else {
      gm_program_name(ch.program)
    };
    println!(
      "    ch {:>2}: {name} ({} notes)",
      ch.channel + 1,
      ch.note_count
    );
  }
}

fn warn_soundfont(p: Option<&std::path::Path>) {
  if p.is_some() {
    eprintln!(
      "warning: --soundfont is ignored; rumpl synthesizes voices algorithmically"
    );
  }
}