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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* Purpose: Another morse code program that uses select
* Copyright (C) 4Front Technologies, 2002-2004. Released under GPLv2/CDDL.
*
* Description:
* This program is a significantly more complicated version of
* {!nlink morse.c}. It uses the {!nlink select} system call to be able to
* handle keyboard input and audio output at the same time. It shows how
* to prevent output underruns by writing silent samples to the audio device
* when there is no "payload" signal to play.
*
* What the program does is playing randomly selected morse symbols
* (based on the command line options). It then waits until the
* users hits a key. If the input character matches the morse output then
* the program continues by playing another morse character. If there was an
* error then the next character is repeated. The Esc key is used to stop
* the program.
*
* Parts on this program are common with {!nlink morse.c} and commented only
* there.
*
* The {!nlink morse3.c} program is a slightly different version of this one.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#include <math.h>
#include <termios.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#define BUFSZ (16*1024)
#define SRATE 48000
#define ATTACK 100
#define CHARDELAY 3
char randomlist[65536];
int nrandom = 0;
int done = 0;
int chars_to_play = 100;
int dotsize;
int audiofd = -1;
static int ncodes;
static int playc;
static int terminal_fd = 0;
static struct termios ti, saved_ti;
static int totalchars = 0, totalerrors = 0, errors = 0;
static time_t t0;
double a, step;
#include "charlist.h"
static void
terminate (int sig)
{
time_t t;
t = time (0) - t0;
if (terminal_fd == -1)
return;
if (tcsetattr (terminal_fd, TCSAFLUSH, &saved_ti) == -1)
{
perror ("tcgetattr");
exit (-1);
}
printf ("\n\nTotal characters: %d\n", totalchars);
printf ("Errors: %d\n", totalerrors);
printf ("Elapsed time: %d seconds\n", t);
if (errors == 0) totalchars--;
if (totalchars <= totalerrors) totalchars = 0;
else
{
totalchars -= totalerrors;
totalchars += totalchars / 5;
}
printf ("Characters per minute: %g\n",
t > 0?(float) totalchars / (float) t * 60.0:0);
exit (sig);
}
static int
genpulse (short *buf, int w, int state)
{
int i, l;
a = 0.0;
l = w * dotsize;
for (i = 0; i < ATTACK; i++)
{
double tmp = 0x7fff * cos (a * M_PI / 180.0);
tmp *= (double) (i) / (double) ATTACK;
*buf++ = (int) tmp *state;
a += step;
if (a > 360.0)
a -= 360.0;
}
for (i = ATTACK; i < l - ATTACK; i++)
{
double tmp = 0x7fff * cos (a * M_PI / 180.0);
*buf++ = (int) tmp *state;
a += step;
if (a > 360.0)
a -= 360.0;
}
for (i = l - ATTACK; i < l; i++)
{
double tmp = 0x7fff * cos (a * M_PI / 180.0);
tmp *= (double) (l - i) / (double) ATTACK;
*buf++ = (int) tmp *state;
a += step;
if (a > 360.0)
a -= 360.0;
}
return l;
}
static void
playerror (void)
{
short buffer[65536], *buf = buffer;
int i, l;
a = 0.0;
l = dotsize;
for (i = 0; i < ATTACK; i++)
{
double tmp = 0x7fff * cos (a * M_PI / 180.0);
tmp *= (double) (i) / (double) ATTACK;
*buf++ = (int) tmp;
a += step * 2.0;
if (a > 360.0)
a -= 360.0;
}
for (i = ATTACK; i < l - ATTACK; i++)
{
double tmp = 0x7fff * cos (a * M_PI / 180.0);
*buf++ = (int) tmp;
a += step * 2.0;
if (a > 360.0)
a -= 360.0;
}
for (i = l - ATTACK; i < l; i++)
{
double tmp = 0x7fff * cos (a * M_PI / 180.0);
tmp *= (double) (l - i) / (double) ATTACK;
*buf++ = (int) tmp;
a += step * 2.0;
if (a > 360.0)
a -= 360.0;
}
write (audiofd, buffer, 2 * l);
}
static int
genmorse (short *buf, char c)
{
int l = 0, i;
const char *s;
if (c == ' ')
return genpulse (buf, 4, 0);
for (i = 0; i < ncodes; i++)
if (Chars[i] == c)
{
s = Codes[i];
while (*s)
{
if (*s++ == '.')
l += genpulse (&buf[l], 1, 1);
else
l += genpulse (&buf[l], 3, 1);
l += genpulse (&buf[l], 1, 0);
}
l += genpulse (&buf[l], CHARDELAY, 0);
return l;
}
printf ("<?>");
fflush (stdout);
return 0;
}
static void
playchar (char c)
{
short buf[16 * BUFSZ];
int l;
l = 0;
if (c <= 'Z' && c >= 'A')
c += 32;
switch (c)
{
case ' ':
case '\n':
case '\t':
l = genmorse (buf, ' ');
break;
case '\r':
break;
case 'Å':
l = genmorse (buf, 'å');
break;
case 'Ä':
l = genmorse (buf, 'ä');
break;
case 'Ö':
l = genmorse (buf, 'ö');
break;
case 'Ü':
l = genmorse (buf, 'ü');
break;
default:
l = genmorse (buf, c);
}
/*
* Next write whatever we have in the buffer. Note that this write will
* block but that time is at most the time required to play one morse
* code character. There is no need to avoid blocking because it doesn't cause
* any annoying delays.
*
* This is often the case with audio application. While many programmers think
* blocking is evil it's actually programmer's best friend.
*/
if (write (audiofd, buf, 2 * l) != 2 * l)
{
perror ("write audio");
terminate (15);
}
}
/*
* The randomplay() routine picks a character from the list of characters
* selected for practice.
*/
static void
randomplay (void)
{
int old = playc;
if (totalchars == chars_to_play)
{
done = 1;
return;
}
// while (playc==old)
{
int i, x, tmp;
x = random () % nrandom;
playc = randomlist[x];
#if 1
if (x != nrandom - 1)
{
tmp = randomlist[x];
for (i = x; i < nrandom - 1; i++)
randomlist[i] = randomlist[i + 1];
randomlist[nrandom - 1] = tmp;
}
#endif
}
playchar (playc);
totalchars++;
}
static int
findcode (char c)
{
int i;
for (i = 0; i < ncodes; i++)
if (Chars[i] == c)
return i;
return 0;
}
int
main (int argc, char *argv[])
{
char *devname = "/dev/dsp";
short buf[32 * BUFSZ];
char line[1024];
int i, parm;
int l, speed, wpm = 12;
fd_set readfds, writefds;
if (argc > 1)
devname = argv[1];
if (argc > 2)
{
wpm = atoi (argv[2]);
if (wpm == 0)
wpm = 12;
}
ncodes = strlen (Chars);
srandom (time (0));
if (argc > 3)
{
for (i = 3; i < argc; i++)
parse_charlist (argv[i]);
}
else
{
strcpy (randomlist, Chars);
nrandom = strlen (randomlist);
}
if (nrandom < 2)
{
printf ("Bad character list\n");
exit (-1);
}
randomlist[nrandom] = 0;
printf ("Practicing codes: %s\n", randomlist);
for (i = 0; i <= nrandom; i += 4)
{
int j, k;
char line[256], tmp[20];
memset (line, ' ', 80), line[78] = 0;
for (j = 0; j < 4; j++)
if (i + j <= nrandom)
{
int ix;
ix = findcode (randomlist[i + j]);
sprintf (tmp, "%c %s", randomlist[i + j], Codes[ix]);
for (k = 0; k < strlen (tmp); k++)
line[j * 20 + k] = tmp[k];
}
printf ("%s\n", line);
}
speed = wpm;
printf ("Words per minute %d. Characters per minute %d\n", wpm, wpm * 5);
dotsize = SRATE / speed;
if ((audiofd = open (devname, O_WRONLY, 0)) == -1)
{
perror (devname);
exit (-1);
}
parm = 0x0003000a;
ioctl (audiofd, SNDCTL_DSP_SETFRAGMENT, &parm);
parm = AFMT_S16_LE;
if (ioctl (audiofd, SNDCTL_DSP_SETFMT, &parm) == -1)
{
perror ("SETFMT");
close (audiofd);
exit (-1);
}
if (parm != AFMT_S16_LE)
{
printf
("Error: 32/24 bit sample format is not supported by the device\n");
printf ("%08x/%08x\n", parm, AFMT_S16_LE);
close (audiofd);
exit (-1);
}
parm = SRATE;
if (ioctl (audiofd, SNDCTL_DSP_SPEED, &parm) == -1)
{
perror ("SPEED");
close (audiofd);
exit (-1);
}
if (parm != SRATE)
{
printf
("Error: %d Hz sampling rate is not supported by the device (%d)\n",
SRATE, parm);
close (audiofd);
exit (-1);
}
if (tcgetattr (terminal_fd, &saved_ti) == -1)
{
perror ("tcgetattr");
exit (-1);
}
signal (SIGINT, terminate);
/*
* Set up the terminal (stdin) for single character input.
*/
if (tcgetattr (terminal_fd, &ti) == -1)
{
perror ("tcgetattr");
exit (-1);
}
ti.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
ti.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
ti.c_cflag &= ~(CSIZE | PARENB);
ti.c_cflag |= CS8;
ti.c_oflag &= ~(OPOST);
ti.c_cc[VMIN] = 1;
ti.c_cc[VTIME] = 1;
if (tcsetattr (terminal_fd, TCSAFLUSH, &ti) == -1)
{
perror ("tcgetattr");
exit (-1);
}
a = 0.0;
/*
* Play some "extra" audio data to delay the startup. This just gives
* some extra time to the user to prepare before we start.
*/
step = 360.0 * 600.0 / parm;
l = 0;
l += genpulse (&buf[l], 1, 0);
write (audiofd, buf, l * 2);
memset (buf, 0, 4096);
for (i = 0; i < 2; i++)
write (audiofd, buf, 4096);
/*
* The actual playback starts here
*/
randomplay ();
t0 = time (0);
while (!done)
{
int n;
/*
* Set up select for output events on the audio device and input events on
* the keyboard.
*/
FD_ZERO (&readfds);
FD_ZERO (&writefds);
FD_SET (audiofd, &writefds);
FD_SET (0, &readfds);
/*
* Call select with no timeouts
*/
if ((n = select (audiofd + 1, &readfds, &writefds, NULL, NULL)) == -1)
{
perror ("select");
exit (-1);
}
if (n == 0)
continue;
if (FD_ISSET (0, &readfds)) /* 0 means stdin */
{
/*
* Handling of keyboard input. Check if the answer was right.
*/
if (read (0, line, 1) == 1)
{
if (*line == 27) /* ESC */
terminate (SIGINT);
if ((unsigned char) *line != (unsigned char) playc)
{
int x;
totalerrors++;
chars_to_play += 4;
randomlist[nrandom++] = playc;
randomlist[nrandom++] = playc;
for (x = 0; x < nrandom; x++)
if (randomlist[x] == *line)
{
randomlist[nrandom++] = *line;
break;
}
playerror ();
if (++errors > 3)
{
printf ("It is '%c' not '%c'\r\n", playc, *line);
fflush (stdout);
}
playchar (playc);
}
else
{
errors = 0;
randomplay ();
}
}
}
if (FD_ISSET (audiofd, &writefds))
{
/*
* The audio device is ready to accept more data. Keep the device happy by
* writing some silent samples to it.
*
* Note that the real "playload" signal will be played by the playchar()
* routine.
*/
memset (buf, 0, 1024);
write (audiofd, buf, 1024);
}
}
/*
* Everything done. Restore the teminal settings and exit.
*/
terminate (15);
close (audiofd);
exit (0);
}
|