diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..90b3a9e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hound" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62adaabb884c94955b19907d60019f4e145d091c75345379e70d1ee696f7854f" + +[[package]] +name = "morse-translator" +version = "0.1.0" +dependencies = [ + "hound", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..36f27be --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "morse-translator" +version = "0.1.0" +edition = "2021" + +[dependencies] +hound = "3.5.1" diff --git a/morse.wav b/morse.wav new file mode 100644 index 0000000..ca22bb5 Binary files /dev/null and b/morse.wav differ diff --git a/morse_danish.wav b/morse_danish.wav new file mode 100644 index 0000000..b791469 Binary files /dev/null and b/morse_danish.wav differ diff --git a/sine.wav b/sine.wav new file mode 100644 index 0000000..3f951e0 Binary files /dev/null and b/sine.wav differ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..42d4e25 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,59 @@ +use hound::{self, WavWriter}; +use std::f32::consts::PI; +use std::{fs, i16, io}; + +const SAMPLE_RATE: f32 = 44100.0; +const MORSE_FREQ: f32 = 600.0; +const UNIT_TIME: f32 = 100.0 / 1000.0; // IN MS +const CHAR_TIME: f32 = 50.0 / 1000.0; + +const SPEC: hound::WavSpec = hound::WavSpec { + channels: 1, + sample_rate: 44100, + bits_per_sample: 16, + sample_format: hound::SampleFormat::Int, +}; + +fn write_freq(writer: &mut WavWriter>, freq: f32, duration: f32) { + let sample_count = (duration * SAMPLE_RATE).ceil() as i32; + for t in (0..sample_count).map(|x| x as f32 / SAMPLE_RATE) { + let sample = (t * freq * 2.0 * PI).sin(); + let amplitude = i16::MAX as f32; + writer.write_sample((sample * amplitude) as i16).unwrap(); + } +} + +fn write_morse(filename: &str, morse: &str) { + let mut writer = hound::WavWriter::create(filename, SPEC).unwrap(); + + //for t in (0..sample_count).map(|x| x as f32 / SAMPLE_RATE) { + // let sample = (t * MORSE_FREQ * 2.0 * PI).sin(); + // let amplitude = i16::MAX as f32; + // writer.write_sample((sample * amplitude) as i16).unwrap(); + //} + + for c in morse.chars() { + // We give a margin of CHAR_TIME between dots and dashes + // and we give 1.5 * UNIT_TIME between characters + if c == '.' { + write_freq(&mut writer, MORSE_FREQ, UNIT_TIME); + write_freq(&mut writer, 0.0, CHAR_TIME); + } else if c == '-' { + write_freq(&mut writer, MORSE_FREQ, 3.0 * UNIT_TIME); + write_freq(&mut writer, 0.0, CHAR_TIME); + } else if c == ' ' { + write_freq(&mut writer, 0.0, 1.5 * UNIT_TIME); + } else { + panic!("The morse code given is not valid!"); + } + } + write_freq(&mut writer, 0.0, UNIT_TIME); // End padding +} + +fn main() { + // -.. .- -. .. ... ... + + write_morse("morse_danish.wav", "-.. .- -. .. ... ...."); + //write_freq(&mut writer, 350.0, 1.0); + //write_freq(&mut writer, 650.0, 1.0); +} diff --git a/test.wav b/test.wav new file mode 100644 index 0000000..07a0b79 Binary files /dev/null and b/test.wav differ