Subversion Repositories svnkaklik

Rev

Rev 537 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 537 Rev 538
1
///////////////////////////////////////////////////////////////////////////////////
1
///////////////////////////////////////////////////////////////////////////////////
2
//                        This small demo of sonar.
2
//                        This small demo of sonar.
3
// Program allow distance measuring.
3
// Program allow distance measuring.
4
//
4
//
5
//
5
//
6
///////////////////////////////////////////////////////////////////////////////////
6
///////////////////////////////////////////////////////////////////////////////////
7
 
7
 
8
#include <stdio.h>
8
#include <stdio.h>
9
#include <stdlib.h>
9
#include <stdlib.h>
10
#include <string.h>
10
#include <string.h>
11
#include <sched.h>
11
#include <sched.h>
12
#include <errno.h>
12
#include <errno.h>
13
#include <getopt.h>
13
#include <getopt.h>
14
#include <alsa/asoundlib.h>
14
#include <alsa/asoundlib.h>
15
#include <sys/time.h>
15
#include <sys/time.h>
16
#include <math.h>
16
#include <math.h>
17
 
17
 
18
static char *device = "plughw:0,0";			/* playback device */
18
static char *device = "plughw:0,0";			/* playback device */
19
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
19
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
20
static unsigned int rate = 98000;			/* stream rate */
20
static unsigned int rate = 98000;			/* stream rate */
21
static unsigned int channels = 1;			/* count of channels */
21
static unsigned int channels = 1;			/* count of channels */
22
static unsigned int buffer_time = 500000;		/* ring buffer length in us */
22
static unsigned int buffer_time = 500000;		/* ring buffer length in us */
23
static unsigned int period_time = 100000;		/* period time in us */
23
static unsigned int period_time = 100000;		/* period time in us */
24
static int verbose = 0;					/* verbose flag */
24
static int verbose = 0;					/* verbose flag */
25
static int resample = 1;				/* enable alsa-lib resampling */
25
static int resample = 1;				/* enable alsa-lib resampling */
26
static int period_event = 0;				/* produce poll event after each period */
26
static int period_event = 0;				/* produce poll event after each period */
27
 
27
 
28
#define SOUND_SPEED	340
28
#define SOUND_SPEED	340
-
 
29
#define SIGNAL_SAMPLES 100000
29
 
30
 
30
unsigned int chirp_size;
31
unsigned int chirp_size;
31
 
32
 
32
int period=0;
33
int period=0;
33
int cperiod=0;
34
int cperiod=0;
34
int chirp[100000];
35
int chirp[100000];
35
short signal[1000000];		// record 6s of input samples
36
short signal[1000000];		// record 6s of input samples
36
 
37
 
37
static snd_pcm_sframes_t buffer_size;	// size of buffer at sound card
38
static snd_pcm_sframes_t buffer_size;	// size of buffer at sound card
38
static snd_pcm_sframes_t period_size;	//samples per frame
39
static snd_pcm_sframes_t period_size;	//samples per frame
39
static snd_output_t *output = NULL;
40
static snd_output_t *output = NULL;
40
 
41
 
41
static int set_hwparams(snd_pcm_t *handle,
42
static int set_hwparams(snd_pcm_t *handle,
42
			snd_pcm_hw_params_t *params,
43
			snd_pcm_hw_params_t *params,
43
			snd_pcm_access_t access)
44
			snd_pcm_access_t access)
44
{
45
{
45
	unsigned int rrate;
46
	unsigned int rrate;
46
	snd_pcm_uframes_t size;
47
	snd_pcm_uframes_t size;
47
	int err, dir;
48
	int err, dir;
48
 
49
 
49
	/* choose all parameters */
50
	/* choose all parameters */
50
	err = snd_pcm_hw_params_any(handle, params);
51
	err = snd_pcm_hw_params_any(handle, params);
51
	if (err < 0) {
52
	if (err < 0) {
52
		printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
53
		printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
53
		return err;
54
		return err;
54
	}
55
	}
55
	/* set hardware resampling */
56
	/* set hardware resampling */
56
	err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);
57
	err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);
57
	if (err < 0) {
58
	if (err < 0) {
58
		printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
59
		printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
59
		return err;
60
		return err;
60
	}
61
	}
