Subversion Repositories svnkaklik

Rev

Rev 262 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 262 Rev 334
1
//
1
//
2
// sidd.c:  A VLF signal monitor.
2
// sidd.c:  A VLF signal monitor.
3
//
3
//
4
// author: Paul Nicholson, paul@abelian.demon.co.uk
4
// author: Paul Nicholson, paul@abelian.demon.co.uk
5
//
5
//
6
 
6
 
7
#include <stdlib.h>
7
#include <stdlib.h>
8
#include <unistd.h>
8
#include <unistd.h>
9
#include <math.h>
9
#include <math.h>
10
#include <stdio.h>
10
#include <stdio.h>
11
#include <sys/stat.h>
11
#include <sys/stat.h>
12
#include <sys/ioctl.h>
12
#include <sys/ioctl.h>
13
#include <sys/time.h>
13
#include <sys/time.h>
14
#include <sys/param.h>
14
#include <sys/param.h>
15
#include <fcntl.h>
15
#include <fcntl.h>
16
#include <errno.h>
16
#include <errno.h>
17
#include <stdarg.h>
17
#include <stdarg.h>
18
#include <ctype.h>
18
#include <ctype.h>
19
#include <string.h>
19
#include <string.h>
20
#include <signal.h>
20
#include <signal.h>
21
#include <time.h>
21
#include <time.h>
22
#include <sched.h>
22
#include <sched.h>
23
#include <linux/soundcard.h>
23
#include <linux/soundcard.h>
24
 
24
 
25
#include <fftw3.h>
25
#include <fftw3.h>
26
 
26
 
27
///////////////////////////////////////////////////////////////////////////////
27
///////////////////////////////////////////////////////////////////////////////
28
//  Tuneable Settings                                                        //
28
//  Tuneable Settings                                                        //
29
///////////////////////////////////////////////////////////////////////////////
29
///////////////////////////////////////////////////////////////////////////////
30
 
30
 
31
#define VERSION "0.93"
31
#define VERSION "0.93"
32
 
32
 
33
//
33
//
34
//  Number of soundcard bytes to read at a time.
34
//  Number of soundcard bytes to read at a time.
35
#define NREAD 2048
35
#define NREAD 2048
36
 
36
 
37
//
37
//
38
//  Max number of bands which can be read from the config file.
38
//  Max number of bands which can be read from the config file.
39
#define MAXBANDS 20
39
#define MAXBANDS 20
40
 
40
 
41
//
41
//
42
//  Name of the configuration file.
42
//  Name of the configuration file.
43
#define CONFIG_FILE "sidd.conf"
43
#define CONFIG_FILE "sidd.conf"
44
 
44
 
45
///////////////////////////////////////////////////////////////////////////////
45
///////////////////////////////////////////////////////////////////////////////
46
//  Globals and fixed definitions                                            // 
46
//  Globals and fixed definitions                                            // 
47
///////////////////////////////////////////////////////////////////////////////
47
///////////////////////////////////////////////////////////////////////////////
48
//
48
//
49
//  Default values here are over-ridden by the config file.
49
//  Default values here are over-ridden by the config file.
50
 
50
 
51
int mode = 1;                                          //  1 = mono, 2 = stereo
51
int mode = 1;                                          //  1 = mono, 2 = stereo
52
int bits = 16;                                    // Sample width, 8 or 16 bits
52
int bits = 16;                                    // Sample width, 8 or 16 bits
53
int BINS = 2048;                                    // Number of frequency bins
53
int BINS = 2048;                                    // Number of frequency bins
54
#define FFTWID (2 * BINS)                    // Number of samples in FFT period
54
#define FFTWID (2 * BINS)                    // Number of samples in FFT period
55
 
55
 
56
int background = 1;                        // Set zero if running in foreground
56
int background = 1;                        // Set zero if running in foreground
57
int fdi;                                                   // Input file handle
57
int fdi;                                                   // Input file handle
58
int fdm;                                                   // Mixer file handle
58
int fdm;                                                   // Mixer file handle
59
int VFLAG = 0;                                    //  Set non-zero by -v option
59
int VFLAG = 0;                                    //  Set non-zero by -v option
60
int MFLAG = 0;                                    //  Set non-zero by -m option
60
int MFLAG = 0;                                    //  Set non-zero by -m option
61
 
61
 
62
int spec_max = 100;       // Issue a spectrum for every spec_max output records
62
int spec_max = 100;       // Issue a spectrum for every spec_max output records
63
int spec_cnt = 0;
63
int spec_cnt = 0;
64
int sample_rate = 100000;                                 // Samples per second
64
int sample_rate = 100000;                                 // Samples per second
65
 
65
 
66
int chans = 1;
66
int chans = 1;
67
int alert_on = 0;
67
int alert_on = 0;
68
 
68
 
69
int priority = 0;                       // Set to 1 if high scheduling priority
69
int priority = 0;                       // Set to 1 if high scheduling priority
70
struct sigaction sa;
70
struct sigaction sa;
71
char mailaddr[100];
71
char mailaddr[100];
72
 
72
 
73
double los_thresh = 0;                    // Threshold for loss of signal, 0..1
73
double los_thresh = 0;                    // Threshold for loss of signal, 0..1
74
int los_timeout = 0;        // Number of seconds before loss of signal declared
74
int los_timeout = 0;        // Number of seconds before loss of signal declared
75
 
75
 
76
double DF;                                   // Frequency resolution of the FFT
76
double DF;                                   // Frequency resolution of the FFT
77
int bailout_flag = 0;                           // To prevent bailout() looping
77
int bailout_flag = 0;                           // To prevent bailout() looping
78
int grab_cnt = 0;                       // Count of samples into the FFT buffer
78
int grab_cnt = 0;                       // Count of samples into the FFT buffer
79
 
79
 
80
// Mixer gain settings requested by config file.
80
// Mixer gain settings requested by config file.
81
int req_lgain = -1;              // Line gain
81
int req_lgain = -1;              // Line gain
82
int req_igain = -1;              // Input gain 
82
int req_igain = -1;              // Input gain 
83
int req_rgain = -1;              // Record level
83
int req_rgain = -1;              // Record level
84
 
84
 
85
//
85
//
86
// Various filenames, contents set by config file.
86
// Various filenames, contents set by config file.
87
//
87
//
88
char logfile[100] = "";
88
char logfile[100] = "";
89
char device[100] = "/dev/dsp";
89
char device[100] = "/dev/dsp";
90
char mixer[100] = "/dev/mixer";
90
char mixer[100] = "/dev/mixer";
91
char spectrum_file[100] = "/tmp/sidspec"; 
91
char spectrum_file[100] = "/tmp/sidspec"; 
92
char datadir[100] = ".";
92
char datadir[100] = ".";
93
 
93
 
94
//
94
//
95
// Table of frequency bands to monitor
95
// Table of frequency bands to monitor
96
//
96
//
97
 
97
 
98
struct BAND
98
struct BAND
99
{
99
{
100
   char ident[50];
100
   char ident[50];
101
 
101
 
102
   int start;
102
   int start;
103
   int end;
103
   int end;
104
}
104
}
105
 bands[MAXBANDS];    // Table of bands to be monitored
105
 bands[MAXBANDS];    // Table of bands to be monitored
106
 
106
 
107
int nbands = 0;
107
int nbands = 0;
108
 
108
 
109
//
109
//
110
//  Independent state variables and buffers for left and right channels
110
//  Independent state variables and buffers for left and right channels
111
//
111
//
112
struct CHAN
112
struct CHAN
113
{
113
{
114
   char *name;
114
   char *name;
115
   double *signal_avg;
115
   double *signal_avg;
116
   double *powspec;
116
   double *powspec;
117
   double *fft_inbuf;
117
   double *fft_inbuf;
118
   fftw_complex *fft_data;
118
   fftw_complex *fft_data;
119
   fftw_plan ffp;
119
   fftw_plan ffp;
120
   double peak;
120
   double peak;
121
   double sum_sq;
121
   double sum_sq;
122
   int los_state;
122
   int los_state;
123
   time_t los_time;
123
   time_t los_time;
124
   FILE *fo;
124
   FILE *fo;
125
   char fname[100];
125
   char fname[100];
126
}
126
}
127
 left = { "left" }, right = { "right" };
127
 left = { "left" }, right = { "right" };
128
 
128
 
129
///////////////////////////////////////////////////////////////////////////////
129
///////////////////////////////////////////////////////////////////////////////
130
//  Various Utility Functions                                                //
130
//  Various Utility Functions                                                //
131
///////////////////////////////////////////////////////////////////////////////
131
///////////////////////////////////////////////////////////////////////////////
132
 
