Subversion Repositories svnkaklik

Rev

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

Rev 526 Rev 527
Line 21... Line 21...
21
static double freq = 440;				/* sinusoidal wave frequency in Hz */
21
static double freq = 440;				/* sinusoidal wave frequency in Hz */
22
static int verbose = 0;					/* verbose flag */
22
static int verbose = 0;					/* verbose flag */
23
static int resample = 1;				/* enable alsa-lib resampling */
23
static int resample = 1;				/* enable alsa-lib resampling */
24
static int period_event = 0;				/* produce poll event after each period */
24
static int period_event = 0;				/* produce poll event after each period */
25
 
25
 
-
 
26
#define  CHIRP_SIZE	1000000
-
 
27
 
26
int period=0;
28
int period=0;
-
 
29
int cperiod=0;
27
short sample[1000000];
30
short chirp[CHIRP_SIZE];
-
 
31
short signal[44100*6];		// record 6s of input samples
28
 
32
 
29
static snd_pcm_sframes_t buffer_size;	// size of buffer at sound card
33
static snd_pcm_sframes_t buffer_size;	// size of buffer at sound card
30
static snd_pcm_sframes_t period_size;	//samples per frame
34
static snd_pcm_sframes_t period_size;	//samples per frame
31
static snd_output_t *output = NULL;
35
static snd_output_t *output = NULL;
32
 
36
 
Line 218... Line 222...
218
//  if((spozdeni+delka) < delka_pole)
222
//  if((spozdeni+delka) < delka_pole)
219
    for(t=0;t < delka_pole;t++) pole[t] = round ( 10000*sin(2*M_PI*(t+faze)*(f0+(k/2)*(t+faze))) );
223
    for(t=0;t < delka_pole;t++) pole[t] = round ( 10000*sin(2*M_PI*(t+faze)*(f0+(k/2)*(t+faze))) );
220
    faze +=t;
224
    faze +=t;
221
//  else return 0;
225
//  else return 0;
222
 
226
 
-
 
227
}*/
-
 
228
 
223
}
229
 
224
 
230
 
225
int linear_windowed_chirp(int *pole, int delka_pole){  // vygeneruje linearni chirp a vzorky ulozi do pole
231
int linear_windowed_chirp(int *pole, int delka_pole){  // vygeneruje linearni chirp a vzorky ulozi do pole
226
 
232
 
227
static const float f0 = 0.0001;
-
 
228
static const float k = 0.00001;
233
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
229
 
234
 
-
 
235
static const float f0 = 1/1000;
-
 
236
static const float fmax = 1/10000;
230
int t;
237
static const float Tw = 10;
231
 
238
 
232
// (0.35875 - 0.48829 cos(t) + 0.14128 cos(2t) - 0.01168 cos(3t))
-
 
233
    for(t=0;t < delka_pole;t++) pole[t] = (short) round ( (0.35875 - 0.48829*cos((t+faze)*0.0001) + 0.14128*cos(.0002*(t+faze)) - 0.01168*cos(.0003*(t+faze)))*30000*sin(2*M_PI*(t+faze)*(f0+(k/2)*(t+faze))) );
-
 
234
    faze +=t;
239
static float k;
235
 
240
 
236
 
241
 
-
 
242
//k=2*(fmax-Tw*f0)/Tw*Tw;
-
 
243
k=1/10000;
-
 
244
 
-
 
245
int n;
-
 
246
double t;
-
 
247
 
-
 
248
   for(n=0;n < delka_pole;n++){
-
 
249
      t=n*1/rate;
-
 
250
      pole[n] = (short) round ( /*(0.35875 - 0.48829*cos((t)*0.0001) + 0.14128*cos(.0002*(t)) - 0.01168*cos(.0003*(t)))*/  maxval*sin(2*M_PI*(t)*(f0+(k/2)*(t))) );
-
 
251
   }
237
}*/
252
}
238
 
253
 
239
int sine(int *pole, int delka_pole)
254
int sine(int *pole, int delka_pole)
240
{
255
{
-
 
256
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
241
int t;
257
int t;
242
  for(t=0;t < delka_pole;t++) pole[t] = (short) round(10000*sin( (double)(t)/10.0));
258
  for(t=0;t < delka_pole;t++) pole[t] = (short) round(maxval*sin( (double)(t)/10.0));
243
}
259
}
244
 
260
 
245
 
261
 
246
 
262
 
