mod compare;
mod statement;
use std::fs;
use std::io::{BufWriter, Write as _};
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, bail};
use clap::{Args, Subcommand, ValueEnum};
use duet_abc::{Dynamics, WriteOptions, off_level_velocities};
use duet_capture::{
A440_CENTS, ANALYSIS_RATE, Error, Event, FileSource, Given, Hits, Kind, LaneMode, Layer1, Mono,
NoteModel, Performance, PerformanceFrames, PerformanceHead, Prepare, Quantized, ReadNote,
Source, Stage, a4_cents_of_hz, assemble, estimate, merge_collisions, quantize_apart,
quantize_offsets, read,
};
#[cfg(feature = "basic-pitch")]
use duet_capture::{BasicPitch, Thresholds};
use duet_core::{Control, Grid, Key, Position, Song, TimeMap, TimeSig};
use duet_schemas::{Document, Stream};
use crate::config::Config;
use crate::drums::{ReadOptions, grid_of_song, refuse_overwrite};
use crate::io::{tool, with_end};
#[derive(Subcommand)]
pub enum CaptureCommand {
Melody(TakeArgs),
Rhythm(TakeArgs),
Compare(compare::CompareArgs),
}
#[derive(Args)]
pub struct TakeArgs {
input: PathBuf,
#[arg(long)]
tempo: Option<f64>,
#[arg(long)]
meter: Option<TimeSig>,
#[arg(long)]
key: Option<String>,
#[arg(long)]
downbeat: Option<f64>,
#[arg(long, allow_hyphen_values = true)]
tuning: Option<TuningArg>,
#[arg(long, value_enum)]
reader: Option<ReaderArg>,
#[cfg(feature = "basic-pitch")]
#[arg(long, value_name = "ACTIVATION", allow_hyphen_values = true)]
onset_threshold: Option<f64>,
#[cfg(feature = "basic-pitch")]
#[arg(long, value_name = "ACTIVATION", allow_hyphen_values = true)]
frame_threshold: Option<f64>,
#[cfg(feature = "basic-pitch")]
#[arg(long, value_name = "MS", allow_hyphen_values = true)]
min_note_ms: Option<f64>,
#[arg(long)]
grid: Option<Grid>,
#[arg(long)]
lane: Option<String>,
#[arg(short, long, value_name = "STEM")]
output: Option<PathBuf>,
}
#[derive(Clone, Copy, PartialEq, Eq, ValueEnum)]
enum ReaderArg {
Rules,
Hmm,
#[cfg(feature = "basic-pitch")]
BasicPitch,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Take {
Melody,
Rhythm,
}
pub fn run(command: CaptureCommand, config: &Config) -> Result<()> {
let (take, args) = match command {
CaptureCommand::Melody(args) => (Take::Melody, args),
CaptureCommand::Rhythm(args) => (Take::Rhythm, args),
CaptureCommand::Compare(args) => return compare::run(&args),
};
let key = flags(take, &args)?;
let grid = match args.grid {
Some(grid) => grid,
None => DEFAULT_GRID.parse()?,
};
grid.step_ticks(Song::default().ticks_per_quarter)
.with_context(|| format!("--grid {grid}"))?;
let outputs = Outputs::new(&args.input, args.output.as_deref(), take)?;
#[cfg(feature = "basic-pitch")]
let basic_pitch = basic_pitch_of(&args)?;
let named = || args.input.display().to_string();
#[cfg(feature = "basic-pitch")]
let keep = args.reader == Some(ReaderArg::BasicPitch);
#[cfg(not(feature = "basic-pitch"))]
let keep = false;
let (mut events, kept, samples) =
analyse(&args.input, take, args.lane.is_some(), &outputs, keep)?;
#[cfg(not(feature = "basic-pitch"))]
let _ = samples;
let mut read_said: Vec<String> = Vec::new();
let (performance, reading) = match kept {
Some((file, frames)) => {
let given = args.tuning.map(|t| t.a4_cents);
let reading = frames
.map(|frames| -> Result<_> {
Ok(match args.reader.unwrap_or(ReaderArg::Hmm) {
ReaderArg::Rules => read(&frames, &events, given),
ReaderArg::Hmm => NoteModel::default().read(&frames, &events, given),
#[cfg(feature = "basic-pitch")]
ReaderArg::BasicPitch => {
let read = basic_pitch
.as_ref()
.context("basic-pitch was not loaded")?
.read(&samples, &frames, &events, given)?;
read_said.push(statement::basic_pitch(&read));
read.reading
}
})
})
.transpose()
.with_context(named)?;
if let Some(reading) = &reading {
events.extend(reading.notes.iter().map(ReadNote::event));
events.sort_by_key(event_frame);
}
(Some(file), reading)
}
None => (None, None),
};
let what = match take {
Take::Melody => "notes",
Take::Rhythm => "hits",
};
if !events.iter().any(|e| match take {
Take::Melody => matches!(e, Event::Note { .. }),
Take::Rhythm => matches!(e, Event::Hit { .. }),
}) {
return Err(anyhow::Error::from(Error::NothingFound { what }).context(named()));
}
let given = Given {
tempo_bpm: args.tempo,
meter: args.meter,
downbeat_secs: args.downbeat,
};
let timing = estimate(&events, ANALYSIS_RATE, &given).with_context(named)?;
let kind = match take {
Take::Melody => Kind::Melody,
Take::Rhythm => Kind::Rhythm {
lanes: config.lanes.clone(),
asked: args.lane.clone(),
},
};
let mut captured = assemble(&events, ANALYSIS_RATE, &timing, key, &kind).with_context(named)?;
let ms_per_quarter = 1000.0 * timing.beat_period_frames / f64::from(ANALYSIS_RATE);
let quantized = quantize_apart(&captured.song, grid, &kind).and_then(|q| {
let offsets = quantize_offsets(&captured.song, &q.song, q.grid, ms_per_quarter)?;
Ok((q, offsets))
});
if let Ok((q, offsets)) = &quantized {
captured.analysis.grid = Some(q.grid);
captured.analysis.offsets.clone_from(offsets);
}
let meter = *timing.meter.value();
let advice = reading
.as_ref()
.and_then(|reading| statement::tuning_advice(&reading.tuning));
let mut said = statement::timing(&timing, ANALYSIS_RATE);
if let Some(reading) = &reading {
said.extend(statement::tuning(&reading.tuning));
said.extend(statement::reader(reading));
said.append(&mut read_said);
said.push(statement::notes_read(reading));
said.extend(statement::breathy(reading, ANALYSIS_RATE));
said.extend(statement::rejoined(reading));
}
said.extend(statement::key(captured.analysis.key.as_ref()));
captured.analysis.reading = reading;
let lead_in = statement::lead_in(
captured.lead_in,
captured.downbeat_tick,
meter,
captured.song.ticks_per_quarter,
);
let song_said = [said.clone(), vec![lead_in.clone()]].concat();
let mut song = Document::new(tool(), with_end(&captured.song, config.end)?);
song.statements.clone_from(&song_said);
let song = song.write()?;
let analysis = Document::new(tool(), captured.analysis).write()?;
for line in &song_said {
eprintln!("{line}");
}
if let Some(advice) = advice {
eprintln!("{advice}");
}
write_all(&[(&outputs.song, &song), (&outputs.analysis, &analysis)])?;
if let Some(performance) = performance {
performance.keep();
}
let wrote = || {
format!(
"{}: {} not written; {} and {} hold the performance",
named(),
outputs.score.display(),
outputs.song.display(),
outputs.analysis.display()
)
};
let (quantized, _) = quantized.map_err(|e| anyhow::Error::from(e).context(wrote()))?;
let scored = Scoring {
take,
kind: &kind,
grid,
grid_given: args.grid.is_some(),
downbeat_tick: captured.downbeat_tick,
meter,
said,
lead_in,
config,
}
.score(quantized)
.with_context(wrote)?;
for line in &scored.stated {
eprintln!("{line}");
}
write_new(&outputs.score, &scored.text).with_context(wrote)
}
const DEFAULT_GRID: &str = "1/16";
struct Scoring<'a> {
take: Take,
kind: &'a Kind,
grid: Grid,
grid_given: bool,
downbeat_tick: u64,
meter: TimeSig,
said: Vec<String>,
lead_in: String,
config: &'a Config,
}
struct Scored {
text: String,
stated: Vec<String>,
}
impl Scoring<'_> {
fn score(self, quantized: Quantized) -> Result<Scored> {
let mut stated = Vec::new();
if quantized.grid != self.grid {
stated.push(statement::refined(
self.grid,
self.grid_given,
quantized.grid,
self.take == Take::Rhythm,
));
}
let trimmed = without_empty_lead_in(&quantized.song, self.downbeat_tick, self.meter)?;
if trimmed.is_some() {
stated.push(statement::LEAD_IN_REMOVED.to_owned());
}
let mut score = trimmed.unwrap_or(quantized.song);
for merge in merge_collisions(&mut score, self.kind)? {
stated.push(match self.take {
Take::Melody => statement::merged_notes(&merge),
Take::Rhythm => {
let lane = self
.config
.lanes
.by_key(merge.kept.pitch.midi())
.map_or_else(
|| format!("key {}", merge.kept.pitch.midi()),
|l| l.name.clone(),
);
statement::merged_hits(&merge, &lane)
}
});
}
let mut score_said = self.said;
if !stated.iter().any(|l| l == statement::LEAD_IN_REMOVED) {
score_said.push(self.lead_in);
}
score_said.extend(stated.iter().cloned());
let text = match self.take {
Take::Melody => {
let options = WriteOptions::default().with_dynamics(Dynamics::Nearest);
let abc = duet_abc::write(&with_end(&score, self.config.end)?, &options)?;
let moved = off_level_velocities(&score);
if moved > 0 {
stated.push(statement::snapped(
moved,
"the nearest dynamic's level in the .abc",
));
}
commented(&abc, "%", &score_said)
}
Take::Rhythm => {
let options = ReadOptions {
grid: quantized.grid,
track: None,
bars: None,
snap_levels: false,
};
let grid = grid_of_song(&score, &options, self.config)?;
let mut doc = Document::new(tool(), grid);
doc.statements = score_said;
doc.write()?
}
};
Ok(Scored { text, stated })
}
}
#[cfg(feature = "basic-pitch")]
fn basic_pitch_of(args: &TakeArgs) -> Result<Option<BasicPitch>> {
let given = [
("--onset-threshold", args.onset_threshold),
("--frame-threshold", args.frame_threshold),
("--min-note-ms", args.min_note_ms),
];
if args.reader != Some(ReaderArg::BasicPitch) {
if let Some((flag, _)) = given.iter().find(|(_, v)| v.is_some()) {
bail!("{flag} is for --reader basic-pitch; no other reader decodes with it");
}
return Ok(None);
}
let own = Thresholds::BASIC_PITCH;
let thresholds = Thresholds {
onset_threshold: args.onset_threshold.unwrap_or(own.onset_threshold),
frame_threshold: args.frame_threshold.unwrap_or(own.frame_threshold),
minimum_note_length_ms: args.min_note_ms.unwrap_or(own.minimum_note_length_ms),
};
Ok(Some(BasicPitch::new()?.with_thresholds(thresholds)?))
}
fn flags(take: Take, args: &TakeArgs) -> Result<Option<Key>> {
if let Some(bpm) = args.tempo
&& !(bpm.is_finite() && bpm > 0.0)
{
bail!("--tempo {bpm}: a tempo must be a positive number of quarter notes a minute");
}
match take {
Take::Melody => {
if args.lane.is_some() {
bail!("--lane is for a rhythm take; a melody's notes are pitched, not in lanes");
}
if let Some(bpm) = args.tempo
&& bpm.is_finite()
&& bpm.fract() != 0.0
{
bail!(
"--tempo {bpm}: a melody's tempo must be a whole number of bpm, because \
its ABC states the tempo as Q:1/4=<bpm> and ABC's Q: holds only a whole \
number; give --tempo {} or {}",
bpm.floor(),
bpm.ceil()
);
}
}
Take::Rhythm => {
if args.key.is_some() {
bail!("--key is for a melody take; a rhythm take has no key");
}
if args.tuning.is_some() {
bail!("--tuning is for a melody take; a rhythm take's hits have no pitch");
}
if args.reader.is_some() {
bail!("--reader is for a melody take; a rhythm take's hits are not read as notes");
}
}
}
args.key
.as_deref()
.map(|name| duet_abc::key_from_name(name).with_context(|| format!("--key {name}")))
.transpose()
}
struct Outputs {
song: PathBuf,
score: PathBuf,
analysis: PathBuf,
performance: Option<PathBuf>,
}
impl Outputs {
fn new(input: &Path, stem: Option<&Path>, take: Take) -> Result<Outputs> {
let stem = stem.map_or_else(|| input.with_extension(""), Path::to_path_buf);
let named = |ext: &str| {
let mut path = stem.clone().into_os_string();
path.push(ext);
PathBuf::from(path)
};
let outputs = Outputs {
song: named(".duet"),
score: named(match take {
Take::Melody => ".abc",
Take::Rhythm => ".grid.duet",
}),
analysis: named(".capture.duet"),
performance: (take == Take::Melody).then(|| named(".performance.duet.jsonl")),
};
let all: Vec<&PathBuf> = [&outputs.song, &outputs.score, &outputs.analysis]
.into_iter()
.chain(&outputs.performance)
.collect();
for &path in &all {
refuse_overwrite(input, path, "capture")?;
}
for path in all {
if path.exists() {
bail!(
"{} exists; capture writes new files and never overwrites one, so move it \
or name another stem with -o",
path.display()
);
}
}
Ok(outputs)
}
}
#[derive(Debug, Clone, Copy)]
pub struct TuningArg {
a4_cents: f64,
}
const MAX_TUNING_CENTS: f64 = 100.0;
impl std::str::FromStr for TuningArg {
type Err = String;
fn from_str(s: &str) -> std::result::Result<TuningArg, String> {
let text = s.trim();
let offset = if let Some(cents) = text.strip_suffix('c') {
let cents: f64 = cents.parse().map_err(|_| {
format!("{s:?}: cents from 440 Hz are a signed number before c, such as +44c")
})?;
if !cents.is_finite() {
return Err(format!("{s:?}: cents must be a finite number"));
}
cents
} else {
let hz: f64 = text.parse().map_err(|_| {
format!("{s:?}: a tuning is A4 in Hz (440, 442) or cents from 440 Hz (+44c)")
})?;
if !(hz.is_finite() && hz > 0.0) {
return Err(format!("{s:?}: A4 in Hz must be a positive number"));
}
a4_cents_of_hz(hz) - A440_CENTS
};
if offset.abs() > MAX_TUNING_CENTS {
return Err(format!(
"{s:?} is {offset:+.0} cents from 440 Hz; a tuning is at most \
{MAX_TUNING_CENTS} cents (a key) either way of it"
));
}
Ok(TuningArg {
a4_cents: A440_CENTS + offset,
})
}
}
fn event_frame(event: &Event) -> u64 {
match *event {
Event::Onset { frame, .. } | Event::Hit { frame, .. } => frame,
Event::Note { start, .. } => start,
_ => u64::MAX,
}
}
enum Analyser {
Melody {
stage: Box<Performance>,
file: PerformanceFile,
frames: Option<PerformanceFrames>,
},
Rhythm(Box<Hits>),
}
impl Analyser {
fn push(&mut self, mono: Mono, events: &mut Vec<Event>) -> Result<()> {
match self {
Self::Melody { stage, .. } => {
let mut kept = Vec::new();
stage.push(mono, &mut kept)?;
self.keep(kept, events)
}
Self::Rhythm(hits) => Ok(hits.push(mono, events)?),
}
}
fn finish(&mut self, events: &mut Vec<Event>) -> Result<()> {
match self {
Self::Melody { stage, .. } => {
let mut kept = Vec::new();
stage.finish(&mut kept)?;
self.keep(kept, events)?;
match self {
Self::Melody { file, .. } => file.flush(),
Self::Rhythm(_) => Ok(()),
}
}
Self::Rhythm(hits) => Ok(hits.finish(events)?),
}
}
fn keep(&mut self, kept: Vec<Layer1>, events: &mut Vec<Event>) -> Result<()> {
let Self::Melody { file, frames, .. } = self else {
return Ok(());
};
for layer in &kept {
match layer {
Layer1::Head(head) => *frames = Some(PerformanceFrames::new(head)),
Layer1::Frames(block) => frames
.as_mut()
.context("a performance's frames came before its head")?
.push(block)?,
&Layer1::Onset { frame, strength } => {
events.push(Event::Onset { frame, strength });
}
_ => {}
}
}
file.write(kept)
}
}
fn analyse(
input: &Path,
take: Take,
asked: bool,
outputs: &Outputs,
keep_samples: bool,
) -> Result<(Vec<Event>, Option<Kept>, Vec<f32>)> {
let mut source = FileSource::open(input)?;
let mut prepare = Prepare::new(ANALYSIS_RATE);
let mut stage = match (take, &outputs.performance) {
(Take::Melody, Some(path)) => Analyser::Melody {
stage: Box::new(Performance::new(ANALYSIS_RATE)),
file: PerformanceFile::create(path)?,
frames: None,
},
(Take::Melody, None) => bail!("a melody take has a performance file"),
(Take::Rhythm, _) => Analyser::Rhythm(Box::new(Hits::new(
ANALYSIS_RATE,
if asked {
LaneMode::Asked
} else {
LaneMode::Beatbox
},
))),
};
let (mut mono, mut events, mut samples) = (Vec::new(), Vec::new(), Vec::new());
let named = || input.display().to_string();
while let Some(chunk) = source.next_chunk()? {
prepare.push(chunk, &mut mono).with_context(named)?;
for m in mono.drain(..) {
if keep_samples {
samples.extend_from_slice(&m.samples);
}
stage.push(m, &mut events).with_context(named)?;
}
}
prepare.finish(&mut mono).with_context(named)?;
for m in mono.drain(..) {
if keep_samples {
samples.extend_from_slice(&m.samples);
}
stage.push(m, &mut events).with_context(named)?;
}
stage.finish(&mut events).with_context(named)?;
let kept = match stage {
Analyser::Melody { file, frames, .. } => Some((file, frames)),
Analyser::Rhythm(_) => None,
};
Ok((events, kept, samples))
}
type Kept = (PerformanceFile, Option<PerformanceFrames>);
struct PerformanceFile {
path: PathBuf,
file: Option<BufWriter<fs::File>>,
headed: bool,
}
impl PerformanceFile {
fn create(path: &Path) -> Result<PerformanceFile> {
let file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)
.with_context(|| path.display().to_string())?;
Ok(PerformanceFile {
path: path.to_path_buf(),
file: Some(BufWriter::new(file)),
headed: false,
})
}
fn write(&mut self, kept: Vec<Layer1>) -> Result<()> {
for layer in kept {
let line = match layer {
Layer1::Head(head) if !self.headed => {
self.headed = true;
Stream::<PerformanceHead>::new(tool(), head).head_line()?
}
Layer1::Frames(block) if self.headed => {
Stream::<PerformanceHead>::record_line(&block)?
}
Layer1::Frames(_) => bail!("a performance's frames came before its head"),
_ => continue,
};
self.out()?
.write_all(line.as_bytes())
.with_context(|| self.path.display().to_string())?;
}
Ok(())
}
fn flush(&mut self) -> Result<()> {
self.out()?
.flush()
.with_context(|| self.path.display().to_string())
}
fn out(&mut self) -> Result<&mut BufWriter<fs::File>> {
self.file
.as_mut()
.with_context(|| format!("{} is already closed", self.path.display()))
}
fn keep(mut self) {
self.file = None;
}
}
impl Drop for PerformanceFile {
fn drop(&mut self) {
if self.file.take().is_some() {
let _ = fs::remove_file(&self.path);
}
}
}
fn without_empty_lead_in(score: &Song, downbeat_tick: u64, meter: TimeSig) -> Result<Option<Song>> {
if downbeat_tick == 0 {
return Ok(None);
}
let from = score.time_map()?;
let to = TimeMap::new(score.ticks_per_quarter, meter)?;
let moved = |at: Position| -> Result<Option<Position>> {
let tick = from.tick(at)?;
Ok(tick.checked_sub(downbeat_tick).map(|t| to.position(t)))
};
let mut out = score.clone();
out.time_sig = meter;
out.time_changes.retain(|c| c.at != Position::bar(2));
for track in &mut out.tracks {
for note in &mut track.notes {
let Some(at) = moved(note.at)? else {
return Ok(None);
};
note.at = at;
}
for control in &mut track.controls {
let at = match control {
Control::Cc { at, .. } | Control::Bend { at, .. } | Control::Program { at, .. } => {
at
}
other => bail!("capture cannot move a {other:?} out of a lead-in"),
};
let Some(new) = moved(*at)? else {
return Ok(None);
};
*at = new;
}
}
Ok(Some(out))
}
fn commented(text: &str, prefix: &str, lines: &[String]) -> String {
let (first, rest) = text.split_once('\n').unwrap_or((text, ""));
let mut out = format!("{first}\n");
for line in lines {
out.push_str(&format!("{prefix} {line}\n"));
}
out.push_str(rest);
out
}
fn write_all(files: &[(&Path, &str)]) -> Result<()> {
for (i, (path, text)) in files.iter().enumerate() {
if let Err(e) = write_new(path, text) {
let removed: Vec<String> = files[..i]
.iter()
.filter(|(p, _)| fs::remove_file(p).is_ok())
.map(|(p, _)| p.display().to_string())
.collect();
if removed.is_empty() {
return Err(e);
}
return Err(e.context(format!(
"capture wrote nothing: removed {} again",
removed.join(" and ")
)));
}
}
Ok(())
}
fn write_new(path: &Path, text: &str) -> Result<()> {
let mut file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)
.with_context(|| path.display().to_string())?;
if let Err(e) = file.write_all(text.as_bytes()) {
let _ = fs::remove_file(path);
return Err(e).with_context(|| path.display().to_string());
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use duet_core::{Note, Pitch, TimeChange, Track};
fn sig(s: &str) -> TimeSig {
s.parse().expect("a time signature")
}
fn with_pickup(ticks: &[u64]) -> Song {
let mut song = Song::default();
song.time_sig = sig("1/4");
song.time_changes.push(TimeChange {
at: Position::bar(2),
time_sig: sig("4/4"),
});
let map = song.time_map().expect("map");
let mut track = Track::new("melody", 1);
for &t in ticks {
track.notes.push(Note::new(
map.position(t),
Pitch::new(60).expect("C4"),
1.0,
80,
));
}
song.tracks.push(track);
song
}
#[test]
fn an_empty_pickup_is_taken_out() {
let song = with_pickup(&[480, 960, 2400]);
let out = without_empty_lead_in(&song, 480, sig("4/4"))
.expect("moves")
.expect("the pickup is empty");
assert_eq!(out.time_sig, sig("4/4"));
assert!(out.time_changes.is_empty());
let at: Vec<String> = out.tracks[0]
.notes
.iter()
.map(|n| n.at.to_string())
.collect();
assert_eq!(at, ["1.1.0", "1.2.0", "2.1.0"]);
}
#[test]
fn a_pickup_with_a_note_in_it_stays() {
let song = with_pickup(&[0, 480]);
assert!(
without_empty_lead_in(&song, 480, sig("4/4"))
.expect("ok")
.is_none()
);
}
#[test]
fn a_song_with_no_lead_in_is_left_alone() {
let song = with_pickup(&[0]);
assert!(
without_empty_lead_in(&song, 0, sig("4/4"))
.expect("ok")
.is_none()
);
}
#[test]
fn a_failed_write_removes_what_was_written() {
let dir = std::env::temp_dir().join(format!("duet-capture-write-{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("dir");
let first = dir.join("take.notes");
let second = dir.join("missing").join("take.abc");
let err = write_all(&[(&first, "notes\n"), (&second, "abc\n")]).unwrap_err();
assert!(!first.exists(), "the .notes was left behind");
let message = format!("{err:#}");
assert!(
message.contains("removed") && message.contains("take.notes"),
"{message}"
);
std::fs::remove_dir_all(&dir).expect("clean up");
}
#[test]
fn comments_go_after_the_first_line() {
let lines = ["tempo: estimated 92 bpm".to_owned()];
assert_eq!(
commented("X:1\nT:x\n", "%", &lines),
"X:1\n% tempo: estimated 92 bpm\nT:x\n"
);
}
}