132
 
133
//
133
//
134
//  Issue a message to the log file, if the verbosity level is high enough...
134
//  Issue a message to the log file, if the verbosity level is high enough...
135
//
135
//
136
 
136
 
137
void report( int level, char *format, ...)
137
void report( int level, char *format, ...)
138
{
138
{
139
   va_list ap;
139
   va_list ap;
140
   void bailout( char *format, ...);
140
   void bailout( char *format, ...);
141
   char temp[ 200];
141
   char temp[ 200];
142
 
142
 
143
   if( VFLAG < level) return;
143
   if( VFLAG < level) return;
144
 
144
 
145
   va_start( ap, format);
145
   va_start( ap, format);
146
   vsprintf( temp, format, ap);
146
   vsprintf( temp, format, ap);
147
   va_end( ap);
147
   va_end( ap);
148
 
148
 
149
   if( !logfile[0] || !background)
149
   if( !logfile[0] || !background)
150
      if( background != 2) fprintf( stderr, "%s\n", temp);
150
      if( background != 2) fprintf( stderr, "%s\n", temp);
151
 
151
 
152
   if( logfile[0])
152
   if( logfile[0])
153
   {
153
   {
154
      time_t now = time( NULL);
154
      time_t now = time( NULL);
155
      struct tm *tm = gmtime( &now);
155
      struct tm *tm = gmtime( &now);
156
      FILE *flog = NULL;
156
      FILE *flog = NULL;
157
   
157
   
158
      if( (flog = fopen( logfile, "a+")) == NULL)
158
      if( (flog = fopen( logfile, "a+")) == NULL)
159
         bailout( "cannot open logfile [%s]: %s", logfile, strerror( errno));
159
         bailout( "cannot open logfile [%s]: %s", logfile, strerror( errno));
160
   
160
   
161
      fprintf( flog, "%04d/%02d/%02d %02d:%02d:%02d %s\n", 
161
      fprintf( flog, "%04d/%02d/%02d %02d:%02d:%02d %s\n", 
162
                tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
162
                tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
163
                tm->tm_hour, tm->tm_min, tm->tm_sec, temp);
163
                tm->tm_hour, tm->tm_min, tm->tm_sec, temp);
164
      fclose( flog);
164
      fclose( flog);
165
   }
165
   }
166
}
166
}
167
 
167
 
168
void alert( char *format, ...)
168
void alert( char *format, ...)
169
{
169
{
170
   FILE *f;
170
   FILE *f;
171
   va_list( ap);
171
   va_list( ap);
172
   char cmd[100], temp[100];
172
   char cmd[100], temp[100];
173
 
173
 
174
   va_start( ap, format);
174
   va_start( ap, format);
175
   vsprintf( temp, format, ap);
175
   vsprintf( temp, format, ap);
176
   va_end( ap);
176
   va_end( ap);
177
 
177
 
178
   report( -1, "%s", temp);
178
   report( -1, "%s", temp);
179
 
179
 
180
   if( !alert_on || !mailaddr[0]) return;
180
   if( !alert_on || !mailaddr[0]) return;
181
 
181
 
182
   sprintf( cmd, "mail -s 'sidd alert' '%s'", mailaddr);
182
   sprintf( cmd, "mail -s 'sidd alert' '%s'", mailaddr);
183
   if( (f=popen( cmd, "w")) == NULL)
183
   if( (f=popen( cmd, "w")) == NULL)
184
   {
184
   {
185
      report( 0, "cannot exec [%s]: %s", cmd, strerror( errno));
185
      report( 0, "cannot exec [%s]: %s", cmd, strerror( errno));
186
      return;
186
      return;
187
   }
187
   }
188
 
188
 
189
   fprintf( f, "sidd: %s\n", temp);
189
   fprintf( f, "sidd: %s\n", temp);
190
   fclose( f);
190
   fclose( f);
191
}
191
}
192
 
192
 
193
//
193
//
194
//  We try to exit the program through here, if possible.
194
//  We try to exit the program through here, if possible.
195
//  
195
//  
196
 
196
 
197
void bailout( char *format, ...)
197
void bailout( char *format, ...)
198
{
198
{
199
   va_list ap;
199
   va_list ap;
200
   char temp[ 200];
200
   char temp[ 200];
201
 
201
 
202
   if( bailout_flag) exit( 1);
202
   if( bailout_flag) exit( 1);
203
   bailout_flag = 1;
203
   bailout_flag = 1;
204
   va_start( ap, format);
204
   va_start( ap, format);
205
   vsprintf( temp, format, ap);
205
   vsprintf( temp, format, ap);
206
   va_end( ap);
206
   va_end( ap);
207
 
207
 
208
   alert( "terminating: %s", temp);
208
   alert( "terminating: %s", temp);
209
   exit( 1);
209
   exit( 1);
210
}
210
}
211
 
211
 
212
//
212
//
213
//  Exit with a message if we get any signals.
213
//  Exit with a message if we get any signals.
214
//  
214
//  
215
 
215
 
216
void handle_sigs( int signum)
216
void handle_sigs( int signum)
217
{
217
{
218
   bailout( "got signal %d", signum);
218
   bailout( "got signal %d", signum);
219
}
219
}
220
 
220
 
221
///////////////////////////////////////////////////////////////////////////////
221
///////////////////////////////////////////////////////////////////////////////
222
//  Soundcard Setup                                                          //
222
//  Soundcard Setup                                                          //
223
///////////////////////////////////////////////////////////////////////////////
223
///////////////////////////////////////////////////////////////////////////////
224
 
224
 
225
//
225
//
226
//  Prepare the input stream, setting up the soundcard if the input
226
//  Prepare the input stream, setting up the soundcard if the input
227
//  is a character device.
227
//  is a character device.
228
//
228
//
229
 
229
 
230
void setup_input_stream( void)
230
void setup_input_stream( void)
231
{
231
{
232
   struct stat st;
232
   struct stat st;
233
 
233
 
234
   report( 1, "taking data from [%s]", device);
234
   report( 1, "taking data from [%s]", device);
235
 
235
 
236
   if( (fdi = open( device, O_RDONLY)) < 0)
236
   if( (fdi = open( device, O_RDONLY)) < 0)
237
      bailout( "cannot open [%s]: %s", strerror( errno));
237
      bailout( "cannot open [%s]: %s", strerror( errno));
238
 
238
 
239
   if( fstat( fdi, &st) < 0)
239
   if( fstat( fdi, &st) < 0)
240
      bailout( "cannot stat input stream: %s", strerror( errno));
240
      bailout( "cannot stat input stream: %s", strerror( errno));
241
 
241
 
242
   if( S_ISCHR( st.st_mode)) 
242
   if( S_ISCHR( st.st_mode)) 
243
   {
243
   {
244
      int blksize;
244
      int blksize;
245
      int fragreq = 0x7fff000a;
245
      int fragreq = 0x7fff000a;
246
      unsigned int format;
246
      unsigned int format;
247
      unsigned int req_format = AFMT_S16_LE;
247
      unsigned int req_format = AFMT_S16_LE;
248
      if( bits == 8) req_format = AFMT_U8;
248
      if( bits == 8) req_format = AFMT_U8;
249
 
249
 
250
      if (ioctl( fdi, SNDCTL_DSP_SETFRAGMENT, &fragreq))
250
      if (ioctl( fdi, SNDCTL_DSP_SETFRAGMENT, &fragreq))
251
         report( 01, "cannot set fragment size");
251
         report( 01, "cannot set fragment size");
252
 
252
 
253
      if( ioctl( fdi, SNDCTL_DSP_RESET, NULL) < 0)
253
      if( ioctl( fdi, SNDCTL_DSP_RESET, NULL) < 0)
254
         bailout( "cannot reset input device");
254
         bailout( "cannot reset input device");
255
 
255
 
256
      chans = mode;
256
      chans = mode;
257
      if( ioctl( fdi, SNDCTL_DSP_CHANNELS, &chans) < 0)
257
      if( ioctl( fdi, SNDCTL_DSP_CHANNELS, &chans) < 0)
258
         bailout( "cannot set channels on input device");
258
         bailout( "cannot set channels on input device");
259
 
259
 
260
      if( ioctl( fdi, SNDCTL_DSP_GETFMTS, &format) < 0)
260
      if( ioctl( fdi, SNDCTL_DSP_GETFMTS, &format) < 0)
261
         bailout( "cannot get formats from input device");
261
         bailout( "cannot get formats from input device");
262
 
262
 
263
      report( 2, "formats available: %08X", format);
263
      report( 2, "formats available: %08X", format);
264
      if( (format & req_format) == 0)
264
      if( (format & req_format) == 0)
265
      {
265
      {
266
         report( 0, "available dsp modes are %08X", format);
266
         report( 0, "available dsp modes are %08X", format);
267
         bailout( "unable to set %d bit dsp mode", bits);
267
         bailout( "unable to set %d bit dsp mode", bits);
268
      }
268
      }
269
 
269
 
270
      format = req_format;
270
      format = req_format;
271
      if( ioctl( fdi, SNDCTL_DSP_SETFMT, &format) < 0)
271
      if( ioctl( fdi, SNDCTL_DSP_SETFMT, &format) < 0)
272
         bailout( "cannot set dsp format on input device");
272
         bailout( "cannot set dsp format on input device");
273
 
273
 
274
      if( ioctl( fdi, SNDCTL_DSP_GETBLKSIZE, &blksize) < 0)
274
      if( ioctl( fdi, SNDCTL_DSP_GETBLKSIZE, &blksize) < 0)
275
         bailout( "cannot get block size from input device");
275
         bailout( "cannot get block size from input device");
276
 
276
 
277
      report( 2, "dsp block size: %d", blksize);
277
      report( 2, "dsp block size: %d", blksize);
278
      if( ioctl( fdi, SNDCTL_DSP_CHANNELS, &chans) < 0)
278
      if( ioctl( fdi, SNDCTL_DSP_CHANNELS, &chans) < 0)
279
         bailout( "cannot get channels from input device");
279
         bailout( "cannot get channels from input device");
280
 
280
 
281
      report( 1, "requesting rate %d", sample_rate);
281
      report( 1, "requesting rate %d", sample_rate);
282
      if( ioctl( fdi, SNDCTL_DSP_SPEED, &sample_rate) < 0)
282
      if( ioctl( fdi, SNDCTL_DSP_SPEED, &sample_rate) < 0)
283
         bailout( "cannot set sample rate of input device");
283
         bailout( "cannot set sample rate of input device");
284
 
284
 
285
      report( 1, "actual rate set: %d samples/sec", sample_rate);
285
      report( 1, "actual rate set: %d samples/sec", sample_rate);
286
      report( 1, "soundcard channels: %d  bits: %d", chans, bits);
286
      report( 1, "soundcard channels: %d  bits: %d", chans, bits);
287
   }
287
   }
288
}
288
}
289
 
