/* how to compile: * gcc -lpulse -lpulse-simple test.c -o test */ #include #include #include #include int main() { pa_simple *simple; pa_sample_spec spec; int error; bool first = true; pa_usec_t started_at = 0; size_t bytes_read = 0; spec.format = PA_SAMPLE_S16NE; spec.channels = 1; spec.rate = 44100; simple = pa_simple_new(NULL, "Fooapp", PA_STREAM_RECORD, NULL, "Foostream", &spec, NULL, NULL, &error); if (!simple) { printf("pa_simple_new() failed: %s\n", pa_strerror(error)); return 0; } while (true) { int r; uint8_t buf[10000]; pa_usec_t now; pa_usec_t timestamp; pa_usec_t read_length; pa_usec_t latency; int diff; r = pa_simple_read(simple, buf, sizeof(buf), &error); if (r < 0) { printf("pa_simple_read() failed: %s\n", pa_strerror(error)); break; } now = pa_rtclock_now(); if (first) { started_at = now; first = false; } timestamp = now - started_at; bytes_read += sizeof(buf); read_length = pa_bytes_to_usec(bytes_read, &spec); latency = pa_simple_get_latency(simple, NULL); diff = (int) timestamp - (int) (read_length + latency); fprintf(stdout, "%u,%i\n", (unsigned) timestamp, diff); fflush(stdout); } pa_simple_free(simple); return 0; }