Hi, I've made a test-case in the form of a simple C program which generates 10 sec of 440 Hz sinus. With a define, it can generate samples at 44.1 kHz or et 48 kHz. Direct output at 48 kHz sounds perfect but output at 44.1 kHz through the alsa rate plugin clearly shows a problem generating high frequencies. I've attached the test case to this mail. It can be compiled with: gcc -lasound alsatest.c -o alsatest Please chane the samplingRate define to test with 44100 and 48000
Thanks, have a nice day, Steph
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <alsa/asoundlib.h> #define samplingRate 44100 #define duration 10 #define sampleCount (samplingRate * duration * 2) int main (int argc, char *argv[]) { int i; int err; int rate = samplingRate; short buf[sampleCount]; snd_pcm_t *playback_handle; snd_pcm_hw_params_t *hw_params; if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) { fprintf (stderr, "cannot open audio device %s (%s)\n", argv[1], snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) { fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) { fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &rate, 0)) < 0) { fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) { fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) { fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)); exit (1); } snd_pcm_hw_params_free (hw_params); if ((err = snd_pcm_prepare (playback_handle)) < 0) { fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err)); exit (1); } /* fill the buffer */ for (i = 0; i < samplingRate * duration; i++) { const double sinF = 440.0; double val = sin( ((double)i * 2.0 * M_PI * sinF) / (samplingRate)); buf[i * 2] = val * 32767; buf[i * 2 + 1] = val * 32767; } if ((err = snd_pcm_writei (playback_handle, buf, sampleCount)) != sampleCount) { fprintf (stderr, "write to audio interface failed (%s)\n", snd_strerror (err)); exit (1); } snd_pcm_close (playback_handle); exit (0); }