247
static void async_playback_callback(snd_async_handler_t *ahandler)
263
static void async_playback_callback(snd_async_handler_t *ahandler)
248
{
264
{
249
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
265
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
250
	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
266
/*	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
251
	signed short *samples = data->samples;
267
	signed short *samples = data->samples;
252
	snd_pcm_channel_area_t *areas = data->areas;
268
	snd_pcm_channel_area_t *areas = data->areas;*/
253
	snd_pcm_sframes_t avail;
269
	snd_pcm_sframes_t avail;
254
	int err;
270
	int err;
255
	
271
	
256
	avail = snd_pcm_avail_update(handle);
272
	avail = snd_pcm_avail_update(handle);
257
	while (avail >= period_size) {
-
 
258
//		generate_sine(areas, 0, period_size, &data->phase);
273
	while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {
259
//	linear_windowed_chirp(signal,period_size);
-
 
260
 
274
 
261
		err = snd_pcm_writei(handle, sample, period_size);
275
		err = snd_pcm_writei(handle, (chirp+period*period_size), period_size);
262
		if (err < 0) {
276
		if (err < 0) {
263
			printf("Write error: %s\n", snd_strerror(err));
277
			printf("Write error: %s\n", snd_strerror(err));
264
			exit(EXIT_FAILURE);
278
			exit(EXIT_FAILURE);
265
		}
279
		}
266
		if (err != period_size) {
280
		if (err != period_size) {
267
			printf("Write error: written %i expected %li\n", err, period_size);
281
			printf("Write error: written %i expected %li\n", err, period_size);
268
			exit(EXIT_FAILURE);
282
			exit(EXIT_FAILURE);
269
		}
283
		}
270
		avail = snd_pcm_avail_update(handle);
284
		avail = snd_pcm_avail_update(handle);
-
 
285
		period++;
271
	}
286
	}
272
}
287
}
273
 
288
 
-
 
289
static void async_capture_callback(snd_async_handler_t *ahandler)
-
 
290
{
-
 
291
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
-
 
292
/*	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
-
 
293
	signed short *samples = data->samples;
-
 
294
	snd_pcm_channel_area_t *areas = data->areas;*/
-
 
295
	snd_pcm_sframes_t avail;
-
 
296
	int err;
-
 
297
	
-
 
298
	avail = snd_pcm_avail_update(handle);
-
 
299
//	while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {
-
 
300
 
-
 
301
		err = snd_pcm_readi(handle, (signal+cperiod*period_size), period_size);
-
 
302
		if (err < 0) {
-
 
303
			printf("Read error: %s\n", snd_strerror(err));
-
 
304
			exit(EXIT_FAILURE);
-
 
305
		}
-
 
306
		if (err != period_size) {
-
 
307
			printf("Read error: red %i expected %li\n", err, period_size);
-
 
308
			exit(EXIT_FAILURE);
-
 
309
		}
-
 
310
		avail = snd_pcm_avail_update(handle);
-
 
311
		cperiod++;
-
 
312
//	}
-
 
313
}
-
 
314
 
-
 
315
 
