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.8 2008/05/30 21:47:04 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
[] = {
114 {'!', "-.-.--"}, /* KW */
118 {'=', "-...-"}, /* BT */
121 {'(', "-.--."}, /* KN */
124 {'+', ".-.-."}, /* AR */
127 {'@', ".--.-."}, /* AC */
133 static const struct morsetab iso8859tab
[] = {
148 static const struct morsetab koi8rtab
[] = {
150 * the cyrillic alphabet; you'll need a KOI8R font in order
151 * to see the actual characters
154 {'Â', "-..."}, /* be */
155 {'×', ".--"}, /* ve */
156 {'Ç', "--."}, /* ge */
157 {'Ä', "-.."}, /* de */
159 {'£', "."}, /* yo, the same as ye */
160 {'Ö', "...-"}, /* she */
161 {'Ú', "--.."}, /* ze */
163 {'Ê', ".---"}, /* i kratkoye */
164 {'Ë', "-.-"}, /* ka */
165 {'Ì', ".-.."}, /* el */
166 {'Í', "--"}, /* em */
167 {'Î', "-."}, /* en */
168 {'Ï', "---"}, /* o */
169 {'Ð', ".--."}, /* pe */
170 {'Ò', ".-."}, /* er */
171 {'Ó', "..."}, /* es */
173 {'Õ', "..-"}, /* u */
174 {'Æ', "..-."}, /* ef */
175 {'È', "...."}, /* kha */
176 {'Ã', "-.-."}, /* ce */
177 {'Þ', "---."}, /* che */
178 {'Û', "----"}, /* sha */
179 {'Ý', "--.-"}, /* shcha */
180 {'Ù', "-.--"}, /* yi */
181 {'Ø', "-..-"}, /* myakhkij znak */
182 {'Ü', "..-.."}, /* ae */
183 {'À', "..--"}, /* yu */
184 {'Ñ', ".-.-"}, /* ya */
194 void alloc_soundbuf(struct tone_data
*, double, int);
195 void morse(char, int);
196 void show(const char *, int);
197 void play(const char *, int);
198 void ttyout(const char *, int);
199 void sighandler(int);
201 #define GETOPTOPTS "d:ef:opP:sw:W:"
203 "usage: morse [-s] [-e] [-p | -o] [-P device] [-d device] [-w speed] [-W speed] [-f frequency] [string ...]\n"
205 static int oflag
, pflag
, sflag
, eflag
;
206 static int wpm
= 20; /* words per minute */
207 static int farnsworth
= -1;
208 #define FREQUENCY 600
209 static int freq
= FREQUENCY
;
210 static char *device
; /* for tty-controlled generator */
212 static struct tone_data tone_dot
, tone_dash
, tone_silence
, tone_letter_silence
;
213 #define DSP_RATE 44100
214 static const char *snddev
= NULL
;
218 #define WORD_SPACE (7 - CHAR_SPACE)
219 static float dot_clock
, word_clock
;
221 struct termios otty
, ntty
;
224 static const struct morsetab
*hightab
;
227 main(int argc
, char **argv
)
233 while ((ch
= getopt(argc
, argv
, GETOPTOPTS
)) != -1)
240 setvbuf(stdout
, 0, _IONBF
, 0);
261 farnsworth
= atoi(optarg
);
265 fputs(USAGE
, stderr
);
268 if (pflag
+ !!device
+ sflag
> 1) {
269 fputs("morse: only one of -o, -p, -d and -s allowed\n", stderr
);
272 if ((pflag
|| device
) && ((wpm
< 1) || (wpm
> 60) || (farnsworth
> 60))) {
273 fputs("morse: insane speed\n", stderr
);
276 if ((pflag
|| device
) && (freq
== 0))
278 if (pflag
|| device
) {
280 * A note on how to get to this magic 1.2:
281 * x WPM = 50*x dits per minute (norm word "PARIS").
282 * dits per sec = dits per minute / 60, thus
283 * dits per sec = 50 * x / 60 = x / (60 / 50) = x / 1.2
285 dot_clock
= wpm
/ 1.2; /* dots/sec */
286 dot_clock
= 1 / dot_clock
; /* duration of a dot */
288 word_clock
= dot_clock
;
291 * This is how to get to this formula:
292 * PARIS = 22 dit (symbols) + 9 symbol spaces = 31 symbol times
295 * The symbol times are in dot_clock, so the spaces have to
296 * make up to reach the farnsworth time.
299 word_clock
= (60.0 / farnsworth
- 31 * dot_clock
) / 19;
301 if (snddev
== NULL
) {
304 else /* only pflag */
309 snd_chan_param param
;
311 if (oflag
&& strcmp(snddev
, "-") == 0)
312 spkr
= STDOUT_FILENO
;
314 spkr
= open(snddev
, O_WRONLY
, 0);
316 err(1, "%s", snddev
);
317 param
.play_rate
= DSP_RATE
;
318 param
.play_format
= AFMT_S16_NE
;
320 param
.rec_format
= 0;
321 if (!oflag
&& ioctl(spkr
, AIOSFMT
, ¶m
) != 0)
322 err(1, "%s: set format", snddev
);
323 alloc_soundbuf(&tone_dot
, dot_clock
, 1);
324 alloc_soundbuf(&tone_dash
, DASH_LEN
* dot_clock
, 1);
325 alloc_soundbuf(&tone_silence
, dot_clock
, 0);
326 alloc_soundbuf(&tone_letter_silence
, word_clock
, 0);
329 if ((line
= open(device
, O_WRONLY
| O_NONBLOCK
)) == -1) {
330 perror("open tty line");
333 if (tcgetattr(line
, &otty
) == -1) {
334 perror("tcgetattr() failed");
338 ntty
.c_cflag
|= CLOCAL
;
339 tcsetattr(line
, TCSANOW
, &ntty
);
340 lflags
= fcntl(line
, F_GETFL
);
341 lflags
&= ~O_NONBLOCK
;
342 fcntl(line
, F_SETFL
, &lflags
);
343 ioctl(line
, TIOCMGET
, &lflags
);
344 lflags
&= ~TIOCM_RTS
;
346 ioctl(line
, TIOCMSET
, &lflags
);
347 (void)signal(SIGHUP
, sighandler
);
348 (void)signal(SIGINT
, sighandler
);
349 (void)signal(SIGQUIT
, sighandler
);
350 (void)signal(SIGTERM
, sighandler
);
356 if (setlocale(LC_CTYPE
, "") != NULL
&&
357 *(codeset
= nl_langinfo(CODESET
)) != '\0') {
358 if (strcmp(codeset
, "KOI8-R") == 0)
360 else if (strcmp(codeset
, "ISO8859-1") == 0 ||
361 strcmp(codeset
, "ISO8859-15") == 0)
362 hightab
= iso8859tab
;
368 for (p
= *argv
; *p
; ++p
) {
371 if (*p
== '<' || *p
== '>') {
375 if (strchr("> \r\n", *(p
+ 1)) != NULL
)
385 while ((ch
= getchar()) != EOF
) {
396 if (strchr("> \r\n", tch
) != NULL
)
405 tcsetattr(line
, TCSANOW
, &otty
);
410 alloc_soundbuf(struct tone_data
*tone
, double len
, int on
)
414 samples
= DSP_RATE
* len
;
415 tone
->len
= samples
* sizeof(*tone
->data
);
416 tone
->data
= malloc(tone
->len
);
417 if (tone
->data
== NULL
)
420 bzero(tone
->data
, tone
->len
);
425 * We create a sinus with the specified frequency and smooth
426 * the edges to reduce key clicks.
428 for (i
= 0; i
< samples
; i
++) {
431 #define FILTER_SAMPLES 100
432 if (i
< FILTER_SAMPLES
|| i
> samples
- FILTER_SAMPLES
) {
439 if (i
> FILTER_SAMPLES
)
442 pow((double)(FILTER_SAMPLES
- fi
) /
448 if (i
< FILTER_SAMPLES
)
449 filter
= (double)i
/ FILTER_SAMPLES
;
451 filter
= (double)(samples
- i
) / FILTER_SAMPLES
;
454 tone
->data
[i
] = 32767 * sin((double)i
/ samples
* len
* freq
* 2 * M_PI
) *
460 morse(char c
, int prosign
)
462 const struct morsetab
*m
;
464 if (isalpha((unsigned char)c
))
465 c
= tolower((unsigned char)c
);
466 if ((c
== '\r') || (c
== '\n'))
480 for (m
= ((unsigned char)c
< 0x80? mtab
: hightab
);
481 m
!= NULL
&& m
->inchar
!= '\0';
483 if (m
->inchar
== c
) {
485 play(m
->morse
, prosign
);
487 ttyout(m
->morse
, prosign
);
489 show(m
->morse
, prosign
);
495 show(const char *s
, int prosign
)
501 printf(" %s", *s
== '.' ? "dit" : "dah");
507 play(const char *s
, int prosign
)
511 struct tone_data
*tone
;
514 * We don't need to usleep() here, as the sound device blocks.
516 for (c
= s
; *c
!= '\0'; c
++) {
527 duration
= WORD_SPACE
;
528 tone
= &tone_letter_silence
;
531 errx(1, "invalid morse digit");
533 while (duration
-- > 0)
534 write(spkr
, tone
->data
, tone
->len
);
535 /* Only space within a symbol */
536 if (c
[1] != '\0' || prosign
)
537 write(spkr
, tone_silence
.data
, tone_silence
.len
);
541 duration
= CHAR_SPACE
;
542 while (duration
-- > 0)
543 write(spkr
, tone_letter_silence
.data
, tone_letter_silence
.len
);
545 /* Sync out the audio data with other output */
547 ioctl(spkr
, SNDCTL_DSP_SYNC
, NULL
);
551 ttyout(const char *s
, int prosign
)
554 int duration
, on
, lflags
;
556 for (c
= s
; *c
!= '\0'; c
++) {
560 duration
= dot_clock
;
564 duration
= dot_clock
* DASH_LEN
;
568 duration
= word_clock
* WORD_SPACE
;
575 ioctl(line
, TIOCMGET
, &lflags
);
577 ioctl(line
, TIOCMSET
, &lflags
);
582 ioctl(line
, TIOCMGET
, &lflags
);
583 lflags
&= ~TIOCM_RTS
;
584 ioctl(line
, TIOCMSET
, &lflags
);
585 duration
= dot_clock
* 1000000;
586 /* Only space within a symbol */
587 if (c
[1] != '\0' || prosign
)
591 duration
= word_clock
* CHAR_SPACE
* 1000000;
597 sighandler(int signo
)
600 ioctl(line
, TIOCMSET
, &olflags
);
601 tcsetattr(line
, TCSANOW
, &otty
);
603 signal(signo
, SIG_DFL
);
604 (void)kill(getpid(), signo
);