c++ - Clicking in ALSA tone generator -
i'm learning use alsa on raspberry pi 2. i've written small test program in c++ generate 440 hz test tone. makes tone, there clicking sound twice per second in tone.
does have idea why might happening? code below.
#include <cmath> #include <climits> #include <iostream> #include <alsa/asoundlib.h> #include "definitions.hpp" using namespace std; int main() { int ret; snd_pcm_t* pcm_handle; // device handle snd_pcm_stream_t stream = snd_pcm_stream_playback; snd_pcm_hw_params_t* hwparams; // hardware information char* pcm_name = strdup("plughw:0,0"); // on-board audio jack int rate = 48000; const uint16 freq = 440; long unsigned int buffersize = 8192*4; const uint32 len = buffersize*100; const float32 arg = 2 * 3.141592 * freq / rate; sint16 vals[len]; for(int = 0; < len; = + 2) { vals[i] = shrt_max * cos(arg * / 2); } snd_pcm_hw_params_alloca(&hwparams); ret = snd_pcm_open(&pcm_handle, pcm_name, stream, 0); cout << "opening: " << snd_strerror(ret) << endl; ret = snd_pcm_hw_params_any(pcm_handle, hwparams); cout << "initializing hwparams structure: " << snd_strerror(ret) << endl; ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, snd_pcm_access_rw_interleaved); cout << "setting access: " << snd_strerror(ret) << endl; ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, snd_pcm_format_s16_le); cout << "setting format: " << snd_strerror(ret) << endl; ret = snd_pcm_hw_params_set_rate(pcm_handle, hwparams, rate, (int)0); cout << "setting rate: " << snd_strerror(ret) << endl; ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2); cout << "setting channels: " << snd_strerror(ret) << endl; ret = snd_pcm_hw_params_set_periods(pcm_handle, hwparams, 2, 0); cout << "setting periods: " << snd_strerror(ret) << endl; ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffersize); cout << "setting buffer size: " << snd_strerror(ret) << endl; ret = snd_pcm_hw_params(pcm_handle, hwparams); cout << "applying parameters: " << snd_strerror(ret) << endl; cout << endl << endl; const void* ptr = (const void*)&vals; int err; { ptr += buffersize; ret = snd_pcm_writei(pcm_handle, ptr, len); if(ret < 0) { err = snd_pcm_prepare(pcm_handle); cout << "preparing: " << snd_strerror(err) << endl; } } while(ret < 0); cout << "writing data: " << ret << ", " << snd_strerror(ret) << endl; }
when run it, terminal output. of course, there's no write error, number of bits written.
pi@raspberrypi:~/radio $ ./bin/alsatest opening: success initializing hwparams structure: success setting access: success setting format: success setting rate: success setting channels: success setting periods: success setting buffer size: success applying parameters: success writing data: 344110, unknown error 344110
update - next day ok. i've hooked output handy-dandy oscilloscope , saw following waveform. there seems discontinuity in signal every time there's click. added few lines count how many nearly-zero values next each other in sinusoid array, , there none. oddly enough, alsa sample program /test/pcm.c makes perfect wave. perhaps need write in small chunks? there doesn't seem difference between code , example.
Comments
Post a Comment