289
 
290
///////////////////////////////////////////////////////////////////////////////
290
///////////////////////////////////////////////////////////////////////////////
291
//  Output Functions                                                         //
291
//  Output Functions                                                         //
292
///////////////////////////////////////////////////////////////////////////////
292
///////////////////////////////////////////////////////////////////////////////
293
 
293
 
294
void maybe_output_spectrum( void)
294
void maybe_output_spectrum( void)
295
{
295
{
296
   FILE *f;
296
   FILE *f;
297
   int i;
297
   int i;
298
 
298
 
299
   if( ++spec_cnt < spec_max) return;  // Wait for spec_max records
299
   if( ++spec_cnt < spec_max) return;  // Wait for spec_max records
300
   spec_cnt = 0;
300
   spec_cnt = 0;
301
 
301
 
302
   if( !spectrum_file[0]) return;     // Spectrum file not wanted.
302
   if( !spectrum_file[0]) return;     // Spectrum file not wanted.
303
 
303
 
304
   if( (f=fopen( spectrum_file, "w+")) == NULL)
304
   if( (f=fopen( spectrum_file, "w+")) == NULL)
305
      bailout( "cannot open spectrum file %s, %s", strerror( errno));
305
      bailout( "cannot open spectrum file %s, %s", strerror( errno));
306
 
306
 
307
   if( mode == 1){
307
   if( mode == 1){
308
      fprintf( f, "Frequency PowerL \n");
308
      fprintf( f, "Frequency PowerL \n");
309
      for( i=0; i<BINS; i++) fprintf( f, "%.5e %.5e\n", 
309
      for( i=0; i<BINS; i++) fprintf( f, "%.5e %.5e\n", 
310
             (i+0.5) * DF, left.signal_avg[i]/spec_max);}
310
             (i+0.5) * DF, left.signal_avg[i]/spec_max);}
311
   else{
311
   else{
312
      fprintf( f, "Frequncy PowerL PowerR \n");
312
      fprintf( f, "Frequncy PowerL PowerR \n");
313
      for( i=0; i<BINS; i++) fprintf( f, "%.5e %.5e %.5e\n", 
313
      for( i=0; i<BINS; i++) fprintf( f, "%.5e %.5e %.5e\n", 
314
             (i+0.5) * DF, left.signal_avg[i]/spec_max,
314
             (i+0.5) * DF, left.signal_avg[i]/spec_max,
315
                          right.signal_avg[i]/spec_max);}
315
                          right.signal_avg[i]/spec_max);}
316
   fclose( f);
316
   fclose( f);
317
 
317
 
318
   for( i=0; i<BINS; i++) left.signal_avg[i] = 0;
318
   for( i=0; i<BINS; i++) left.signal_avg[i] = 0;
319
   if( mode == 2) for( i=0; i<BINS; i++) right.signal_avg[i] = 0;
319
   if( mode == 2) for( i=0; i<BINS; i++) right.signal_avg[i] = 0;
320
}
320
}
321
 
321
 
322
void output_record( struct CHAN *c, char *prefix, double fsecs)
322
void output_record( struct CHAN *c, char *prefix, double fsecs)
323
{
323
{
324
   int i, j;
324
   int i, j;
325
   char test[100];
325
   char test[100];
326
 
326
 
327
   if( mode == 1)
327
   if( mode == 1)
328
      sprintf( test, "%s.dat", prefix); 
328
      sprintf( test, "%s.dat", prefix); 
329
   else
329
   else
330
      sprintf( test, "%s.%s.dat", prefix, c->name); 
330
      sprintf( test, "%s.%s.dat", prefix, c->name); 
331
    
331
    
332
   if( !c->fo || strcmp( test, c->fname))
332
   if( !c->fo || strcmp( test, c->fname))
333
   {
333
   {
334
      if( c->fo) fclose( c->fo);
334
      if( c->fo) fclose( c->fo);
335
      strcpy( c->fname, test);
335
      strcpy( c->fname, test);
336
      report( 0, "using output file [%s]", c->fname);
336
      report( 0, "using output file [%s]", c->fname);
337
      if( (c->fo=fopen( c->fname, "a+")) == NULL)
337
      if( (c->fo=fopen( c->fname, "a+")) == NULL)
338
         bailout( "cannot open [%s], %s", c->fname, strerror( errno));
338
         bailout( "cannot open [%s], %s", c->fname, strerror( errno));
339
   }
339
   }
340
 
340
 
341
   fprintf( c->fo, "%.3f %.3f %.3f", fsecs, c->peak, sqrt( c->sum_sq/FFTWID));
341
   fprintf( c->fo, "%.3f %.3f %.3f", fsecs, c->peak, sqrt( c->sum_sq/FFTWID));
342
 
342
 
343
   for( i=0; i<nbands; i++)
343
   for( i=0; i<nbands; i++)
344
   {
344
   {
345
      double e = 0;
345
      double e = 0;
346
      int n1 = bands[i].start/DF;
346
      int n1 = bands[i].start/DF;
347
      int n2 = bands[i].end/DF;
347
      int n2 = bands[i].end/DF;
348
      for( j=n1; j<= n2; j++) e += c->powspec[j];
348
      for( j=n1; j<= n2; j++) e += c->powspec[j];
349
      e /= n2 - n1 + 1;
349
      e /= n2 - n1 + 1;
350
      fprintf( c->fo, " %.2e", e);
350
      fprintf( c->fo, " %.2e", e);
351
   }
351
   }
352
   fprintf( c->fo, "\n");
352
   fprintf( c->fo, "\n");
353
   fflush( c->fo);
353
   fflush( c->fo);
354
 
354
 
355
   c->peak = c->sum_sq = 0;
355
   c->peak = c->sum_sq = 0;
356
}
356
}
357
 
357
 