61
	/* set the interleaved read/write format */
62
	/* set the interleaved read/write format */
62
	err = snd_pcm_hw_params_set_access(handle, params, access);
63
	err = snd_pcm_hw_params_set_access(handle, params, access);
63
	if (err < 0) {
64
	if (err < 0) {
64
		printf("Access type not available for playback: %s\n", snd_strerror(err));
65
		printf("Access type not available for playback: %s\n", snd_strerror(err));
65
		return err;
66
		return err;
66
	}
67
	}
67
	/* set the sample format */
68
	/* set the sample format */
68
	err = snd_pcm_hw_params_set_format(handle, params, format);
69
	err = snd_pcm_hw_params_set_format(handle, params, format);
69
	if (err < 0) {
70
	if (err < 0) {
70
		printf("Sample format not available for playback: %s\n", snd_strerror(err));
71
		printf("Sample format not available for playback: %s\n", snd_strerror(err));
71
		return err;
72
		return err;
72
	}
73
	}
73
	/* set the count of channels */
74
	/* set the count of channels */
74
	err = snd_pcm_hw_params_set_channels(handle, params, channels);
75
	err = snd_pcm_hw_params_set_channels(handle, params, channels);
75
	if (err < 0) {
76
	if (err < 0) {
76
		printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err));
77
		printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err));
77
		return err;
78
		return err;
78
	}
79
	}
79
	/* set the stream rate */
80
	/* set the stream rate */
80
	rrate = rate;
81
	rrate = rate;
81
	err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
82
	err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
82
	if (err < 0) {
83
	if (err < 0) {
83
		printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
84
		printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
84
		return err;
85
		return err;
85
	}
86
	}
86
	if (rrate != rate) {
87
	if (rrate != rate) {
87
		printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
88
		printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
88
		return -EINVAL;
89
		return -EINVAL;
89
	}
90
	}
90
	else printf("Rate set to %i Hz\n", rate, err); 
91
	else printf("Rate set to %i Hz\n", rate, err); 
91
	/* set the buffer time */
92
	/* set the buffer time */
92
	err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
93
	err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
93
	if (err < 0) {
94
	if (err < 0) {
94
		printf("Unable to set buffer time %i for playback: %s\n", buffer_time, snd_strerror(err));
95
		printf("Unable to set buffer time %i for playback: %s\n", buffer_time, snd_strerror(err));
95
		return err;
96
		return err;
96
	}
97
	}
97
	err = snd_pcm_hw_params_get_buffer_size(params, &size);
98
	err = snd_pcm_hw_params_get_buffer_size(params, &size);
98
	if (err < 0) {
99
	if (err < 0) {
99
		printf("Unable to get buffer size for playback: %s\n", snd_strerror(err));
100
		printf("Unable to get buffer size for playback: %s\n", snd_strerror(err));
100
		return err;
101
		return err;
101
	}
102
	}
102
	buffer_size = size;
103
	buffer_size = size;
103
	/* set the period time */
104
	/* set the period time */
104
	err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
105
	err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
105
	if (err < 0) {
106
	if (err < 0) {
106
		printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
107
		printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
107
		return err;
108
		return err;
108
	}
109
	}
109
	err = snd_pcm_hw_params_get_period_size(params, &size, &dir);
110
	err = snd_pcm_hw_params_get_period_size(params, &size, &dir);
110
	if (err < 0) {
111
	if (err < 0) {
111
		printf("Unable to get period size for playback: %s\n", snd_strerror(err));
112
		printf("Unable to get period size for playback: %s\n", snd_strerror(err));
112
		return err;
113
		return err;
113
	}
114
	}
114
	period_size = size;
115
	period_size = size;
115
	/* write the parameters to device */
116
	/* write the parameters to device */
116
	err = snd_pcm_hw_params(handle, params);
117
	err = snd_pcm_hw_params(handle, params);
117
	if (err < 0) {
118
	if (err < 0) {
118
		printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
119
		printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
119
		return err;
120
		return err;
120
	}
121
	}
121
	return 0;
122
	return 0;
122
}
123
}
123
 
124
 
