Subversion Repositories svnkaklik

Rev

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

Rev 530 Rev 531
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
26
unsigned int chirp_size;
27
 
27
 
28
int period=0;
28
int period=0;
29
int cperiod=0;
29
int cperiod=0;
30
short chirp[CHIRP_SIZE];
30
int chirp[1000000];
31
short signal[44100*6];		// record 6s of input samples
31
short signal[44100*6];		// record 6s of input samples
32
 
32
 
33
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
34
static snd_pcm_sframes_t period_size;	//samples per frame
34
static snd_pcm_sframes_t period_size;	//samples per frame
35
static snd_output_t *output = NULL;
35
static snd_output_t *output = NULL;
Line 226... Line 226...
226
 
226
 
227
}*/
227
}*/
228
 
228
 
229
 
229
 
230
 
230
 
231
int linear_windowed_chirp(int *pole, int delka_pole){  // vygeneruje linearni chirp a vzorky ulozi do pole
231
unsigned int linear_windowed_chirp(unsigned int *pole, unsigned int delka_pole){  // vygeneruje linearni chirp a vzorky ulozi do pole
232
 
232
 
233
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
233
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
234
 
234
 
235
static const float f0 = 1/1000;
235
static const float f0 = 1000;
236
static const float fmax = 1/10000;
236
static const float fmax = 5000;
237
static const float Tw = 10;
237
static const float Tw = 0.2;
238
 
-
 
239
static float k;
238
static float k;
240
 
239
 
241
 
-
 
242
//k=2*(fmax-Tw*f0)/Tw*Tw;
-
 
243
k=1/10000;
-
 
244
 
-
 
245
int n;
240
unsigned int n=0;
246
double t;
241
double t;
-
 
242
unsigned int perioda;
247
 
243
 
-
 
244
k=2*(fmax-f0)/Tw;
-
 
245
perioda = rate*Tw; 
-
 
246
 
248
   for(n=0;n < delka_pole;n++){
247
   for(n=0;n<=perioda;n++){
249
      t=n*1/rate;
248
      t = (double) n/ (double)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))) );
249
      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))) );
251
   }
250
   }
-
 
251
   return perioda;
252
}
252
}
253
 
253
 
254
int sine(int *pole, int delka_pole)
254
int sine(unsigned int *pole, unsigned int delka_pole)
255
{
255
{
256
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
256
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
-
 
257
unsigned int n;
257
int t;
258
double t;
-
 
259
 
-
 
260
  for(n=0;n < delka_pole;n++){
-
 
261
    t = 440.0 * (double) n/ (double)rate;
258
  for(t=0;t < delka_pole;t++) pole[t] = (short) round(maxval*sin( (double)(t)/10.0));
262
    pole[n] = (short) round(maxval*sin(2*M_PI*t));
-
 
263
  }
259
}
264
}
260
 
265
 
261
 
266
 
262
 
267
 
263
static void async_playback_callback(snd_async_handler_t *ahandler)
268
static void async_playback_callback(snd_async_handler_t *ahandler)
Line 268... Line 273...
268
	snd_pcm_channel_area_t *areas = data->areas;*/
273
	snd_pcm_channel_area_t *areas = data->areas;*/
269
	snd_pcm_sframes_t avail;
274
	snd_pcm_sframes_t avail;
270
	int err;
275
	int err;
271
	
276
	
272
	avail = snd_pcm_avail_update(handle);
277
	avail = snd_pcm_avail_update(handle);