358
void output_records( void)
358
void output_records( void)
359
{
359
{
360
   struct timeval tv;
360
   struct timeval tv;
361
   struct tm *tm;
361
   struct tm *tm;
362
   double fsecs;
362
   double fsecs;
363
   time_t ud;
363
   time_t ud;
364
   char prefix[100];
364
   char prefix[100];
365
 
365
 
366
   gettimeofday( &tv, NULL);
366
   gettimeofday( &tv, NULL);
367
   fsecs = tv.tv_sec + 1e-6 * tv.tv_usec;
367
   fsecs = tv.tv_sec + 1e-6 * tv.tv_usec;
368
   ud = tv.tv_sec - tv.tv_sec % 86400;
368
   ud = tv.tv_sec - tv.tv_sec % 86400;
369
   tm = gmtime( &ud);
369
   tm = gmtime( &ud);
370
   sprintf( prefix, "%s/%02d%02d%02d", datadir,
370
   sprintf( prefix, "%s/%02d%02d%02d", datadir,
371
                  tm->tm_year - 100, tm->tm_mon+1, tm->tm_mday);
371
                  tm->tm_year - 100, tm->tm_mon+1, tm->tm_mday);
372
 
372
 
373
   output_record( &left, prefix, fsecs);
373
   output_record( &left, prefix, fsecs);
374
   if( mode == 2) output_record( &right, prefix, fsecs);
374
   if( mode == 2) output_record( &right, prefix, fsecs);
375
}
375
}
376
 
376
 
377
void check_los( struct CHAN *c)
377
void check_los( struct CHAN *c)
378
{
378
{
379
   if( !c->los_state)
379
   if( !c->los_state)
380
   {
380
   {
381
      if( !c->los_time && c->peak < los_thresh) time( &c->los_time);
381
      if( !c->los_time && c->peak < los_thresh) time( &c->los_time);
382
      if( c->los_time && c->peak > los_thresh) c->los_time = 0;
382
      if( c->los_time && c->peak > los_thresh) c->los_time = 0;
383
      if( c->los_time && c->los_time + los_timeout < time( NULL))
383
      if( c->los_time && c->los_time + los_timeout < time( NULL))
384
      {
384
      {
385
         c->los_state = 1;
385
         c->los_state = 1;
386
         c->los_time = 0;
386
         c->los_time = 0;
387
         if( mode == 1) alert( "loss of signal");
387
         if( mode == 1) alert( "loss of signal");
388
         else alert( "loss of signal on %s", c->name);
388
         else alert( "loss of signal on %s", c->name);
389
      }
389
      }
390
   }
390
   }
391
   else
391
   else
392
   {
392
   {
393
      if( !c->los_time && c->peak > los_thresh) time( &c->los_time);
393
      if( !c->los_time && c->peak > los_thresh) time( &c->los_time);
394
      if( c->los_time && c->peak < los_thresh) c->los_time = 0;
394
      if( c->los_time && c->peak < los_thresh) c->los_time = 0;
395
      if( c->los_time && c->los_time + los_timeout < time( NULL))
395
      if( c->los_time && c->los_time + los_timeout < time( NULL))
396
      {
396
      {
397
         c->los_state = 0;
397
         c->los_state = 0;
398
         c->los_time = 0;
398
         c->los_time = 0;
399
         if( mode == 1) alert( "signal restored");
399
         if( mode == 1) alert( "signal restored");
400
         else alert( "signal restored on %s", c->name);
400
         else alert( "signal restored on %s", c->name);
401
      }
401
      }
402
   } 
402
   } 
403
}
403
}
404
 
404
 
405
///////////////////////////////////////////////////////////////////////////////
405
///////////////////////////////////////////////////////////////////////////////
406
//  Signal Processing                                                        //
406
//  Signal Processing                                                        //
407
///////////////////////////////////////////////////////////////////////////////
407
///////////////////////////////////////////////////////////////////////////////
408
 
408
 
409
void process_fft( struct CHAN *c)
409
void process_fft( struct CHAN *c)
410
{
410
{
411
   int i;
411
   int i;
412
 
412
 
413
   //
413
   //
414
   //  Do the FFT.  First time through, initialise the fft plan.
414
   //  Do the FFT.  First time through, initialise the fft plan.
415
   //
415
   //
416
 
416
 
417
   if( !c->ffp)
417
   if( !c->ffp)
418
      c->ffp = fftw_plan_dft_r2c_1d( FFTWID, c->fft_inbuf, c->fft_data,
418
      c->ffp = fftw_plan_dft_r2c_1d( FFTWID, c->fft_inbuf, c->fft_data,
419
                           FFTW_ESTIMATE | FFTW_DESTROY_INPUT);
419
                           FFTW_ESTIMATE | FFTW_DESTROY_INPUT);
420
 
420
 
421
   fftw_execute( c->ffp);
421
   fftw_execute( c->ffp);
422
 
422
 
423
   //
423
   //
424
   //  Obtain squared amplitude of each bin.
424
   //  Obtain squared amplitude of each bin.
425
   //
425
   //
426
 
426
 
427
   c->powspec[ 0] = 0.0;  // Zero the DC component
427
   c->powspec[ 0] = 0.0;  // Zero the DC component
428
   for( i=1; i<BINS; i++)
428
   for( i=1; i<BINS; i++)
429
   {
429
   {
430
      double t1 = c->fft_data[ i][0];  
430
      double t1 = c->fft_data[ i][0];  
431
      double t2 = c->fft_data[ i][1]; 
431
      double t2 = c->fft_data[ i][1]; 
432
      c->powspec[ i] = t1*t1 + t2*t2;
432
      c->powspec[ i] = t1*t1 + t2*t2;
433
   }
433
   }
434
 
434
 
435
   //
435
   //
436
   //  Accumulate average signal levels in each bin.  signal_avg is used
436
   //  Accumulate average signal levels in each bin.  signal_avg is used
437
   //  only for the spectrum file output.
437
   //  only for the spectrum file output.
438
   //
438
   //
439
 
439
 
440
   for( i=0; i<BINS; i++) c->signal_avg[ i] += c->powspec[i];
440
   for( i=0; i<BINS; i++) c->signal_avg[ i] += c->powspec[i];
441
   check_los( c);
441
   check_los( c);
442
}
442
}
443
 
443
 
444
void insert_sample( struct CHAN *c, double f)
444
void insert_sample( struct CHAN *c, double f)
445
{
445
{
446
   c->sum_sq += f * f;
446
   c->sum_sq += f * f;
447
   if( f > c->peak) c->peak = f;
447
   if( f > c->peak) c->peak = f;
448
   if( f < -c->peak) c->peak = -f;
448
   if( f < -c->peak) c->peak = -f;
449
 
449
 
450
   c->fft_inbuf[ grab_cnt] = f * sin( grab_cnt/(double) FFTWID * M_PI);
450
   c->fft_inbuf[ grab_cnt] = f * sin( grab_cnt/(double) FFTWID * M_PI);
451
}
451
}
452
 
452
 
453
void maybe_do_fft( void)
453
void maybe_do_fft( void)
454
{
454
{
455
   if( ++grab_cnt < FFTWID) return;
455
   if( ++grab_cnt < FFTWID) return;
456
   grab_cnt = 0;
456
   grab_cnt = 0;
457
 
457
 
458
   process_fft( &left);
458
   process_fft( &left);
459
   if( mode == 2) process_fft( &right);
459
   if( mode == 2) process_fft( &right);
460
 
460
 
461
   output_records();
461
   output_records();
462
   maybe_output_spectrum(); 
462
   maybe_output_spectrum(); 
463
}
463
}
464
 
464
 
465
//
465
//
466
// Main signal processing loop.  Never returns.
466
// Main signal processing loop.  Never returns.
467
//
467
//
468
 
468
 