124
static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams)
125
static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams)
125
{
126
{
126
	int err;
127
	int err;
127
 
128
 
128
	/* get the current swparams */
129
	/* get the current swparams */
129
	err = snd_pcm_sw_params_current(handle, swparams);
130
	err = snd_pcm_sw_params_current(handle, swparams);
130
	if (err < 0) {
131
	if (err < 0) {
131
		printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
132
		printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
132
		return err;
133
		return err;
133
	}
134
	}
134
	/* start the transfer when the buffer is almost full: */
135
	/* start the transfer when the buffer is almost full: */
135
	/* (buffer_size / avail_min) * avail_min */
136
	/* (buffer_size / avail_min) * avail_min */
136
	err = snd_pcm_sw_params_set_start_threshold(handle, swparams, (buffer_size / period_size) * period_size);
137
	err = snd_pcm_sw_params_set_start_threshold(handle, swparams, (buffer_size / period_size) * period_size);
137
	if (err < 0) {
138
	if (err < 0) {
138
		printf("Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
139
		printf("Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
139
		return err;
140
		return err;
140
	}
141
	}
141
	/* allow the transfer when at least period_size samples can be processed */
142
	/* allow the transfer when at least period_size samples can be processed */
142
	/* or disable this mechanism when period event is enabled (aka interrupt like style processing) */
143
	/* or disable this mechanism when period event is enabled (aka interrupt like style processing) */
143
	err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_event ? buffer_size : period_size);
144
	err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_event ? buffer_size : period_size);
144
	if (err < 0) {
145
	if (err < 0) {
145
		printf("Unable to set avail min for playback: %s\n", snd_strerror(err));
146
		printf("Unable to set avail min for playback: %s\n", snd_strerror(err));
146
		return err;
147
		return err;
147
	}
148
	}
148
	/* enable period events when requested */
149
	/* enable period events when requested */
149
	if (period_event) {
150
	if (period_event) {
150
		err = snd_pcm_sw_params_set_period_event(handle, swparams, 1);
151
		err = snd_pcm_sw_params_set_period_event(handle, swparams, 1);
151
		if (err < 0) {
152
		if (err < 0) {
152
			printf("Unable to set period event: %s\n", snd_strerror(err));
153
			printf("Unable to set period event: %s\n", snd_strerror(err));
153
			return err;
154
			return err;
154
		}
155
		}
155
	}
156
	}
156
	/* write the parameters to the playback device */
157
	/* write the parameters to the playback device */
157
	err = snd_pcm_sw_params(handle, swparams);
158
	err = snd_pcm_sw_params(handle, swparams);
158
	if (err < 0) {
159
	if (err < 0) {
159
		printf("Unable to set sw params for playback: %s\n", snd_strerror(err));
160
		printf("Unable to set sw params for playback: %s\n", snd_strerror(err));
160
		return err;
161
		return err;
161
	}
162
	}
162
	return 0;
163
	return 0;
163
}
164
}
164
 
165
 
165
struct async_private_data {
166
struct async_private_data {
166
	signed short *samples;
167
	signed short *samples;
167
	snd_pcm_channel_area_t *areas;
168
	snd_pcm_channel_area_t *areas;
168
	unsigned int period;
169
	unsigned int period;
169
};
170
};
170
 
171
 
171
/*int linear_chirp(int *pole, int delka_pole){  // vygeneruje linearni chirp a vzorky ulozi do pole
172
/*int linear_chirp(int *pole, int delka_pole){  // vygeneruje linearni chirp a vzorky ulozi do pole
172
 
173
 
173
static const float f0 = 0.0001;
174
static const float f0 = 0.0001;
174
static const float k = 0.00001;
175
static const float k = 0.00001;
175
 
176
 
176
int t;
177
int t;
177
 
178
 
178
//  if((spozdeni+delka) < delka_pole)
179
//  if((spozdeni+delka) < delka_pole)
179
    for(t=0;t < delka_pole;t++) pole[t] = round ( 10000*sin(2*M_PI*(t+faze)*(f0+(k/2)*(t+faze))) );
180
    for(t=0;t < delka_pole;t++) pole[t] = round ( 10000*sin(2*M_PI*(t+faze)*(f0+(k/2)*(t+faze))) );
180
    faze +=t;
181
    faze +=t;
181
//  else return 0;
182
//  else return 0;
182
 
183
 
183
}*/
184
}*/
184
 
