morsewave 0.1.0

High-performance Morse code encoder/decoder with WebAssembly support and Web Audio API playback
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! # MorseWave
//!
//! A high-performance Morse code encoder/decoder library built with Rust and WebAssembly.
//!
//! MorseWave provides blazing-fast Morse code encoding and decoding capabilities,
//! along with real-time audio playback using the Web Audio API.
//!
//! ## Features
//!
//! - **Fast Encoding/Decoding**: Convert text to Morse code and back in <1ms
//! - **Audio Playback**: Real-time Morse code audio synthesis
//! - **WebAssembly Support**: Compile to WASM for browser usage
//! - **Full Character Set**: Supports letters, numbers, and punctuation
//!
//! ## Example
//!
//! ```rust
//! use morsewave::MorseWave;
//!
//! let morse = MorseWave::new();
//! let encoded = morse.text_to_morse("HELLO");
//! assert_eq!(encoded, ".... . .-.. .-.. ---");
//!
//! let decoded = morse.morse_to_text(".... . .-.. .-.. ---");
//! assert_eq!(decoded, "HELLO");
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use wasm_bindgen::prelude::*;
use web_sys::AudioContext;

/// Represents a Morse code message with its text, encoded form, and timestamp.
///
/// # Fields
///
/// * `text` - The original text message
/// * `morse` - The Morse code representation
/// * `timestamp` - Unix timestamp in milliseconds
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MorseMessage {
    pub text: String,
    pub morse: String,
    pub timestamp: f64,
}

/// Core Morse code encoder and decoder.
///
/// Provides bidirectional conversion between text and Morse code using
/// International Morse Code standard (ITU-R M.1677-1).
///
/// # Example
///
/// ```rust
/// use morsewave::MorseCodec;
///
/// let codec = MorseCodec::new();
/// let morse = codec.encode("SOS");
/// assert_eq!(morse, "... --- ...");
/// ```
pub struct MorseCodec {
    encode_map: HashMap<char, &'static str>,
    decode_map: HashMap<&'static str, char>,
}

impl Default for MorseCodec {
    fn default() -> Self {
        Self::new()
    }
}

impl MorseCodec {
    /// Creates a new MorseCodec with standard International Morse Code mappings.
    ///
    /// Initializes lookup tables for encoding and decoding.
    ///
    /// # Example
    ///
    /// ```rust
    /// use morsewave::MorseCodec;
    /// let codec = MorseCodec::new();
    /// ```
    pub fn new() -> Self {
        let pairs = vec![
            ('A', ".-"),
            ('B', "-..."),
            ('C', "-.-."),
            ('D', "-.."),
            ('E', "."),
            ('F', "..-."),
            ('G', "--."),
            ('H', "...."),
            ('I', ".."),
            ('J', ".---"),
            ('K', "-.-"),
            ('L', ".-.."),
            ('M', "--"),
            ('N', "-."),
            ('O', "---"),
            ('P', ".--."),
            ('Q', "--.-"),
            ('R', ".-."),
            ('S', "..."),
            ('T', "-"),
            ('U', "..-"),
            ('V', "...-"),
            ('W', ".--"),
            ('X', "-..-"),
            ('Y', "-.--"),
            ('Z', "--.."),
            ('0', "-----"),
            ('1', ".----"),
            ('2', "..---"),
            ('3', "...--"),
            ('4', "....-"),
            ('5', "....."),
            ('6', "-...."),
            ('7', "--..."),
            ('8', "---.."),
            ('9', "----."),
            ('.', ".-.-.-"),
            (',', "--..--"),
            ('?', "..--.."),
            ('!', "-.-.--"),
            ('/', "-..-."),
            ('(', "-.--."),
            (')', "-.--.-"),
            ('&', ".-..."),
            (':', "---..."),
            (';', "-.-.-."),
            ('=', "-...-"),
            ('+', ".-.-."),
            ('-', "-....-"),
            ('_', "..--.-"),
            ('"', ".-..-."),
            ('$', "...-..-"),
            ('@', ".--.-."),
            (' ', "/"),
        ];

        let mut encode_map = HashMap::new();
        let mut decode_map = HashMap::new();

        for (ch, morse) in pairs.iter() {
            encode_map.insert(*ch, *morse);
            decode_map.insert(*morse, *ch);
        }

        MorseCodec {
            encode_map,
            decode_map,
        }
    }

    /// Encodes text into Morse code.
    ///
    /// Converts uppercase letters, numbers, and punctuation to Morse code.
    /// Unknown characters are silently ignored.
    ///
    /// # Arguments
    ///
    /// * `text` - The text to encode
    ///
    /// # Returns
    ///
    /// Morse code string with spaces between letters and '/' for word spaces
    ///
    /// # Example
    ///
    /// ```rust
    /// use morsewave::MorseCodec;
    /// let codec = MorseCodec::new();
    /// assert_eq!(codec.encode("HELLO"), ".... . .-.. .-.. ---");
    /// ```
    pub fn encode(&self, text: &str) -> String {
        text.to_uppercase()
            .chars()
            .filter_map(|ch| self.encode_map.get(&ch))
            .map(|s| s.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    }

    /// Decodes Morse code back into text.
    ///
    /// Converts Morse code sequences into their corresponding characters.
    /// Invalid Morse sequences are silently ignored.
    ///
    /// # Arguments
    ///
    /// * `morse` - The Morse code to decode (space-separated)
    ///
    /// # Returns
    ///
    /// Decoded text string
    ///
    /// # Example
    ///
    /// ```rust
    /// use morsewave::MorseCodec;
    /// let codec = MorseCodec::new();
    /// assert_eq!(codec.decode("... --- ..."), "SOS");
    /// ```
    pub fn decode(&self, morse: &str) -> String {
        morse
            .split(' ')
            .filter_map(|code| self.decode_map.get(code))
            .collect()
    }
}

/// WebAssembly-compatible Morse code interface.
///
/// Provides WASM bindings for encoding, decoding, and validating Morse code
/// in browser environments.
///
/// # Example
///
/// ```javascript
/// import init, { MorseWave } from './pkg/morsewave.js';
///
/// await init();
/// const morse = new MorseWave();
/// console.log(morse.text_to_morse("HELLO"));
/// ```
#[wasm_bindgen]
pub struct MorseWave {
    codec: MorseCodec,
}

impl Default for MorseWave {
    fn default() -> Self {
        Self::new()
    }
}

#[wasm_bindgen]
impl MorseWave {
    /// Creates a new MorseWave instance.
    ///
    /// Initializes panic hook for better error messages in browser console.
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        console_error_panic_hook::set_once();
        MorseWave {
            codec: MorseCodec::new(),
        }
    }

