-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseCodeTranslatorPage.jsx
More file actions
208 lines (194 loc) · 6.88 KB
/
MorseCodeTranslatorPage.jsx
File metadata and controls
208 lines (194 loc) · 6.88 KB
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
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import {
ArrowLeftIcon,
SpeakerSimpleHighIcon,
TranslateIcon,
WaveSineIcon,
} from '@phosphor-icons/react';
import Seo from '../../components/Seo';
import { useAchievements } from '../../context/AchievementContext';
import GenerativeArt from '../../components/GenerativeArt';
import BreadcrumbTitle from '../../components/BreadcrumbTitle';
const MORSE_CODE_MAP = {
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: '--..',
1: '.----',
2: '..---',
3: '...--',
4: '....-',
5: '.....',
6: '-....',
7: '--...',
8: '---..',
9: '----.',
0: '-----',
' ': '/',
',': '--..--',
'.': '.-.-.-',
'?': '..--..',
'/': '-..-.',
'-': '-....-',
'(': '-.--.',
')': '-.--.-',
};
const REVERSE_MORSE_CODE_MAP = Object.fromEntries(
Object.entries(MORSE_CODE_MAP).map(([key, value]) => [value, key]),
);
const MorseCodeTranslatorPage = () => {
const { unlockAchievement } = useAchievements();
const [text, setText] = useState('');
const [morseCode, setMorseCode] = useState('');
const handleTextChange = (e) => {
const newText = e.target.value.toUpperCase();
setText(newText);
const newMorseCode = newText
.split('')
.map((char) => MORSE_CODE_MAP[char] || '')
.join(' ');
setMorseCode(newMorseCode);
if (newText === 'SOS') unlockAchievement('sos_signal');
};
const handleMorseChange = (e) => {
const newMorseCode = e.target.value;
setMorseCode(newMorseCode);
const newText = newMorseCode
.split(' ')
.map((code) => REVERSE_MORSE_CODE_MAP[code] || '')
.join('');
setText(newText);
if (newText === 'SOS') unlockAchievement('sos_signal');
};
const playMorseCode = () => {
const audioContext = new (
window.AudioContext || window.webkitAudioContext
)();
const dotDuration = 80;
const dashDuration = dotDuration * 3;
const spaceDuration = dotDuration;
let time = audioContext.currentTime;
const playTone = (duration) => {
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.frequency.value = 600;
gainNode.gain.setValueAtTime(0, time);
gainNode.gain.linearRampToValueAtTime(1, time + 0.01);
gainNode.gain.linearRampToValueAtTime(0, time + duration);
oscillator.start(time);
oscillator.stop(time + duration);
};
for (const char of morseCode) {
if (char === '.') {
playTone(dotDuration / 1000);
time += (dotDuration + spaceDuration) / 1000;
} else if (char === '-') {
playTone(dashDuration / 1000);
time += (dashDuration + spaceDuration) / 1000;
} else if (char === ' ') {
time += (dotDuration * 3) / 1000;
} else if (char === '/') {
time += (dotDuration * 7) / 1000;
}
}
};
return (
<div className="min-h-screen bg-[#050505] text-white selection:bg-emerald-500/30 font-sans">
<Seo
title="Morse Translator | Fezcodex"
description="Translate natural language into binary pulses and vice-versa."
keywords={['Fezcodex', 'morse code', 'translator', 'converter']}
/>
<div className="mx-auto max-w-7xl px-6 py-24 md:px-12">
<header className="mb-20">
<Link
to="/apps"
className="mb-8 inline-flex items-center gap-2 text-xs font-mono text-gray-500 hover:text-white transition-colors uppercase tracking-widest"
>
<ArrowLeftIcon weight="bold" />
<span>Applications</span>
</Link>
<div className="flex flex-col md:flex-row md:items-end justify-between gap-12">
<div className="space-y-4">
<BreadcrumbTitle
title="Morse Code"
slug="mct"
variant="brutalist"
/>
<p className="text-xl text-gray-400 max-w-2xl font-light leading-relaxed">
Auditory signaling protocol. Bilateral translation between
plaintext and standardized Morse code pulses.
</p>
</div>
<button
onClick={playMorseCode}
disabled={!morseCode}
className="group relative inline-flex items-center gap-3 px-8 py-4 bg-white text-black hover:bg-emerald-400 transition-all duration-300 font-mono uppercase tracking-widest text-sm font-black rounded-sm disabled:opacity-20"
>
<SpeakerSimpleHighIcon weight="bold" size={20} />
<span>Auditory Feed</span>
</button>
</div>
</header>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-12">
<div className="relative border border-white/10 bg-white/[0.02] p-8 rounded-sm group overflow-hidden">
<div className="absolute inset-0 opacity-5 pointer-events-none">
<GenerativeArt seed="MORSE_TEXT" className="w-full h-full" />
</div>
<h3 className="font-mono text-[10px] font-bold text-gray-500 uppercase tracking-widest mb-6 flex items-center gap-2">
<TranslateIcon weight="fill" className="text-emerald-500" />
Language_Input
</h3>
<textarea
value={text}
onChange={handleTextChange}
className="w-full h-64 bg-black/40 border border-white/5 p-6 font-mono text-xl text-gray-300 focus:border-emerald-500 focus:outline-none transition-all rounded-sm resize-none scrollbar-hide uppercase"
placeholder="Insert plaintext..."
/>
</div>
<div className="relative border border-white/10 bg-white/[0.02] p-8 rounded-sm group overflow-hidden">
<div className="absolute inset-0 opacity-5 pointer-events-none">
<GenerativeArt seed="MORSE_PULSE" className="w-full h-full" />
</div>
<h3 className="font-mono text-[10px] font-bold text-gray-500 uppercase tracking-widest mb-6 flex items-center gap-2">
<WaveSineIcon weight="fill" className="text-emerald-500" />
Pulse_Sequence
</h3>
<textarea
value={morseCode}
onChange={handleMorseChange}
className="w-full h-64 bg-black/40 border border-white/5 p-6 font-mono text-2xl text-emerald-400 focus:border-emerald-500 focus:outline-none transition-all rounded-sm resize-none scrollbar-hide tracking-[0.2em]"
placeholder=".... --- .-.. -.."
/>
</div>
</div>
</div>
</div>
);
};
export default MorseCodeTranslatorPage;