Skip to content

Commit 0110dcb

Browse files
committed
Add parallelization using rayon #1
1 parent 07e869b commit 0110dcb

File tree

3 files changed

+90
-18
lines changed

3 files changed

+90
-18
lines changed

Cargo.lock

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2024"
77
anyhow = "1.0.100"
88
clap = { version = "4.5.48", features = ["derive"] }
99
hound = "3.5.1"
10+
rayon = "1.11.0"
1011
rubato = "0.16.2"
1112
walkdir = "2.5.0"
1213

src/main.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::error::Error;
22
use std::fs;
33
use std::path::{Path, PathBuf};
4+
use std::sync::atomic::{AtomicUsize, Ordering};
45

56
use clap::Parser;
67
use hound::{SampleFormat, WavReader, WavSpec, WavWriter};
8+
use rayon::prelude::*;
79
use rubato::{
810
Resampler, SincFixedIn, SincInterpolationParameters, SincInterpolationType, WindowFunction,
911
};
@@ -31,29 +33,46 @@ fn main() -> Result<(), Box<dyn Error>> {
3133
// Ensure output directory exists
3234
fs::create_dir_all(&args.output_dir)?;
3335

36+
let total_files = AtomicUsize::new(0);
37+
let processed_files = AtomicUsize::new(0);
38+
3439
// Recursively process all .wav files
35-
for entry in WalkDir::new(&args.input_dir)
40+
WalkDir::new(&args.input_dir)
3641
.follow_links(true)
3742
.into_iter()
3843
.filter_map(|e| e.ok())
39-
{
40-
if entry.file_type().is_file()
41-
&& entry
42-
.path()
43-
.extension()
44-
.and_then(|s| s.to_str())
45-
.is_some_and(|ext| ext.eq_ignore_ascii_case("wav"))
46-
&& let Ok(rel_path) = entry.path().strip_prefix(&args.input_dir)
47-
{
48-
let out_path = args.output_dir.join(rel_path);
49-
if let Some(parent) = out_path.parent() {
50-
fs::create_dir_all(parent)?;
51-
}
52-
if let Err(e) = process_wav(entry.path(), &out_path) {
53-
eprintln!("Error processing {}: {}", entry.path().display(), e);
44+
.par_bridge()
45+
.for_each(|entry| {
46+
if entry.file_type().is_file()
47+
&& entry
48+
.path()
49+
.extension()
50+
.and_then(|s| s.to_str())
51+
.is_some_and(|ext| ext.eq_ignore_ascii_case("wav"))
52+
{
53+
total_files.fetch_add(1, Ordering::Relaxed);
54+
if let Ok(rel_path) = entry.path().strip_prefix(&args.input_dir) {
55+
let out_path = args.output_dir.join(rel_path);
56+
if let Some(parent) = out_path.parent() {
57+
if let Err(e) = fs::create_dir_all(parent) {
58+
eprintln!("Error creating directory {}: {}", parent.display(), e);
59+
return;
60+
}
61+
}
62+
if let Err(e) = process_wav(entry.path(), &out_path) {
63+
eprintln!("Error processing {}: {}", entry.path().display(), e);
64+
} else {
65+
processed_files.fetch_add(1, Ordering::Relaxed);
66+
}
67+
}
5468
}
55-
}
56-
}
69+
});
70+
71+
println!(
72+
"Finished. Processed {} out of {} WAV files.",
73+
processed_files.load(Ordering::Relaxed),
74+
total_files.load(Ordering::Relaxed)
75+
);
5776

5877
Ok(())
5978
}

0 commit comments

Comments
 (0)