Skip to content

Commit 3c51fcd

Browse files
authored
Merge pull request #4 from TheAwiteb/strip-header
Remove the wave header when decoding
2 parents e37840d + 2ea86e6 commit 3c51fcd

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ The following benchmarks were made on a 4.600GHz 12th Gen Intel i7-12700H CPU wi
5656
## Decoding
5757
| File size | Audio file size | Audio length | Speed | Link |
5858
|-----------|-----------------|------|-------| ---- |
59-
| 2687.94MB | 2687.94MB | 01:28:13 | 12.76s | [Soundcloud-link](https://soundcloud.com/awiteb/pop-os-2204-amd64-intel-23iso) |
60-
| 35.3MB | 35.3MB | 00:01::27 | 206.04ms | [Soundcloud-link](https://soundcloud.com/awiteb/rust-1671zip) |
59+
| 2687.94MB | 2687.94MB | 01:28:13 | 5.14s | [Soundcloud-link](https://soundcloud.com/awiteb/pop-os-2204-amd64-intel-23iso) |
60+
| 35.3MB | 35.3MB | 00:01::27 | 117.58ms | [Soundcloud-link](https://soundcloud.com/awiteb/rust-1671zip) |
6161

6262
## Disclaimer
6363
This tool was designed for educational purposes as it explains how to save data in an audio file. It is not recommended to exploit this thing to use cloud audio storage services to store your data, as your account may be banned.

src/errors.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub enum Error {
1212
Io(std::io::Error),
1313
/// Larg file size error (maxnimum file size is 4 GB)
1414
LargeFileSize,
15+
/// Invalid wav file error
16+
InvalidWavFile(String),
1517
}
1618

1719
impl From<hound::Error> for Error {
@@ -32,6 +34,7 @@ impl fmt::Display for Error {
3234
Error::Hound(err) => write!(f, "Hound error: {}", err),
3335
Error::Io(err) => write!(f, "IO error: {}", err),
3436
Error::LargeFileSize => write!(f, "File size is too large, maximum file size is 4 GB"),
37+
Error::InvalidWavFile(msg) => write!(f, "Invalid wav file, {}", msg),
3538
}
3639
}
3740
}

src/lib.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use file_utils::{read::Read, write::Write};
22
use std::{
33
fs,
4-
io::{BufReader, BufWriter},
4+
io::{BufReader, BufWriter, Seek},
55
path,
66
};
77

@@ -58,11 +58,15 @@ pub fn encode(file: fs::File, wav_output: impl AsRef<path::Path>) -> Result<()>
5858
/// decode("test.wav", "test.txt").unwrap();
5959
/// ```
6060
pub fn decode(file: impl AsRef<path::Path>, output: impl AsRef<path::Path>) -> Result<()> {
61-
utils::check_file_size(&fs::File::open(&file)?)?;
62-
let mut reader = hound::WavReader::open(file)?;
61+
let output_file = fs::File::open(&file)?;
62+
utils::check_file_size(&output_file)?;
63+
utils::check_wav_file_size(&output_file)?;
64+
let mut reader = BufReader::new(output_file);
6365
let mut writer = BufWriter::new(fs::File::create(output)?);
64-
for sample in reader.samples() {
65-
writer.write_i16(sample?)?;
66+
// Skip the header, to get to the data (the header is 44 bytes long)
67+
reader.seek(std::io::SeekFrom::Start(44))?;
68+
while let Ok(bytes) = reader.read_i16() {
69+
writer.write_i16(bytes)?;
6670
}
6771
Ok(())
6872
}

src/utils.rs

+13
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ pub(crate) fn check_file_size(file: &fs::File) -> Result<()> {
77
.then(|| Err(Error::LargeFileSize))
88
.unwrap_or(Ok(()))
99
}
10+
11+
/// Check if the wav file size is valid, the minimum size is 44 bytes. (the maximum will checked with `check_file_size`)
12+
/// ### Note
13+
/// The 44 bytes are the header of the wav file.
14+
pub(crate) fn check_wav_file_size(file: &fs::File) -> Result<()> {
15+
(file.metadata()?.len() < 44)
16+
.then(|| {
17+
Err(Error::InvalidWavFile(
18+
"the minimum size is 44 bytes".to_owned(),
19+
))
20+
})
21+
.unwrap_or(Ok(()))
22+
}

0 commit comments

Comments
 (0)