Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/src/silence_gen.c @ 4:26cd8f1ef0b1
import spandsp-0.0.6pre17
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 15:50:58 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:c6c5a16ce2f2 | 4:26cd8f1ef0b1 |
---|---|
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 Lesser General Public License version 2.1, | |
14 * as 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 Lesser General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU Lesser General Public | |
22 * License 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.23 2009/09/04 14:38:46 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \file */ | |
29 | |
30 #if defined(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 "floating_fudge.h" | |
46 #include <assert.h> | |
47 #include <limits.h> | |
48 | |
49 #include "spandsp/telephony.h" | |
50 #include "spandsp/logging.h" | |
51 #include "spandsp/async.h" | |
52 #include "spandsp/silence_gen.h" | |
53 | |
54 #include "spandsp/private/silence_gen.h" | |
55 | |
56 SPAN_DECLARE_NONSTD(int) silence_gen(silence_gen_state_t *s, int16_t *amp, int max_len) | |
57 { | |
58 if (s->remaining_samples != INT_MAX) | |
59 { | |
60 if (max_len >= s->remaining_samples) | |
61 { | |
62 max_len = s->remaining_samples; | |
63 if (max_len && s->status_handler) | |
64 s->status_handler(s->status_user_data, SIG_STATUS_SHUTDOWN_COMPLETE); | |
65 } | |
66 s->remaining_samples -= max_len; | |
67 } | |
68 if (INT_MAX - s->total_samples >= max_len) | |
69 s->total_samples += max_len; | |
70 memset(amp, 0, max_len*sizeof(int16_t)); | |
71 return max_len; | |
72 } | |
73 /*- End of function --------------------------------------------------------*/ | |
74 | |
75 SPAN_DECLARE(void) silence_gen_always(silence_gen_state_t *s) | |
76 { | |
77 s->remaining_samples = INT_MAX; | |
78 } | |
79 /*- End of function --------------------------------------------------------*/ | |
80 | |
81 SPAN_DECLARE(void) silence_gen_set(silence_gen_state_t *s, int silent_samples) | |
82 { | |
83 s->remaining_samples = silent_samples; | |
84 s->total_samples = 0; | |
85 } | |
86 /*- End of function --------------------------------------------------------*/ | |
87 | |
88 SPAN_DECLARE(void) silence_gen_alter(silence_gen_state_t *s, int silent_samples) | |
89 { | |
90 /* Block negative silences */ | |
91 if (silent_samples < 0) | |
92 { | |
93 if (-silent_samples > s->remaining_samples) | |
94 silent_samples = -s->remaining_samples; | |
95 } | |
96 s->remaining_samples += silent_samples; | |
97 s->total_samples += silent_samples; | |
98 } | |
99 /*- End of function --------------------------------------------------------*/ | |
100 | |
101 SPAN_DECLARE(int) silence_gen_remainder(silence_gen_state_t *s) | |
102 { | |
103 return s->remaining_samples; | |
104 } | |
105 /*- End of function --------------------------------------------------------*/ | |
106 | |
107 SPAN_DECLARE(int) silence_gen_generated(silence_gen_state_t *s) | |
108 { | |
109 return s->total_samples; | |
110 } | |
111 /*- End of function --------------------------------------------------------*/ | |
112 | |
113 SPAN_DECLARE(void) silence_gen_status_handler(silence_gen_state_t *s, modem_tx_status_func_t handler, void *user_data) | |
114 { | |
115 s->status_handler = handler; | |
116 s->status_user_data = user_data; | |
117 } | |
118 /*- End of function --------------------------------------------------------*/ | |
119 | |
120 SPAN_DECLARE(silence_gen_state_t *) silence_gen_init(silence_gen_state_t *s, int silent_samples) | |
121 { | |
122 if (s == NULL) | |
123 { | |
124 if ((s = (silence_gen_state_t *) malloc(sizeof(*s))) == NULL) | |
125 return NULL; | |
126 } | |
127 memset(s, 0, sizeof(*s)); | |
128 s->remaining_samples = silent_samples; | |
129 return s; | |
130 } | |
131 /*- End of function --------------------------------------------------------*/ | |
132 | |
133 SPAN_DECLARE(int) silence_gen_release(silence_gen_state_t *s) | |
134 { | |
135 return 0; | |
136 } | |
137 /*- End of function --------------------------------------------------------*/ | |
138 | |
139 SPAN_DECLARE(int) silence_gen_free(silence_gen_state_t *s) | |
140 { | |
141 if (s) | |
142 free(s); | |
143 return 0; | |
144 } | |
145 /*- End of function --------------------------------------------------------*/ | |
146 | |
147 /* The following dummy routines, to absorb data, don't really have a proper home, | |
148 so they have been put here. */ | |
149 | |
150 SPAN_DECLARE_NONSTD(int) span_dummy_rx(void *user_data, const int16_t amp[], int len) | |
151 { | |
152 return 0; | |
153 } | |
154 /*- End of function --------------------------------------------------------*/ | |
155 | |
156 SPAN_DECLARE(int) span_dummy_mod(void *user_data, int16_t amp[], int len) | |
157 { | |
158 return len; | |
159 } | |
160 /*- End of function --------------------------------------------------------*/ | |
161 | |
162 SPAN_DECLARE_NONSTD(int) span_dummy_rx_fillin(void *user_data, int len) | |
163 { | |
164 return 0; | |
165 } | |
166 /*- End of function --------------------------------------------------------*/ | |
167 /*- End of file ------------------------------------------------------------*/ |