    /// Converts text to Morse code.
    ///
    /// # Arguments
    ///
    /// * `text` - Text to encode
    ///
    /// # Returns
    ///
    /// Morse code string
    pub fn text_to_morse(&self, text: &str) -> String {
        self.codec.encode(text)
    }

    /// Converts Morse code to text.
    ///
    /// # Arguments
    ///
    /// * `morse` - Morse code to decode
    ///
    /// # Returns
    ///
    /// Decoded text string
    pub fn morse_to_text(&self, morse: &str) -> String {
        self.codec.decode(morse)
    }

    /// Validates Morse code syntax.
    ///
    /// Checks if input contains only valid Morse characters (., -, /, space).
    ///
    /// # Arguments
    ///
    /// * `morse` - Morse code to validate
    ///
    /// # Returns
    ///
    /// true if valid, false otherwise
    pub fn validate_morse(&self, morse: &str) -> bool {
        morse
            .split_whitespace()
            .all(|code| code.chars().all(|c| c == '.' || c == '-' || c == '/'))
    }
}

/// Web Audio API-based Morse code audio player.
///
/// Generates authentic Morse code tones using sine wave oscillators.
/// Supports adjustable speed (WPM) and standard timing rules.
///
/// # Example
///
/// ```javascript
/// const player = new AudioPlayer(20); // 20 WPM
/// player.play_morse("... --- ...");
/// ```
#[wasm_bindgen]
pub struct AudioPlayer {
    context: AudioContext,
    dot_duration: f64,
}

#[wasm_bindgen]
impl AudioPlayer {
    /// Creates a new AudioPlayer with specified speed.
    ///
    /// # Arguments
    ///
    /// * `wpm` - Words per minute (5-40 recommended)
    ///
    /// # Returns
    ///
    /// Result containing AudioPlayer or JsValue error
    ///
    /// # Formula
    ///
    /// dot_duration_ms = 1200 / WPM
    #[wasm_bindgen(constructor)]
    pub fn new(wpm: f64) -> Result<AudioPlayer, JsValue> {
        let context = AudioContext::new()?;
        let dot_duration = 1200.0 / wpm;

        Ok(AudioPlayer {
            context,
            dot_duration,
        })
    }

    /// Plays Morse code audio.
    ///
    /// Synthesizes audio for dots, dashes, and spaces with proper timing.
    ///
    /// # Arguments
    ///
    /// * `morse` - Morse code string to play
    ///
    /// # Returns
    ///
    /// Result indicating success or error
    ///
    /// # Timing
    ///
    /// * Dot: 1 unit
    /// * Dash: 3 units
    /// * Gap between elements: 1 unit
    /// * Gap between letters: 3 units (space)
    /// * Gap between words: 7 units (/)
    pub fn play_morse(&self, morse: &str) -> Result<(), JsValue> {
        let mut time = self.context.current_time();

        for ch in morse.chars() {
            match ch {
                '.' => {
                    self.play_tone(time, self.dot_duration)?;
                    time += self.dot_duration / 1000.0;
                    time += self.dot_duration / 1000.0;
                }
                '-' => {
                    self.play_tone(time, self.dot_duration * 3.0)?;
                    time += (self.dot_duration * 3.0) / 1000.0;
                    time += self.dot_duration / 1000.0;
                }
                ' ' => {
                    time += (self.dot_duration * 3.0) / 1000.0;
                }
                '/' => {
                    time += (self.dot_duration * 7.0) / 1000.0;
                }
                _ => {}
            }
        }

        Ok(())
    }

    /// Plays a single tone at specified time and duration.
    ///
    /// # Arguments
    ///
    /// * `start_time` - AudioContext time to start
    /// * `duration` - Duration in milliseconds
    ///
    /// # Audio Properties
    ///
    /// * Frequency: 800 Hz sine wave
    /// * Volume: 0.3 gain
    fn play_tone(&self, start_time: f64, duration: f64) -> Result<(), JsValue> {
        let oscillator = self.context.create_oscillator()?;
        let gain_node = self.context.create_gain()?;

        oscillator.set_type(web_sys::OscillatorType::Sine);

        let freq_param = oscillator.frequency();
        freq_param.set_value(800.0);

        let gain_param = gain_node.gain();
        gain_param.set_value(0.3);

        oscillator.connect_with_audio_node(&gain_node)?;
        gain_node.connect_with_audio_node(&self.context.destination())?;

        oscillator.start_with_when(start_time)?;
        oscillator.stop_with_when(start_time + duration / 1000.0)?;

        Ok(())
    }

    /// Updates playback speed.
    ///
    /// # Arguments
    ///
    /// * `wpm` - New words per minute speed
    pub fn set_wpm(&mut self, wpm: f64) {
        self.dot_duration = 1200.0 / wpm;
    }
}