185
 
185
// vygeneruje linearni chirp a vzorky ulozi do pole
186
// vygeneruje linearni chirp a vzorky ulozi do pole
186
unsigned int linear_windowed_chirp(unsigned int *pole, unsigned int delka_pole)
187
unsigned int linear_windowed_chirp(unsigned int *pole, unsigned int delka_pole)
187
{  
188
{  
188
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
189
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
189
 
190
 
190
static const float f0 = 8000;
191
static const float f0 = 3000;
191
static const float fmax = 10000;
192
static const float fmax = 5000;
192
static const float Tw = 0.002;
193
static const float Tw = 0.002;
193
static float k;
194
static float k;
194
 
195
 
195
unsigned int n=0;
196
unsigned int n=0;
196
double t;
197
double t;
197
unsigned int perioda;
198
unsigned int perioda;
198
 
199
 
199
  k=2*(fmax-f0)/Tw;
200
  k=2*(fmax-f0)/Tw;
200
  perioda = rate*Tw; 
201
  perioda = rate*Tw; 
201
 
202
 
202
  for(n=0;n<=perioda;n++){
203
  for(n=0;n<=perioda;n++){
203
     t = (double) n/ (double)rate;
204
     t = (double) n/ (double)rate;
204
     pole[n] = (short) round ( (0.35875 - 0.48829*cos(2*M_PI*t*1/Tw) + 0.14128*cos(2*M_PI*2*t*1/Tw) - 0.01168*cos(2*M_PI*3*t*1/Tw))*maxval*sin(2*M_PI*(t)*(f0+(k/2)*(t))) );
205
     pole[n] = (short) round ( (0.35875 - 0.48829*cos(2*M_PI*t*1/Tw) + 0.14128*cos(2*M_PI*2*t*1/Tw) - 0.01168*cos(2*M_PI*3*t*1/Tw))*maxval*sin(2*M_PI*(t)*(f0+(k/2)*(t))) );
205
  }
206
  }
206
  return perioda;
207
  return perioda;
207
}
208
}
208
 
209
 
209
// generate sine samples and store
210
// generate sine samples and store
210
int sine(unsigned int *pole, unsigned int delka_pole)
211
int sine(unsigned int *pole, unsigned int delka_pole)
211
{
212
{
212
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
213
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
213
unsigned int n;
214
unsigned int n;
214
double t;
215
double t;
215
 
216
 
216
  for(n=0;n < delka_pole;n++){
217
  for(n=0;n < delka_pole;n++){
217
    t = 440.0 * (double) n/ (double)rate;
218
    t = 440.0 * (double) n/ (double)rate;
218
    pole[n] = (short) round(maxval*sin(2*M_PI*t));
219
    pole[n] = (short) round(maxval*sin(2*M_PI*t));
219
  }
220
  }
220
}
221
}
221
 
222
 
222
 
223
 
223
 
224
 
224
static void async_playback_callback(snd_async_handler_t *ahandler)
225
static void async_playback_callback(snd_async_handler_t *ahandler)
225
{
226
{
226
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
227
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
227
/*	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
228
/*	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
228
	signed short *samples = data->samples;
229
	signed short *samples = data->samples;
229
	snd_pcm_channel_area_t *areas = data->areas;*/
230
	snd_pcm_channel_area_t *areas = data->areas;*/
230
	snd_pcm_sframes_t avail;
231
	snd_pcm_sframes_t avail;
231
	int err;
232
	int err;
232
	
233
	
233
	avail = snd_pcm_avail_update(handle);
234
	avail = snd_pcm_avail_update(handle);
234
	while ((avail >= period_size) && ((period*period_size) < chirp_size) ) {
235
	while ((avail >= period_size) && ((period*period_size) < chirp_size) ) {
235
 
236
 
236
		err = snd_pcm_writei(handle, (chirp+period*period_size), period_size);
237
		err = snd_pcm_writei(handle, (chirp+period*period_size), period_size);
237
		if (err < 0) {
238
		if (err < 0) {
238
			printf("Write error: %s\n", snd_strerror(err));
239
			printf("Write error: %s\n", snd_strerror(err));
239
			exit(EXIT_FAILURE);
240
			exit(EXIT_FAILURE);
240
		}
241
		}
241
		if (err != period_size) {
242
		if (err != period_size) {
242
			printf("Write error: written %i expected %li\n", err, period_size);
243
			printf("Write error: written %i expected %li\n", err, period_size);
243
			exit(EXIT_FAILURE);
244
			exit(EXIT_FAILURE);
244
		}
245
		}
245
		avail = snd_pcm_avail_update(handle);
246
		avail = snd_pcm_avail_update(handle);
246
		period++;
247
		period++;
247
	}
248
	}
248
}
249
}
249
 
