Subversion Repositories svnkaklik

Compare Revisions

Ignore whitespace Rev 530 → Rev 531

/programy/C/ix86/sound/sonar
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/programy/C/ix86/sound/sonar.c
23,11 → 23,11
static int resample = 1; /* enable alsa-lib resampling */
static int period_event = 0; /* produce poll event after each period */
 
#define CHIRP_SIZE 1000000
unsigned int chirp_size;
 
int period=0;
int cperiod=0;
short chirp[CHIRP_SIZE];
int chirp[1000000];
short signal[44100*6]; // record 6s of input samples
 
static snd_pcm_sframes_t buffer_size; // size of buffer at sound card
228,34 → 228,39
 
 
 
int linear_windowed_chirp(int *pole, int delka_pole){ // vygeneruje linearni chirp a vzorky ulozi do pole
unsigned int linear_windowed_chirp(unsigned int *pole, unsigned int delka_pole){ // vygeneruje linearni chirp a vzorky ulozi do pole
 
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
 
static const float f0 = 1/1000;
static const float fmax = 1/10000;
static const float Tw = 10;
 
static const float f0 = 1000;
static const float fmax = 5000;
static const float Tw = 0.2;
static float k;
 
unsigned int n=0;
double t;
unsigned int perioda;
 
//k=2*(fmax-Tw*f0)/Tw*Tw;
k=1/10000;
k=2*(fmax-f0)/Tw;
perioda = rate*Tw;
 
int n;
double t;
 
for(n=0;n < delka_pole;n++){
t=n*1/rate;
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))) );
for(n=0;n<=perioda;n++){
t = (double) n/ (double)rate;
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))) );
}
return perioda;
}
 
int sine(int *pole, int delka_pole)
int sine(unsigned int *pole, unsigned int delka_pole)
{
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
int t;
for(t=0;t < delka_pole;t++) pole[t] = (short) round(maxval*sin( (double)(t)/10.0));
unsigned int n;
double t;
 
for(n=0;n < delka_pole;n++){
t = 440.0 * (double) n/ (double)rate;
pole[n] = (short) round(maxval*sin(2*M_PI*t));
}
}
 
 
270,7 → 275,7
int err;
avail = snd_pcm_avail_update(handle);
while ((avail >= period_size) && ((period*period_size) < (CHIRP_SIZE-100)) ) {
while ((avail >= period_size) && ((period*period_size) < chirp_size) ) {
 
err = snd_pcm_writei(handle, (chirp+period*period_size), period_size);
if (err < 0) {
296,7 → 301,7
int err;
avail = snd_pcm_avail_update(handle);
// while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {
while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {
 
err = snd_pcm_readi(handle, (signal+cperiod*period_size), period_size);
if (err < 0) {
309,7 → 314,7
}
avail = snd_pcm_avail_update(handle);
cperiod++;
// }
}
}
 
 
364,36 → 369,8
exit(EXIT_FAILURE);
}
 
// allocate memory for frame (package of samples)
frame = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
if (frame == NULL) {
printf("No enough memory\n");
exit(EXIT_FAILURE);
}
chirp_size=linear_windowed_chirp(chirp,1000000);
 
 
// dummy structure
//allocate memory for frame structure definition
areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
if (areas == NULL) {
printf("No enough memory\n");
exit(EXIT_FAILURE);
}
//fill areas by definition of frame structure
for (chn = 0; chn < channels; chn++) {
areas[chn].addr = frame; // frame start adress
areas[chn].first = chn * snd_pcm_format_physical_width(format); // ofset to first sample (in bits)
areas[chn].step = channels * snd_pcm_format_physical_width(format); // step between samples
}
 
data.samples = frame;
data.areas = areas;
data.period = 1;
// end of dummy structure
 
 
sine(chirp,100000);
 
/// register playback callback
err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
if (err < 0) {
400,7 → 377,7
printf("Unable to register async handler\n");
exit(EXIT_FAILURE);
}
for (period = 0; period < 3; period++) {
for (period = 0; period < 2; period++) {
 
err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
if (err < 0) {
446,17 → 423,14
 
/* because all other work is done in the signal handler,
suspend the process */
while(cperiod<3) {
while(cperiod<10) {
sleep(1);
}
 
out=fopen("./output.txt","w");
for(i=0;i<=100000;i++) fprintf(out,"%d ",signal[i]);
fclose(out);
for(i=0;i<=100000;i++) fprintf(out,"%6d %6d \n",chirp[i],signal[i]);
fclose(out);
 
free(areas);
free(frame);
 
snd_pcm_close(playback_handle);
snd_pcm_close(capture_handle);
return 0;