/* http://lists.gnu.org/archive/html/protux-devel/2003-02/msg00048.html */

/**** detect_sample_rates.c ****/
#include <stdio.h>
#include <stdlib.h>
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>

int main(int argc, char **argv)
{
        int i;
        int rates[13] = {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 192000, 0};
        snd_pcm_t *playback_handle;
        snd_pcm_hw_params_t *hw_params;
	snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;

        if (argc < 2)
        {
                fprintf(stderr, "usage : xxx [p|c] hw:0\n");
                exit(1);
        }

	if ( argv[2] ) {
		if ( ! strncmp(argv[2], "p", 1 ) ) { stream = SND_PCM_STREAM_PLAYBACK; }
		if ( ! strncmp(argv[2], "c", 1 ) ) { stream = SND_PCM_STREAM_CAPTURE; }
	}

        for (i = 0; rates[i] != 0; i++)
          {
            if (snd_pcm_open(&playback_handle, argv[1], stream, SND_PCM_NONBLOCK) < 0)
              exit(1);
            
            if (snd_pcm_hw_params_malloc(&hw_params) < 0)
              exit(1);
            if (snd_pcm_hw_params_any(playback_handle, hw_params) < 0)
              exit(1);
            if (!snd_pcm_hw_params_test_rate(playback_handle, hw_params, rates[i], 0))
              fprintf(stdout, "%6i supported\n", rates[i]);
            
            snd_pcm_close(playback_handle);
          }
        exit(0);
}
/**** EOF ****/
