def _set_start_time(self, source, timestamp): delay = asound.snd_pcm_sframes_t() check(asound.snd_pcm_delay(self._device.pcm, ctypes.byref(delay))) delay = (delay.value / source.audio_format.bytes_per_sample / float(source.audio_format.bytes_per_second)) self._start_time = self._get_asound_time() - timestamp - delay
def write(self, audio_data): samples = audio_data.length // self.audio_format.bytes_per_sample samples_out = asound.snd_pcm_writei(self.pcm, audio_data.data, samples) if samples_out < 0: if samples_out == -11: # EAGAIN return elif samples_out == -32: # EPIPE (xrun) check(asound.snd_pcm_prepare(self.pcm)) return else: raise ALSAException(asound.snd_strerror(samples_out)) delay = asound.snd_pcm_sframes_t() check(asound.snd_pcm_delay(self.pcm, delay)) alsatime = self._get_asound_time() + \ delay.value / float(self.audio_format.sample_rate) self._timestamps.append((alsatime, audio_data.timestamp)) audio_data.consume(samples_out * self.audio_format.bytes_per_sample, self.audio_format)