274
int main(int argc, char *argv[])
316
int main(int argc, char *argv[])
275
{
317
{
276
	snd_pcm_t *playback_handle, *capture_handle;
318
	snd_pcm_t *playback_handle, *capture_handle;
277
	int err;
319
	int err;
278
	snd_pcm_hw_params_t *hwparams;
320
	snd_pcm_hw_params_t *hwparams;
Line 280... Line 322...
280
	signed short *frame;  // pointer to array of samples
322
	signed short *frame;  // pointer to array of samples
281
	unsigned int chn;
323
	unsigned int chn;
282
	snd_pcm_channel_area_t *areas;
324
	snd_pcm_channel_area_t *areas;
283
 
325
 
284
	struct async_private_data data;
326
	struct async_private_data data;
285
	snd_async_handler_t *ahandler;
327
	snd_async_handler_t *chandler, *phandler;
286
	int count;
328
	int count;
287
	unsigned int i;
329
	unsigned int i,j;
-
 
330
 
-
 
331
	FILE *out;
288
 
332
 
289
 
333
 
290
	snd_pcm_hw_params_alloca(&hwparams);
334
	snd_pcm_hw_params_alloca(&hwparams);
291
	snd_pcm_sw_params_alloca(&swparams);
335
	snd_pcm_sw_params_alloca(&swparams);
292
 
336
 
Line 313... Line 357...
313
	
357
	
314
	if ((err = set_hwparams(capture_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
358
	if ((err = set_hwparams(capture_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
315
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
359
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
316
		exit(EXIT_FAILURE);
360
		exit(EXIT_FAILURE);
317
	}
361
	}
318
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
362
/*	if ((err = set_swparams(capture_handle, swparams)) < 0) {
319
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
363
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
320
		exit(EXIT_FAILURE);
364
		exit(EXIT_FAILURE);
321
	}
365
	}*/
322
 
366
 
323
// allocate memory for frame (package of samples)
367
// allocate memory for frame (package of samples)
324
	frame = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
368
	frame = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
325
	if (frame == NULL) {
369
	if (frame == NULL) {
326
		printf("No enough memory\n");
370
		printf("No enough memory\n");
327
		exit(EXIT_FAILURE);
371
		exit(EXIT_FAILURE);
328
	}
372
	}
329
 
373
 
-
 
374
 
-
 
375
// dummy structure
330
//allocate memory for frame structure definition	
376
//allocate memory for frame structure definition	
331
	areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
377
	areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
332
	if (areas == NULL) {
378
	if (areas == NULL) {
333
		printf("No enough memory\n");
379
		printf("No enough memory\n");
334
		exit(EXIT_FAILURE);
380
		exit(EXIT_FAILURE);
Line 338... Line 384...
338
		areas[chn].addr = frame; 	// frame start adress
384
		areas[chn].addr = frame; 	// frame start adress
339
		areas[chn].first = chn * snd_pcm_format_physical_width(format); // ofset to first sample (in bits)
385
		areas[chn].first = chn * snd_pcm_format_physical_width(format); // ofset to first sample (in bits)
340
		areas[chn].step = channels * snd_pcm_format_physical_width(format); // step between samples
386
		areas[chn].step = channels * snd_pcm_format_physical_width(format); // step between samples
341
	}
387
	}
342
 
388
 
343
        sine(sample,100000);
-
 
344
 
-
 
345
 
-
 
346
	data.samples = frame;
389
       	data.samples = frame;
347
	data.areas = areas;
390
	data.areas = areas;
348
	data.period = 1;
391
	data.period = 1;
-
 
392
// end of dummy structure 
349
 
393
 
350
 
394
 
-
 
395
 sine(chirp,100000);
-
 
396
 
351
// register playback callback
397
/// register playback callback 
352
	err = snd_async_add_pcm_handler(&ahandler, playback_handle, async_playback_callback, &data);
398
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
353
	if (err < 0) {
399
	if (err < 0) {
354
		printf("Unable to register async handler\n");
400
		printf("Unable to register async handler\n");
355
		exit(EXIT_FAILURE);
401
		exit(EXIT_FAILURE);
356
	}
402
	}
357
	for (data.period = 0; data.period < 2; data.period++) {
403
	for (period = 0; period < 3; period++) {
358
 
404
 
359
		err = snd_pcm_writei(playback_handle, sample, period_size);
405
		err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
360
		if (err < 0) {
406
		if (err < 0) {
361
			printf("Initial write error: %s\n", snd_strerror(err));
407
			printf("Initial write error: %s\n", snd_strerror(err));
362
			exit(EXIT_FAILURE);
408
			exit(EXIT_FAILURE);
363
		}
409
		}
364
		if (err != period_size) {
410
		if (err != period_size) {
365
			printf("Initial write error: written %i expected %li\n", err, period_size);
411
			printf("Initial write error: written %i expected %li\n", err, period_size);
366
			exit(EXIT_FAILURE);
412
			exit(EXIT_FAILURE);
367
		}
413
		}
368
	}
414
	}
369
 
415
 
-
 
416
// register capture callback 
-
 
417
	err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
-
 
418
	if (err < 0) {
-
 
419
		printf("Unable to register async handler\n");
-
 
420
		exit(EXIT_FAILURE);
-
 
421
	}
-
 
422
 
370
//start playback
423
//start playback
371
	if (snd_pcm_state(playback_handle) == SND_PCM_STATE_PREPARED) {
424
	if (snd_pcm_state(playback_handle) == SND_PCM_STATE_PREPARED) {
372
		err = snd_pcm_start(playback_handle);
425
		err = snd_pcm_start(playback_handle);
373
		if (err < 0) {
426
		if (err < 0) {
374
			printf("Start error: %s\n", snd_strerror(err));
427
			printf("Start error: %s\n", snd_strerror(err));
375
			exit(EXIT_FAILURE);
428
			exit(EXIT_FAILURE);
376
		}
429
		}
377
	}
430
	}
378
 
431
 
-
 
432
//start capture
-
 
433
	if ((err = snd_pcm_prepare (capture_handle)) < 0) {
-
 
434
		fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
-
 
435
			 snd_strerror (err));
-
 
436
		exit (1);
-
 
437
	}
-
 
438
 
-
 
439
	err = snd_pcm_start(capture_handle);
-
 
440
	if (err < 0) {
-
 
441
			printf("Start error: %s\n", snd_strerror(err));
-
 
442
			exit(EXIT_FAILURE);
-
 
443
	}
-
 
444
	
-
 
445
 
-
 
446
 
379
	/* because all other work is done in the signal handler,
447
	/* because all other work is done in the signal handler,
380
	   suspend the process */
448
	   suspend the process */
381
	for(i=0; i<=10;i++) {
449
	while(cperiod<10) {
382
		sleep(1);
450
		sleep(1);
383
	}
451
	}	
384
 
-
 
385
 
452
 
-
 
453
	out=fopen("./output.txt","w");
-
 
454
	for(i=0;i<=100000;i++) fprintf(out,"%d ",signal[i]); 
-
 
455
	fclose(out);	
386
 
456
 
387
	free(areas);
457
	free(areas);
388
	free(frame);
458
	free(frame);
-
 
459
 
389
	snd_pcm_close(playback_handle);
460
	snd_pcm_close(playback_handle);
390
	snd_pcm_close(capture_handle);
461
	snd_pcm_close(capture_handle);
391
	return 0;
462
	return 0;
392
}
463
}
393
 
464