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)
Example #2
0
    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)
 def check(err):
     if err < 0:
         raise ALSAException(asound.snd_strerror(err))
     return err
Example #4
0
 def check(err):
     if err < 0:
         raise ALSAException(asound.snd_strerror(err))
     return err
Example #5
0
    def dispatch_events(self):
        if not self._sources:
            return

        if not self._playing:
            # If paused, just update the video texture.
            if self._texture:
                self._sources[0]._update_texture(self, self.time)
            return

        # Create a device if there isn't one.  TODO only if source and source
        # has audio
        if not self._device:
            self._device = Device('plug:front')
            self._device.prepare(self._sources[0])

        self_time = self.time 

        # Passed EOS?
        source = self._sources[0]
        while source and source.duration < self_time:
            if self._eos_action == self.EOS_NEXT:
                self.next()
            elif self._eos_action == self.EOS_STOP:
                self.stop()
                self._sources = []
                return
            self.dispatch_event('on_eos')

            self_time -= source.duration
            self._cumulative_buffer_time -= source.duration
            assert self._cumulative_buffer_time >= -0.001 # some float err ok
            try:
                source = self._sources[0]
                self._set_start_time(self._sources[0], self_time)
            except IndexError:
                source = None
                self._start_time = None

        # Ensure device buffer is full
        try:
            source = self._sources[self._source_read_index]
        except IndexError:
            source = None
        while (source and 
               self._cumulative_buffer_time + self._current_buffer_time - self_time
                  < self._min_buffer_time):
            if self._queue_audio_data:
                audio_data = self._queue_audio_data
                self._queue_audio_data = None
            else:
                max_bytes = int(
                  self._min_buffer_time * source.audio_format.bytes_per_second)
                max_bytes = min(max_bytes, self._max_buffer_size)
                audio_data = source._get_audio_data(max_bytes)

            if audio_data: 
                samples = \
                    audio_data.length // source.audio_format.bytes_per_sample
                samples_out = asound.snd_pcm_writei(self._device.pcm, 
                    audio_data.data, samples)
                if samples_out < 0:
                    if samples_out == -11: # EAGAIN
                        self._queue_audio_data = audio_data
                    elif samples_out == -32: # EPIPE
                        # xrun recovery
                        check(asound.snd_pcm_prepare(self._device.pcm))
                        self._queue_audio_data = audio_data
                    else:
                        raise ALSAException(asound.snd_strerror(samples_out))
                elif samples_out < samples:
                    audio_data.consume(
                        samples_out * source.audio_format.bytes_per_sample,
                        source.audio_format)
                    self._current_buffer_time = audio_data.timestamp
                    self._queue_audio_data = audio_data
                else:
                    self._current_buffer_time = \
                        audio_data.timestamp + audio_data.duration
                    
                if self._start_time is None:
                    # XXX start playback
                    self._set_start_time(source, audio_data.timestamp)
                    
            else:
                # EOS on read source
                self._cumulative_buffer_time += source.duration
                self._current_buffer_time = 0.
                if self._eos_action == self.EOS_NEXT:
                    self._source_read_index += 1
                    try:
                        # preroll
                        source = self._sources[self._source_read_index]
                        source._play()
                    except IndexError:
                        source = None
                elif self._eos_action == self.EOS_LOOP:
                    source._seek(0)
                elif self._eos_action == self.EOS_PAUSE:
                    source = None
                elif self._eos_action == self.EOS_STOP:
                    source = None
                else:
                    assert False, 'Invalid eos_action'
                    source = None

        # Update video texture
        if self._texture:
            self._sources[0]._update_texture(self, self_time)