# HG changeset patch # User Amine Sehili # Date 1551025013 -3600 # Node ID eda276f5d871ff537a8a2c57f5ea3bfcaf116cd4 # Parent 8b1b9010d6ae96c41988a85922b75173fba947b8 Refactor StdinAudioSource diff -r 8b1b9010d6ae -r eda276f5d871 auditok/io.py --- a/auditok/io.py Sun Feb 24 16:05:36 2019 +0100 +++ b/auditok/io.py Sun Feb 24 17:16:53 2019 +0100 @@ -604,7 +604,7 @@ return None -class StdinAudioSource(AudioSource): +class StdinAudioSource(_FileAudioSource): """ A class for an :class:`AudioSource` that reads data from standard input. """ @@ -614,10 +614,18 @@ sampling_rate=DEFAULT_SAMPLE_RATE, sample_width=DEFAULT_SAMPLE_WIDTH, channels=DEFAULT_NB_CHANNELS, + use_channel=0, ): - AudioSource.__init__(self, sampling_rate, sample_width, channels) + _FileAudioSource.__init__( + self, sampling_rate, sample_width, channels, use_channel + ) self._is_open = False + self._sample_size = sample_width * channels + if PYTHON_3: + self._stream = sys.stdin.buffer + else: + self._stream = sys.stdin def is_open(self): return self._is_open @@ -628,20 +636,12 @@ def close(self): self._is_open = False - def read(self, size): - if not self._is_open: - raise IOError("Stream is not open") - - to_read = size * self.sample_width * self.channels - if sys.version_info >= (3, 0): - data = sys.stdin.buffer.read(to_read) - else: - data = sys.stdin.read(to_read) - - if data is None or len(data) < 1: - return None - - return data + def _read_from_stream(self, size): + bytes_to_read = size * self._sample_size + data = self._stream.read(bytes_to_read) + if data: + return data + return None class PyAudioPlayer: