-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ecac805
Showing
6 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "data2sound" | ||
description = "A library to convert data to sound, and vice versa" | ||
version = "0.1.0" | ||
rust-version = "1.56.1" | ||
edition = "2021" | ||
authors = ["TheAwiteb <[email protected]>"] | ||
repository = "https://github.com/TheAwiteb/data2sound" | ||
documentation = "https://docs.rs/data2sound" | ||
license = "MIT" | ||
readme = "README.md" | ||
keywords = ["todo", "sdk"] | ||
categories = ["Audio"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
file-utils = "= 0.1.5" | ||
hound = "= 3.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 TheAwiteb | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Data to sound | ||
A simple crate to convert data to sound, and sound to data. The sound file format is .wav. | ||
You can use it as a library or as a command line tool. | ||
|
||
## Minimum supported Rust version | ||
The minimum supported Rust version is 1.56.1. | ||
|
||
## Note | ||
The sound frequency is 202860Hz (202.86kHz), and the sound is mono. The sound is encoded in 16 bits. | ||
## Disadvantages | ||
- The wave file size limit is 4GB, so you can't store more than 4GB of data in a single file. | ||
- The decoding speed is slow, so it will take a long time to decode a large file. (see benchmarks) | ||
## Advantages | ||
- The sound file is a standard .wav file, so you can play it with any audio player. | ||
- The sound file will be the same size as the data file. | ||
|
||
|
||
## Usage | ||
### Library | ||
Add this to your Cargo.toml: | ||
```toml | ||
[dependencies] | ||
data2sound = "0.1.0" | ||
``` | ||
See the documentation for more information about the library. | ||
|
||
### Command line Interface | ||
Install the tool with cargo: | ||
```bash | ||
cargo install data2sound | ||
``` | ||
Run the tool with: | ||
```bash | ||
data2sound --help | ||
``` | ||
|
||
And to convert a file to sound: | ||
```bash | ||
data2sound encode input_file output_file.wav | ||
``` | ||
And to convert a sound file to data: | ||
```bash | ||
data2sound decode input_file.wav output_file | ||
``` | ||
|
||
## Use cases | ||
### Infinite storage | ||
You can use this tool to store your data in soundcloud, or any other sound hosting service. The data is stored in the sound file, so you can store as much data as you want. The only limit is the size of the sound file. | ||
|
||
## About soundcloud | ||
Soundcloud is a service that allows you to upload and share your music. It is a great place to store your music, but it is also a great place to store your data. The data is stored in the sound file, so you can store as much data as you want. The only limit is the size of the sound file which is 4GB. | ||
|
||
## benchmarks | ||
The following benchmarks were made on a 4.600GHz 12th Gen Intel i7-12700H CPU with 16GB of RAM. | ||
### Encoding | ||
| File size | Audio file size | Audio length | Speed | Link | | ||
|-----------|-----------------|------|-------| ---- | | ||
| 2687.94MB | 2687.94MB | 01:28:13 | 323.00s | [Soundcloud-link](https://soundcloud.com/awiteb/pop-os-2204-amd64-intel-23iso) | | ||
| 35.3MB | 35.3MB | 00:01::27 | 4.11s | [Soundcloud-link](https://soundcloud.com/awiteb/rust-1671zip) | | ||
## Decoding | ||
| File size | Audio file size | Audio length | Speed | Link | | ||
|-----------|-----------------|------|-------| ---- | | ||
| 2687.94MB | 2687.94MB | 01:28:13 | ~1930.29s | [Soundcloud-link](https://soundcloud.com/awiteb/pop-os-2204-amd64-intel-23iso) | | ||
| 35.3MB | 35.3MB | 00:01::27 | 25.35s | [Soundcloud-link](https://soundcloud.com/awiteb/rust-1671zip) | | ||
|
||
|
||
## License | ||
This project is licensed under the MIT license. See the LICENSE file for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use file_utils::{read::Read, write::Write}; | ||
use std::{fs, path}; | ||
|
||
/// The sample rate to use when encoding | ||
pub const SAMPLE_RATE: u32 = 202860; | ||
/// The result type for this crate, which is just a re-export of `hound::Result` | ||
pub use hound::Result; | ||
|
||
/// Encode file to wav file by converting the bytes to a sine wave, then writing the sine wave to a wav file | ||
/// # Arguments | ||
/// * `file` - The file to encode | ||
/// * `wav_output` - The wav file to write the sine wave to | ||
/// # Example | ||
/// ```rust|no_run | ||
/// use data2sound::encode; | ||
/// use std::fs; | ||
/// let file = fs::File::open("test.txt").unwrap(); | ||
/// encode(file, "test.wav").unwrap(); | ||
/// ``` | ||
pub fn encode(mut file: fs::File, wav_output: impl AsRef<path::Path>) -> Result<()> { | ||
let spec = hound::WavSpec { | ||
channels: 1, | ||
sample_rate: SAMPLE_RATE, | ||
bits_per_sample: 16, | ||
sample_format: hound::SampleFormat::Int, | ||
}; | ||
let str_path = wav_output.as_ref().display().to_string(); | ||
let wav_output = if !str_path.ends_with(".wav") { | ||
format!("{}.wav", wav_output.as_ref().display()) | ||
} else { | ||
str_path | ||
}; | ||
let mut writer = hound::WavWriter::create(wav_output, spec)?; | ||
while let Ok(byte) = file.read_i16() { | ||
writer.write_sample(byte)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Decode wav file to file by converting the sine wave to bytes | ||
/// # Arguments | ||
/// * `file` - The wav file to decode | ||
/// * `output` - The file to write the bytes to | ||
/// # Example | ||
/// ```rust|no_run | ||
/// use data2sound::decode; | ||
/// use std::fs; | ||
/// decode("test.wav", "test.txt").unwrap(); | ||
/// ``` | ||
pub fn decode(file: impl AsRef<path::Path>, output: impl AsRef<path::Path>) -> Result<()> { | ||
let mut reader = hound::WavReader::open(file.as_ref()).unwrap(); | ||
let mut writer = fs::File::create(output).unwrap(); | ||
for sample in reader.samples() { | ||
writer.write_i16(sample?)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs; | ||
|
||
#[test] | ||
fn test_encode_decode() { | ||
fs::write("test.txt", "Some context").unwrap(); | ||
encode(fs::File::open("test.txt").unwrap(), "test.wav").unwrap(); | ||
decode("test.wav", "test2.txt").unwrap(); | ||
let file = fs::File::open("test.txt").unwrap(); | ||
let file2 = fs::File::open("test2.txt").unwrap(); | ||
assert_eq!( | ||
fs::read_to_string("test.txt").unwrap(), | ||
fs::read_to_string("test2.txt").unwrap() | ||
); | ||
assert_eq!( | ||
file.metadata().unwrap().len(), | ||
file2.metadata().unwrap().len() | ||
); | ||
fs::remove_file("test.txt").unwrap(); | ||
fs::remove_file("test2.txt").unwrap(); | ||
fs::remove_file("test.wav").unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{env, fs}; | ||
|
||
const HELP_MESSAGE: &str = r#"Usage: data2sound <command> <input> <output> | ||
Commands: | ||
encode, e Encode a file to a wav file | ||
decode, d Decode a wav file to a file | ||
Options: | ||
--help, -h Print this help message | ||
--version, -v Print the version | ||
"#; | ||
|
||
fn version() { | ||
println!( | ||
"data2sound {} {}", | ||
env!("CARGO_PKG_VERSION"), | ||
env!("CARGO_PKG_REPOSITORY") | ||
); | ||
} | ||
|
||
fn help() { | ||
println!("{}", HELP_MESSAGE); | ||
} | ||
|
||
fn main() -> data2sound::Result<()> { | ||
// Skip the first argument, which is the path to the executable | ||
let args: Vec<String> = env::args().collect(); | ||
if args.iter().any(|arg| arg == "--version" || arg == "-v") { | ||
version() | ||
} else if args.iter().any(|arg| arg == "--help" || arg == "-h") || args.len() < 4 { | ||
help() | ||
} else { | ||
let mut args = args.iter().skip(1); | ||
let command = args.next().unwrap(); | ||
let input = args.next().unwrap(); | ||
let output = args.next().unwrap(); | ||
match command.as_str() { | ||
"encode" | "e" => data2sound::encode(fs::File::open(input).unwrap(), output)?, | ||
"decode" | "d" => data2sound::decode(input, output)?, | ||
_ => eprintln!( | ||
"Unknown command '{}' Run 'data2sound --help' for more information", | ||
command | ||
), | ||
} | ||
} | ||
Ok(()) | ||
} |