2 * This program only tracks the difference between system time
3 * and audio time, as reported in snd_pcm_status(). It should be
4 * helpful to verify the information reported by drivers.
7 #include "../include/asoundlib.h"
10 static char *device = "hw:0,0";
12 snd_output_t *output = NULL;
14 long long timestamp2ns(snd_htimestamp_t t)
18 nsec = t.tv_sec * 1000000000;
24 long long timediff(snd_htimestamp_t t1, snd_htimestamp_t t2)
26 long long nsec1, nsec2;
28 nsec1 = timestamp2ns(t1);
29 nsec2 = timestamp2ns(t2);
34 void gettimestamp(snd_pcm_t *handle, snd_htimestamp_t *timestamp,
35 snd_htimestamp_t *trigger_timestamp,
36 snd_pcm_uframes_t *avail, snd_pcm_sframes_t *delay)
39 snd_pcm_status_t *status;
41 snd_pcm_status_alloca(&status);
42 if ((err = snd_pcm_status(handle, status)) < 0) {
43 printf("Stream status error: %s\n", snd_strerror(err));
46 snd_pcm_status_get_trigger_htstamp(status, trigger_timestamp);
47 snd_pcm_status_get_htstamp(status, timestamp);
48 *avail = snd_pcm_status_get_avail(status);
49 *delay = snd_pcm_status_get_delay(status);
53 #define PCM_LINK /* sync start for playback and capture */
54 #define TRACK_CAPTURE /* dump capture timing info */
55 #define TRACK_PLAYBACK /* dump playback timing info */
56 #define PLAYBACK_BUFFERS 4
63 snd_pcm_t *handle_p = NULL;
64 snd_pcm_t *handle_c = NULL;
65 snd_pcm_sframes_t frames;
66 snd_htimestamp_t tstamp_c, tstamp_p;
67 snd_htimestamp_t trigger_tstamp_c, trigger_tstamp_p;
68 unsigned char buffer_p[PERIOD*4*4];
69 unsigned char buffer_c[PERIOD*4*4];
71 snd_pcm_sw_params_t *swparams_p;
72 snd_pcm_sw_params_t *swparams_c;
74 snd_pcm_uframes_t curr_count_c;
75 snd_pcm_uframes_t frame_count_c = 0;
76 snd_pcm_uframes_t curr_count_p;
77 snd_pcm_uframes_t frame_count_p = 0;
79 snd_pcm_sframes_t delay_p, delay_c;
80 snd_pcm_uframes_t avail_p, avail_c;
82 if ((err = snd_pcm_open(&handle_p, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
83 printf("Playback open error: %s\n", snd_strerror(err));
86 if ((err = snd_pcm_set_params(handle_p,
88 SND_PCM_ACCESS_RW_INTERLEAVED,
92 500000)) < 0) { /* 0.5sec */
93 printf("Playback open error: %s\n", snd_strerror(err));
97 snd_pcm_sw_params_alloca(&swparams_p);
98 /* get the current swparams */
99 err = snd_pcm_sw_params_current(handle_p, swparams_p);
101 printf("Unable to determine current swparams_p: %s\n", snd_strerror(err));
106 err = snd_pcm_sw_params_set_tstamp_mode(handle_p, swparams_p, SND_PCM_TSTAMP_ENABLE);
108 printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
112 /* write the sw parameters */
113 err = snd_pcm_sw_params(handle_p, swparams_p);
115 printf("Unable to set swparams_p : %s\n", snd_strerror(err));
119 if ((err = snd_pcm_open(&handle_c, device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
120 printf("Capture open error: %s\n", snd_strerror(err));
123 if ((err = snd_pcm_set_params(handle_c,
125 SND_PCM_ACCESS_RW_INTERLEAVED,
129 500000)) < 0) { /* 0.5sec */
130 printf("Capture open error: %s\n", snd_strerror(err));
134 snd_pcm_sw_params_alloca(&swparams_c);
135 /* get the current swparams */
136 err = snd_pcm_sw_params_current(handle_c, swparams_c);
138 printf("Unable to determine current swparams_c: %s\n", snd_strerror(err));
143 err = snd_pcm_sw_params_set_tstamp_mode(handle_c, swparams_c, SND_PCM_TSTAMP_ENABLE);
145 printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
149 /* write the sw parameters */
150 err = snd_pcm_sw_params(handle_c, swparams_c);
152 printf("Unable to set swparams_c : %s\n", snd_strerror(err));
157 if ((err = snd_pcm_link(handle_c, handle_p)) < 0) {
158 printf("Streams link error: %s\n", snd_strerror(err));
163 i = PLAYBACK_BUFFERS;
165 frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
167 printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
170 frame_count_p += frames;
173 if (PLAYBACK_BUFFERS != 4)
174 snd_pcm_start(handle_p);
177 /* need to start capture explicitly */
178 snd_pcm_start(handle_c);
183 frames = snd_pcm_wait(handle_c, -1);
185 printf("snd_pcm_wait failed: %s\n", snd_strerror(frames));
189 frames = snd_pcm_readi(handle_c, buffer_c, PERIOD);
191 printf("snd_pcm_readi failed: %s\n", snd_strerror(frames));
194 frame_count_c += frames;
196 frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
198 printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
202 frame_count_p += frames;
204 #if defined(TRACK_PLAYBACK)
205 gettimestamp(handle_p, &tstamp_p, &trigger_tstamp_p, &avail_p, &delay_p);
207 curr_count_p = frame_count_p - delay_p; /* written minus queued */
209 printf("playback: systime: %lli nsec, sample time %lli nsec \tsystime delta %lli \n",
210 timediff(tstamp_p,trigger_tstamp_p),
211 (long long)round(((float)curr_count_p * 1000000000.0 / 48000.0)),
212 timediff(tstamp_p, trigger_tstamp_p) - (long long)round((double)curr_count_p * 1000000000.0 / 48000.0)
216 #if defined(TRACK_CAPTURE)
217 gettimestamp(handle_c, &tstamp_c, &trigger_tstamp_c, &avail_c, &delay_c);
219 curr_count_c = frame_count_c + delay_c; /* read plus queued */
221 printf("\t capture: systime: %lli nsec, sample time %lli nsec \tsystime delta %lli \n",
222 timediff(tstamp_c,trigger_tstamp_c),
223 (long long)round(((float)curr_count_c * 1000000000.0 / 48000.0)),
224 timediff(tstamp_c, trigger_tstamp_c) - (long long)round((double)curr_count_c * 1000000000.0 / 48000.0)
232 snd_pcm_close(handle_p);
234 snd_pcm_close(handle_c);