File: morse.c

package info (click to toggle)
brltty 6.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,776 kB
  • sloc: ansic: 150,447; java: 13,484; sh: 9,667; xml: 5,702; tcl: 2,634; makefile: 2,328; awk: 713; lisp: 366; python: 321; ml: 301
file content (317 lines) | stat: -rw-r--r-- 7,473 bytes parent folder | download | duplicates (2)
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
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2025 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

#include "prologue.h"

#include <string.h>

#include "log.h"
#include "morse.h"
#include "tune.h"
#include "utf8.h"

static const MorsePattern morsePatterns[] = {
  [WC_C('a')] = 0B101,
  [WC_C('b')] = 0B11110,
  [WC_C('c')] = 0B11010,
  [WC_C('d')] = 0B1110,
  [WC_C('e')] = 0B11,
  [WC_C('f')] = 0B11011,
  [WC_C('g')] = 0B1100,
  [WC_C('h')] = 0B11111,
  [WC_C('i')] = 0B111,
  [WC_C('j')] = 0B10001,
  [WC_C('k')] = 0B1010,
  [WC_C('l')] = 0B11101,
  [WC_C('m')] = 0B100,
  [WC_C('n')] = 0B110,
  [WC_C('o')] = 0B1000,
  [WC_C('p')] = 0B11001,
  [WC_C('q')] = 0B10100,
  [WC_C('r')] = 0B1101,
  [WC_C('s')] = 0B1111,
  [WC_C('t')] = 0B10,
  [WC_C('u')] = 0B1011,
  [WC_C('v')] = 0B10111,
  [WC_C('w')] = 0B1001,
  [WC_C('x')] = 0B10110,
  [WC_C('y')] = 0B10010,
  [WC_C('z')] = 0B11100,

#ifdef HAVE_WCHAR_H
  [WC_C('ä')] = 0B10101,
  [WC_C('á')] = 0B101001,
  [WC_C('å')] = 0B101001,
  [WC_C('é')] = 0B111011,
  [WC_C('ñ')] = 0B100100,
  [WC_C('ö')] = 0B11000,
  [WC_C('ü')] = 0B10011,
#endif /* HAVE_WCHAR_H */

  [WC_C('0')] = 0B100000,
  [WC_C('1')] = 0B100001,
  [WC_C('2')] = 0B100011,
  [WC_C('3')] = 0B100111,
  [WC_C('4')] = 0B101111,
  [WC_C('5')] = 0B111111,
  [WC_C('6')] = 0B111110,
  [WC_C('7')] = 0B111100,
  [WC_C('8')] = 0B111000,
  [WC_C('9')] = 0B110000,

  [WC_C('.')] = 0B1010101,
  [WC_C(',')] = 0B1001100,
  [WC_C('?')] = 0B1110011,
  [WC_C('!')] = 0B1001010,
  [WC_C(':')] = 0B1111000,
  [WC_C('\'')] = 0B1100001,
  [WC_C('"')] = 0B1101101,
  [WC_C('(')] = 0B110010,
  [WC_C(')')] = 0B1010010,
  [WC_C('=')] = 0B101110,
  [WC_C('+')] = 0B110101,
  [WC_C('-')] = 0B1011110,
  [WC_C('/')] = 0B110110,
  [WC_C('&')] = 0B111101,
  [WC_C('@')] = 0B1101001,

  [0] = 0
};

MorsePattern
getMorsePattern (wchar_t character) {
  character = towlower(character);
  if (character < 0) return 0;
  if (character >= ARRAY_COUNT(morsePatterns)) return 0;
  return morsePatterns[character];
}

struct  MorseObjectStruct {
  struct {
    unsigned int frequency;
    unsigned int unit;
  } parameters;

  struct {
    unsigned wasSpace:1;
  } state;

  struct {
    ToneElement *array;
    size_t size;
    size_t count;
  } elements;
};

static int
addMorseElement (MorseObject *morse, const ToneElement *element) {
  if (morse->elements.count == morse->elements.size) {
    size_t newSize = morse->elements.size? (morse->elements.size << 1): 0X10;
    ToneElement *newArray = realloc(morse->elements.array, (newSize * sizeof(*newArray)));

    if (!newArray) {
      logMallocError();
      return 0;
    }

    morse->elements.array = newArray;
    morse->elements.size = newSize;
  }

  morse->elements.array[morse->elements.count++] = *element;
  return 1;
}

static int
addMorseMark (MorseObject *morse, unsigned int units) {
  ToneElement element = TONE_PLAY((morse->parameters.unit * units), morse->parameters.frequency);
  return addMorseElement(morse, &element);
}

