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 $
32 * $DragonFly: src/games/morse/morse.c,v 1.8 2008/05/30 21:47:04 corecode Exp $
36 * Taught to send *real* morse by Lyndon Nerenberg (VE7TCP/VE6BBM)
37 * <lyndon@orthanc.com>
41 #include <sys/soundcard.h>
61 static const struct morsetab mtab
[] = {
110 {'!', "-.-.--"}, /* KW */
114 {'=', "-...-"}, /* BT */
117 {'(', "-.--."}, /* KN */
120 {'+', ".-.-."}, /* AR */
123 {'@', ".--.-."}, /* AC */
129 static const struct morsetab iso8859tab
[] = {
144 static const struct morsetab koi8rtab
[] = {
146 * the cyrillic alphabet; you'll need a KOI8R font in order
147 * to see the actual characters
150 {'Â', "-..."}, /* be */
151 {'×', ".--"}, /* ve */
152 {'Ç', "--."}, /* ge */
153 {'Ä', "-.."}, /* de */
155 {'£', "."}, /* yo, the same as ye */
156 {'Ö', "...-"}, /* she */
157 {'Ú', "--.."}, /* ze */
159 {'Ê', ".---"}, /* i kratkoye */
160 {'Ë', "-.-"}, /* ka */
161 {'Ì', ".-.."}, /* el */
162 {'Í', "--"}, /* em */
163 {'Î', "-."}, /* en */
164 {'Ï', "---"}, /* o */
165 {'Ð', ".--."}, /* pe */
166 {'Ò', ".-."}, /* er */
167 {'Ó', "..."}, /* es */
169 {'Õ', "..-"}, /* u */
170 {'Æ', "..-."}, /* ef */
171 {'È', "...."}, /* kha */
172 {'Ã', "-.-."}, /* ce */
173 {'Þ', "---."}, /* che */
174 {'Û', "----"}, /* sha */
175 {'Ý', "--.-"}, /* shcha */
176 {'Ù', "-.--"}, /* yi */
177 {'Ø', "-..-"}, /* myakhkij znak */
178 {'Ü', "..-.."}, /* ae */
179 {'À', "..--"}, /* yu */
180 {'Ñ', ".-.-"}, /* ya */
190 void alloc_soundbuf(struct tone_data
*, double, int);
191 void morse(char, int);
192 void show(const char *, int);
193 void play(const char *, int);
194 void ttyout(const char *, int);
195 void sighandler(int);
197 #define GETOPTOPTS "d:ef:opP:sw:W:"
199 "usage: morse [-s] [-e] [-p | -o] [-P device] [-d device] [-w speed] [-W speed] [-f frequency] [string ...]\n"
201 static int oflag
, pflag
, sflag
, eflag
;
202 static int wpm
= 20; /* words per minute */
203 static int farnsworth
= -1;
204 #define FREQUENCY 600
205 static int freq
= FREQUENCY
;
206 static char *device
; /* for tty-controlled generator */
208 static struct tone_data tone_dot
, tone_dash
, tone_silence
, tone_letter_silence
;
209 #define DSP_RATE 44100
210 static const char *snddev
= NULL
;
214 #define WORD_SPACE (7 - CHAR_SPACE)
215 static float dot_clock
, word_clock
;
217 struct termios otty
, ntty
;
220 static const struct morsetab
*hightab
;
223 main(int argc
, char **argv
)
229 while ((ch
= getopt(argc
, argv
, GETOPTOPTS
)) != -1)
236 setvbuf(stdout
, 0, _IONBF
, 0);
257 farnsworth
= atoi(optarg
);
261 fputs(USAGE
, stderr
);
264 if (pflag
+ !!device
+ sflag
> 1) {
265 fputs("morse: only one of -o, -p, -d and -s allowed\n", stderr
);
268 if ((pflag
|| device
) && ((wpm
< 1) || (wpm
> 60) || (farnsworth
> 60))) {
269 fputs("morse: insane speed\n", stderr
);
272 if ((pflag
|| device
) && (freq
== 0))
274 if (pflag
|| device
) {
276 * A note on how to get to this magic 1.2:
277 * x WPM = 50*x dits per minute (norm word "PARIS").
278 * dits per sec = dits per minute / 60, thus
279 * dits per sec = 50 * x / 60 = x / (60 / 50) = x / 1.2
281 dot_clock
= wpm
/ 1.2; /* dots/sec */
282 dot_clock
= 1 / dot_clock
; /* duration of a dot */
284 word_clock
= dot_clock
;
287 * This is how to get to this formula:
288 * PARIS = 22 dit (symbols) + 9 symbol spaces = 31 symbol times
291 * The symbol times are in dot_clock, so the spaces have to
292 * make up to reach the farnsworth time.
295 word_clock
= (60.0 / farnsworth
- 31 * dot_clock
) / 19;
297 if (snddev
== NULL
) {
300 else /* only pflag */
305 snd_chan_param param
;
307 if (oflag
&& strcmp(snddev
, "-") == 0)
308 spkr
= STDOUT_FILENO
;
310 spkr
= open(snddev
, O_WRONLY
, 0);
312 err(1, "%s", snddev
);
313 param
.play_rate
= DSP_RATE
;
314 param
.play_format
= AFMT_S16_NE
;
316 param
.rec_format
= 0;
317 if (!oflag
&& ioctl(spkr
, AIOSFMT
, ¶m
) != 0)
318 err(1, "%s: set format", snddev
);
319 alloc_soundbuf(&tone_dot
, dot_clock
, 1);
320 alloc_soundbuf(&tone_dash
, DASH_LEN
* dot_clock
, 1);
321 alloc_soundbuf(&tone_silence
, dot_clock
, 0);
322 alloc_soundbuf(&tone_letter_silence
, word_clock
, 0);
325 if ((line
= open(device
, O_WRONLY
| O_NONBLOCK
)) == -1) {
326 perror("open tty line");
329 if (tcgetattr(line
, &otty
) == -1) {
330 perror("tcgetattr() failed");
334 ntty
.c_cflag
|= CLOCAL
;
335 tcsetattr(line
, TCSANOW
, &ntty
);
336 lflags
= fcntl(line
, F_GETFL
);
337 lflags
&= ~O_NONBLOCK
;
338 fcntl(line
, F_SETFL
, &lflags
);
339 ioctl(line
, TIOCMGET
, &lflags
);
340 lflags
&= ~TIOCM_RTS
;
342 ioctl(line
, TIOCMSET
, &lflags
);
343 (void)signal(SIGHUP
, sighandler
);
344 (void)signal(SIGINT
, sighandler
);
345 (void)signal(SIGQUIT
, sighandler
);
346 (void)signal(SIGTERM
, sighandler
);
352 if (setlocale(LC_CTYPE
, "") != NULL
&&
353 *(codeset
= nl_langinfo(CODESET
)) != '\0') {
354 if (strcmp(codeset
, "KOI8-R") == 0)
356 else if (strcmp(codeset
, "ISO8859-1") == 0 ||
357 strcmp(codeset
, "ISO8859-15") == 0)
358 hightab
= iso8859tab
;
364 for (p
= *argv
; *p
; ++p
) {
367 if (*p
== '<' || *p
== '>') {
371 if (strchr("> \r\n", *(p
+ 1)) != NULL
)
381 while ((ch
= getchar()) != EOF
) {
392 if (strchr("> \r\n", tch
) != NULL
)
401 tcsetattr(line
, TCSANOW
, &otty
);
406 alloc_soundbuf(struct tone_data
*tone
, double len
, int on
)
410 samples
= DSP_RATE
* len
;
411 tone
->len
= samples
* sizeof(*tone
->data
);
412 tone
->data
= malloc(tone
->len
);
413 if (tone
->data
== NULL
)
416 bzero(tone
->data
, tone
->len
);
421 * We create a sinus with the specified frequency and smooth
422 * the edges to reduce key clicks.
424 for (i
= 0; i
< samples
; i
++) {
427 #define FILTER_SAMPLES (DSP_RATE * 8 / 1000) /* 8 ms ramp time */
428 if (i
< FILTER_SAMPLES
|| i
> samples
- FILTER_SAMPLES
) {
431 if (i
> FILTER_SAMPLES
)
433 #if defined(TRIANGLE_FILTER)
437 filter
= (double)fi
/ FILTER_SAMPLES
;
438 #elif defined(GAUSS_FILTER)
443 pow((double)(FILTER_SAMPLES
- fi
) /
449 filter
= (1 + cos(M_PI
* (FILTER_SAMPLES
- fi
) / FILTER_SAMPLES
)) / 2;
452 tone
->data
[i
] = 32767 * sin((double)i
/ samples
* len
* freq
* 2 * M_PI
) *
458 morse(char c
, int prosign
)
460 const struct morsetab
*m
;
462 if (isalpha((unsigned char)c
))
463 c
= tolower((unsigned char)c
);
464 if ((c
== '\r') || (c
== '\n'))
478 for (m
= ((unsigned char)c
< 0x80? mtab
: hightab
);
479 m
!= NULL
&& m
->inchar
!= '\0';
481 if (m
->inchar
== c
) {
483 play(m
->morse
, prosign
);
485 ttyout(m
->morse
, prosign
);
487 show(m
->morse
, prosign
);
493 show(const char *s
, int prosign
)
499 printf(" %s", *s
== '.' ? "dit" : "dah");
505 play(const char *s
, int prosign
)
509 struct tone_data
*tone
;
512 * We don't need to usleep() here, as the sound device blocks.
514 for (c
= s
; *c
!= '\0'; c
++) {
525 duration
= WORD_SPACE
;
526 tone
= &tone_letter_silence
;
529 errx(1, "invalid morse digit");
531 while (duration
-- > 0)
532 write(spkr
, tone
->data
, tone
->len
);
533 /* Only space within a symbol */
534 if (c
[1] != '\0' || prosign
)
535 write(spkr
, tone_silence
.data
, tone_silence
.len
);
539 duration
= CHAR_SPACE
;
540 while (duration
-- > 0)
541 write(spkr
, tone_letter_silence
.data
, tone_letter_silence
.len
);
543 /* Sync out the audio data with other output */
545 ioctl(spkr
, SNDCTL_DSP_SYNC
, NULL
);
549 ttyout(const char *s
, int prosign
)
552 int duration
, on
, lflags
;
554 for (c
= s
; *c
!= '\0'; c
++) {
558 duration
= dot_clock
;
562 duration
= dot_clock
* DASH_LEN
;
566 duration
= word_clock
* WORD_SPACE
;
573 ioctl(line
, TIOCMGET
, &lflags
);
575 ioctl(line
, TIOCMSET
, &lflags
);
580 ioctl(line
, TIOCMGET
, &lflags
);
581 lflags
&= ~TIOCM_RTS
;
582 ioctl(line
, TIOCMSET
, &lflags
);
583 duration
= dot_clock
* 1000000;
584 /* Only space within a symbol */
585 if (c
[1] != '\0' || prosign
)
589 duration
= word_clock
* CHAR_SPACE
* 1000000;
595 sighandler(int signo
)
598 ioctl(line
, TIOCMSET
, &olflags
);
599 tcsetattr(line
, TCSANOW
, &otty
);
601 signal(signo
, SIG_DFL
);
602 (void)kill(getpid(), signo
);