summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs28
1 files changed, 14 insertions, 14 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 32eb119..df2665e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,26 +4,26 @@
mod decoder;
pub mod generator;
-pub use generator::MorseGenerator;
use decoder::MorseDecoder;
+pub use generator::MorseGenerator;
use anyhow::Result;
/// High-level convenience function to decode a WAV file directly
-///
+///
/// # Example
/// ```no_run
/// use ditdah::decode_wav_file;
-///
+///
/// let decoded_text = decode_wav_file("morse.wav").unwrap();
/// println!("Decoded: {}", decoded_text);
/// ```
pub fn decode_wav_file<P: AsRef<std::path::Path>>(path: P) -> Result<String> {
use hound::{SampleFormat, WavReader};
-
+
let mut reader = WavReader::open(path)?;
let spec = reader.spec();
-
+
// Check supported formats
if spec.sample_format != SampleFormat::Int && spec.sample_format != SampleFormat::Float {
anyhow::bail!(
@@ -31,10 +31,10 @@ pub fn decode_wav_file<P: AsRef<std::path::Path>>(path: P) -> Result<String> {
spec.sample_format
);
}
-
+
// Create decoder with automatic sample rate conversion
let mut decoder = MorseDecoder::new(spec.sample_rate, 12000)?;
-
+
// Read all samples
let samples_f32: Vec<f32> = if spec.sample_format == SampleFormat::Int {
reader
@@ -44,7 +44,7 @@ pub fn decode_wav_file<P: AsRef<std::path::Path>>(path: P) -> Result<String> {
} else {
reader.samples::<f32>().map(|s| s.unwrap()).collect()
};
-
+
// Convert to mono if necessary
let mono_samples: Vec<f32> = if spec.channels > 1 {
samples_f32
@@ -54,22 +54,22 @@ pub fn decode_wav_file<P: AsRef<std::path::Path>>(path: P) -> Result<String> {
} else {
samples_f32
};
-
+
// Process in chunks for better memory efficiency
const CHUNK_SIZE: usize = 4096;
for chunk in mono_samples.chunks(CHUNK_SIZE) {
decoder.process(chunk)?;
}
-
+
decoder.finalize()
}
/// High-level convenience function to decode audio samples directly
-///
+///
/// # Example
/// ```no_run
/// use ditdah::decode_samples;
-///
+///
/// // For real audio data with sufficient length for processing
/// let samples = vec![0.0; 48000]; // 4 seconds of audio at 12kHz
/// let decoded_text = decode_samples(&samples, 12000).unwrap();
@@ -77,11 +77,11 @@ pub fn decode_wav_file<P: AsRef<std::path::Path>>(path: P) -> Result<String> {
/// ```
pub fn decode_samples(samples: &[f32], sample_rate: u32) -> Result<String> {
let mut decoder = MorseDecoder::new(sample_rate, 12000)?;
-
+
const CHUNK_SIZE: usize = 4096;
for chunk in samples.chunks(CHUNK_SIZE) {
decoder.process(chunk)?;
}
-
+
decoder.finalize()
}