static int
addMorseGap (MorseObject *morse, unsigned int units) {
  ToneElement element = TONE_REST((morse->parameters.unit * units));
  return addMorseElement(morse, &element);
}

int
addMorsePattern (MorseObject *morse, MorsePattern pattern) {
  if (pattern) {
    int addGap = 0;

    while (pattern != 0B1) {
      if (!addGap) {
        addGap = 1;
      } else if (!addMorseGap(morse, MORSE_UNITS_GAP_SYMBOL)) {
        return 0;
      }

      unsigned int units = (pattern & 0B1)? MORSE_UNITS_MARK_SHORT: MORSE_UNITS_MARK_LONG;
      if (!addMorseMark(morse, units)) return 0;
      pattern >>= 1;
    }
  }

  return 1;
}

int
addMorseCharacter (MorseObject *morse, wchar_t character) {
  if (!iswspace(character)) {
    if (morse->state.wasSpace) {
      morse->state.wasSpace = 0;
    } else if (!addMorseGap(morse, MORSE_UNITS_GAP_LETTER)) {
      return 0;
    }

    if (!addMorsePattern(morse, getMorsePattern(character))) return 0;
  } else if (!morse->state.wasSpace) {
    morse->state.wasSpace = 1;
    if (!addMorseGap(morse, MORSE_UNITS_GAP_WORD)) return 0;
  }

  return 1;
}

int
addMorseSpace (MorseObject *morse) {
  return addMorseCharacter(morse, WC_C(' '));
}

int
addMorseCharacters (MorseObject *morse, const wchar_t *characters, size_t count) {
  const wchar_t *character = characters;
  const wchar_t *end = character + count;

  while (character < end) {
    if (!addMorseCharacter(morse, *character++)) return 0;
  }

  return 1;
}

int
addMorseString (MorseObject *morse, const char *string) {
  size_t size = strlen(string) + 1;
  wchar_t characters[size];

  const char *byte = string;
  wchar_t *end = characters;

  convertUtf8ToWchars(&byte, &end, size);
  return addMorseCharacters(morse, characters, (end - characters));
}

int
playMorseSequence (MorseObject *morse) {
  {
    ToneElement element = TONE_STOP();
    if (!addMorseElement(morse, &element)) return 0;
  }

  tunePlayTones(morse->elements.array, 0);
  tuneSynchronize();
  return 1;
}

void
clearMorseSequence (MorseObject *morse) {
  morse->elements.count = 0;
  morse->state.wasSpace = 1;
}

unsigned int
getMorsePitch (MorseObject *morse) {
  return morse->parameters.frequency;
}

int
setMorsePitch (MorseObject *morse, unsigned int frequency) {
  if (frequency < 1) return 0;
  if (frequency > 0XFFFF) return 0;

  morse->parameters.frequency = frequency;
  return 1;
}

static inline unsigned int
getMorseReferenceDuration (unsigned int unitsPerMinute) {
  return 60000 / unitsPerMinute;
}

static unsigned int
getMorseSpeed (MorseObject *morse, unsigned int unitsPerMinute) {
  return getMorseReferenceDuration(unitsPerMinute) / morse->parameters.unit;
}

static int
setMorseSpeed (MorseObject *morse, unsigned int speed, unsigned int unitsPerMinute) {
  unsigned int unitDuration = getMorseReferenceDuration(unitsPerMinute) / speed;
  if (unitDuration < 10) return 0;

  morse->parameters.unit = unitDuration;
  return 1;
}

unsigned int
getMorseWordsPerMinute (MorseObject *morse) {
  return getMorseSpeed(morse, MORSE_UNITS_PER_WORD);
}

int
setMorseWordsPerMinute (MorseObject *morse, unsigned int speed) {
  return setMorseSpeed(morse, speed, MORSE_UNITS_PER_WORD);
}

unsigned int
getMorseGroupsPerMinute (MorseObject *morse) {
  return getMorseSpeed(morse, MORSE_UNITS_PER_GROUP);
}

int
setMorseGroupsPerMinute (MorseObject *morse, unsigned int speed) {
  return setMorseSpeed(morse, speed, MORSE_UNITS_PER_GROUP);
}

void *
newMorseObject (void) {
  MorseObject *morse;

  if ((morse = malloc(sizeof(*morse)))) {
    memset(morse, 0, sizeof(*morse));

    setMorsePitch(morse, 440);
    setMorseWordsPerMinute(morse, 20);

    morse->elements.array = NULL;
    morse->elements.size = 0;

    clearMorseSequence(morse);
    return morse;
  } else {
    logMallocError();
  }

  return NULL;
}

void
destroyMorseObject (MorseObject *morse) {
  if (morse->elements.array) free(morse->elements.array);
  free(morse);
}