469
void process_signal( void)
469
void process_signal( void)
470
{
470
{
471
   unsigned char buff[ NREAD];
471
   unsigned char buff[ NREAD];
472
 
472
 
473
   while( 1) 
473
   while( 1) 
474
   {
474
   {
475
      int i, q;
475
      int i, q;
476
 
476
 
477
      if( (q=read( fdi, buff, NREAD)) <= 0) 
477
      if( (q=read( fdi, buff, NREAD)) <= 0) 
478
      {
478
      {
479
         if( !q || errno == ENOENT || errno == 0) 
479
         if( !q || errno == ENOENT || errno == 0) 
480
         {  
480
         {  
481
            sched_yield();
481
            sched_yield();
482
            usleep( 50000); 
482
            usleep( 50000); 
483
            continue;
483
            continue;
484
         }
484
         }
485
 
485
 
486
         report( 0, "input file: read error, count=%d errno=%d", q, errno);
486
         report( 0, "input file: read error, count=%d errno=%d", q, errno);
487
         exit( 1);
487
         exit( 1);
488
      }
488
      }
489
 
489
 
490
      //  Unpack the input buffer into signed 16 bit words.
490
      //  Unpack the input buffer into signed 16 bit words.
491
      //  then scale to -1..+1 for further processing.
491
      //  then scale to -1..+1 for further processing.
492
      //  We use 'chans' to decide if the soundcard is giving stereo or
492
      //  We use 'chans' to decide if the soundcard is giving stereo or
493
      //  mono samples, rather than 'mode', because some cards will refuse
493
      //  mono samples, rather than 'mode', because some cards will refuse
494
      //  to do mono.  
494
      //  to do mono.  
495
      if( bits == 16)
495
      if( bits == 16)
496
      {
496
      {
497
         if( chans == 1)
497
         if( chans == 1)
498
         {
498
         {
499
            for( i=0; i<q; i += 2)
499
            for( i=0; i<q; i += 2)
500
            {
500
            {
501
               int fh = *(short *)(buff + i);
501
               int fh = *(short *)(buff + i);
502
   
502
   
503
               insert_sample( &left, fh/32768.0);
503
               insert_sample( &left, fh/32768.0);
504
               maybe_do_fft();
504
               maybe_do_fft();
505
            }
505
            }
506
         }
506
         }
507
         else  // chans must be 2
507
         else  // chans must be 2
508
         {
508
         {
509
            if( mode == 1)
509
            if( mode == 1)
510
               for( i=0; i<q; i += 4)
510
               for( i=0; i<q; i += 4)
511
               {
511
               {
512
                  int fh = *(short *)(buff + i);
512
                  int fh = *(short *)(buff + i);
513
                  insert_sample( &left, fh/32768.0);
513
                  insert_sample( &left, fh/32768.0);
514
                  maybe_do_fft();
514
                  maybe_do_fft();
515
               }
515
               }
516
            else  // mode == 2
516
            else  // mode == 2
517
               for( i=0; i<q; i += 4)
517
               for( i=0; i<q; i += 4)
518
               {
518
               {
519
                  int fh = *(short *)(buff + i);
519
                  int fh = *(short *)(buff + i);
520
                  insert_sample( &left, fh/32768.0);
520
                  insert_sample( &left, fh/32768.0);
521
   
521
   
522
                  fh = *(short *)(buff + i + 2);
522
                  fh = *(short *)(buff + i + 2);
523
                  insert_sample( &right, fh/32768.0);
523
                  insert_sample( &right, fh/32768.0);
524
                  maybe_do_fft();
524
                  maybe_do_fft();
525
               }
525
               }
526
         }
526
         }
527
      }
527
      }
528
      else   // bits must be 8
528
      else   // bits must be 8
529
      {
529
      {
530
         if( chans == 1)
530
         if( chans == 1)
531
         {
531
         {
532
            for( i=0; i<q; i++)
532
            for( i=0; i<q; i++)
533
            {
533
            {
534
               int fh = ((short)buff[i] - 128)*256;
534
               int fh = ((short)buff[i] - 128)*256;
535
               insert_sample( &left, fh/32768.0);
535
               insert_sample( &left, fh/32768.0);
536
               maybe_do_fft();
536
               maybe_do_fft();
537
            }
537
            }
538
         }
538
         }
539
         else  // chans must be 2
539
         else  // chans must be 2
540
         {
540
         {
541
            if( mode == 1)
541
            if( mode == 1)
542
               for( i=0; i<q; i += 2)
542
               for( i=0; i<q; i += 2)
543
               {
543
               {
544
                  int fh = ((short)buff[i] - 128)*256;
544
                  int fh = ((short)buff[i] - 128)*256;
545
                  insert_sample( &left, fh/32768.0);
545
                  insert_sample( &left, fh/32768.0);
546
                  maybe_do_fft();
546
                  maybe_do_fft();
547
               }
547
               }
548
            else  // mode == 2
548
            else  // mode == 2
549
               for( i=0; i<q; i += 2)
549
               for( i=0; i<q; i += 2)
550
               {
550
               {
551
                  int fh = ((short)buff[i] - 128)*256;
551
                  int fh = ((short)buff[i] - 128)*256;
552
                  insert_sample( &left, fh/32768.0);
552
                  insert_sample( &left, fh/32768.0);
553
   
553
   
554
                  fh = ((short)buff[i+1] - 128)*256;
554
                  fh = ((short)buff[i+1] - 128)*256;
555
                  insert_sample( &right, fh/32768.0);
555
                  insert_sample( &right, fh/32768.0);
556
                  maybe_do_fft();
556
                  maybe_do_fft();
557
               }
557
               }
558
         }
558
         }
559
      }
559
      }
560
   }
560
   }
561
}
561
}
562
 
562
 
563
///////////////////////////////////////////////////////////////////////////////
563
///////////////////////////////////////////////////////////////////////////////
564
//  Configuration File Stuff                                                 //
564
//  Configuration File Stuff                                                 //
565
///////////////////////////////////////////////////////////////////////////////
565
///////////////////////////////////////////////////////////////////////////////
566
 
566
 
567
void config_band( char *ident, char *start, char *end)
567
void config_band( char *ident, char *start, char *end)
568
{
568
{
569
   struct BAND *b = bands + nbands++;
569
   struct BAND *b = bands + nbands++;
570
 
570
 
571
   if( nbands == MAXBANDS) bailout( "too many bands specified in config file");
571
   if( nbands == MAXBANDS) bailout( "too many bands specified in config file");
572
 
572
 
573
   strcpy( b->ident, ident);
573
   strcpy( b->ident, ident);
574
   b->start = atoi( start);
574
   b->start = atoi( start);
575
   b->end = atoi( end);
575
   b->end = atoi( end);
576
 
576
 
577
   report( 1, "band %s %d %d", b->ident, b->start, b->end);
577
   report( 1, "band %s %d %d", b->ident, b->start, b->end);
578
}
578
}
579
 
579
 
