This source file includes following definitions.
- teletone_dds_phase_rate
- teletone_dds_state_modulate_sample
- teletone_dds_state_set_tx_level
- teletone_dds_state_reset_accum
- teletone_dds_state_set_tone
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 #ifndef LIBTELETONE_GENERATE_H
37 #define LIBTELETONE_GENERATE_H
38 #ifdef __cplusplus
39 extern "C" {
40 #ifdef _doh
41 }
42 #endif
43 #endif
44
45 #include <stdio.h>
46 #include <stdlib.h>
47
48 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
49 #ifndef __inline__
50 #define __inline__ inline
51 #endif
52 #endif
53
54 #ifdef _MSC_VER
55 #ifndef __inline__
56 #define __inline__ __inline
57 #endif
58
59 typedef unsigned __int64 uint64_t;
60 typedef unsigned __int32 uint32_t;
61 typedef unsigned __int16 uint16_t;
62 typedef unsigned __int8 uint8_t;
63 typedef __int64 int64_t;
64 typedef __int32 int32_t;
65 typedef __int16 int16_t;
66 typedef __int8 int8_t;
67 #else
68 #include <stdint.h>
69 #endif
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 #include <fcntl.h>
73 #include <math.h>
74 #if !defined(powf) && !defined(_WIN64)
75 extern float powf (float, float);
76 #endif
77 #include <string.h>
78 #include <errno.h>
79 #ifndef _MSC_VER
80 #include <unistd.h>
81 #endif
82 #include <assert.h>
83 #include <stdarg.h>
84 #include "libteletone.h"
85
86 #define TELETONE_VOL_DB_MAX 0
87 #define TELETONE_VOL_DB_MIN -63
88 #define MAX_PHASE_TONES 4
89
90 struct teletone_dds_state {
91 uint32_t phase_rate[MAX_PHASE_TONES];
92 uint32_t scale_factor;
93 uint32_t phase_accumulator;
94 teletone_process_t tx_level;
95 };
96 typedef struct teletone_dds_state teletone_dds_state_t;
97
98 #define SINE_TABLE_MAX 128
99 #define SINE_TABLE_LEN (SINE_TABLE_MAX - 1)
100 #define MAX_PHASE_ACCUMULATOR 0x10000 * 0x10000
101
102
103 #define DBM0_MAX_POWER (3.14f + 3.02f)
104
105 TELETONE_API_DATA extern int16_t TELETONE_SINES[SINE_TABLE_MAX];
106
107 static __inline__ int32_t teletone_dds_phase_rate(teletone_process_t tone, uint32_t rate)
108 {
109 return (int32_t) ((tone * MAX_PHASE_ACCUMULATOR) / rate);
110 }
111
112 static __inline__ int16_t teletone_dds_state_modulate_sample(teletone_dds_state_t *dds, uint32_t pindex)
113 {
114 int32_t bitmask = dds->phase_accumulator, sine_index = (bitmask >>= 23) & SINE_TABLE_LEN;
115 int16_t sample;
116
117 if (pindex >= MAX_PHASE_TONES) {
118 pindex = 0;
119 }
120
121 if (bitmask & SINE_TABLE_MAX) {
122 sine_index = SINE_TABLE_LEN - sine_index;
123 }
124
125 sample = TELETONE_SINES[sine_index];
126
127 if (bitmask & (SINE_TABLE_MAX * 2)) {
128 sample *= -1;
129 }
130
131 dds->phase_accumulator += dds->phase_rate[pindex];
132 return (int16_t) (sample * dds->scale_factor >> 15);
133 }
134
135 static __inline__ void teletone_dds_state_set_tx_level(teletone_dds_state_t *dds, float tx_level)
136 {
137 dds->scale_factor = (int) (powf(10.0f, (tx_level - DBM0_MAX_POWER) / 20.0f) * (32767.0f * 1.414214f));
138 dds->tx_level = tx_level;
139 }
140
141 static __inline__ void teletone_dds_state_reset_accum(teletone_dds_state_t *dds)
142 {
143 dds->phase_accumulator = 0;
144 }
145
146 static __inline__ int teletone_dds_state_set_tone(teletone_dds_state_t *dds, teletone_process_t tone, uint32_t rate, uint32_t pindex)
147 {
148 if (pindex < MAX_PHASE_TONES) {
149 dds->phase_rate[pindex] = teletone_dds_phase_rate(tone, rate);
150 return 0;
151 }
152
153 return -1;
154 }
155
156
157
158
159
160
161
162
163
164 typedef int16_t teletone_audio_t;
165 struct teletone_generation_session;
166 typedef int (*tone_handler)(struct teletone_generation_session *ts, teletone_tone_map_t *map);
167
168
169 struct teletone_generation_session {
170
171 teletone_tone_map_t TONES[TELETONE_TONE_RANGE];
172
173 int channels;
174
175 int rate;
176
177 int duration;
178
179 int wait;
180
181 int tmp_duration;
182
183 int tmp_wait;
184
185 int loops;
186
187 int LOOPS;
188
189 float decay_factor;
190
191 int decay_direction;
192
193 int decay_step;
194
195 float volume;
196
197 int debug;
198
199 FILE *debug_stream;
200
201 void *user_data;
202
203 teletone_audio_t *buffer;
204
205 int datalen;
206
207 int samples;
208
209 int dynamic;
210 tone_handler handler;
211 };
212
213 typedef struct teletone_generation_session teletone_generation_session_t;
214
215
216
217
218
219
220
221
222
223 TELETONE_API(int) teletone_set_tone(teletone_generation_session_t *ts, int index, ...);
224
225
226
227
228
229
230
231 TELETONE_API(int) teletone_set_map(teletone_tone_map_t *map, ...);
232
233
234
235
236
237
238
239
240
241 TELETONE_API(int) teletone_init_session(teletone_generation_session_t *ts, int buflen, tone_handler handler, void *user_data);
242
243
244
245
246
247
248 TELETONE_API(int) teletone_destroy_session(teletone_generation_session_t *ts);
249
250
251
252
253
254
255
256 TELETONE_API(int) teletone_mux_tones(teletone_generation_session_t *ts, teletone_tone_map_t *map);
257
258
259
260
261
262
263
264 TELETONE_API(int) teletone_run(teletone_generation_session_t *ts, const char *cmd);
265
266 #ifdef __cplusplus
267 }
268 #endif
269
270 #endif
271
272
273
274
275
276
277
278
279
280
281