-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.c
411 lines (356 loc) · 12.3 KB
/
source.c
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
407
408
409
410
411
/* source.c
* Copyright (C) 2002-2008 Claudio Girardi
*
* 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 2 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, write to the Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include "source.h"
#include "audio.h"
#include "fft.h"
#include "mtm.h"
#include "hparma.h"
#include "lmp.h"
#include "cw_rx.h"
#include "util.h"
#include "avg.h"
#include "glfer.h"
#include "wav_fmt.h"
#include "g_main.h"
#include "g_scope.h"
#ifdef HAVE_DMALLOC_H
#include <dmalloc.h> /* dmalloc.h should be included last */
#endif
static int audio_fd = -1;
static int psd_n;
static char *fname = NULL;
static float *psdbuf = NULL;
static gint input_tag = -1;
static int file_ended = 0;
perf_timer_t perf_timer, proc_timer;
extern opt_t opt;
extern glfer_t glfer;
extern avg_data_t avgdata;
extern fft_params_t fft_gram_par;
extern mtm_params_t mtm_gram_par;
extern hparma_params_t hparma_gram_par;
extern lmp_params_t lmp_gram_par;
static float getclock(void)
{
clock_t nclock;
float nsec;
nclock = clock();
nsec = (float) nclock / CLOCKS_PER_SEC;
return nsec;
}
static float timefn(int action)
{
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
double newtime;
static double oldtime = 0.0;
if (gettimeofday(&tv, NULL))
perror("gettimeofday");
newtime = (double) tv.tv_sec + (double) tv.tv_usec / 1.0e06;
//printf("%e %e %e\n", newtime, oldtime, newtime - oldtime);
if (action == 0)
oldtime = newtime;
return (newtime - oldtime);
#endif /* HAVE_GETTIMEOFDAY */
}
/* reads the audio data, calls the fft processing function and the drawing
* function
* (called from within gtk_main whenever data is available */
static void audio_available(gpointer data, gint source, GdkInputCondition condition)
{
float *audio_buf;
int i, n;
int n_eff = opt.data_block_size * (1.0 - opt.data_blocks_overlap);
if (fname) { /* we have a file open for reading */
wav_read(&audio_buf, &n);
if (n == 0) {
file_ended = 1;
stop_reading_audio();
close_wav_file();
}
} else {
/* fill audio_buf */
audio_read(&audio_buf, &n);
/* n is the number of blocks of requested size read from the audio device */ }
for (i = 0; i < n; i++) {
/* get current time */
perf_timer.stop = tc_time();
/* elapsed time since last call */
perf_timer.delta = perf_timer.stop - perf_timer.start;
glfer.cpu_usage = proc_timer.delta / perf_timer.delta;
/* start new period */
perf_timer.start = perf_timer.stop;
/* start new data processing period */
proc_timer.start = perf_timer.stop;
switch (glfer.current_mode) {
case MODE_FFT :
fft_do(audio_buf + i * n_eff, &fft_gram_par);
fft_psd(psdbuf, NULL, &fft_gram_par);
if (glfer.scope_window) scope_window_draw(fft_gram_par);
break;
case MODE_MTM :
mtm_do(audio_buf + i * n_eff, psdbuf, NULL, &mtm_gram_par);
if (glfer.scope_window) scope_window_draw(mtm_gram_par.fft);
break;
case MODE_HPARMA :
hparma_do(audio_buf + i * n_eff, psdbuf, NULL, &hparma_gram_par);
if (glfer.scope_window) scope_window_draw(hparma_gram_par.fft);
break;
case MODE_LMP :
lmp_do(audio_buf + i * n_eff, psdbuf, NULL, &lmp_gram_par);
if (glfer.scope_window) scope_window_draw(lmp_gram_par.fft);
break;
default:
g_print("audio_available: MODE_UNKNOWN\n");
}
/* jason_do(audio_buf + i * opt.data_block_size, opt.data_block_size); */
//rx_cw(audio_buf + i * opt.data_block_size, opt.data_block_size);
main_window_draw(psdbuf);
/* end of data processing */
proc_timer.stop = tc_time();
proc_timer.delta = proc_timer.stop - proc_timer.start;
}
}
void init_audio(char *filename)
{
int n_eff = opt.data_block_size * (1.0 - opt.data_blocks_overlap);
if (filename) {
/* use a new file as audio source */
/* in case there is a file currently open as audio source, close it */
if (glfer.input_source == FILE_SOURCE) {
close_wav_file();
/* free filename string */
free(fname);
/* set pointer to null (to signal that source is not a file anymore) */
fname = NULL;
} else if (glfer.input_source == SOUNDCARD_SOURCE) {
/* close soundcard */
audio_close();
}
/* copy file name, as passed parameter might be deallocated later */
fname = strdup(filename);
audio_fd = open_wav_file(fname, n_eff, &opt.sample_rate);
file_ended = 0;
glfer.input_source = FILE_SOURCE;
} else /* no filename specified, continue use current audio source */
if (glfer.input_source == SOUNDCARD_SOURCE) {
glfer.input_source = SOUNDCARD_SOURCE; /* not needed... */
} else if (glfer.input_source == FILE_SOURCE) { /* continue to read current file */
glfer.input_source = FILE_SOURCE; /* not needed... */
} else { /* no current audio source and no filename specified; read from soundcard */
audio_fd = audio_init(opt.audio_device, &opt.sample_rate, n_eff);
glfer.input_source = SOUNDCARD_SOURCE;
}
}
void close_audio(void)
{
/* close current source */
if (glfer.input_source == SOUNDCARD_SOURCE) {
/* was reading from soundcard */
audio_close();
} else {
/* was reading from file */
close_wav_file();
/* free filename string */
free(fname);
/* set pointer to null (to signal that source is not a file anymore) */
fname = NULL;
}
audio_fd = -1;
glfer.input_source = SOURCE_NONE;
}
void toggle_stop_start(GtkWidget * widget, gpointer data)
{
if (input_tag == -1) {
start_reading_audio();
} else {
stop_reading_audio();
}
}
void start_reading_audio()
{
int n_eff = opt.data_block_size * (1.0 - opt.data_blocks_overlap);
if (glfer.input_source == SOUNDCARD_SOURCE) { /* read from soundcard */
/* audio_available() has to be called whenever there's data on audio_fd */
input_tag = gdk_input_add(audio_fd, GDK_INPUT_READ, audio_available, NULL);
} else if (glfer.input_source == FILE_SOURCE) { /* read from file */
if (file_ended == 1) {
audio_fd = open_wav_file(fname, n_eff, &opt.sample_rate);
file_ended = 0;
}
input_tag = g_idle_add((GSourceFunc)audio_available, NULL);
}
stop_start_button_set_label("Stop");
}
void stop_reading_audio()
{
if (input_tag != -1) {
/* if currently reading from a source */
gtk_input_remove(input_tag);
input_tag = -1;
stop_start_button_set_label("Start");
/* do not close an open file, so that pressing again the Start/Stop button continues the file processing */
}
}
void change_params(int mode_sel)
{
/* save current open file name (if any) */
char *current_fname;
if (fname)
current_fname = strdup(fname);
else
current_fname = NULL;
/* check if this is the first call at program start-up */
if (glfer.current_mode != MODE_NONE) {
/* stop reading from the current data source */
stop_reading_audio();
/* close current mode */
switch (glfer.current_mode) {
case MODE_FFT:
fft_close(&fft_gram_par);
break;
case MODE_MTM:
mtm_close(&mtm_gram_par);
break;
case MODE_HPARMA:
hparma_close(&hparma_gram_par);
break;
case MODE_LMP:
lmp_close(&lmp_gram_par);
break;
default:
g_print("change_params: MODE_UNKNOWN\n");
}
FREE_MAYBE(psdbuf);
}
/* initialize mode */
switch (mode_sel) {
case MODE_FFT :
if ((fft_gram_par.n != opt.data_block_size) || (fft_gram_par.overlap != opt.data_blocks_overlap)) {
close_audio();
init_audio(current_fname);
}
if (fft_gram_par.n != opt.data_block_size) {
/* delete and reallocate averaging buffer */
delete_avg(&avgdata);
alloc_avg(&avgdata, opt.data_block_size, opt.avgsamples);
glfer.avgfill = 0; /* reset ratio of average buffer full */
glfer.avgtime = (float) opt.avgsamples * (float) opt.data_block_size * (1.0 / (float) opt.sample_rate)*(1.0-opt.data_blocks_overlap);
}
psd_n = opt.data_block_size / 2 + 1;
psdbuf = calloc(psd_n, sizeof(float));
fft_gram_par.n = opt.data_block_size;
fft_gram_par.window_type = opt.window_type;
fft_gram_par.overlap = opt.data_blocks_overlap;
fft_gram_par.a = opt.limiter_a;
fft_gram_par.limiter = opt.enable_limiter;
fft_init(&fft_gram_par);
break;
case MODE_MTM :
if ((mtm_gram_par.fft.n != opt.data_block_size) || (mtm_gram_par.fft.overlap != opt.data_blocks_overlap)) {
close_audio();
init_audio(current_fname);
}
if (fft_gram_par.n != opt.data_block_size) {
/* delete and reallocate averaging buffer */
delete_avg(&avgdata);
alloc_avg(&avgdata, opt.data_block_size, opt.avgsamples);
glfer.avgfill = 0; /* reset ratio of average buffer full */
glfer.avgtime = (float) opt.avgsamples * (float) opt.data_block_size * (1.0 / (float) opt.sample_rate)*(1.0-opt.data_blocks_overlap);
}
psd_n = opt.data_block_size / 2 + 1;
psdbuf = calloc(psd_n, sizeof(float));
mtm_gram_par.fft.n = opt.data_block_size;;
mtm_gram_par.fft.window_type = RECTANGULAR_WINDOW; /* that is, no windowing is used */
mtm_gram_par.fft.overlap = opt.data_blocks_overlap;
mtm_gram_par.fft.a = opt.limiter_a;
mtm_gram_par.fft.limiter = opt.enable_limiter;
mtm_gram_par.w = opt.mtm_w;
mtm_gram_par.kmax = opt.mtm_k;
mtm_init(&mtm_gram_par);
break;
case MODE_HPARMA :
if ((hparma_gram_par.fft.n != opt.data_block_size) || (hparma_gram_par.fft.overlap != opt.data_blocks_overlap)) {
close_audio();
init_audio(current_fname);
}
if (fft_gram_par.n != opt.data_block_size) {
/* delete and reallocate averaging buffer */
delete_avg(&avgdata);
alloc_avg(&avgdata, opt.data_block_size, opt.avgsamples);
glfer.avgfill = 0; /* reset ratio of average buffer full */
glfer.avgtime = (float) opt.avgsamples * (float) opt.data_block_size * (1.0 / (float) opt.sample_rate)*(1.0-opt.data_blocks_overlap);
}
psd_n = opt.data_block_size / 2 + 1;
psdbuf = calloc(psd_n, sizeof(float));
hparma_gram_par.fft.n = opt.data_block_size;
hparma_gram_par.fft.window_type = RECTANGULAR_WINDOW;
hparma_gram_par.fft.overlap = opt.data_blocks_overlap;
hparma_gram_par.fft.a = opt.limiter_a;
hparma_gram_par.fft.limiter = opt.enable_limiter;
hparma_gram_par.t = opt.hparma_t;
hparma_gram_par.p_e = opt.hparma_p_e;
hparma_gram_par.q_e = -1;
hparma_init(&hparma_gram_par);
break;
case MODE_LMP :
if ((lmp_gram_par.fft.n != opt.data_block_size) || (lmp_gram_par.fft.overlap != opt.data_blocks_overlap)) {
close_audio();
init_audio(current_fname);
}
if (fft_gram_par.n != opt.data_block_size) {
/* delete and reallocate averaging buffer */
delete_avg(&avgdata);
alloc_avg(&avgdata, opt.data_block_size, opt.avgsamples);
glfer.avgfill = 0; /* reset ratio of average buffer full */
glfer.avgtime = (float) opt.avgsamples * (float) opt.data_block_size * (1.0 / (float) opt.sample_rate)*(1.0-opt.data_blocks_overlap);
}
psd_n = opt.data_block_size / 2 + 1;
psdbuf = calloc(psd_n, sizeof(float));
lmp_gram_par.fft.n = opt.data_block_size;
lmp_gram_par.fft.window_type = RECTANGULAR_WINDOW;
lmp_gram_par.fft.overlap = opt.data_blocks_overlap;
lmp_gram_par.fft.a = opt.limiter_a;
lmp_gram_par.fft.limiter = opt.enable_limiter;
lmp_gram_par.avg = opt.lmp_av;
lmp_init(&lmp_gram_par);
break;
default:
g_print("change_params: MODE_UNKNOWN\n");
}
glfer.current_mode = mode_sel;
main_window_init(psd_n, 0, (float) opt.sample_rate / 2);
start_reading_audio();
}