Subversion Repositories svnkaklik

Rev

Rev 558 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 558 Rev 560
Line 20... Line 20...
20
static char *device = "plughw:0,0";			/* playback device */
20
static char *device = "plughw:0,0";			/* playback device */
21
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
21
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
22
static unsigned int rate = 98000;			/* stream rate */
22
static unsigned int rate = 98000;			/* stream rate */
23
static unsigned int buffer_time = 500000;		/* ring buffer length in us */
23
static unsigned int buffer_time = 500000;		/* ring buffer length in us */
24
static unsigned int period_time = 100000;		/* period time in us */
24
static unsigned int period_time = 100000;		/* period time in us */
25
static int verbose = 0;					/* verbose flag */
-
 
26
static int resample = 1;				/* enable alsa-lib resampling */
25
static int resample = 1;				/* enable alsa-lib resampling */
27
static int period_event = 0;				/* produce poll event after each period */
26
static int period_event = 0;				/* produce poll event after each period */
28
 
27
 
29
#define SOUND_SPEED	340
28
#define SOUND_SPEED	340
30
#define SIGNAL_SAMPLES 100000
29
#define SIGNAL_SAMPLES 100000
Line 188... Line 187...
188
// vygeneruje linearni chirp a vzorky ulozi do pole
187
// vygeneruje linearni chirp a vzorky ulozi do pole
189
unsigned int linear_windowed_chirp(unsigned int *pole, unsigned int delka_pole,unsigned int offset)
188
unsigned int linear_windowed_chirp(unsigned int *pole, unsigned int delka_pole,unsigned int offset)
190
{
189
{
191
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
190
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
192
 
191
 
193
static const float f0 = 1000;
192
static const float f0 = 1000;		//starting frequency
194
static const float fmax = 7000;
193
static const float fmax = 7000;		//ending frequency
195
static const float Tw = 0.002;
194
static const float Tw = 0.002;
196
static float k;
195
static float k;
197
 
196
 
198
unsigned int n=0;
197
unsigned int n=0;
199
double t;
198
double t;
Line 236... Line 235...
236
 
235
 
237
/////////// CALL BACK STUFF ///////////////////
236
/////////// CALL BACK STUFF ///////////////////
238
static void async_playback_callback(snd_async_handler_t *ahandler)
237
static void async_playback_callback(snd_async_handler_t *ahandler)
239
{
238
{
240
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
239
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
241
/*	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
-
 
242
	signed short *samples = data->samples;
-
 
243
	snd_pcm_channel_area_t *areas = data->areas;*/
-
 
244
	snd_pcm_sframes_t avail;
240
	snd_pcm_sframes_t avail;
245
	int err;
241
	int err;
246
	
242
	
247
	avail = snd_pcm_avail_update(handle);
243
	avail = snd_pcm_avail_update(handle);
248
	while ((avail >= period_size) && ((period*period_size) < chirp_size) ) {
244
	while ((avail >= period_size) && ((period*period_size) < chirp_size) ) {
Line 262... Line 258...
262
}
258
}
263
 
259
 
264
static void async_capture_callback(snd_async_handler_t *ahandler)
260
static void async_capture_callback(snd_async_handler_t *ahandler)
265
{
261
{
266
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
262
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
267
/*	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
-
 
268
	signed short *samples = data->samples;
-
 
269
	snd_pcm_channel_area_t *areas = data->areas;*/
-
 
270
	snd_pcm_sframes_t avail;
263
	snd_pcm_sframes_t avail;
271
	int err;
264
	int err;
272
	
265
	
273
	avail = snd_pcm_avail_update(handle);
266
	avail = snd_pcm_avail_update(handle);
274
	while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {  // segmentation fault checking disabled
267
	while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {  // segmentation fault checking disabled
Line 344... Line 337...
344
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
337
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
345
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
338
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
346
		exit(EXIT_FAILURE);
339
		exit(EXIT_FAILURE);
347
	}
340
	}
348
 
341
 
349
/// generate ping pattern
342
// generate ping pattern
350
 
343
 
351
        chirp_size=linear_windowed_chirp(chirp,1000000, CHIRP_OFFSET);
344
        chirp_size=linear_windowed_chirp(chirp,1000000, CHIRP_OFFSET);
352
 
345
 
353
/// register playback callback 
346
// register playback callback 
354
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
347
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
355
	if (err < 0) {
348
	if (err < 0) {
356
		printf("Unable to register async handler\n");
349
		printf("Unable to register async handler\n");
357
		exit(EXIT_FAILURE);
350
		exit(EXIT_FAILURE);
358
	}
351
	}
-
 
352
 
-
 
353
	if ((err = snd_pcm_prepare (playback_handle)) < 0) {
-
 
354
		fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
-
 
355
			 snd_strerror (err));
-
 
356
		exit (1);
-
 
357
	}
-
 
358
 
359
	for (period = 0; period < 2; period++) {
359
/*	for (period = 0; period < 2; period++) {
360
 
360
 
361
		err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
361
		err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
362
		if (err < 0) {
362
		if (err < 0) {
363
			printf("Initial write error: %s\n", snd_strerror(err));
363
			printf("Initial write error: %s\n", snd_strerror(err));
364
			exit(EXIT_FAILURE);
364
			exit(EXIT_FAILURE);
365
		}
365
		}
366
		if (err != period_size) {
366
		if (err != period_size) {
367
			printf("Initial write error: written %i expected %li\n", err, period_size);
367
			printf("Initial write error: written %i expected %li\n", err, period_size);
368
			exit(EXIT_FAILURE);
368
			exit(EXIT_FAILURE);
369
		}
369
		}
370
	}
370
	}*/
371
 
371
 
372
// register capture callback 
372
// register capture callback 
373
	err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
373
	err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
374
	if (err < 0) {
374
	if (err < 0) {
375
		printf("Unable to register async handler\n");
375
		printf("Unable to register async handler\n");
Line 435... Line 435...
435
	  delay[2] = n;
435
	  delay[2] = n;
436
	  r = correlationr[n];
436
	  r = correlationr[n];
437
	  }
437
	  }
438
	}
438
	}
439
 
439
 
440
  out=fopen("./output.txt","w");
440
  out=fopen("/tmp/sonar.txt","w");
441
  j=0;
441
  j=0;
442
  for(i=0;i<=100000;i++){
442
  for(i=0;i<=100000;i++){
443
    fprintf(out,"%6d %6d %6d %6d %9ld %9ld\n",i,chirp[i],L_signal[i],R_signal[i],correlationl[i], correlationr[i]);
443
    fprintf(out,"%6d %6d %6d %6d %9ld %9ld\n",i,chirp[i],L_signal[i],R_signal[i],correlationl[i], correlationr[i]);
444
    j+=2;
444
    j+=2;
445
  }
445
  }