|
1 | 1 | use std::error::Error; |
2 | 2 | use std::fs; |
3 | 3 | use std::path::{Path, PathBuf}; |
| 4 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
4 | 5 |
|
5 | 6 | use clap::Parser; |
6 | 7 | use hound::{SampleFormat, WavReader, WavSpec, WavWriter}; |
| 8 | +use rayon::prelude::*; |
7 | 9 | use rubato::{ |
8 | 10 | Resampler, SincFixedIn, SincInterpolationParameters, SincInterpolationType, WindowFunction, |
9 | 11 | }; |
@@ -31,29 +33,46 @@ fn main() -> Result<(), Box<dyn Error>> { |
31 | 33 | // Ensure output directory exists |
32 | 34 | fs::create_dir_all(&args.output_dir)?; |
33 | 35 |
|
| 36 | + let total_files = AtomicUsize::new(0); |
| 37 | + let processed_files = AtomicUsize::new(0); |
| 38 | + |
34 | 39 | // Recursively process all .wav files |
35 | | - for entry in WalkDir::new(&args.input_dir) |
| 40 | + WalkDir::new(&args.input_dir) |
36 | 41 | .follow_links(true) |
37 | 42 | .into_iter() |
38 | 43 | .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 | + } |
54 | 68 | } |
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 | + ); |
57 | 76 |
|
58 | 77 | Ok(()) |
59 | 78 | } |
|
0 commit comments