273
	while ((avail >= period_size) && ((period*period_size) < (CHIRP_SIZE-100)) ) {
278
	while ((avail >= period_size) && ((period*period_size) < chirp_size) ) {
274
 
279
 
275
		err = snd_pcm_writei(handle, (chirp+period*period_size), period_size);
280
		err = snd_pcm_writei(handle, (chirp+period*period_size), period_size);
276
		if (err < 0) {
281
		if (err < 0) {
277
			printf("Write error: %s\n", snd_strerror(err));
282
			printf("Write error: %s\n", snd_strerror(err));
278
			exit(EXIT_FAILURE);
283
			exit(EXIT_FAILURE);
Line 294... Line 299...
294
	snd_pcm_channel_area_t *areas = data->areas;*/
299
	snd_pcm_channel_area_t *areas = data->areas;*/
295
	snd_pcm_sframes_t avail;
300
	snd_pcm_sframes_t avail;
296
	int err;
301
	int err;
297
	
302
	
298
	avail = snd_pcm_avail_update(handle);
303
	avail = snd_pcm_avail_update(handle);
299
//	while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {
304
	while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {
300
 
305
 
301
		err = snd_pcm_readi(handle, (signal+cperiod*period_size), period_size);
306
		err = snd_pcm_readi(handle, (signal+cperiod*period_size), period_size);
302
		if (err < 0) {
307
		if (err < 0) {
303
			printf("Read error: %s\n", snd_strerror(err));
308
			printf("Read error: %s\n", snd_strerror(err));
304
			exit(EXIT_FAILURE);
309
			exit(EXIT_FAILURE);
Line 307... Line 312...
307
			printf("Read error: red %i expected %li\n", err, period_size);
312
			printf("Read error: red %i expected %li\n", err, period_size);
308
			exit(EXIT_FAILURE);
313
			exit(EXIT_FAILURE);
309
		}
314
		}
310
		avail = snd_pcm_avail_update(handle);
315
		avail = snd_pcm_avail_update(handle);
311
		cperiod++;
316
		cperiod++;
312
//	}
317
	}
313
}
318
}
314
 
319
 
315
 
320
 
316
int main(int argc, char *argv[])
321
int main(int argc, char *argv[])
317
{
322
{
Line 362... Line 367...
362
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
367
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
363
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
368
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
364
		exit(EXIT_FAILURE);
369
		exit(EXIT_FAILURE);
365
	}
370
	}
366
 
371
 
367
// allocate memory for frame (package of samples)
-
 
368
	frame = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
-
 
369
	if (frame == NULL) {
-
 
370
		printf("No enough memory\n");
-
 
371
		exit(EXIT_FAILURE);
-
 
372
	}
-
 
373
 
-
 
374
 
-
 
375
// dummy structure
-
 
376
//allocate memory for frame structure definition	
-
 
377
	areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
372
        chirp_size=linear_windowed_chirp(chirp,1000000);
378
	if (areas == NULL) {
-
 
379
		printf("No enough memory\n");
-
 
380
		exit(EXIT_FAILURE);
-
 
381
	}
-
 
382
//fill areas by definition of frame structure
-
 
383
	for (chn = 0; chn < channels; chn++) {
-
 
384
		areas[chn].addr = frame; 	// frame start adress
-
 
385
		areas[chn].first = chn * snd_pcm_format_physical_width(format); // ofset to first sample (in bits)
-
 
386
		areas[chn].step = channels * snd_pcm_format_physical_width(format); // step between samples
-
 
387
	}
-
 
388
 
-
 
389
       	data.samples = frame;
-
 
390
	data.areas = areas;
-
 
391
	data.period = 1;
-
 
392
// end of dummy structure 
-
 
393
 
-
 
394
 
-
 
395
 sine(chirp,100000);
-
 
396
 
373
 
397
/// register playback callback 
374
/// register playback callback 
398
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
375
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
399
	if (err < 0) {
376
	if (err < 0) {
400
		printf("Unable to register async handler\n");
377
		printf("Unable to register async handler\n");
401
		exit(EXIT_FAILURE);
378
		exit(EXIT_FAILURE);
402
	}
379
	}
403
	for (period = 0; period < 3; period++) {
380
	for (period = 0; period < 2; period++) {
404
 
381
 
405
		err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
382
		err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
406
		if (err < 0) {
383
		if (err < 0) {
407
			printf("Initial write error: %s\n", snd_strerror(err));
384
			printf("Initial write error: %s\n", snd_strerror(err));
408
			exit(EXIT_FAILURE);
385
			exit(EXIT_FAILURE);
Line 444... Line 421...
444
	
421
	
445
 
422
 
446
 
423
 
447
	/* because all other work is done in the signal handler,
424
	/* because all other work is done in the signal handler,
448
	   suspend the process */
425
	   suspend the process */
449
	while(cperiod<3) {
426
	while(cperiod<10) {
450
		sleep(1);
427
		sleep(1);
451
	}	
428
	}	
452
 
429
 
453
	out=fopen("./output.txt","w");
430
	out=fopen("./output.txt","w");
454
	for(i=0;i<=100000;i++) fprintf(out,"%d ",signal[i]); 
431
	for(i=0;i<=100000;i++) fprintf(out,"%6d	%6d \n",chirp[i],signal[i]); 
455
	fclose(out);	
432
	fclose(out);
456
 
-
 
457
	free(areas);
-
 
458
	free(frame);
-
 
459
 
433
 
460
	snd_pcm_close(playback_handle);
434
	snd_pcm_close(playback_handle);
461
	snd_pcm_close(capture_handle);
435
	snd_pcm_close(capture_handle);
462
	return 0;
436
	return 0;
463
}
437
}