5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * silence_gen.c - A silence generator, for inserting timed silences.
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2006 Steve Underwood
|
|
9 *
|
|
10 * All rights reserved.
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License version 2, as
|
|
14 * published by the Free Software Foundation.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24 *
|
|
25 * $Id: silence_gen.c,v 1.6 2006/11/19 14:07:25 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /*! \file */
|
|
29
|
|
30 #ifdef HAVE_CONFIG_H
|
|
31 #include "config.h"
|
|
32 #endif
|
|
33
|
|
34 #include <inttypes.h>
|
|
35 #include <stdlib.h>
|
|
36 #include <stdio.h>
|
|
37 #include <fcntl.h>
|
|
38 #include <string.h>
|
|
39 #if defined(HAVE_TGMATH_H)
|
|
40 #include <tgmath.h>
|
|
41 #endif
|
|
42 #if defined(HAVE_MATH_H)
|
|
43 #include <math.h>
|
|
44 #endif
|
|
45 #include <assert.h>
|
|
46 #include <limits.h>
|
|
47
|
|
48 #include "spandsp/telephony.h"
|
|
49 #include "spandsp/logging.h"
|
|
50 #include "spandsp/silence_gen.h"
|
|
51
|
|
52 int silence_gen(silence_gen_state_t *s, int16_t *amp, int max_len)
|
|
53 {
|
|
54 if (s->remaining_samples != INT_MAX)
|
|
55 {
|
|
56 if (max_len > s->remaining_samples)
|
|
57 max_len = s->remaining_samples;
|
|
58 s->remaining_samples -= max_len;
|
|
59 }
|
|
60 if (INT_MAX - s->total_samples >= max_len)
|
|
61 s->total_samples += max_len;
|
|
62 memset(amp, 0, max_len*sizeof(int16_t));
|
|
63 return max_len;
|
|
64 }
|
|
65 /*- End of function --------------------------------------------------------*/
|
|
66
|
|
67 void silence_gen_always(silence_gen_state_t *s)
|
|
68 {
|
|
69 s->remaining_samples = INT_MAX;
|
|
70 }
|
|
71 /*- End of function --------------------------------------------------------*/
|
|
72
|
|
73 void silence_gen_set(silence_gen_state_t *s, int silent_samples)
|
|
74 {
|
|
75 s->remaining_samples = silent_samples;
|
|
76 s->total_samples = 0;
|
|
77 }
|
|
78 /*- End of function --------------------------------------------------------*/
|
|
79
|
|
80 void silence_gen_alter(silence_gen_state_t *s, int silent_samples)
|
|
81 {
|
|
82 /* Block negative silences */
|
|
83 if (silent_samples < 0)
|
|
84 {
|
|
85 if (-silent_samples > s->remaining_samples)
|
|
86 silent_samples = -s->remaining_samples;
|
|
87 }
|
|
88 s->remaining_samples += silent_samples;
|
|
89 s->total_samples += silent_samples;
|
|
90 }
|
|
91 /*- End of function --------------------------------------------------------*/
|
|
92
|
|
93 int silence_gen_remainder(silence_gen_state_t *s)
|
|
94 {
|
|
95 return s->remaining_samples;
|
|
96 }
|
|
97 /*- End of function --------------------------------------------------------*/
|
|
98
|
|
99 int silence_gen_generated(silence_gen_state_t *s)
|
|
100 {
|
|
101 return s->total_samples;
|
|
102 }
|
|
103 /*- End of function --------------------------------------------------------*/
|
|
104
|
|
105 silence_gen_state_t *silence_gen_init(silence_gen_state_t *s, int silent_samples)
|
|
106 {
|
|
107 memset(s, 0, sizeof(*s));
|
|
108 s->remaining_samples = silent_samples;
|
|
109 return s;
|
|
110 }
|
|
111 /*- End of function --------------------------------------------------------*/
|
|
112 /*- End of file ------------------------------------------------------------*/
|