aboutsummaryrefslogtreecommitdiffstats
path: root/src/zeitansage/zeitansage.c
blob: 835e5aff2dec27d2ecc4e07851f859897c45953f (plain)
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
/* Zeitansage processing
 *
 * (C) 2020 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#include "../libsample/sample.h"
#include "../libdebug/debug.h"
#include "../libmobile/call.h"
#include "../libmobile/cause.h"
#include "zeitansage.h"

#define db2level(db)	pow(10, (double)(db) / 20.0)

/* list of calls */
zeit_call_t *zeit_call_list = NULL;

double audio_gain;
double early_audio;

#define	BEEP_TIME	16000	/* adjust distance from beep in samples */

int16_t *bntie_spl;
int bntie_size;
int bntie_time; /* sample index when intro is over */
int16_t *urrr_spl[24];
int urrr_size[24];
int urrr_time; /* sample index when hour is over */
int16_t *minuten_spl[60];
int minuten_size[60];
int minuten_time; /* sample index when minute is over */
int16_t *sekunden_spl[60];
int sekunden_size[60];
int sekunden_time; /* sample index when second is over */
int16_t *tut_spl;
int tut_size;
int tut_time; /* sample index when beep is over */

static const char *call_state_name(enum zeit_call_state state)
{
	static char invalid[16];

	switch (state) {
	case ZEIT_CALL_NULL:
		return "(NULL)";
	case ZEIT_CALL_BEEP:
		return "BEEP";
	case ZEIT_CALL_INTRO:
		return "INTRO";
	case ZEIT_CALL_HOUR:
		return "HOUR";
	case ZEIT_CALL_MINUTE:
		return "MINUTE";
	case ZEIT_CALL_SECOND:
		return "SECOND";
	case ZEIT_CALL_PAUSE:
		return "PAUSE";
	}

	sprintf(invalid, "invalid(%d)", state);
	return invalid;
}

static void zeit_display_status(void)
{
	zeit_call_t *call;

	display_status_start();
	for (call = zeit_call_list; call; call = call->next)
		display_status_subscriber(call->caller_id, call_state_name(call->state));
	display_status_end();
}


static void call_new_state(zeit_call_t *call, enum zeit_call_state new_state)
{
	if (call->state == new_state)
		return;
	PDEBUG(DZEIT, DEBUG_DEBUG, "State change: %s -> %s\n", call_state_name(call->state), call_state_name(new_state));
	call->state = new_state;
	zeit_display_status();
}

/* global init */
int zeit_init(double audio_level_dBm, int alerting)
{
	int i;

	/* the recordings are speech level, so we apply gain as is */
	audio_gain = db2level(audio_level_dBm);

	early_audio = alerting;

	/* get maximum length for each speech segment */
	tut_time = BEEP_TIME;
	bntie_time = bntie_size + tut_time;
	urrr_time = 0;
	for (i = 0; i < 24; i++) {
		if (urrr_size[i] > urrr_time)
			urrr_time = urrr_size[i];
	}
	urrr_time += bntie_time;
	minuten_time = 0;
	for (i = 0; i < 60; i++) {
		if (minuten_size[i] > minuten_time)
			minuten_time = minuten_size[i];
	}
	minuten_time += urrr_time;
	sekunden_time = 0;
	for (i = 0; i < 60; i += 10) {
		if (sekunden_size[i] > sekunden_time)
			sekunden_time = sekunden_size[i];
	}
	sekunden_time += minuten_time;

	PDEBUG(DZEIT, DEBUG_DEBUG, "Total time to play announcement, starting with beep: %.2f seconds\n", (double)sekunden_time / 8000.0);

	return 0;
}

/* global exit */
void zeit_exit(void)
{
}

/* calculate what time to speak */
static void zeit_calc_time(zeit_call_t *call, time_t time_sec)
{
	struct tm *tm;

	/* we speak 10 seconds in advance */
	time_sec += 10;

	tm = localtime(&time_sec);
	call->h = tm->tm_hour;
	call->m = tm->tm_min;
	call->s = tm->tm_sec;

	PDEBUG(DZEIT, DEBUG_INFO, "The time at the next beep is: %d:%02d:%02d\n", call->h, call->m, call->s);
}

static void call_timeout(struct timer *timer);

/* Create call instance */
static zeit_call_t *zeit_call_create(uint32_t callref, const char *id)
{
	zeit_call_t *call, **callp;
	double now, time_offset;
	time_t time_sec;

	PDEBUG(DZEIT, DEBUG_INFO, "Creating call instance to play time for caller '%s'.\n", id);

	/* create */
	call = calloc(1, sizeof(*call));
	if (!call) {
		PDEBUG(DZEIT, DEBUG_ERROR, "No mem!\n");
		abort();
	}

	/* init */
	call->callref = callref;
	strncpy(call->caller_id, id, sizeof(call->caller_id) - 1);
	timer_init(&call->timer, call_timeout, call);
	now = get_time();
	time_offset = fmod(now, 10.0);
	time_sec = (int)floor(now / 10.0) * 10;
	call->spl_time = (int)(time_offset * 8000.0);
	zeit_calc_time(call, time_sec);
	timer_start(&call->timer, 10.0 - time_offset);

	/* link */
	callp = &zeit_call_list;
	while ((*callp))
		callp = &(*callp)->next;
	(*callp) = call;

	return call;
}

/* Destroy call instance */
static void zeit_call_destroy(zeit_call_t *call)
{
	zeit_call_t **callp;

	/* unlink */
	callp = &zeit_call_list;
	while ((*callp) != call)
		callp = &(*callp)->next;
	(*callp) = call->next;

	/* cleanup */
	timer_exit(&call->timer);

	/* destroy */
	free(call);

	/* update display */
	zeit_display_status();
}

