<div dir="ltr">Source and sinks that share the same clock source are those found in the same sound card, right? Which are the options if this is not the case, if for example I use a USB mic with my system speakers? Is there any way to prevent the process from clock driff?<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 30, 2015 at 5:01 PM, Tanu Kaskinen <span dir="ltr"><<a href="mailto:tanuk@iki.fi" target="_blank">tanuk@iki.fi</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Wed, 2015-12-30 at 16:46 +0200, Alexandros Chronopoulos wrote:<br>
> Hi,<br>
><br>
> I am using PulseAudio api to implement a scenario which synchronize an<br>
> input stream with an output stream. Synchronize means that whenever input<br>
> data is available the same number of frames should be set for writing in<br>
> the output.<br>
><br>
> In order to achieve that I use two separate streams one for record and the<br>
> other for playback. In the record call back I check the writable size and<br>
> if it is big enough I write the data otherwise I reject the packet. This<br>
> causes some distortion in the output especially when latency is small.<br>
><br>
> Trying to overcome this problem I stopped checking for writable data. I use<br>
> pa_stream_begin_write instead before the pa_stream_write to allocated the<br>
> desired data size. This approach seems to work very well even for very<br>
> small latency, even when the reported writable size is zero.<br>
><br>
> My question is, if the approach above is safe and what is the actual<br>
> representation of the number returned from pa_stream_writable_size method?<br>
<br>
</span>Hmm, I see the documentation for pa_stream_writable_size() is wrong:<br>
"Return the number of bytes that may be written using<br>
pa_stream_write()." The documentation implies that you can't write more<br>
than that, but actually the returned value tells how much the server<br>
has requested from you.<br>
<br>
If you write more than the server has requested, the extra amount will<br>
be buffered in a per-stream buffer in the server. The size of the<br>
stream buffer is the maxlength parameter of pa_buffer_attr. The default<br>
maxlength allows you to write a lot to the stream buffer before writes<br>
start to fail.<br>
<br>
Writing at the same rate as you read from the capture stream, like you<br>
do, is safe if the sink and the source share the same clock source. If<br>
they use different clocks, however, there will be some clock drift,<br>
which means that either the playback buffer runs empty at some point,<br>
causing a drop-out, or the playback buffer accumulates slowly more and<br>
more data, causing the latency to grow.<br>
<span class="HOEnZb"><font color="#888888"><br>
-- <br>
Tanu<br>
</font></span></blockquote></div><br></div>