bliss-rs/examples/distance.rs

34 lines
1.0 KiB
Rust
Raw Normal View History

use bliss_rs::bliss_lib::Song;
2021-05-14 17:35:08 +03:00
use std::env;
/**
* Simple utility to print distance between two songs according to bliss.
*
* Takes two file paths, and analyze the corresponding songs, printing
2021-05-14 17:35:08 +03:00
* the distance between the two files according to bliss.
*/
fn main() -> Result<(), String> {
let mut paths = env::args().skip(1).take(2);
let first_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?;
let second_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?;
let song1 = Song::from_path(&first_path).map_err(|x| x.to_string())?;
let song2 = Song::from_path(&second_path).map_err(|x| x.to_string())?;
2021-05-14 17:35:08 +03:00
2024-02-11 09:20:48 +02:00
let mut distance_squared: f64 = 0.0;
let analysis1 = song1.analysis.as_bytes();
let analysis2 = song2.analysis.as_bytes();
for (i, feature1) in analysis1.iter().enumerate() {
distance_squared += (feature1 - analysis2[i]).pow(2) as f64;
}
2021-05-14 17:35:08 +03:00
println!(
2021-06-13 15:07:17 +03:00
"d({:?}, {:?}) = {}",
&first_path,
&second_path,
2024-02-11 09:20:48 +02:00
distance_squared.sqrt(),
2021-05-14 17:35:08 +03:00
);
Ok(())
}