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
|
/*
BSD Zero Clause License
Copyright (c) 2023 Samuel Wirajaya
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
================================================================================
C W S N D
================================================================================
*/
#include <ctype.h> /* toupper */
#include <limits.h> /* strtoul */
#include <math.h> /* sin */
#include <stdint.h> /* int16_t */
#include <sndio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define VERSION "0.9.0"
typedef int16_t sample_t;
#define SAMPLE_MAX INT16_MAX
struct sio_hdl *hdl;
struct sio_par par;
/* options */
char *progname;
char *suffix;
int eflg;
int pflg;
int tonefrq;
int effwpm;
int wpm;
/* audio data buffer layout:
[~-~~~-------]
^dot ^space
^dash
*/
sample_t *buf;
size_t dotsz;
size_t cspsz;
size_t wspsz;
sample_t *buf_dot;
sample_t *buf_dash;
sample_t *buf_space;
/* buffer for stuff */
#define TXBUFSZ 1024
char txbuf[TXBUFSZ];
void
calcsz(void) {
/* To calculate size (in # of elements) of the dot sound.
"PARIS " -> 50 dot-durations
[. --- --- . . --- . --- . . . . . . ]
12345678901234567890123456789012345678901234567890
dot duration in seconds:
dotdur = (60 s/min) / (50 dots/word * wpm in words/min)
Let s = rate * pchan
dot size in # of elements:
dotsz = s * dotdur
= s * 6 / (5 * wpm)
*/
size_t s = par.rate * par.pchan;
dotsz = (s * 6) / (5 * wpm);
/* To calculate size (# of elements) of the spaces.
"$PARIS" as "prosign" without space -> 35 dot-durations
[. --- --- . . --- . --- . . . . . .]
12345678901234567890123456789012345
50 - 35 = 15
-> 15 dot-duration units (spdotdur) that can be stretched to
match Farnsworth speed
The string "PARIS PARIS PARIS ..." (repeated effwpm times
with trailing space) should take 60 seconds:
effwpm * (15 * spdotdur + 35 * dotdur) == 60 seconds
spacing-dot duration solved from above eq:
(60 / effwpm) - (35 * dotdur)
spdotdur = -----------------------------
15
spacing-dot size from spdotdur and simplifying:
spdotsz = s * spdotdur
(s * 12 / effwpm) - (7 * dotsz)
= -------------------------------
3
character space size cspsz = spdotsz * 2
word space size wspsz = spdotsz * 4
*/
cspsz = (
(s * 24 / effwpm) - (14 * dotsz)
/*--------------------------------*/
) / 3;
wspsz = (
(s * 48 / effwpm) - (28 * dotsz)
/*--------------------------------*/
) / 3;
}
void
mksin(sample_t *ptr, size_t n) {
unsigned int i = 0, j;
double fi = 0., val;
/* TODO try windowing */
while (1) {
val = sin(6.283185307 * tonefrq * (fi / par.rate));
for (j = 0; j < par.pchan; ++j) {
ptr[i] = (sample_t)round(SAMPLE_MAX * val);
if (++i == n) {
return;
}
}
fi += 1.;
}
}
int
mkbuf(void) {
/* audio data buffer layout:
[~-~~~-------]
^dot ^space
^dash
*/
buf = (sample_t *)calloc(dotsz * (2 + 4) + wspsz, sizeof(sample_t));
if (!buf) {
perror("can't allocate buffer");
return 0;
}
mksin(buf_dot = buf, dotsz);
mksin(buf_dash = &buf[dotsz * 2], dotsz * 3);
buf_space = &buf[dotsz * 6];
return 1;
}
size_t
dot(void) {
return sio_write(hdl, buf_dot,
/* tone blank */
sizeof(sample_t) * dotsz * 2);
}
size_t
dash(void) {
return sio_write(hdl, buf_dash,
/* tone tone tone blank */
sizeof(sample_t) * dotsz * 4);
}
size_t
csp(void) {
return sio_write(hdl, buf_space, sizeof(sample_t) * cspsz);
}
size_t
wsp(void) {
return sio_write(hdl, buf_space, sizeof(sample_t) * wspsz);
}
void
usage(void) {
printf(
"CW Sounder for OpenBSD (version " VERSION ")\n"
"usage: %s [-EPh] [-f EFFWPM] [-w WPM] [-s SUFFIX] [-t TONEFRQ] "
"[file]\n"
" -E: echo mode\n"
" -P: prompt mode\n"
" -h: displays this message\n"
" -f: sets the Farnsworth (effective) speed in words per minute\n"
" -w: sets the speed in words (=\"PARIS \") per minute [%d]\n"
" -t: sets the output tone frequency in cycles per second [%d]\n"
, progname, wpm, tonefrq);
}
/* ----------------------------------------------------------------------------
* T H E M A I N F U N C T I O N
* ----------------------------------------------------------------------------
*/
int
main(int argc, char **argv) {
FILE *f;
char *p;
int opt, noprint, prosgn, contd;
int retval = 1;
#define _CLEAN_EXIT(label, exitcode) retval=(exitcode);goto label;
/* desired sndio params */
struct sio_par req = {
.bits = 16,
.bps = 2,
.le = SIO_LE_NATIVE,
.sig = 1,
.pchan = 1,
.rate = 44100,
};
/* parse options */
progname = argv[0];
suffix = NULL;
eflg = 0;
pflg = 0;
tonefrq = 600;
effwpm = -1;
wpm = 20;
f = stdin;
while ((opt = getopt(argc, argv, "EPf:hs:t:w:")) != -1) {
switch (opt) {
case 'E':
eflg = 1;
break;
case 'P':
pflg = 1;
eflg = 1;
break;
case 'h':
usage();
_CLEAN_EXIT(end, 0);
case 'f':
effwpm = (int)strtoul(optarg, NULL, 10);
break;
case 's':
suffix = optarg;
break;
case 't':
tonefrq = (int)strtoul(optarg, NULL, 10);
break;
case 'w':
wpm = (int)strtoul(optarg, NULL, 10);
break;
case '?':
default:
usage();
_CLEAN_EXIT(end, 1);
}
}
argc -= optind;
argv += optind;
if (argc == 1) {
f = fopen(argv[0], "r");
} else if (argc > 1) {
usage();
_CLEAN_EXIT(end, 1);
}
/* validate options */
if (!f) {
perror("error opening file");
_CLEAN_EXIT(end, 1);
}
if ((tonefrq < 20) || (tonefrq > 22000)) {
fprintf(stderr, "error: tonefrq out of range (20--22000)\n");
usage();
_CLEAN_EXIT(after_fopen, 1);
}
if ((wpm < 5) || (wpm > tonefrq / 4)) {
fprintf(stderr, "error: wpm out of range (5--%d)\n", tonefrq / 4);
usage();
_CLEAN_EXIT(after_fopen, 1);
}
if (effwpm == -1) {
effwpm = wpm;
}
if ((effwpm < 1) || (effwpm > wpm)) {
fprintf(stderr, "error: Farnsworth speed out of range (1--%d)\n", wpm);
usage();
_CLEAN_EXIT(after_fopen, 1);
}
/* get sndio handle for audio, with desired params if possible */
hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
if (!hdl) {
fprintf(stderr, "sio_open\n");
_CLEAN_EXIT(after_fopen, 1);
}
sio_initpar(&par);
par.bits = req.bits;
par.bps = req.bps;
par.le = req.le;
par.sig = req.sig;
par.pchan = req.pchan;
par.rate = req.rate;
if (!sio_getpar(hdl, &par)) {
fprintf(stderr, "failed to get sio parameters\n");
_CLEAN_EXIT(after_sio_open, 1);
}
#define _CHECK_PAR(p) \
if (par.p != req.p) { \
fprintf(stderr, \
"sio_getpar unexpected " #p " (requested %u, got %u)\n", \
req.p, par.p); \
_CLEAN_EXIT(after_sio_open, 1); \
}
_CHECK_PAR(bits) _CHECK_PAR(bps) _CHECK_PAR(le) _CHECK_PAR(sig)
/* calculate size and prepare audio data buffer */
calcsz();
if (!mkbuf()) {
_CLEAN_EXIT(after_sio_open, 1);
}
#define _PLAYBACK_ERROR(fmt, ...) \
{ \
fprintf(stderr, "error playing back: " fmt "\n" __VA_ARGS__); \
_CLEAN_EXIT(after_mkbuf, 1); \
}
#define _DOT if (!dot()) _PLAYBACK_ERROR("sio_write dot");
#define _DASH if (!dash()) _PLAYBACK_ERROR("sio_write dash");
#define _CSP if (!csp()) _PLAYBACK_ERROR("sio_write csp");
#define _WSP if (!wsp()) _PLAYBACK_ERROR("sio_write wsp");
if (!sio_start(hdl))
_PLAYBACK_ERROR("sio_start");
/* main loop */
contd = 1;
while (contd) {
/* prompt */
if (pflg) {
printf("* ");
}
if (!fgets(txbuf, TXBUFSZ, f)) {
/* ending, send space then suffix (if any) */
if (suffix) {
contd = 0;
_CSP;
_WSP;
p = suffix;
} else {
break;
}
} else {
/* business as usual */
p = txbuf;
}
/* not the most code-efficient way to deal with Morse
encoding, but it works. */
prosgn = 0;
for (; *p; ++p) {
*p = toupper(*p);
noprint = 0;
switch (*p) {
#define _BRK if (!prosgn) { _CSP } break;
case 'A': _DOT _DASH _BRK
case 'B': _DASH _DOT _DOT _DOT _BRK
case 'C': _DASH _DOT _DASH _DOT _BRK
case 'D': _DASH _DOT _DOT _BRK
case 'E': _DOT _BRK
case 'F': _DOT _DOT _DASH _DOT _BRK
case 'G': _DASH _DASH _DOT _BRK
case 'H': _DOT _DOT _DOT _DOT _BRK
case 'I': _DOT _DOT _BRK
case 'J': _DOT _DASH _DASH _DASH _BRK
case 'K': _DASH _DOT _DASH _BRK
case 'L': _DOT _DASH _DOT _DOT _BRK
case 'M': _DASH _DASH _BRK
case 'N': _DASH _DOT _BRK
case 'O': _DASH _DASH _DASH _BRK
case 'P': _DOT _DASH _DASH _DOT _BRK
case 'Q': _DASH _DASH _DOT _DASH _BRK
case 'R': _DOT _DASH _DOT _BRK
case 'S': _DOT _DOT _DOT _BRK
case 'T': _DASH _BRK
case 'U': _DOT _DOT _DASH _BRK
case 'V': _DOT _DOT _DOT _DASH _BRK
case 'W': _DOT _DASH _DASH _BRK
case 'X': _DASH _DOT _DOT _DASH _BRK
case 'Y': _DASH _DOT _DASH _DASH _BRK
case 'Z': _DASH _DASH _DOT _DOT _BRK
case '1': _DOT _DASH _DASH _DASH _DASH _BRK
case '2': _DOT _DOT _DASH _DASH _DASH _BRK
case '3': _DOT _DOT _DOT _DASH _DASH _BRK
case '4': _DOT _DOT _DOT _DOT _DASH _BRK
case '5': _DOT _DOT _DOT _DOT _DOT _BRK
case '6': _DASH _DOT _DOT _DOT _DOT _BRK
case '7': _DASH _DASH _DOT _DOT _DOT _BRK
case '8': _DASH _DASH _DASH _DOT _DOT _BRK
case '9': _DASH _DASH _DASH _DASH _DOT _BRK
case '0': _DASH _DASH _DASH _DASH _DASH _BRK
case '?':
prosgn = 0; /* punctuation marks break prosigns */
_DOT _DOT _DASH _DASH _DOT _DOT _BRK
case '=':
prosgn = 0;
_DASH _DOT _DOT _DOT _DASH _BRK
case '/':
prosgn = 0;
_DASH _DOT _DOT _DASH _DOT _BRK
case ',':
prosgn = 0;
_DASH _DASH _DOT _DOT _DASH _DASH _BRK
case '.':
prosgn = 0;
_DOT _DASH _DOT _DASH _DOT _DASH _BRK
case '$':
prosgn = 1;
break;
case ' ':
case '\n':
prosgn = 0;
_WSP;
break;
default:
noprint = 1;
}
/* echo */
if (eflg && !noprint) {
fputc(*p, stdout);
fflush(stdout);
}
}
}
if (!sio_stop(hdl))
_PLAYBACK_ERROR("sio_stop");
printf("\n");
sleep(1);
retval = 0;
after_mkbuf:
free(buf);
after_sio_open:
sio_close(hdl);
after_fopen:
if (f && f != stdin) {
fclose(f);
}
end:
return retval;
}
|