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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1988, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)morse.c 8.1 (Berkeley) 5/31/93
31 * $FreeBSD: src/games/morse/morse.c,v 1.12.2.2 2002/03/12 17:45:15 phantom Exp $
35 * Taught to send *real* morse by Lyndon Nerenberg (VE7TCP/VE6BBM)
36 * <lyndon@orthanc.com>
40 #include <sys/soundcard.h>
60 static const struct morsetab mtab[] = {
109 {'!', "-.-.--"}, /* KW */
113 {'=', "-...-"}, /* BT */
116 {'(', "-.--."}, /* KN */
119 {'+', ".-.-."}, /* AR */
122 {'@', ".--.-."}, /* AC */
128 static const struct morsetab iso8859tab[] = {
143 static const struct morsetab koi8rtab[] = {
145 * the cyrillic alphabet; you'll need a KOI8R font in order
146 * to see the actual characters
149 {'Â', "-..."}, /* be */
150 {'×', ".--"}, /* ve */
151 {'Ç', "--."}, /* ge */
152 {'Ä', "-.."}, /* de */
154 {'£', "."}, /* yo, the same as ye */
155 {'Ö', "...-"}, /* she */
156 {'Ú', "--.."}, /* ze */
158 {'Ê', ".---"}, /* i kratkoye */
159 {'Ë', "-.-"}, /* ka */
160 {'Ì', ".-.."}, /* el */
161 {'Í', "--"}, /* em */
162 {'Î', "-."}, /* en */
163 {'Ï', "---"}, /* o */
164 {'Ð', ".--."}, /* pe */
165 {'Ò', ".-."}, /* er */
166 {'Ó', "..."}, /* es */
168 {'Õ', "..-"}, /* u */
169 {'Æ', "..-."}, /* ef */
170 {'È', "...."}, /* kha */
171 {'Ã', "-.-."}, /* ce */
172 {'Þ', "---."}, /* che */
173 {'Û', "----"}, /* sha */
174 {'Ý', "--.-"}, /* shcha */
175 {'Ù', "-.--"}, /* yi */
176 {'Ø', "-..-"}, /* myakhkij znak */
177 {'Ü', "..-.."}, /* ae */
178 {'À', "..--"}, /* yu */
179 {'Ñ', ".-.-"}, /* ya */
189 void alloc_soundbuf(struct tone_data *, double, int);
190 void morse(char, int);
191 void show(const char *, int);
192 void play(const char *, int);
193 void ttyout(const char *, int);
194 void sighandler(int);
196 #define GETOPTOPTS "d:ef:lopP:sw:W:"
198 "usage: morse [-els] [-p | -o] [-P device] [-d device] [-w speed] [-W speed] [-f frequency] [string ...]\n"
200 static int lflag, oflag, pflag, sflag, eflag;
201 static int wpm = 20; /* words per minute */
202 static int farnsworth = -1;
203 #define FREQUENCY 600
204 static int freq = FREQUENCY;
205 static char *device; /* for tty-controlled generator */
207 static struct tone_data tone_dot, tone_dash, tone_silence, tone_letter_silence;
208 #define DSP_RATE 44100
209 static const char *snddev = NULL;
213 #define WORD_SPACE (7 - CHAR_SPACE)
214 static float dot_clock, word_clock;
216 struct termios otty, ntty;
219 static const struct morsetab *hightab;
222 main(int argc, char **argv)
228 while ((ch = getopt(argc, argv, GETOPTOPTS)) != -1)
235 setvbuf(stdout, 0, _IONBF, 0);
259 farnsworth = atoi(optarg);
263 fputs(USAGE, stderr);
266 if (sflag && lflag) {
267 fputs("morse: only one of -l and -s allowed\n", stderr);
270 if (pflag + !!device + sflag + lflag > 1) {
271 fputs("morse: only one of -o, -p, -d and -l, -s allowed\n", stderr);
274 if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (farnsworth > 60))) {
275 fputs("morse: insane speed\n", stderr);
278 if ((pflag || device) && (freq == 0))
280 if (pflag || device) {
282 * A note on how to get to this magic 1.2:
283 * x WPM = 50*x dits per minute (norm word "PARIS").
284 * dits per sec = dits per minute / 60, thus
285 * dits per sec = 50 * x / 60 = x / (60 / 50) = x / 1.2
287 dot_clock = wpm / 1.2; /* dots/sec */
288 dot_clock = 1 / dot_clock; /* duration of a dot */
290 word_clock = dot_clock;
293 * This is how to get to this formula:
294 * PARIS = 22 dit (symbols) + 9 symbol spaces = 31 symbol times
297 * The symbol times are in dot_clock, so the spaces have to
298 * make up to reach the farnsworth time.
301 word_clock = (60.0 / farnsworth - 31 * dot_clock) / 19;
303 if (snddev == NULL) {
306 else /* only pflag */
311 snd_chan_param param;
313 if (oflag && strcmp(snddev, "-") == 0)
314 spkr = STDOUT_FILENO;
316 spkr = open(snddev, O_WRONLY, 0);
318 err(1, "%s", snddev);
319 param.play_rate = DSP_RATE;
320 param.play_format = AFMT_S16_NE;
322 param.rec_format = 0;
323 if (!oflag && ioctl(spkr, AIOSFMT, ¶m) != 0)
324 err(1, "%s: set format", snddev);
325 alloc_soundbuf(&tone_dot, dot_clock, 1);
326 alloc_soundbuf(&tone_dash, DASH_LEN * dot_clock, 1);
327 alloc_soundbuf(&tone_silence, dot_clock, 0);
328 alloc_soundbuf(&tone_letter_silence, word_clock, 0);
331 if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
332 perror("open tty line");
335 if (tcgetattr(line, &otty) == -1) {
336 perror("tcgetattr() failed");
340 ntty.c_cflag |= CLOCAL;
341 tcsetattr(line, TCSANOW, &ntty);
342 lflags = fcntl(line, F_GETFL);
343 lflags &= ~O_NONBLOCK;
344 fcntl(line, F_SETFL, &lflags);
345 ioctl(line, TIOCMGET, &lflags);
346 lflags &= ~TIOCM_RTS;
348 ioctl(line, TIOCMSET, &lflags);
349 (void)signal(SIGHUP, sighandler);
350 (void)signal(SIGINT, sighandler);
351 (void)signal(SIGQUIT, sighandler);
352 (void)signal(SIGTERM, sighandler);
358 if (setlocale(LC_CTYPE, "") != NULL &&
359 *(codeset = nl_langinfo(CODESET)) != '\0') {
360 if (strcmp(codeset, "KOI8-R") == 0)
362 else if (strcmp(codeset, "ISO8859-1") == 0 ||
363 strcmp(codeset, "ISO8859-15") == 0)
364 hightab = iso8859tab;
372 for (p = *argv; *p; ++p) {
375 if (*p == '<' || *p == '>') {
379 if (strchr("> \r\n", *(p + 1)) != NULL)
389 while ((ch = getchar()) != EOF) {
400 if (strchr("> \r\n", tch) != NULL)
409 tcsetattr(line, TCSANOW, &otty);
414 alloc_soundbuf(struct tone_data *tone, double len, int on)
418 samples = DSP_RATE * len;
419 tone->len = samples * sizeof(*tone->data);
420 tone->data = malloc(tone->len);
421 if (tone->data == NULL)
424 bzero(tone->data, tone->len);
429 * We create a sinus with the specified frequency and smooth
430 * the edges to reduce key clicks.
432 for (i = 0; i < samples; i++) {
435 #define FILTER_SAMPLES (DSP_RATE * 8 / 1000) /* 8 ms ramp time */
436 if (i < FILTER_SAMPLES || i > samples - FILTER_SAMPLES) {
439 if (i > FILTER_SAMPLES)
441 #if defined(TRIANGLE_FILTER)
445 filter = (double)fi / FILTER_SAMPLES;
446 #elif defined(GAUSS_FILTER)
451 pow((double)(FILTER_SAMPLES - fi) /
457 filter = (1 + cos(M_PI * (FILTER_SAMPLES - fi) / FILTER_SAMPLES)) / 2;
460 tone->data[i] = 32767 * sin((double)i / samples * len * freq * 2 * M_PI) *
466 morse(char c, int prosign)
468 const struct morsetab *m;
470 if (isalpha((unsigned char)c))
471 c = tolower((unsigned char)c);
472 if ((c == '\r') || (c == '\n'))
488 for (m = ((unsigned char)c < 0x80? mtab: hightab);
489 m != NULL && m->inchar != '\0';
491 if (m->inchar == c) {
493 play(m->morse, prosign);
495 ttyout(m->morse, prosign);
497 show(m->morse, prosign);
503 show(const char *s, int prosign)
512 printf(" %s", *s == '.' ? "dit" : "dah");
518 play(const char *s, int prosign)
522 struct tone_data *tone;
525 * We don't need to usleep() here, as the sound device blocks.
527 for (c = s; *c != '\0'; c++) {
538 duration = WORD_SPACE;
539 tone = &tone_letter_silence;
542 errx(1, "invalid morse digit");
544 while (duration-- > 0)
545 write(spkr, tone->data, tone->len);
546 /* Only space within a symbol */
547 if (c[1] != '\0' || prosign)
548 write(spkr, tone_silence.data, tone_silence.len);
552 duration = CHAR_SPACE;
553 while (duration-- > 0)
554 write(spkr, tone_letter_silence.data, tone_letter_silence.len);
556 /* Sync out the audio data with other output */
558 ioctl(spkr, SNDCTL_DSP_SYNC, NULL);
562 ttyout(const char *s, int prosign)
565 int duration, on, lflags;
567 for (c = s; *c != '\0'; c++) {
571 duration = dot_clock;
575 duration = dot_clock * DASH_LEN;
579 duration = word_clock * WORD_SPACE;
586 ioctl(line, TIOCMGET, &lflags);
588 ioctl(line, TIOCMSET, &lflags);
593 ioctl(line, TIOCMGET, &lflags);
594 lflags &= ~TIOCM_RTS;
595 ioctl(line, TIOCMSET, &lflags);
596 duration = dot_clock * 1000000;
597 /* Only space within a symbol */
598 if (c[1] != '\0' || prosign)
602 duration = word_clock * CHAR_SPACE * 1000000;
608 sighandler(int signo)
611 ioctl(line, TIOCMSET, &olflags);
612 tcsetattr(line, TCSANOW, &otty);
614 signal(signo, SIG_DFL);
615 (void)kill(getpid(), signo);