250
 
250
static void async_capture_callback(snd_async_handler_t *ahandler)
251
static void async_capture_callback(snd_async_handler_t *ahandler)
251
{
252
{
252
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
253
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
253
/*	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
254
/*	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
254
	signed short *samples = data->samples;
255
	signed short *samples = data->samples;
255
	snd_pcm_channel_area_t *areas = data->areas;*/
256
	snd_pcm_channel_area_t *areas = data->areas;*/
256
	snd_pcm_sframes_t avail;
257
	snd_pcm_sframes_t avail;
257
	int err;
258
	int err;
258
	
259
	
259
	avail = snd_pcm_avail_update(handle);
260
	avail = snd_pcm_avail_update(handle);
260
	while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {  // segmentation fault checking disabled
261
	while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {  // segmentation fault checking disabled
261
 
262
 
262
		err = snd_pcm_readi(handle, (signal+cperiod*period_size), period_size);
263
		err = snd_pcm_readi(handle, (signal+cperiod*period_size), period_size);
263
		if (err < 0) {
264
		if (err < 0) {
264
			printf("Read error: %s\n", snd_strerror(err));
265
			printf("Read error: %s\n", snd_strerror(err));
265
			exit(EXIT_FAILURE);
266
			exit(EXIT_FAILURE);
266
		}
267
		}
267
		if (err != period_size) {
268
		if (err != period_size) {
268
			printf("Read error: red %i expected %li\n", err, period_size);
269
			printf("Read error: red %i expected %li\n", err, period_size);
269
			exit(EXIT_FAILURE);
270
			exit(EXIT_FAILURE);
270
		}
271
		}
271
		avail = snd_pcm_avail_update(handle);
272
		avail = snd_pcm_avail_update(handle);
272
		cperiod++;
273
		cperiod++;
273
	}
274
	}
274
}
275
}
275
 
276
 
276
 
277
 