580
void load_config( void)
580
void load_config( void)
581
{
581
{
582
   int lino = 0, nf;
582
   int lino = 0, nf;
583
   FILE *f;
583
   FILE *f;
584
   char buff[100], *p, *fields[20];
584
   char buff[100], *p, *fields[20];
585
 
585
 
586
   if( (f=fopen( CONFIG_FILE, "r")) == NULL)
586
   if( (f=fopen( CONFIG_FILE, "r")) == NULL)
587
      bailout( "no config file found");
587
      bailout( "no config file found");
588
 
588
 
589
   while( fgets( buff, 99, f))
589
   while( fgets( buff, 99, f))
590
   {
590
   {
591
      lino++;
591
      lino++;
592
 
592
 
593
      if( (p=strchr( buff, '\r')) != NULL) *p = 0;
593
      if( (p=strchr( buff, '\r')) != NULL) *p = 0;
594
      if( (p=strchr( buff, '\n')) != NULL) *p = 0;
594
      if( (p=strchr( buff, '\n')) != NULL) *p = 0;
595
      if( (p=strchr( buff, ';')) != NULL) *p = 0;
595
      if( (p=strchr( buff, ';')) != NULL) *p = 0;
596
 
596
 
597
      p = buff;  nf = 0;
597
      p = buff;  nf = 0;
598
      while( 1)
598
      while( 1)
599
      {
599
      {
600
         while( *p && isspace( *p)) p++;
600
         while( *p && isspace( *p)) p++;
601
         if( !*p) break;
601
         if( !*p) break;
602
         fields[nf++] = p;
602
         fields[nf++] = p;
603
         while( *p && !isspace( *p)) p++;
603
         while( *p && !isspace( *p)) p++;
604
         if( *p) *p++ = 0;
604
         if( *p) *p++ = 0;
605
      }
605
      }
606
      if( !nf) continue;
606
      if( !nf) continue;
607
 
607
 
608
      if( nf == 4 && !strcasecmp( fields[0], "band")) 
608
      if( nf == 4 && !strcasecmp( fields[0], "band")) 
609
         config_band( fields[1], fields[2], fields[3]);
609
         config_band( fields[1], fields[2], fields[3]);
610
      else
610
      else
611
      if( nf == 2 && !strcasecmp( fields[0], "logfile"))
611
      if( nf == 2 && !strcasecmp( fields[0], "logfile"))
612
      {
612
      {
613
         strcpy( logfile, fields[1]);
613
         strcpy( logfile, fields[1]);
614
         report( 1, "logfile %s", logfile);
614
         report( 1, "logfile %s", logfile);
615
      }
615
      }
616
      else
616
      else
617
      if( nf == 3 && !strcasecmp( fields[0], "los"))
617
      if( nf == 3 && !strcasecmp( fields[0], "los"))
618
      {
618
      {
619
         los_thresh = atof( fields[1]);
619
         los_thresh = atof( fields[1]);
620
         los_timeout = atoi( fields[2]);
620
         los_timeout = atoi( fields[2]);
621
         report( 1, "los threshold %.3f, timeout %d seconds", 
621
         report( 1, "los threshold %.3f, timeout %d seconds", 
622
                    los_thresh, los_timeout);
622
                    los_thresh, los_timeout);
623
      }
623
      }
624
      else
624
      else
625
      if( nf == 2 && !strcasecmp( fields[0], "device"))
625
      if( nf == 2 && !strcasecmp( fields[0], "device"))
626
         strcpy( device, fields[1]);
626
         strcpy( device, fields[1]);
627
      else
627
      else
628
      if( nf == 2 && !strcasecmp( fields[0], "mixer"))
628
      if( nf == 2 && !strcasecmp( fields[0], "mixer"))
629
         strcpy( mixer, fields[1]);
629
         strcpy( mixer, fields[1]);
630
      else
630
      else
631
      if( nf == 2 && !strcasecmp( fields[0], "mode"))
631
      if( nf == 2 && !strcasecmp( fields[0], "mode"))
632
      {
632
      {
633
         if( !strcasecmp( fields[1], "mono")) mode = 1;
633
         if( !strcasecmp( fields[1], "mono")) mode = 1;
634
         else
634
         else
635
         if( !strcasecmp( fields[1], "stereo")) mode = 2;
635
         if( !strcasecmp( fields[1], "stereo")) mode = 2;
636
         else
636
         else
637
            bailout( "error in config file, line %d", lino);
637
            bailout( "error in config file, line %d", lino);
638
      }
638
      }
639
      else
639
      else
640
      if( nf == 2 && !strcasecmp( fields[0], "bits"))
640
      if( nf == 2 && !strcasecmp( fields[0], "bits"))
641
      {
641
      {
642
         bits = atoi( fields[1]);
642
         bits = atoi( fields[1]);
643
         if( bits != 8 && bits != 16)
643
         if( bits != 8 && bits != 16)
644
            bailout( "can only do 8 or 16 bits, config file line %d", lino);
644
            bailout( "can only do 8 or 16 bits, config file line %d", lino);
645
      }
645
      }
646
      else
646
      else
647
      if( nf == 3 && !strcasecmp( fields[0], "spectrum"))
647
      if( nf == 3 && !strcasecmp( fields[0], "spectrum"))
648
      {
648
      {
649
         strcpy( spectrum_file, fields[1]);
649
         strcpy( spectrum_file, fields[1]);
650
         spec_max = atoi( fields[2]);
650
         spec_max = atoi( fields[2]);
651
      }
651
      }
652
      else
652
      else
653
      if( nf == 2 && !strcasecmp( fields[0], "sched")
653
      if( nf == 2 && !strcasecmp( fields[0], "sched")
654
                  && !strcasecmp( fields[1], "high"))
654
                  && !strcasecmp( fields[1], "high"))
655
      {
655
      {
656
         priority = 1;
656
         priority = 1;
657
      }
657
      }
658
      else
658
      else
659
      if( nf == 4 && !strcasecmp( fields[0], "gain"))
659
      if( nf == 4 && !strcasecmp( fields[0], "gain"))
660
      {
660
      {
661
         int left = atoi( fields[2]);
661
         int left = atoi( fields[2]);
662
         int right = atoi( fields[3]);
662
         int right = atoi( fields[3]);
663
         int gain = (right << 8) | left;
663
         int gain = (right << 8) | left;
664
 
664
 
665
         if( !strcasecmp( fields[1], "line")) req_lgain = gain;
665
         if( !strcasecmp( fields[1], "line")) req_lgain = gain;
666
         else
666
         else
667
         if( !strcasecmp( fields[1], "overall")) req_igain = gain;
667
         if( !strcasecmp( fields[1], "overall")) req_igain = gain;
668
         else
668
         else
669
         if( !strcasecmp( fields[1], "record")) req_rgain = gain;
669
         if( !strcasecmp( fields[1], "record")) req_rgain = gain;
670
         else
670
         else
671
            bailout( "unknown gain control [%s]", fields[1]);
671
            bailout( "unknown gain control [%s]", fields[1]);
672
      }
672
      }
673
      else
673
      else
674
      if( nf == 2 && !strcasecmp( fields[0], "rate"))
674
      if( nf == 2 && !strcasecmp( fields[0], "rate"))
675
         sample_rate = atoi( fields[1]);
675
         sample_rate = atoi( fields[1]);
676
      else
676
      else
677
      if( nf == 2 && !strcasecmp( fields[0], "bins"))
677
      if( nf == 2 && !strcasecmp( fields[0], "bins"))
678
         BINS = atoi( fields[1]);
678
         BINS = atoi( fields[1]);
679
      else
679
      else
680
      if( nf == 2 && !strcasecmp( fields[0], "datadir"))
680
      if( nf == 2 && !strcasecmp( fields[0], "datadir"))
681
      {
681
      {
682
         struct stat st;
682
         struct stat st;
683
         strcpy( datadir, fields[1]);
683
         strcpy( datadir, fields[1]);
684
         if( stat( datadir, &st) < 0 || !S_ISDIR( st.st_mode))
684
         if( stat( datadir, &st) < 0 || !S_ISDIR( st.st_mode))
685
            bailout( "no data directory, %s", datadir);
685
            bailout( "no data directory, %s", datadir);
686
      }
686
      }
687
      else
687
      else
688
         bailout( "error in config file, line %d", lino);
688
         bailout( "error in config file, line %d", lino);
689
   }
689
   }
690
 
690
 
691
   fclose( f);
691
   fclose( f);
692
}
692
}
693
 
693
 
694
///////////////////////////////////////////////////////////////////////////////
694
///////////////////////////////////////////////////////////////////////////////
695
//  Mixer Stuff                                                              //
695
//  Mixer Stuff                                                              //
696
///////////////////////////////////////////////////////////////////////////////
696
///////////////////////////////////////////////////////////////////////////////
697
 
697
 
698
// Actual mixer values, read by open_mixer()
698
// Actual mixer values, read by open_mixer()
699
int mixer_recmask;      // Recording device mask
699
int mixer_recmask;      // Recording device mask
700
int mixer_stereo;       // Stereo device mask
700
int mixer_stereo;       // Stereo device mask
701
int mixer_line;         // Line input gain setting
701
int mixer_line;         // Line input gain setting
702
int mixer_igain;        // Overall input gain setting
702
int mixer_igain;        // Overall input gain setting
703
int mixer_reclev;       // Recording level setting
703
int mixer_reclev;       // Recording level setting
704
int mixer_recsrc;       // Mask indicating which inputs are set to record
704
int mixer_recsrc;       // Mask indicating which inputs are set to record
705
 
705
 
