#include #include #define ENABLE_SETPARAMS_RATE 1 #define ENABLE_SETPARAMS_PERIOD 2 /* neither set_rate_min() nor set_period_size_near() is ok (but trivial) */ //#define ENABLE_SETPARAMS 0 /* only set_rate_min() is ok */ //#define ENABLE_SETPARAMS ENABLE_SETPARAMS_RATE /* only set_period_size_near() is ok as well */ //#define ENABLE_SETPARAMS ENABLE_SETPARAMS_PERIOD /* both set_rate_min() and set_period_size_near() -> CRASH */ #define ENABLE_SETPARAMS ENABLE_SETPARAMS_RATE | ENABLE_SETPARAMS_PERIOD int pcm_configure(snd_pcm_t *pcm) { unsigned int rate=44100; snd_pcm_uframes_t uframes = 64; snd_pcm_hw_params_t* hw_params; int err; snd_pcm_hw_params_alloca(&hw_params); err = snd_pcm_hw_params_any(pcm, hw_params); if(err<0){fprintf(stderr, "snd_pcm_hw_params_any failed: %s\n", snd_strerror(err));return err;} #if ENABLE_SETPARAMS & 1 err = snd_pcm_hw_params_set_rate_min(pcm, hw_params, &rate, 0); if(err<0){fprintf(stderr, "snd_pcm_hw_params_set_rate_min failed: %s\n", snd_strerror(err));return err;} #endif #if ENABLE_SETPARAMS & 2 err = snd_pcm_hw_params_set_period_size_near(pcm, hw_params, &uframes, 0); if(err<0){fprintf(stderr, "snd_pcm_hw_params_set_period_size failed: %s\n", snd_strerror(err));return err;} #endif err = snd_pcm_hw_params(pcm, hw_params); if(err<0){fprintf(stderr, "snd_pcm_hw_params failed: %s\n", snd_strerror(err));return err;} printf("configuring device succeeded\n"); return 0; } void pcm_open(char*name) { int err=0; snd_pcm_t *pcmIn=0, *pcmOut=0; printf("trying to open '%s' as CAPTURE\n", name); err = snd_pcm_open(&pcmIn, name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK); if(err<0){fprintf(stderr, "snd_pcm_open(CAPTURE) failed: %s\n", snd_strerror(err));goto closeit;} err=snd_pcm_nonblock(pcmIn, 1); if(err<0){fprintf(stderr, "snd_pcm_nonblock failed: %s\n", snd_strerror(err));goto closeit;} pcm_configure(pcmIn); fprintf(stderr, "CAPTURE configured\n"); printf("trying to open '%s' as PLAYBACK\n", name); err = snd_pcm_open(&pcmOut, name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); if(err<0){fprintf(stderr, "snd_pcm_open(PLAYBACK) failed: %s\n", snd_strerror(err));goto closeit;} err=snd_pcm_nonblock(pcmOut, 1); if(err<0){fprintf(stderr, "snd_pcm_nonblock failed: %s\n", snd_strerror(err));goto closeit;} pcm_configure(pcmOut); fprintf(stderr, "PLAYBACK configured\n"); closeit: if(pcmIn)snd_pcm_close(pcmIn); if(pcmOut)snd_pcm_close(pcmOut); } int main(int argc, char**argv) { int i=0; if(argc>1) { for(i=1; i