Subversion Repositories svnkaklik

Compare Revisions

Ignore whitespace Rev 526 → Rev 527

/programy/C/ix86/sound/sonar.c
23,8 → 23,12
static int resample = 1; /* enable alsa-lib resampling */
static int period_event = 0; /* produce poll event after each period */
 
#define CHIRP_SIZE 1000000
 
int period=0;
short sample[1000000];
int cperiod=0;
short chirp[CHIRP_SIZE];
short signal[44100*6]; // record 6s of input samples
 
static snd_pcm_sframes_t buffer_size; // size of buffer at sound card
static snd_pcm_sframes_t period_size; //samples per frame
220,26 → 224,38
faze +=t;
// else return 0;
 
}
}*/
 
 
 
int linear_windowed_chirp(int *pole, int delka_pole){ // vygeneruje linearni chirp a vzorky ulozi do pole
 
static const float f0 = 0.0001;
static const float k = 0.00001;
unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
 
int t;
static const float f0 = 1/1000;
static const float fmax = 1/10000;
static const float Tw = 10;
 
// (0.35875 - 0.48829 cos(t) + 0.14128 cos(2t) - 0.01168 cos(3t))
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))) );
faze +=t;
static float k;
 
 
}*/
//k=2*(fmax-Tw*f0)/Tw*Tw;
k=1/10000;
 
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))) );
}
}
 
int sine(int *pole, 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(10000*sin( (double)(t)/10.0));
for(t=0;t < delka_pole;t++) pole[t] = (short) round(maxval*sin( (double)(t)/10.0));
}
 
 
247,18 → 263,16
static void async_playback_callback(snd_async_handler_t *ahandler)
{
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
/* struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
signed short *samples = data->samples;
snd_pcm_channel_area_t *areas = data->areas;
snd_pcm_channel_area_t *areas = data->areas;*/
snd_pcm_sframes_t avail;
int err;
avail = snd_pcm_avail_update(handle);
while (avail >= period_size) {
// generate_sine(areas, 0, period_size, &data->phase);
// linear_windowed_chirp(signal,period_size);
while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {
 
err = snd_pcm_writei(handle, sample, period_size);
err = snd_pcm_writei(handle, (chirp+period*period_size), period_size);
if (err < 0) {
printf("Write error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
268,9 → 282,37
exit(EXIT_FAILURE);
}
avail = snd_pcm_avail_update(handle);
period++;
}
}
 
static void async_capture_callback(snd_async_handler_t *ahandler)
{
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
/* struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
signed short *samples = data->samples;
snd_pcm_channel_area_t *areas = data->areas;*/
snd_pcm_sframes_t avail;
int err;
avail = snd_pcm_avail_update(handle);
// while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ ) {
 
err = snd_pcm_readi(handle, (signal+cperiod*period_size), period_size);
if (err < 0) {
printf("Read error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if (err != period_size) {
printf("Read error: red %i expected %li\n", err, period_size);
exit(EXIT_FAILURE);
}
avail = snd_pcm_avail_update(handle);
cperiod++;
// }
}
 
 
int main(int argc, char *argv[])
{
snd_pcm_t *playback_handle, *capture_handle;
282,11 → 324,13
snd_pcm_channel_area_t *areas;
 
struct async_private_data data;
snd_async_handler_t *ahandler;
snd_async_handler_t *chandler, *phandler;
int count;
unsigned int i;
unsigned int i,j;
 
FILE *out;
 
 
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_sw_params_alloca(&swparams);
 
315,10 → 359,10
printf("Setting of hwparams failed: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = set_swparams(capture_handle, swparams)) < 0) {
/* if ((err = set_swparams(capture_handle, swparams)) < 0) {
printf("Setting of swparams failed: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
}*/
 
// allocate memory for frame (package of samples)
frame = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
327,6 → 371,8
exit(EXIT_FAILURE);
}
 
 
// dummy structure
//allocate memory for frame structure definition
areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
if (areas == NULL) {
340,23 → 386,23
areas[chn].step = channels * snd_pcm_format_physical_width(format); // step between samples
}
 
sine(sample,100000);
 
 
data.samples = frame;
data.samples = frame;
data.areas = areas;
data.period = 1;
// end of dummy structure
 
 
// register playback callback
err = snd_async_add_pcm_handler(&ahandler, playback_handle, async_playback_callback, &data);
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) {
printf("Unable to register async handler\n");
exit(EXIT_FAILURE);
}
for (data.period = 0; data.period < 2; data.period++) {
for (period = 0; period < 3; period++) {
 
err = snd_pcm_writei(playback_handle, sample, period_size);
err = snd_pcm_writei(playback_handle, (chirp+period*period_size), period_size);
if (err < 0) {
printf("Initial write error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
367,6 → 413,13
}
}
 
// register capture callback
err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
if (err < 0) {
printf("Unable to register async handler\n");
exit(EXIT_FAILURE);
}
 
//start playback
if (snd_pcm_state(playback_handle) == SND_PCM_STATE_PREPARED) {
err = snd_pcm_start(playback_handle);
376,16 → 429,34
}
}
 
//start capture
if ((err = snd_pcm_prepare (capture_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
 
err = snd_pcm_start(capture_handle);
if (err < 0) {
printf("Start error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
 
 
/* because all other work is done in the signal handler,
suspend the process */
for(i=0; i<=10;i++) {
while(cperiod<10) {
sleep(1);
}
}
 
out=fopen("./output.txt","w");
for(i=0;i<=100000;i++) fprintf(out,"%d ",signal[i]);
fclose(out);
 
 
free(areas);
free(frame);
 
snd_pcm_close(playback_handle);
snd_pcm_close(capture_handle);
return 0;