706
void open_mixer( void)
706
void open_mixer( void)
707
{
707
{
708
   if( (fdm = open( mixer, O_RDWR)) < 0)
708
   if( (fdm = open( mixer, O_RDWR)) < 0)
709
      bailout( "cannot open [%s]: %s", mixer, strerror( errno));
709
      bailout( "cannot open [%s]: %s", mixer, strerror( errno));
710
 
710
 
711
   // Determine the available mixer recording gain controls.
711
   // Determine the available mixer recording gain controls.
712
   // We must at least have a line input.
712
   // We must at least have a line input.
713
 
713
 
714
   if( ioctl( fdm, SOUND_MIXER_READ_RECMASK, &mixer_recmask) < 0)
714
   if( ioctl( fdm, SOUND_MIXER_READ_RECMASK, &mixer_recmask) < 0)
715
      bailout( "cannot read mixer devmask");
715
      bailout( "cannot read mixer devmask");
716
 
716
 
717
   if( (mixer_recmask & SOUND_MASK_LINE) == 0)
717
   if( (mixer_recmask & SOUND_MASK_LINE) == 0)
718
      bailout( "mixer has no line device");
718
      bailout( "mixer has no line device");
719
 
719
 
720
   if( ioctl( fdm, SOUND_MIXER_READ_STEREODEVS, &mixer_stereo) < 0)
720
   if( ioctl( fdm, SOUND_MIXER_READ_STEREODEVS, &mixer_stereo) < 0)
721
      bailout( "cannot read mixer stereodevs");
721
      bailout( "cannot read mixer stereodevs");
722
 
722
 
723
   if( ioctl( fdm, SOUND_MIXER_READ_RECSRC, &mixer_recsrc) < 0)
723
   if( ioctl( fdm, SOUND_MIXER_READ_RECSRC, &mixer_recsrc) < 0)
724
      bailout( "cannot read mixer recsrc");
724
      bailout( "cannot read mixer recsrc");
725
 
725
 
726
   // Read the line input gain.  
726
   // Read the line input gain.  
727
   if( ioctl( fdm, SOUND_MIXER_READ_LINE, &mixer_line) < 0)
727
   if( ioctl( fdm, SOUND_MIXER_READ_LINE, &mixer_line) < 0)
728
      bailout( "cannot read mixer line");
728
      bailout( "cannot read mixer line");
729
 
729
 
730
   // Read overall input gain?  Optional.
730
   // Read overall input gain?  Optional.
731
   if( (mixer_recmask & SOUND_MASK_IGAIN) &&
731
   if( (mixer_recmask & SOUND_MASK_IGAIN) &&
732
        ioctl( fdm, SOUND_MIXER_READ_IGAIN, &mixer_igain) < 0)
732
        ioctl( fdm, SOUND_MIXER_READ_IGAIN, &mixer_igain) < 0)
733
           bailout( "cannot read mixer igain");
733
           bailout( "cannot read mixer igain");
734
 
734
 
735
   // Read overall recording level?  Optional.
735
   // Read overall recording level?  Optional.
736
   if( (mixer_recmask & SOUND_MASK_RECLEV) &&
736
   if( (mixer_recmask & SOUND_MASK_RECLEV) &&
737
        ioctl( fdm, SOUND_MIXER_READ_RECLEV, &mixer_reclev) < 0)
737
        ioctl( fdm, SOUND_MIXER_READ_RECLEV, &mixer_reclev) < 0)
738
           bailout( "cannot read mixer reclev");
738
           bailout( "cannot read mixer reclev");
739
}
739
}
740
 
740
 
741
void report_mixer_settings( void)
741
void report_mixer_settings( void)
742
{
742
{
743
   report( 1, "mixer: line input is %s", 
743
   report( 1, "mixer: line input is %s", 
744
     mixer_stereo & SOUND_MASK_LINE ? "stereo" : "mono");
744
     mixer_stereo & SOUND_MASK_LINE ? "stereo" : "mono");
745
 
745
 
746
   report( 1, "mixer: line input is %s", 
746
   report( 1, "mixer: line input is %s", 
747
     mixer_recsrc & SOUND_MASK_LINE ? "on" : "off");
747
     mixer_recsrc & SOUND_MASK_LINE ? "on" : "off");
748
 
748
 
749
   report( 1, "mixer: line input gain: left=%d right=%d", 
749
   report( 1, "mixer: line input gain: left=%d right=%d", 
750
             mixer_line & 0xff, (mixer_line >> 8) & 0xff);
750
             mixer_line & 0xff, (mixer_line >> 8) & 0xff);
751
 
751
 
752
   // Overall input gain?  Optional.
752
   // Overall input gain?  Optional.
753
   if( mixer_recmask & SOUND_MASK_IGAIN)
753
   if( mixer_recmask & SOUND_MASK_IGAIN)
754
   {
754
   {
755
      report( 1, "mixer: igain: left=%d right=%d", 
755
      report( 1, "mixer: igain: left=%d right=%d", 
756
                 mixer_igain & 0xff, (mixer_igain >> 8) & 0xff);
756
                 mixer_igain & 0xff, (mixer_igain >> 8) & 0xff);
757
   }
757
   }
758
   else report( 1, "mixer: igain: n/a");
758
   else report( 1, "mixer: igain: n/a");
759
 
759
 
760
   // Overall recording level?  Optional.
760
   // Overall recording level?  Optional.
761
   if( mixer_recmask & SOUND_MASK_RECLEV)
761
   if( mixer_recmask & SOUND_MASK_RECLEV)
762
   {
762
   {
763
      report( 1, "mixer: reclev: left=%d right=%d", 
763
      report( 1, "mixer: reclev: left=%d right=%d", 
764
                mixer_reclev & 0xff, (mixer_reclev >> 8) & 0xff);
764
                mixer_reclev & 0xff, (mixer_reclev >> 8) & 0xff);
765
   }
765
   }
766
   else report( 1, "mixer: reclev: n/a");
766
   else report( 1, "mixer: reclev: n/a");
767
 
767
 
768
}
768
}
769
 
769
 
770
void setup_mixer( void)
770
void setup_mixer( void)
771
{
771
{
772
   if( req_lgain >= 0)
772
   if( req_lgain >= 0)
773
   {
773
   {
774
      report( 1, "requesting line input gains left=%d right=%d",
774
      report( 1, "requesting line input gains left=%d right=%d",
775
             req_lgain & 0xff, (req_lgain >> 8) & 0xff);
775
             req_lgain & 0xff, (req_lgain >> 8) & 0xff);
776
 
776
 
777
      if( ioctl( fdm, SOUND_MIXER_WRITE_LINE, &req_lgain) < 0 ||
777
      if( ioctl( fdm, SOUND_MIXER_WRITE_LINE, &req_lgain) < 0 ||
778
          ioctl( fdm, SOUND_MIXER_READ_LINE, &mixer_line) < 0)
778
          ioctl( fdm, SOUND_MIXER_READ_LINE, &mixer_line) < 0)
779
         bailout( "error setting mixer line gain");
779
         bailout( "error setting mixer line gain");
780
 
780
 
781
      report( 1, "line input gains set to: left=%d right=%d", 
781
      report( 1, "line input gains set to: left=%d right=%d", 
782
             mixer_line & 0xff, (mixer_line >> 8) & 0xff);
782
             mixer_line & 0xff, (mixer_line >> 8) & 0xff);
783
   }
783
   }
784
 
784
 
785
   if( req_igain >= 0 &&
785
   if( req_igain >= 0 &&
786
       (mixer_recmask & SOUND_MASK_IGAIN))
786
       (mixer_recmask & SOUND_MASK_IGAIN))
787
   {
787
   {
788
      report( 1, "requesting overall input gains left=%d right=%d",
788
      report( 1, "requesting overall input gains left=%d right=%d",
789
             req_igain & 0xff, (req_igain >> 8) & 0xff);
789
             req_igain & 0xff, (req_igain >> 8) & 0xff);
790
 
790
 
791
      if( ioctl( fdm, SOUND_MIXER_WRITE_IGAIN, &req_igain) < 0 ||
791
      if( ioctl( fdm, SOUND_MIXER_WRITE_IGAIN, &req_igain) < 0 ||
792
          ioctl( fdm, SOUND_MIXER_READ_IGAIN, &mixer_igain) < 0)
792
          ioctl( fdm, SOUND_MIXER_READ_IGAIN, &mixer_igain) < 0)
793
         bailout( "error setting mixer overall input gain");
793
         bailout( "error setting mixer overall input gain");
794
 
794
 
795
      report( 1, "overall input gains set to: left=%d right=%d", 
795
      report( 1, "overall input gains set to: left=%d right=%d", 
796
                 mixer_igain & 0xff, (mixer_igain >> 8) & 0xff);
796
                 mixer_igain & 0xff, (mixer_igain >> 8) & 0xff);
797
   }
797
   }
798
 
798
 
799
   if( req_rgain >= 0 &&
799
   if( req_rgain >= 0 &&
800
       (mixer_recmask & SOUND_MASK_RECLEV))
800
       (mixer_recmask & SOUND_MASK_RECLEV))
801
   {
801
   {
802
      report( 1, "requesting overall record levels left=%d right=%d",
802
      report( 1, "requesting overall record levels left=%d right=%d",
803
             req_rgain & 0xff, (req_rgain >> 8) & 0xff);
803
             req_rgain & 0xff, (req_rgain >> 8) & 0xff);
804
 
804
 
805
      if( ioctl( fdm, SOUND_MIXER_WRITE_RECLEV, &req_rgain) < 0 ||
805
      if( ioctl( fdm, SOUND_MIXER_WRITE_RECLEV, &req_rgain) < 0 ||
806
          ioctl( fdm, SOUND_MIXER_READ_RECLEV, &mixer_reclev) < 0)
806
          ioctl( fdm, SOUND_MIXER_READ_RECLEV, &mixer_reclev) < 0)
807
         bailout( "error setting mixer record level");
807
         bailout( "error setting mixer record level");
808
 
808
 
809
      report( 1, "mixer record levels set to: left=%d right=%d", 
809
      report( 1, "mixer record levels set to: left=%d right=%d", 
810
                 mixer_reclev & 0xff, (mixer_reclev >> 8) & 0xff);
810
                 mixer_reclev & 0xff, (mixer_reclev >> 8) & 0xff);
811
   }
811
   }
812
 
812
 
813
   mixer_recsrc = SOUND_MASK_LINE;
813
   mixer_recsrc = SOUND_MASK_LINE;
814
   if( ioctl( fdm, SOUND_MIXER_WRITE_RECSRC, &mixer_recsrc) < 0)
814
   if( ioctl( fdm, SOUND_MIXER_WRITE_RECSRC, &mixer_recsrc) < 0)
815
      bailout( "cannot set mixer recsrc to line");
815
      bailout( "cannot set mixer recsrc to line");
816
}
816
}
817
 
