2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1988, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)morse.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/games/morse/morse.c,v 1.12.2.2 2002/03/12 17:45:15 phantom Exp $
36 * $DragonFly: src/games/morse/morse.c,v 1.4 2007/04/22 10:22:32 corecode Exp $
40 * Taught to send *real* morse by Lyndon Nerenberg (VE7TCP/VE6BBM)
41 * <lyndon@orthanc.com>
45 #include <sys/soundcard.h>
65 static const struct morsetab mtab[] = {
116 {'=', "-...-"}, /* BT */
119 {'(', "-.--."}, /* KN */
122 {'+', ".-.-."}, /* AR */
124 /* prosigns without already assigned values */
126 {'#', ".-..."}, /* AS */
127 {'@', "...-.-"}, /* SK */
128 {'*', "...-."}, /* VE */
129 {'%', "-...-.-"}, /* BK */
135 static const struct morsetab iso8859tab[] = {
150 static const struct morsetab koi8rtab[] = {
152 * the cyrillic alphabet; you'll need a KOI8R font in order
153 * to see the actual characters
156 {'Â', "-..."}, /* be */
157 {'×', ".--"}, /* ve */
158 {'Ç', "--."}, /* ge */
159 {'Ä', "-.."}, /* de */
161 {'£', "."}, /* yo, the same as ye */
162 {'Ö', "...-"}, /* she */
163 {'Ú', "--.."}, /* ze */
165 {'Ê', ".---"}, /* i kratkoye */
166 {'Ë', "-.-"}, /* ka */
167 {'Ì', ".-.."}, /* el */
168 {'Í', "--"}, /* em */
169 {'Î', "-."}, /* en */
170 {'Ï', "---"}, /* o */
171 {'Ð', ".--."}, /* pe */
172 {'Ò', ".-."}, /* er */
173 {'Ó', "..."}, /* es */
175 {'Õ', "..-"}, /* u */
176 {'Æ', "..-."}, /* ef */
177 {'È', "...."}, /* kha */
178 {'Ã', "-.-."}, /* ce */
179 {'Þ', "---."}, /* che */
180 {'Û', "----"}, /* sha */
181 {'Ý', "--.-"}, /* shcha */
182 {'Ù', "-.--"}, /* yi */
183 {'Ø', "-..-"}, /* myakhkij znak */
184 {'Ü', "..-.."}, /* ae */
185 {'À', "..--"}, /* yu */
186 {'Ñ', ".-.-"}, /* ya */
196 void alloc_soundbuf(struct tone_data *, double, int);
197 void show(const char *), play(const char *), morse(char);
198 void ttyout(const char *);
199 void sighandler(int);
201 #define GETOPTOPTS "d:ef:pP:sw:"
203 "usage: morse [-s] [-e] [-p] [-P device] [-d device] [-w speed] [-f frequency] [string ...]\n"
205 static int pflag, sflag, eflag;
206 static int wpm = 20; /* words per minute */
207 #define FREQUENCY 600
208 static int freq = FREQUENCY;
209 static char *device; /* for tty-controlled generator */
211 static struct tone_data tone_dot, tone_dash, tone_silence;
212 #define DSP_RATE 44100
213 static const char *snddev = "/dev/dsp";
217 #define WORD_SPACE (7 - CHAR_SPACE)
218 static float dot_clock;
220 struct termios otty, ntty;
223 static const struct morsetab *hightab;
226 main(int argc, char **argv)
231 while ((ch = getopt(argc, argv, GETOPTOPTS)) != -1)
238 setvbuf(stdout, 0, _IONBF, 0);
257 fputs(USAGE, stderr);
260 if ((pflag || device) && sflag) {
261 fputs("morse: only one of -p, -d and -s allowed\n", stderr);
264 if ((pflag || device) && ((wpm < 1) || (wpm > 60))) {
265 fputs("morse: insane speed\n", stderr);
268 if ((pflag || device) && (freq == 0))
270 if (pflag || device) {
271 dot_clock = wpm / 2.4; /* dots/sec */
272 dot_clock = 1 / dot_clock; /* duration of a dot */
273 dot_clock = dot_clock / 2; /* dot_clock runs at twice */
278 snd_chan_param param;
280 if ((spkr = open(snddev, O_WRONLY, 0)) == -1)
281 err(1, "%s", snddev);
282 param.play_rate = DSP_RATE;
283 param.play_format = AFMT_S16_NE;
285 param.rec_format = 0;
286 if (ioctl(spkr, AIOSFMT, ¶m) != 0)
287 err(1, "%s: set format", snddev);
288 alloc_soundbuf(&tone_dot, dot_clock, 1);
289 alloc_soundbuf(&tone_dash, DASH_LEN * dot_clock, 1);
290 alloc_soundbuf(&tone_silence, dot_clock, 0);
293 if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
294 perror("open tty line");
297 if (tcgetattr(line, &otty) == -1) {
298 perror("tcgetattr() failed");
302 ntty.c_cflag |= CLOCAL;
303 tcsetattr(line, TCSANOW, &ntty);
304 lflags = fcntl(line, F_GETFL);
305 lflags &= ~O_NONBLOCK;
306 fcntl(line, F_SETFL, &lflags);
307 ioctl(line, TIOCMGET, &lflags);
308 lflags &= ~TIOCM_RTS;
310 ioctl(line, TIOCMSET, &lflags);
311 (void)signal(SIGHUP, sighandler);
312 (void)signal(SIGINT, sighandler);
313 (void)signal(SIGQUIT, sighandler);
314 (void)signal(SIGTERM, sighandler);
320 if (setlocale(LC_CTYPE, "") != NULL &&
321 *(codeset = nl_langinfo(CODESET)) != '\0') {
322 if (strcmp(codeset, "KOI8-R") == 0)
324 else if (strcmp(codeset, "ISO8859-1") == 0 ||
325 strcmp(codeset, "ISO8859-15") == 0)
326 hightab = iso8859tab;
331 for (p = *argv; *p; ++p) {
341 while ((ch = getchar()) != EOF) {
348 tcsetattr(line, TCSANOW, &otty);
353 alloc_soundbuf(struct tone_data *tone, double len, int on)
357 samples = DSP_RATE * len;
358 tone->len = samples * sizeof(*tone->data);
359 tone->data = malloc(tone->len);
360 if (tone->data == NULL)
363 bzero(tone->data, tone->len);
368 * We create a sinus with the specified frequency and smooth
369 * the edges to reduce key clicks.
371 for (i = 0; i < samples; i++) {
374 #define FILTER_SAMPLES 100
375 if (i < FILTER_SAMPLES || i > samples - FILTER_SAMPLES) {
382 if (i > FILTER_SAMPLES)
385 pow((double)(fi - FILTER_SAMPLES) /
386 (0.4 * FILTER_SAMPLES), 2));
391 if (i < FILTER_SAMPLES)
392 filter = (double)i / FILTER_SAMPLES;
394 filter = (double)(samples - i) / FILTER_SAMPLES;
397 tone->data[i] = 32767 * sin((double)i / samples * len * freq * 2 * M_PI) *
405 const struct morsetab *m;
407 if (isalpha((unsigned char)c))
408 c = tolower((unsigned char)c);
409 if ((c == '\r') || (c == '\n'))
423 for (m = ((unsigned char)c < 0x80? mtab: hightab);
424 m != NULL && m->inchar != '\0';
426 if (m->inchar == c) {
444 printf(" %s", *s == '.' ? "dit" : "dah");
453 struct tone_data *tone;
456 * We don't need to usleep() here, as the sound device blocks.
458 for (c = s; *c != '\0'; c++) {
469 duration = WORD_SPACE;
470 tone = &tone_silence;
473 errx(1, "invalid morse digit");
475 while (duration-- > 0)
476 write(spkr, tone->data, tone->len);
477 write(spkr, tone_silence.data, tone_silence.len);
479 duration = CHAR_SPACE - 1; /* we already waited 1 after the last symbol */
480 while (duration-- > 0)
481 write(spkr, tone_silence.data, tone_silence.len);
485 ttyout(const char *s)
488 int duration, on, lflags;
490 for (c = s; *c != '\0'; c++) {
494 duration = dot_clock;
498 duration = dot_clock * DASH_LEN;
502 duration = dot_clock * WORD_SPACE;
509 ioctl(line, TIOCMGET, &lflags);
511 ioctl(line, TIOCMSET, &lflags);
516 ioctl(line, TIOCMGET, &lflags);
517 lflags &= ~TIOCM_RTS;
518 ioctl(line, TIOCMSET, &lflags);
519 duration = dot_clock * 1000000;
522 duration = dot_clock * CHAR_SPACE * 1000000;
527 sighandler(int signo)
530 ioctl(line, TIOCMSET, &olflags);
531 tcsetattr(line, TCSANOW, &otty);
533 signal(signo, SIG_DFL);
534 (void)kill(getpid(), signo);