comparison spandsp-0.0.6pre17/src/swept_tone.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 * swept_tone.c - Swept tone generation
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2009 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: swept_tone.c,v 1.2 2009/09/23 16:02:59 steveu Exp $
26 */
27
28 /*! \file */
29
30 #if defined(HAVE_CONFIG_H)
31 #include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <inttypes.h>
36 #include <string.h>
37 #include <stdio.h>
38 #if defined(HAVE_TGMATH_H)
39 #include <tgmath.h>
40 #endif
41 #if defined(HAVE_MATH_H)
42 #include <math.h>
43 #endif
44 #include "floating_fudge.h"
45
46 #include "spandsp/telephony.h"
47 #include "spandsp/logging.h"
48 #include "spandsp/complex.h"
49 #include "spandsp/vector_float.h"
50 #include "spandsp/complex_vector_float.h"
51 #include "spandsp/vector_int.h"
52 #include "spandsp/complex_vector_int.h"
53 #include "spandsp/dds.h"
54
55 #include "spandsp/swept_tone.h"
56
57 #include "spandsp/private/logging.h"
58 #include "spandsp/private/swept_tone.h"
59
60 SPAN_DECLARE(swept_tone_state_t *) swept_tone_init(swept_tone_state_t *s, float start, float end, float level, int duration, int repeating)
61 {
62 if (s == NULL)
63 {
64 if ((s = (swept_tone_state_t *) malloc(sizeof(*s))) == NULL)
65 return NULL;
66 }
67 memset(s, 0, sizeof(*s));
68 s->current_phase_inc =
69 s->starting_phase_inc = dds_phase_rate(start);
70 s->phase_inc_step = dds_phase_rate((end - start)/(float) duration);
71 s->scale = dds_scaling_dbm0(level);
72 s->duration = duration;
73 s->repeating = repeating;
74 s->pos = 0;
75 s->phase = 0;
76 return s;
77 }
78 /*- End of function --------------------------------------------------------*/
79
80 SPAN_DECLARE(int) swept_tone(swept_tone_state_t *s, int16_t amp[], int max_len)
81 {
82 int i;
83 int len;
84 int chunk_len;
85
86 for (len = 0; len < max_len; )
87 {
88 chunk_len = max_len - len;
89 if (chunk_len > s->duration - s->pos)
90 chunk_len = s->duration - s->pos;
91 for (i = len; i < len + chunk_len; i++)
92 {
93 amp[i] = (dds(&s->phase, s->current_phase_inc)*s->scale) >> 15;
94 s->current_phase_inc += s->phase_inc_step;
95 }
96 len += chunk_len;
97 s->pos += chunk_len;
98 if (s->pos >= s->duration)
99 {
100 if (!s->repeating)
101 break;
102 s->pos = 0;
103 s->current_phase_inc = s->starting_phase_inc;
104 }
105 }
106 return len;
107 }
108 /*- End of function --------------------------------------------------------*/
109
110 SPAN_DECLARE(float) swept_tone_current_frequency(swept_tone_state_t *s)
111 {
112 return dds_frequency(s->current_phase_inc);
113 }
114 /*- End of function --------------------------------------------------------*/
115
116 SPAN_DECLARE(int) swept_tone_release(swept_tone_state_t *s)
117 {
118 return 0;
119 }
120 /*- End of function --------------------------------------------------------*/
121
122 SPAN_DECLARE(int) swept_tone_free(swept_tone_state_t *s)
123 {
124 free(s);
125 return 0;
126 }
127 /*- End of function --------------------------------------------------------*/
128 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.