817
 
818
///////////////////////////////////////////////////////////////////////////////
818
///////////////////////////////////////////////////////////////////////////////
819
//  Main                                                                     //
819
//  Main                                                                     //
820
///////////////////////////////////////////////////////////////////////////////
820
///////////////////////////////////////////////////////////////////////////////
821
 
821
 
822
void make_daemon( void)
822
void make_daemon( void)
823
{
823
{
824
   int childpid, fd;
824
   int childpid, fd;
825
 
825
 
826
   if( (childpid = fork()) < 0)
826
   if( (childpid = fork()) < 0)
827
      bailout( "cannot fork: %s", strerror( errno));
827
      bailout( "cannot fork: %s", strerror( errno));
828
   else if( childpid > 0) exit( 0);
828
   else if( childpid > 0) exit( 0);
829
 
829
 
830
   if( setpgrp() == -1) bailout( "cannot setpgrp");
830
   if( setpgrp() == -1) bailout( "cannot setpgrp");
831
 
831
 
832
   if( (childpid = fork()) < 0)
832
   if( (childpid = fork()) < 0)
833
      bailout( "cannot fork: %s", strerror( errno));
833
      bailout( "cannot fork: %s", strerror( errno));
834
   else if( childpid > 0) exit( 0);
834
   else if( childpid > 0) exit( 0);
835
 
835
 
836
   for( fd = 0; fd <NOFILE; fd++) if( fd != fdi) close( fd);
836
   for( fd = 0; fd <NOFILE; fd++) if( fd != fdi) close( fd);
837
   errno = 0;
837
   errno = 0;
838
   background = 2;
838
   background = 2;
839
}
839
}
840
 
840
 
841
void initialise_channel( struct CHAN *c)
841
void initialise_channel( struct CHAN *c)
842
{
842
{
843
   int i;
843
   int i;
844
 
844
 
845
   c->fft_inbuf = (double *) malloc( BINS * 2 * sizeof( double));
845
   c->fft_inbuf = (double *) malloc( BINS * 2 * sizeof( double));
846
   c->fft_data = fftw_malloc( sizeof( fftw_complex) * FFTWID);
846
   c->fft_data = fftw_malloc( sizeof( fftw_complex) * FFTWID);
847
   c->powspec = (double *) malloc( BINS * sizeof( double));
847
   c->powspec = (double *) malloc( BINS * sizeof( double));
848
   c->signal_avg = (double *) malloc( BINS * sizeof( double));
848
   c->signal_avg = (double *) malloc( BINS * sizeof( double));
849
   for( i=0; i<BINS; i++) c->signal_avg[i] = 0;
849
   for( i=0; i<BINS; i++) c->signal_avg[i] = 0;
850
}
850
}
851
 
851
 
852
void setup_signal_handling( void)
852
void setup_signal_handling( void)
853
{
853
{
854
   sa.sa_handler = handle_sigs;
854
   sa.sa_handler = handle_sigs;
855
   sigemptyset( &sa.sa_mask);
855
   sigemptyset( &sa.sa_mask);
856
   sa.sa_flags = 0;
856
   sa.sa_flags = 0;
857
   sigaction( SIGINT, &sa, NULL);
857
   sigaction( SIGINT, &sa, NULL);
858
   sigaction( SIGTERM, &sa, NULL);
858
   sigaction( SIGTERM, &sa, NULL);
859
   sigaction( SIGHUP, &sa, NULL);
859
   sigaction( SIGHUP, &sa, NULL);
860
   sigaction( SIGQUIT, &sa, NULL);
860
   sigaction( SIGQUIT, &sa, NULL);
861
   sigaction( SIGFPE, &sa, NULL);
861
   sigaction( SIGFPE, &sa, NULL);
862
   sigaction( SIGBUS, &sa, NULL);
862
   sigaction( SIGBUS, &sa, NULL);
863
   sigaction( SIGSEGV, &sa, NULL);
863
   sigaction( SIGSEGV, &sa, NULL);
864
}
864
}
865
 
865
 
866
// Set scheduling priority to the minimum SCHED_FIFO value.
866
// Set scheduling priority to the minimum SCHED_FIFO value.
867
void set_scheduling( void)
867
void set_scheduling( void)
868
{
868
{
869
   struct sched_param pa;
869
   struct sched_param pa;
870
   int min = sched_get_priority_min( SCHED_FIFO);
870
   int min = sched_get_priority_min( SCHED_FIFO);
871
 
871
 
872
   pa.sched_priority = min;
872
   pa.sched_priority = min;
873
   if( sched_setscheduler( 0, SCHED_FIFO, &pa) < 0)
873
   if( sched_setscheduler( 0, SCHED_FIFO, &pa) < 0)
874
      report( -1, "cannot set scheduling priority: %s", strerror( errno));
874
      report( -1, "cannot set scheduling priority: %s", strerror( errno));
875
   else
875
   else
876
      report( 0, "using SCHED_FIFO priority %d", min);
876
      report( 0, "using SCHED_FIFO priority %d", min);
877
}
877
}
878
 
878
 
879
int main( int argc, char *argv[])
879
int main( int argc, char *argv[])
880
{
880
{
881
   while( 1)
881
   while( 1)
882
   {
882
   {
883
      int c = getopt( argc, argv, "vfm");
883
      int c = getopt( argc, argv, "vfm");
884
 
884
 
885
      if( c == 'v') VFLAG++;
885
      if( c == 'v') VFLAG++;
886
      else
886
      else
887
      if( c == 'm') MFLAG++;
887
      if( c == 'm') MFLAG++;
888
      else
888
      else
889
      if( c == 'f') background = 0;
889
      if( c == 'f') background = 0;
890
      else if( c == -1) break;
890
      else if( c == -1) break;
891
      else bailout( "unknown option [%c]", c);
891
      else bailout( "unknown option [%c]", c);
892
   }
892
   }
893
 
893
 
894
   setup_signal_handling();
894
   setup_signal_handling();
895
   load_config();
895
   load_config();
896
   open_mixer();
896
   open_mixer();
897
 
897
 
898
   if( MFLAG)
898
   if( MFLAG)
899
   {
899
   {
900
      VFLAG = 1;
900
      VFLAG = 1;
901
      background = 0;
901
      background = 0;
902
      report_mixer_settings();
902
      report_mixer_settings();
903
      exit( 0);
903
      exit( 0);
904
   }
904
   }
905
 
905
 
906
   setup_mixer();
906
   setup_mixer();
907
 
907
 
908
   if( background && !logfile[0]) 
908
   if( background && !logfile[0]) 
909
      report( -1, "warning: no logfile specified for daemon");
909
      report( -1, "warning: no logfile specified for daemon");
910
 
910
 
911
   setup_input_stream();
911
   setup_input_stream();
912
   DF = (double) sample_rate/(double) FFTWID;
912
   DF = (double) sample_rate/(double) FFTWID;
913
 
913
 
914
   report( 1, "resolution: bins=%d fftwid=%d df=%f", BINS, FFTWID, DF);
914
   report( 1, "resolution: bins=%d fftwid=%d df=%f", BINS, FFTWID, DF);
915
   report( 1, "spectrum file: %s", spectrum_file); 
915
   report( 1, "spectrum file: %s", spectrum_file); 
916
 
916
 
917
   initialise_channel( &left);
917
   initialise_channel( &left);
918
   if( mode == 2) initialise_channel( &right);
918
   if( mode == 2) initialise_channel( &right);
919
 
919
 
920
   if( background) make_daemon();
920
   if( background) make_daemon();
921
   if( priority) set_scheduling();
921
   if( priority) set_scheduling();
922
 
922
 
923
   report( 0, "sidd version %s: starting work", VERSION);
923
   report( 0, "sidd version %s: starting work", VERSION);
924
   alert_on = 1;
924
   alert_on = 1;
925
   process_signal();
925
   process_signal();
926
   return 0;
926
   return 0;
927
}
927
}
928
 
928