11 #include <alsa/asoundlib.h>
13 static uint64_t timespec_us(const struct timespec *ts) {
15 ts->tv_sec * 1000000LLU +
16 ts->tv_nsec / 1000LLU;
19 int main(int argc, char *argv[]) {
21 int r, cap, count = 0;
22 snd_pcm_hw_params_t *hwparams;
23 snd_pcm_sw_params_t *swparams;
24 snd_pcm_status_t *status;
26 unsigned rate = 44100;
28 snd_pcm_uframes_t boundary, buffer_size = 44100/10; /* 100s */
30 struct timespec start, last_timestamp = { 0, 0 };
31 uint64_t start_us, last_us = 0;
32 snd_pcm_sframes_t last_avail = 0, last_delay = 0;
33 struct pollfd *pollfds;
35 int64_t sample_count = 0;
36 struct sched_param sp;
39 #ifdef _POSIX_PRIORITY_SCHEDULING
40 sp.sched_priority = 5;
41 r = pthread_setschedparam(pthread_self(), SCHED_RR, &sp);
44 printf("Could not get RT prio. :(\n");
46 snd_pcm_hw_params_alloca(&hwparams);
47 snd_pcm_sw_params_alloca(&swparams);
48 snd_pcm_status_alloca(&status);
50 r = clock_gettime(CLOCK_MONOTONIC, &start);
53 start_us = timespec_us(&start);
55 dev = argc > 1 ? argv[1] : "front:AudioPCI";
56 cap = argc > 2 ? atoi(argv[2]) : 0;
59 r = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0);
61 r = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_CAPTURE, 0);
64 r = snd_pcm_hw_params_any(pcm, hwparams);
67 r = snd_pcm_hw_params_set_rate_resample(pcm, hwparams, 0);
70 r = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
73 r = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE);
76 r = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, NULL);
79 r = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
82 r = snd_pcm_hw_params_set_periods_integer(pcm, hwparams);
85 r = snd_pcm_hw_params_set_periods_near(pcm, hwparams, &periods, &dir);
88 r = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams, &buffer_size);
91 r = snd_pcm_hw_params(pcm, hwparams);
94 r = snd_pcm_hw_params_current(pcm, hwparams);
97 r = snd_pcm_sw_params_current(pcm, swparams);
101 r = snd_pcm_sw_params_set_avail_min(pcm, swparams, 1);
103 r = snd_pcm_sw_params_set_avail_min(pcm, swparams, 0);
106 r = snd_pcm_sw_params_set_period_event(pcm, swparams, 0);
109 r = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
111 r = snd_pcm_sw_params_set_start_threshold(pcm, swparams, buffer_size);
114 r = snd_pcm_sw_params_get_boundary(swparams, &boundary);
116 r = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, boundary);
119 r = snd_pcm_sw_params_set_tstamp_mode(pcm, swparams, SND_PCM_TSTAMP_ENABLE);
122 r = snd_pcm_sw_params(pcm, swparams);
125 r = snd_pcm_prepare(pcm);
128 r = snd_pcm_sw_params_current(pcm, swparams);
131 /* assert(snd_pcm_hw_params_is_monotonic(hwparams) > 0); */
133 n_pollfd = snd_pcm_poll_descriptors_count(pcm);
134 assert(n_pollfd > 0);
136 pollfds = malloc(sizeof(struct pollfd) * n_pollfd);
139 r = snd_pcm_poll_descriptors(pcm, pollfds, n_pollfd);
140 assert(r == n_pollfd);
142 printf("Starting. Buffer size is %u frames\n", (unsigned int) buffer_size);
145 r = snd_pcm_start(pcm);
150 snd_pcm_sframes_t avail, delay;
151 struct timespec now, timestamp;
152 unsigned short revents;
154 uint64_t now_us, timestamp_us;
155 snd_pcm_state_t state;
156 unsigned long long pos;
158 r = poll(pollfds, n_pollfd, 0);
161 r = snd_pcm_poll_descriptors_revents(pcm, pollfds, n_pollfd, &revents);
165 assert((revents & ~POLLOUT) == 0);
167 assert((revents & ~POLLIN) == 0);
169 avail = snd_pcm_avail(pcm);
172 r = snd_pcm_status(pcm, status);
175 /* This assertion fails from time to time. ALSA seems to be broken */
176 /* assert(avail == (snd_pcm_sframes_t) snd_pcm_status_get_avail(status)); */
177 /* printf("%lu %lu\n", (unsigned long) avail, (unsigned long) snd_pcm_status_get_avail(status)); */
179 snd_pcm_status_get_htstamp(status, ×tamp);
180 delay = snd_pcm_status_get_delay(status);
181 state = snd_pcm_status_get_state(status);
183 r = clock_gettime(CLOCK_MONOTONIC, &now);
186 assert(!revents || avail > 0);
188 if ((!cap && avail) || (cap && (unsigned)avail >= buffer_size)) {
189 snd_pcm_sframes_t sframes;
190 static const uint16_t psamples[2] = { 0, 0 };
191 uint16_t csamples[2];
194 sframes = snd_pcm_writei(pcm, psamples, 1);
196 sframes = snd_pcm_readi(pcm, csamples, 1);
197 assert(sframes == 1);
204 memcmp(×tamp, &last_timestamp, sizeof(timestamp)) == 0 &&
205 avail == last_avail &&
206 delay == last_delay) {
211 now_us = timespec_us(&now);
212 timestamp_us = timespec_us(×tamp);
215 pos = (unsigned long long) ((sample_count - handled - delay) * 1000000LU / 44100);
217 pos = (unsigned long long) ((sample_count - handled + delay) * 1000000LU / 44100);
219 if (count++ % 50 == 0)
220 printf("Elapsed\tCPU\tALSA\tPos\tSamples\tavail\tdelay\trevents\thandled\tstate\n");
222 printf("%llu\t%llu\t%llu\t%llu\t%llu\t%li\t%li\t%i\t%i\t%i\n",
223 (unsigned long long) (now_us - last_us),
224 (unsigned long long) (now_us - start_us),
225 (unsigned long long) (timestamp_us ? timestamp_us - start_us : 0),
227 (unsigned long long) sample_count,
235 /** When this assert is hit, most likely something bad
236 * happened, i.e. the avail jumped suddenly. */
237 assert((unsigned) avail <= buffer_size);
241 last_timestamp = timestamp;