277
int main(int argc, char *argv[])
278
int main(int argc, char *argv[])
278
{
279
{
279
	snd_pcm_t *playback_handle, *capture_handle;
280
	snd_pcm_t *playback_handle, *capture_handle;
280
	int err;
281
	int err;
281
	snd_pcm_hw_params_t *hwparams;
282
	snd_pcm_hw_params_t *hwparams;
282
	snd_pcm_sw_params_t *swparams;
283
	snd_pcm_sw_params_t *swparams;
283
	signed short *frame;  // pointer to array of samples
284
	signed short *frame;  // pointer to array of samples
284
	unsigned int chn;
285
	unsigned int chn;
285
	snd_pcm_channel_area_t *areas;
286
	snd_pcm_channel_area_t *areas;
286
 
287
 
287
	struct async_private_data data;
288
	struct async_private_data data;
288
	snd_async_handler_t *chandler, *phandler;
289
	snd_async_handler_t *chandler, *phandler;
289
	int count;
290
	int count;
290
	unsigned int i,j,m,n;
291
	unsigned int i,j,m,n;
291
	unsigned int delay[10];	//store delay of signifed correlation
292
	unsigned int delay[10];	//store delay of signifed correlation
292
  	double r;
293
  	double r;
293
	double correlation[1000000]; //array to store correlation curve
294
	double correlation[1000000]; //array to store correlation curve
294
 
295
 
295
	FILE *out;
296
	FILE *out;
296
 
297
 
297
	snd_pcm_hw_params_alloca(&hwparams);
298
	snd_pcm_hw_params_alloca(&hwparams);
298
	snd_pcm_sw_params_alloca(&swparams);
299
	snd_pcm_sw_params_alloca(&swparams);
299
 
300
 
300
 
301
 
301
//open and set playback device
302
//open and set playback device
302
	if ((err = snd_pcm_open(&playback_handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
303
	if ((err = snd_pcm_open(&playback_handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
303
		printf("Playback open error: %s\n", snd_strerror(err));
304
		printf("Playback open error: %s\n", snd_strerror(err));
304
		return 0;
305
		return 0;
305
	}
306
	}
306
	
307
	
307
	if ((err = set_hwparams(playback_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
308
	if ((err = set_hwparams(playback_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
308
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
309
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
309
		exit(EXIT_FAILURE);
310
		exit(EXIT_FAILURE);
310
	}
311
	}
311
	if ((err = set_swparams(playback_handle, swparams)) < 0) {
312
	if ((err = set_swparams(playback_handle, swparams)) < 0) {
312
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
313
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
313
		exit(EXIT_FAILURE);
314
		exit(EXIT_FAILURE);
314
	}
315
	}
315
 
316
 
316
//open and set capture device
317
//open and set capture device
317
	if ((err = snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
318
	if ((err = snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
318
		printf("Playback open error: %s\n", snd_strerror(err));
319
		printf("Playback open error: %s\n", snd_strerror(err));
319
		return 0;
320
		return 0;
320
	}
321
	}
321
	
322
	
322
	if ((err = set_hwparams(capture_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
323
	if ((err = set_hwparams(capture_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
323
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
324
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
324
		exit(EXIT_FAILURE);
325
		exit(EXIT_FAILURE);
325
	}
326
	}
326
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
327
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
327
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
328
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
328
		exit(EXIT_FAILURE);
329
		exit(EXIT_FAILURE);
329
	}
330
	}
330
 
331
 
331
        chirp_size=linear_windowed_chirp(chirp,1000000);
332
        chirp_size=linear_windowed_chirp(chirp,1000000);
332
 
333
 
333
/// register playback callback 
334
/// register playback callback 
334
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
335
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
335
	if (err < 0) {
336
	if (err < 0) {
336
		printf("Unable to register async handler\n");
337
		printf("Unable to register async handler\n");
337
		exit(EXIT_FAILURE);
338
		exit(EXIT_FAILURE);
338
	}
339
	}
339
	for (period = 0; period < 2; period++) {
340
	for (period = 0; period < 2; period++) {
340
 
341
 
341
		err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
342
		err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
342
		if (err < 0) {
343
		if (err < 0) {
343
			printf("Initial write error: %s\n", snd_strerror(err));
344
			printf("Initial write error: %s\n", snd_strerror(err));
344
			exit(EXIT_FAILURE);
345
			exit(EXIT_FAILURE);
345
		}
346
		}
346
		if (err != period_size) {
347
		if (err != period_size) {
347
			printf("Initial write error: written %i expected %li\n", err, period_size);
348
			printf("Initial write error: written %i expected %li\n", err, period_size);
348
			exit(EXIT_FAILURE);
349
			exit(EXIT_FAILURE);
349
		}
350
		}
350
	}
351
	}
351
 
352
 
352
// register capture callback 
353
// register capture callback 
353
	err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
354
	err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
354
	if (err < 0) {
355
	if (err < 0) {
355
		printf("Unable to register async handler\n");
356
		printf("Unable to register async handler\n");
356
		exit(EXIT_FAILURE);
357
		exit(EXIT_FAILURE);
357
	}
358
	}
358
 
359
 
359
//start playback
-
 
360
	if (snd_pcm_state(playback_handle) == SND_PCM_STATE_PREPARED) {
-
 
361
		err = snd_pcm_start(playback_handle);
-
 
362
		if (err < 0) {
-
 
363
			printf("Start error: %s\n", snd_strerror(err));
-
 
364
			exit(EXIT_FAILURE);
-
 
365
		}
-
 
366
	}
-
 
367
 
360
 
368
//start capture
361
//start capture
369
	if ((err = snd_pcm_prepare (capture_handle)) < 0) {
362
	if ((err = snd_pcm_prepare (capture_handle)) < 0) {
370
		fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
363
		fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
371
			 snd_strerror (err));
364
			 snd_strerror (err));
372
		exit (1);
365
		exit (1);
373
	}
366
	}
374
 
367
 
375
	err = snd_pcm_start(capture_handle);
368
	err = snd_pcm_start(capture_handle);
376
	if (err < 0) {
369
	if (err < 0) {
377
			printf("Start error: %s\n", snd_strerror(err));
370
			printf("Start error: %s\n", snd_strerror(err));
378
			exit(EXIT_FAILURE);
371
			exit(EXIT_FAILURE);
379
	}
372
	}
-
 
373
 
-
 
374
 
-
 
375
//start playback
-
 
376
	if (snd_pcm_state(playback_handle) == SND_PCM_STATE_PREPARED) {
-
 
377
		err = snd_pcm_start(playback_handle);
-
 
378
		if (err < 0) {
-
 
379
			printf("Start error: %s\n", snd_strerror(err));
-
 
380
			exit(EXIT_FAILURE);
-
 
381
		}
-
 
382
	}
-
 
383
 
380
	
384
	
381
//wait until all samples aren't transmitted
385
//wait until all samples aren't transmitted
382
	printf("Waiting for transmitt all samples\n");
386
	printf("Waiting for transmitt all samples\n");
383
	while(cperiod<10) {
387
	while(cperiod<10) {
384
		sleep(1);
388
		sleep(1);
385
		printf(".");
389
		printf(".");
386
	}	
390
	}	
387
	printf("\nData transmitted... \ncorrelating...\n");
391
	printf("\nData transmitted... \ncorrelating...\n");
388
 
392
 
389
#define SIGNAL_SAMPLES 100000
-
 
390
 
-
 
391
	for(n=0; n < (SIGNAL_SAMPLES - chirp_size);n++){			//spocita korelaci pro mozna spozdeni
393
	for(n=0; n < (SIGNAL_SAMPLES - chirp_size);n++){			//spocita korelaci pro mozna spozdeni
392
	  r=0;
394
	  r=0;
393
	  for(m=0;m < chirp_size;m++) r += chirp[m]*signal[m+n];
395
	  for(m=chirp_size;m > 0;m--) r += chirp[m]*signal[m+n];	// reverse echo patern
394
	  correlation[n]=r;
396
	  correlation[n]=abs(r);
395
	}
397
	}
396
 
398
 
397
	printf("\nFinding echos...\n");
399
	printf("\nFinding echos...\n");
398
	r=0;
400
	r=0;
399
	for(n=0; n < (SIGNAL_SAMPLES - chirp_size);n++){			//najde nejvetsi korelace
401
	for(n=0; n < (SIGNAL_SAMPLES - chirp_size);n++){			//najde nejvetsi korelace
400
	  if (r < correlation[n]){
402
	  if (r < correlation[n]){
401
	  delay[1] = n;
403
	  delay[1] = n;
402
	  r = correlation[n];
404
	  r = correlation[n];
403
	  }
405
	  }
404
	}
406
	}
405
 
407
 
406
	out=fopen("./output.txt","w");
408
	out=fopen("./output.txt","w");
407
	for(i=0;i<=100000;i++) fprintf(out,"%6d	%6d %6d %6.2f\n",i,chirp[i],signal[i],correlation[i]); 
409
	for(i=0;i<=100000;i++) fprintf(out,"%6d	%6d %6d %6.2f\n",i,chirp[i],signal[i],correlation[i]); 
408
	fclose(out);
410
	fclose(out);
409
 
411
 
410
	printf("\nEcho zacina na: %d vzorku.\n", delay[1]);
412
	printf("\nEcho zacina na: %d vzorku.\n", delay[1]);
411
	printf("Casove na: %f s\n", ((float)delay[1]/rate));
413
	printf("Casove na: %f s\n", ((float)delay[1]/rate));
412
	printf("vzdalenost: %f m\n", (SOUND_SPEED*(float)delay[1]/rate));
414
	printf("vzdalenost: %f m\n", (SOUND_SPEED*(float)delay[1]/rate));
413
 
415
 
414
	snd_pcm_close(playback_handle);
416
	snd_pcm_close(playback_handle);
415
	snd_pcm_close(capture_handle);
417
	snd_pcm_close(capture_handle);
416
	return 0;
418
	return 0;
417
}
419
}
418
 
420