/* play samples for one call */
static void call_play(zeit_call_t *call)
{
	int i = 0;
	int16_t chunk[160];
	sample_t spl[160];
	int16_t *play_spl;	/* current sample */
	int play_size;		/* current size of sample*/
	int play_index;		/* current sample index */
	int play_max;		/* total length to plax */
	int spl_time;		/* sample offset from start of 10 minutes */

	spl_time = call->spl_time;

next_sample:
	/* select sample from current sample time stamp */
	if (spl_time < tut_time) {
		play_index = spl_time;
		play_max = tut_time;
		play_size = tut_size;
		play_spl = tut_spl;
		call_new_state(call, ZEIT_CALL_BEEP);
	} else
	if (spl_time < bntie_time) {
		play_index = spl_time - tut_time;
		play_max = bntie_time - tut_time;
		play_size = bntie_size;
		play_spl = bntie_spl;
		call_new_state(call, ZEIT_CALL_INTRO);
	} else
	if (spl_time < urrr_time) {
		play_index = spl_time - bntie_time;
		play_max = urrr_time - bntie_time;
		play_size = urrr_size[call->h];
		play_spl = urrr_spl[call->h];
		call_new_state(call, ZEIT_CALL_HOUR);
	} else
	if (spl_time < minuten_time) {
		play_index = spl_time - urrr_time;
		play_max = minuten_time - urrr_time;
		play_size = minuten_size[call->m];
		play_spl = minuten_spl[call->m];
		call_new_state(call, ZEIT_CALL_MINUTE);
	} else
	if (spl_time < sekunden_time) {
		play_index = spl_time - minuten_time;
		play_max = sekunden_time - minuten_time;
		play_size = sekunden_size[call->s];
		play_spl = sekunden_spl[call->s];
		call_new_state(call, ZEIT_CALL_SECOND);
	} else {
		play_index = 0;
		play_max = 0;
		play_size = 0;
		play_spl = NULL;
		call_new_state(call, ZEIT_CALL_PAUSE);
	}

	while (i < 160) {
		if (!play_size) {
			chunk[i++] = 0.0;
			continue;
		}
		/* go to next sample */
		if (play_index == play_max)
			goto next_sample;
		/* announcement or silence, if finished or not set */
		if (play_index < play_size)
			chunk[i++] = play_spl[play_index];
		else
			chunk[i++] = 0.0;
		play_index++;
		spl_time++;
	}

	call->spl_time = spl_time;

	/* convert to samples, apply gain and send toward fixed network */
	int16_to_samples(spl, chunk, 160);
	for (i = 0; i < 160; i++)
		spl[i] *= audio_gain;
	call_up_audio(call->callref, spl, 160);
}

/* loop through all calls and play the announcement */
void call_down_clock(void)
{
	zeit_call_t *call;

	for (call = zeit_call_list; call; call = call->next) {
		/* no callref */
		if (!call->callref)
			continue;
		/* beep or announcement */
		call_play(call);
	}
}

/* Timeout handling */
static void call_timeout(struct timer *timer)
{
	zeit_call_t *call = (zeit_call_t *)timer->priv;
	double now, time_offset;
	time_t time_sec;

	PDEBUG(DZEIT, DEBUG_INFO, "Beep!\n");

	now = get_time();

	time_offset = fmod(now, 10.0);
	time_sec = (int)floor(now / 10.0) * 10;
	/* if somehow the timer fires (a tiny bit) before the 10 seconds start */
	if (time_offset > 5.0) {
		time_offset -= 10.0;
		time_sec += 10;
	}
	call->spl_time = 0;
	zeit_calc_time(call, time_sec);
	timer_start(&call->timer, 10.0 - time_offset);
}

/* Call control starts call towards clock */
int call_down_setup(int callref, const char __attribute__((unused)) *caller_id, enum number_type __attribute__((unused)) caller_type, const char *dialing)
{
	zeit_call_t __attribute__((unused)) *call;

	/* create call process to page station or send out-of-order message */
	call = zeit_call_create(callref, caller_id);
	if (early_audio) {
		call_up_alerting(callref);
		call_up_early(callref);
	} else
		call_up_answer(callref, dialing);

	return 0;
}

void call_down_answer(int __attribute__((unused)) callref)
{
}

static void _release(int callref, int __attribute__((unused)) cause)
{
	zeit_call_t *call;

	PDEBUG(DZEIT, DEBUG_INFO, "Call has been disconnected by network.\n");

	for (call = zeit_call_list; call; call = call->next) {
		if (call->callref == callref)
			break;
	}
	if (!call) {
		PDEBUG(DZEIT, DEBUG_NOTICE, "Outgoing disconnect, but no callref!\n");
		call_up_release(callref, CAUSE_INVALCALLREF);
		return;
	}

	zeit_call_destroy(call);
}

/* Call control sends disconnect.
 * A queued ID will be kept until transmitted by mobile station.
 */
void call_down_disconnect(int callref, int cause)
{
	_release(callref, cause);

	call_up_release(callref, cause);
}

/* Call control releases call toward mobile station. */
void call_down_release(int callref, int cause)
{
	_release(callref, cause);
}

/* Receive audio from call instance. */
void call_down_audio(int __attribute__((unused)) callref, sample_t __attribute__((unused)) *samples, int __attribute__((unused)) count)
{
}

void dump_info(void) {}

void sender_receive(sender_t __attribute__((unused)) *sender, sample_t __attribute__((unused)) *samples, int __attribute__((unused)) length, double __attribute__((unused)) rf_level_db) {}
void sender_send(sender_t __attribute__((unused)) *sender, sample_t __attribute__((unused)) *samples, uint8_t __attribute__((unused)) *power, int __attribute__((unused)) length) {}