changeset 67:be6f8899be43

Make source pep8 compliant
author Amine SEHILI <amine.sehili@gmail.com>
date Sat, 28 Jan 2017 12:47:36 +0100
parents 9b4e308c8733
children a0843d57697b
files auditok/core.py auditok/dataset.py auditok/exceptions.py auditok/io.py
diffstat 4 files changed, 276 insertions(+), 311 deletions(-) [+]
line wrap: on
line diff
--- a/auditok/core.py	Sat Jan 28 12:25:34 2017 +0100
+++ b/auditok/core.py	Sat Jan 28 12:47:36 2017 +0100
@@ -18,42 +18,42 @@
     """
     Class for stream tokenizers. It implements a 4-state automaton scheme
     to extract sub-sequences of interest on the fly.
-    
+
     :Parameters:
-    
+
         `validator` :
             instance of `DataValidator` that implements `is_valid` method.
-        
+
         `min_length` : *(int)*
             Minimum number of frames of a valid token. This includes all \
             tolerated non valid frames within the token.
-            
+
         `max_length` : *(int)*
             Maximum number of frames of a valid token. This includes all \
             tolerated non valid frames within the token.
-        
+
         `max_continuous_silence` : *(int)*
             Maximum number of consecutive non-valid frames within a token.
             Note that, within a valid token, there may be many tolerated \
             *silent* regions that contain each a number of non valid frames up to \
             `max_continuous_silence`
-        
+
         `init_min` : *(int, default=0)*
             Minimum number of consecutive valid frames that must be **initially** \
             gathered before any sequence of non valid frames can be tolerated. This
             option is not always needed, it can be used to drop non-valid tokens as
             early as possible. **Default = 0** means that the option is by default 
             ineffective. 
-                
+
         `init_max_silence` : *(int, default=0)*
             Maximum number of tolerated consecutive non-valid frames if the \
             number already gathered valid frames has not yet reached 'init_min'.
             This argument is normally used if `init_min` is used. **Default = 0**,
             by default this argument is not taken into consideration.
-            
+
         `mode` : *(int, default=0)*
             `mode` can be:
-        
+
         1. `StreamTokenizer.STRICT_MIN_LENGTH`: 
         if token *i* is delivered because `max_length`
         is reached, and token *i+1* is immediately adjacent to
@@ -62,66 +62,65 @@
         least `min_length`. The default behavior is to accept token *i+1*
         event if it is shorter than `min_length` (given that the above conditions
         are fulfilled of course).
-           
+
         :Examples:
-               
+
         In the following code, without `STRICT_MIN_LENGTH`, the 'BB' token is
         accepted although it is shorter than `min_length` (3), because it immediately
         follows the latest delivered token:
-            
+
         .. code:: python
-        
+
             from auditok import StreamTokenizer, StringDataSource, DataValidator
-    
+
             class UpperCaseChecker(DataValidator):
                 def is_valid(self, frame):
                     return frame.isupper()
-                   
-    
+
+
             dsource = StringDataSource("aaaAAAABBbbb")
             tokenizer = StreamTokenizer(validator=UpperCaseChecker(),
                                         min_length=3,
                                         max_length=4,
                                         max_continuous_silence=0)
-         
+
             tokenizer.tokenize(dsource)
-                    
-                    
+
         :output:
-    
+
          .. code:: python
-         
+
             [(['A', 'A', 'A', 'A'], 3, 6), (['B', 'B'], 7, 8)]
 
 
         The following tokenizer will however reject the 'BB' token:
-     
+
         .. code:: python
-                
+
             dsource = StringDataSource("aaaAAAABBbbb")
             tokenizer = StreamTokenizer(validator=UpperCaseChecker(), 
                                         min_length=3, max_length=4,
                                         max_continuous_silence=0,
                                         mode=StreamTokenizer.STRICT_MIN_LENGTH)
             tokenizer.tokenize(dsource)
-        
+
         :output:
-            
+
         .. code:: python
-            
+
             [(['A', 'A', 'A', 'A'], 3, 6)]
-            
-           
+
+
         2. `StreamTokenizer.DROP_TRAILING_SILENCE`: drop all tailing non-valid frames
         from a token to be delivered if and only if it is not **truncated**.
         This can be a bit tricky. A token is actually delivered if:
-           
+
         - a. `max_continuous_silence` is reached
-           
+
         :or:
-           
+
         - b. Its length reaches `max_length`. This is called a **truncated** token
-           
+
         In the current implementation, a `StreamTokenizer`'s decision is only based on already seen
         data and on incoming data. Thus, if a token is truncated at a non-valid but tolerated
         frame (`max_length` is reached but `max_continuous_silence` not yet) any tailing
@@ -130,135 +129,132 @@
         token will not be considered as truncated but a result of *normal* end of detection
         (i.e. no more valid data). In that case the tailing silence can be removed if you use
         the `StreamTokenizer.DROP_TRAILING_SILENCE` mode.
-    
+
         :Example:
-    
+
         .. code:: python
-                       
+
              tokenizer = StreamTokenizer(validator=UpperCaseChecker(), min_length=3,
                                          max_length=6, max_continuous_silence=3,
                                          mode=StreamTokenizer.DROP_TRAILING_SILENCE)
-            
+
              dsource = StringDataSource("aaaAAAaaaBBbbbb")
              tokenizer.tokenize(dsource)
-        
+
         :output:
-            
+
         .. code:: python
-                
+
             [(['A', 'A', 'A', 'a', 'a', 'a'], 3, 8), (['B', 'B'], 9, 10)]
-                    
+
         The first token is delivered with its tailing silence because it is truncated
         while the second one has its tailing frames removed.
-                    
+
         Without `StreamTokenizer.DROP_TRAILING_SILENCE` the output would be:
-                        
+
         .. code:: python
-         
+
             [(['A', 'A', 'A', 'a', 'a', 'a'], 3, 8), (['B', 'B', 'b', 'b', 'b'], 9, 13)]
-    
-        
-        
+
+
         3. `StreamTokenizer.STRICT_MIN_LENGTH | StreamTokenizer.DROP_TRAILING_SILENCE`:
         use both options. That means: first remove tailing silence, then ckeck if the
         token still has at least a length of `min_length`.
     """
-    
-    
+
     SILENCE = 0
     POSSIBLE_SILENCE = 1
-    POSSIBLE_NOISE = 2 
+    POSSIBLE_NOISE = 2
     NOISE = 3
-    
+
     STRICT_MIN_LENGTH = 2
     DROP_TRAILING_SILENCE = 4
     # alias
     DROP_TAILING_SILENCE = 4
-    
-    def __init__(self, validator, 
+
+    def __init__(self, validator,
                  min_length, max_length, max_continuous_silence,
                  init_min=0, init_max_silence=0,
                  mode=0):
-        
+
         if not isinstance(validator, DataValidator):
             raise TypeError("'validator' must be an instance of 'DataValidator'")
-        
+
         if max_length <= 0:
             raise ValueError("'max_length' must be > 0 (value={0})".format(max_length))
-        
+
         if min_length <= 0 or min_length > max_length:
             raise ValueError("'min_length' must be > 0 and <= 'max_length' (value={0})".format(min_length))
-        
+
         if max_continuous_silence >= max_length:
             raise ValueError("'max_continuous_silence' must be < 'max_length' (value={0})".format(max_continuous_silence))
-        
+
         if init_min >= max_length:
             raise ValueError("'init_min' must be < 'max_length' (value={0})".format(max_continuous_silence))
-            
+
         self.validator = validator
         self.min_length = min_length
         self.max_length = max_length
         self.max_continuous_silence = max_continuous_silence
         self.init_min = init_min
         self.init_max_silent = init_max_silence
-        
+
         self._mode = None
         self.set_mode(mode)
         self._strict_min_length = (mode & self.STRICT_MIN_LENGTH) != 0
-        self._drop_tailing_silence  = (mode & self.DROP_TRAILING_SILENCE) != 0
-        
+        self._drop_tailing_silence = (mode & self.DROP_TRAILING_SILENCE) != 0
+
         self._deliver = None
         self._tokens = None
         self._state = None
         self._data = None
         self._contiguous_token = False
-        
+
         self._init_count = 0
         self._silence_length = 0
         self._start_frame = 0
         self._current_frame = 0
-    
+
     def set_mode(self, mode):
         """
         :Parameters:
-        
+
             `mode` : *(int)*
                 New mode, must be one of:
-                    
-                
+
+
             - `StreamTokenizer.STRICT_MIN_LENGTH`
-            
+
             - `StreamTokenizer.DROP_TRAILING_SILENCE`
-            
+
             - `StreamTokenizer.STRICT_MIN_LENGTH | StreamTokenizer.DROP_TRAILING_SILENCE`
-                   
+
             - `0`
-                       
+
         See `StreamTokenizer.__init__` for more information about the mode.
         """
-        
+
         if not mode in [self.STRICT_MIN_LENGTH, self.DROP_TRAILING_SILENCE,
-           self.STRICT_MIN_LENGTH | self.DROP_TRAILING_SILENCE, 0]:
-            
+                        self.STRICT_MIN_LENGTH | self.DROP_TRAILING_SILENCE, 0]:
+
             raise ValueError("Wrong value for mode")
-        
+
         self._mode = mode
         self._strict_min_length = (mode & self.STRICT_MIN_LENGTH) != 0
-        self._drop_tailing_silence  = (mode & self.DROP_TRAILING_SILENCE) != 0
-        
-    
+        self._drop_tailing_silence = (mode & self.DROP_TRAILING_SILENCE) != 0
+
     def get_mode(self):
         """
         Return the current mode. To check whether a specific mode is activated use
         the bitwise 'and' operator `&`. Example:
-           
+
         .. code:: python 
-                
+
             if mode & self.STRICT_MIN_LENGTH != 0:
                do_something()
         """
         return self._mode
-        
+
     def _reinitialize(self):
         self._contiguous_token = False
         self._data = []
@@ -266,112 +262,109 @@
         self._state = self.SILENCE
         self._current_frame = -1
         self._deliver = self._append_token
-    
-    
+
     def tokenize(self, data_source, callback=None):
         """
         Read data from `data_source`, one frame a time, and process the read frames in
         order to detect sequences of frames that make up valid tokens.
-        
+
         :Parameters:
            `data_source` : instance of the :class:`DataSource` class that implements a `read` method.
                'read' should return a slice of signal, i.e. frame (of whatever \
                type as long as it can be processed by validator) and None if \
                there is no more signal.
-        
+
            `callback` : an optional 3-argument function.
                If a `callback` function is given, it will be called each time a valid token
                is found.
-           
-           
+
+
         :Returns:
            A list of tokens if `callback` is None. Each token is tuple with the following elements:
-        
+
             .. code python
-            
+
                 (data, start, end)
-            
+
            where `data` is a list of read frames, `start`: index of the first frame in the
            original data and `end` : index of the last frame. 
-        
+
         """
-        
+
         self._reinitialize()
-        
+
         if callback is not None:
             self._deliver = callback
-        
+
         while True:
-            frame =  data_source.read()
+            frame = data_source.read()
             if frame is None:
                 break
             self._current_frame += 1
             self._process(frame)
-            
+
         self._post_process()
-        
+
         if callback is None:
             _ret = self._tokens
             self._tokens = None
             return _ret
-        
-        
+
     def _process(self, frame):
-        
+
         frame_is_valid = self.validator.is_valid(frame)
-        
+
         if self._state == self.SILENCE:
-            
+
             if frame_is_valid:
                 # seems we got a valid frame after a silence
                 self._init_count = 1
                 self._silence_length = 0
                 self._start_frame = self._current_frame
                 self._data.append(frame)
-                
-                if self._init_count  >= self.init_min:
+
+                if self._init_count >= self.init_min:
                     self._state = self.NOISE
                     if len(self._data) >= self.max_length:
                         self._process_end_of_detection(True)
                 else:
                     self._state = self.POSSIBLE_NOISE
-        
+
         elif self._state == self.POSSIBLE_NOISE:
-            
+
             if frame_is_valid:
                 self._silence_length = 0
                 self._init_count += 1
                 self._data.append(frame)
-                if self._init_count  >= self.init_min:
+                if self._init_count >= self.init_min:
                     self._state = self.NOISE
                     if len(self._data) >= self.max_length:
                         self._process_end_of_detection(True)
-            
-            else:                
+
+            else:
                 self._silence_length += 1
                 if self._silence_length > self.init_max_silent or \
-                len(self._data) + 1 >= self.max_length:
+                        len(self._data) + 1 >= self.max_length:
                     # either init_max_silent or max_length is reached
                     # before _init_count, back to silence
                     self._data = []
                     self._state = self.SILENCE
                 else:
                     self._data.append(frame)
-                    
-                
+
         elif self._state == self.NOISE:
-            
+
             if frame_is_valid:
                 self._data.append(frame)
                 if len(self._data) >= self.max_length:
                     self._process_end_of_detection(True)
-            
-            elif self.max_continuous_silence <= 0 :
+
+            elif self.max_continuous_silence <= 0:
                 # max token reached at this frame will _deliver if _contiguous_token
                 # and not _strict_min_length
                 self._process_end_of_detection()
                 self._state = self.SILENCE
-                
+
             else:
                 # this is the first silent frame following a valid one
                 # and it is tolerated
@@ -380,24 +373,22 @@
                 self._state = self.POSSIBLE_SILENCE
                 if len(self._data) == self.max_length:
                     self._process_end_of_detection(True)
-                    # don't reset _silence_length because we still 
+                    # don't reset _silence_length because we still
                     # need to know the total number of silent frames
-                                   
-                                
-    
+
         elif self._state == self.POSSIBLE_SILENCE:
-            
+
             if frame_is_valid:
                 self._data.append(frame)
                 self._silence_length = 0
                 self._state = self.NOISE
                 if len(self._data) >= self.max_length:
                     self._process_end_of_detection(True)
-                
+
             else:
                 if self._silence_length >= self.max_continuous_silence:
                     if self._silence_length < len(self._data):
-                        # _deliver only gathered frames aren't all silent                    
+                        # _deliver only gathered frames aren't all silent
                         self._process_end_of_detection()
                     else:
                         self._data = []
@@ -408,32 +399,28 @@
                     self._silence_length += 1
                     if len(self._data) >= self.max_length:
                         self._process_end_of_detection(True)
-                        # don't reset _silence_length because we still 
+                        # don't reset _silence_length because we still
                         # need to know the total number of silent frames
-                        
-    
+
     def _post_process(self):
         if self._state == self.NOISE or self._state == self.POSSIBLE_SILENCE:
             if len(self._data) > 0 and len(self._data) > self._silence_length:
                 self._process_end_of_detection()
-    
-    
+
     def _process_end_of_detection(self, truncated=False):
-        
+
         if not truncated and self._drop_tailing_silence and self._silence_length > 0:
             # happens if max_continuous_silence is reached
             # or max_length is reached at a silent frame
             self._data = self._data[0: - self._silence_length]
-        
+
         if (len(self._data) >= self.min_length) or \
-           (len(self._data) > 0 and \
-            not self._strict_min_length and self._contiguous_token):
-            
-            
-            
+           (len(self._data) > 0 and
+                not self._strict_min_length and self._contiguous_token):
+
             _end_frame = self._start_frame + len(self._data) - 1
             self._deliver(self._data, self._start_frame, _end_frame)
-            
+
             if truncated:
                 # next token (if any) will start at _current_frame + 1
                 self._start_frame = self._current_frame + 1
@@ -442,11 +429,9 @@
             else:
                 self._contiguous_token = False
         else:
-            self._contiguous_token = False       
-        
+            self._contiguous_token = False
+
         self._data = []
-            
-    
-    
+
     def _append_token(self, data, start, end):
         self._tokens.append((data, start, end))
--- a/auditok/dataset.py	Sat Jan 28 12:25:34 2017 +0100
+++ b/auditok/dataset.py	Sat Jan 28 12:47:36 2017 +0100
@@ -12,8 +12,7 @@
 16000_mono_bc_noise.wav".format(cd=_current_dir, sep=os.path.sep)
 """A wave file that contains a pronunciation of Arabic numbers from 1 to 6"""
 
-
 was_der_mensch_saet_mono_44100_lead_trail_silence = "{cd}{sep}data{sep}was_\
 der_mensch_saet_das_wird_er_vielfach_ernten_44100Hz_mono_lead_trail_\
 silence.wav".format(cd=_current_dir, sep=os.path.sep)
-""" A wave file that contains a sentence between long leading and trailing periods of silence"""
\ No newline at end of file
+""" A wave file that contains a sentence between long leading and trailing periods of silence"""
--- a/auditok/exceptions.py	Sat Jan 28 12:25:34 2017 +0100
+++ b/auditok/exceptions.py	Sat Jan 28 12:47:36 2017 +0100
@@ -3,7 +3,6 @@
 @author: Amine SEHILI <amine.sehili@gmail.com>
 """
 
+
 class DuplicateArgument(Exception):
     pass
-
-
--- a/auditok/io.py	Sat Jan 28 12:25:34 2017 +0100
+++ b/auditok/io.py	Sat Jan 28 12:47:36 2017 +0100
@@ -39,84 +39,82 @@
 class AudioSource():
     """ 
     Base class for audio source objects.
-        
+
     Subclasses should implement methods to open/close and audio stream 
     and read the desired amount of audio samples.
-    
+
     :Parameters:
-        
+
         `sampling_rate` : int
             Number of samples per second of audio stream. Default = 16000.
-        
+
         `sample_width` : int
             Size in bytes of one audio sample. Possible values : 1, 2, 4.
             Default = 2.
-            
+
         `channels` : int
             Number of channels of audio stream. The current version supports
             only mono audio streams (i.e. one channel).
     """
-    
+
     __metaclass__ = ABCMeta
 
-    def __init__(self, sampling_rate = DEFAULT_SAMPLE_RATE,
-                 sample_width = DEFAULT_SAMPLE_WIDTH,
-                 channels = DEFAULT_NB_CHANNELS):
-  
+    def __init__(self, sampling_rate=DEFAULT_SAMPLE_RATE,
+                 sample_width=DEFAULT_SAMPLE_WIDTH,
+                 channels=DEFAULT_NB_CHANNELS):
+
         if not sample_width in (1, 2, 4):
             raise ValueError("Sample width must be one of: 1, 2 or 4 (bytes)")
-        
+
         if channels != 1:
             raise ValueError("Only mono audio is currently handled")
-            
+
         self.sampling_rate = sampling_rate
         self.sample_width = sample_width
         self.channels = channels
-      
+
     @abstractmethod
     def is_open(self):
         """ Return True if audio source is open, False otherwise """
-    
+
     @abstractmethod
     def open(self):
         """ Open audio source """
-    
+
     @abstractmethod
     def close(self):
         """ Close audio source """
-    
+
     @abstractmethod
     def read(self, size):
         """
         Read and return `size` audio samples at most.
-        
+
         :Parameters:
-        
+
             `size` : int
                 the number of samples to read.
-            
+
         :Returns:
-            
+
             Audio data as a string of length 'N' * 'sample_width' * 'channels', where 'N' is:
-            
+
             - `size` if `size` < 'left_samples'
-            
+
             - 'left_samples' if `size` > 'left_samples' 
-        
-        """ 
-    
+        """
+
     def get_sampling_rate(self):
         """ Return the number of samples per second of audio stream """
         return self.sampling_rate
-    
+
     def get_sample_width(self):
         """ Return the number of bytes used to represent one audio sample """
         return self.sample_width
-    
+
     def get_channels(self):
         """ Return the number of channels of this audio source """
         return self.channels
-    
 
 
 class Rewindable():
@@ -126,192 +124,185 @@
     audio stream as well as method to move to an absolute audio position
     expressed in time or in number of samples. 
     """
-    
+
     __metaclass__ = ABCMeta
-    
+
     @abstractmethod
     def rewind(self):
         """ Go back to the beginning of audio stream """
         pass
-    
+
     @abstractmethod
     def get_position(self):
         """ Return the total number of already read samples """
-    
+
     @abstractmethod
     def get_time_position(self):
         """ Return the total duration in seconds of already read data """
-    
+
     @abstractmethod
     def set_position(self, position):
         """ Move to an absolute position 
-        
+
         :Parameters:
-        
+
             `position` : int
                 number of samples to skip from the start of the stream
         """
-    
+
     @abstractmethod
     def set_time_position(self, time_position):
         """ Move to an absolute position expressed in seconds
-        
+
         :Parameters:
-        
+
             `time_position` : float
                 seconds to skip from the start of the stream
         """
         pass
 
-    
 
 class BufferAudioSource(AudioSource, Rewindable):
     """
     An :class:`AudioSource` that encapsulates and reads data from a memory buffer.
     It implements methods from :class:`Rewindable` and is therefore a navigable :class:`AudioSource`.
     """
-    
+
     def __init__(self, data_buffer,
-                 sampling_rate = DEFAULT_SAMPLE_RATE,
-                 sample_width = DEFAULT_SAMPLE_WIDTH,
-                 channels = DEFAULT_NB_CHANNELS):
-        
-        if len(data_buffer) % (sample_width * channels) !=0:
+                 sampling_rate=DEFAULT_SAMPLE_RATE,
+                 sample_width=DEFAULT_SAMPLE_WIDTH,
+                 channels=DEFAULT_NB_CHANNELS):
+
+        if len(data_buffer) % (sample_width * channels) != 0:
             raise ValueError("length of data_buffer must be a multiple of (sample_width * channels)")
-        
+
         AudioSource.__init__(self, sampling_rate, sample_width, channels)
         self._buffer = data_buffer
         self._index = 0
         self._left = 0 if self._buffer is None else len(self._buffer)
         self._is_open = False
-    
+
     def is_open(self):
         return self._is_open
-        
+
     def open(self):
         self._is_open = True
-    
+
     def close(self):
         self._is_open = False
         self.rewind()
-    
+
     def read(self, size):
         if not self._is_open:
             raise IOError("Stream is not open")
-        
+
         if self._left > 0:
-            
-            to_read = size * self.sample_width * self.channels       
+
+            to_read = size * self.sample_width * self.channels
             if to_read > self._left:
-                to_read = self._left 
-                            
+                to_read = self._left
+
             data = self._buffer[self._index: self._index + to_read]
             self._index += to_read
             self._left -= to_read
-            
+
             return data
-        
+
         return None
-    
+
     def get_data_buffer(self):
         """ Return all audio data as one string buffer. """
         return self._buffer
-    
+
     def set_data(self, data_buffer):
         """ Set new data for this audio stream. 
-        
+
         :Parameters:
-        
+
             `data_buffer` : str, basestring, Bytes
                 a string buffer with a length multiple of (sample_width * channels)
         """
-        if len(data_buffer) % (self.sample_width * self.channels) !=0:
+        if len(data_buffer) % (self.sample_width * self.channels) != 0:
             raise ValueError("length of data_buffer must be a multiple of (sample_width * channels)")
         self._buffer = data_buffer
         self._index = 0
         self._left = 0 if self._buffer is None else len(self._buffer)
-    
+
     def append_data(self, data_buffer):
         """ Append data to this audio stream
-        
+
         :Parameters:
-        
+
             `data_buffer` : str, basestring, Bytes
                 a buffer with a length multiple of (sample_width * channels)
         """
-        
-        if len(data_buffer) % (self.sample_width * self.channels) !=0:
+
+        if len(data_buffer) % (self.sample_width * self.channels) != 0:
             raise ValueError("length of data_buffer must be a multiple of (sample_width * channels)")
-        
+
         self._buffer += data_buffer
         self._left += len(data_buffer)
 
-    
     def rewind(self):
         self.set_position(0)
-    
+
     def get_position(self):
         return self._index / self.sample_width
-    
+
     def get_time_position(self):
-        return float(self._index) / (self.sample_width * self.sampling_rate) 
-    
+        return float(self._index) / (self.sample_width * self.sampling_rate)
+
     def set_position(self, position):
         if position < 0:
             raise ValueError("position must be >= 0")
-        
+
         if self._buffer is None:
             self._index = 0
             self._left = 0
             return
-         
-        position *= self.sample_width 
+
+        position *= self.sample_width
         self._index = position if position < len(self._buffer) else len(self._buffer)
         self._left = len(self._buffer) - self._index
 
-
-    def set_time_position(self, time_position): # time in seconds
+    def set_time_position(self, time_position):  # time in seconds
         position = int(self.sampling_rate * time_position)
         self.set_position(position)
 
 
-
 class WaveAudioSource(AudioSource):
     """
     A class for an `AudioSource` that reads data from a wave file.
-    
+
     :Parameters:
-        
+
         `filename` :
             path to a valid wave file
     """
-    
+
     def __init__(self, filename):
-        
+
         self._filename = filename
         self._audio_stream = None
-        
+
         stream = wave.open(self._filename)
         AudioSource.__init__(self, stream.getframerate(),
-                                   stream.getsampwidth(),
-                                   stream.getnchannels())
+                             stream.getsampwidth(),
+                             stream.getnchannels())
         stream.close()
-    
-    
+
     def is_open(self):
         return self._audio_stream is not None
- 
+
     def open(self):
         if(self._audio_stream is None):
             self._audio_stream = wave.open(self._filename)
-      
-        
+
     def close(self):
         if self._audio_stream is not None:
             self._audio_stream.close()
             self._audio_stream = None
-        
-    
+
     def read(self, size):
         if self._audio_stream is None:
             raise IOError("Stream is not open")
@@ -326,174 +317,165 @@
     """
     A class for an `AudioSource` that reads data the built-in microphone using PyAudio. 
     """
-    
-    def __init__(self, sampling_rate = DEFAULT_SAMPLE_RATE,
-                 sample_width = DEFAULT_SAMPLE_WIDTH,
-                 channels = DEFAULT_NB_CHANNELS,
-                 frames_per_buffer = 1024):
-        
-        
+
+    def __init__(self, sampling_rate=DEFAULT_SAMPLE_RATE,
+                 sample_width=DEFAULT_SAMPLE_WIDTH,
+                 channels=DEFAULT_NB_CHANNELS,
+                 frames_per_buffer=1024):
+
         AudioSource.__init__(self, sampling_rate, sample_width, channels)
         self._chunk_size = frames_per_buffer
-        
+
         import pyaudio
         self._pyaudio_object = pyaudio.PyAudio()
-        self._pyaudio_format = self._pyaudio_object.get_format_from_width(self.sample_width) 
+        self._pyaudio_format = self._pyaudio_object.get_format_from_width(self.sample_width)
         self._audio_stream = None
 
-    
     def is_open(self):
         return self._audio_stream is not None
-    
+
     def open(self):
-        self._audio_stream = self._pyaudio_object.open(format = self._pyaudio_format,
-                                                   channels = self.channels,
-                                                   rate = self.sampling_rate,
-                                                   input = True,
-                                                   output = False,
-                                                   frames_per_buffer = self._chunk_size)
-        
-        
+        self._audio_stream = self._pyaudio_object.open(format=self._pyaudio_format,
+                                                       channels=self.channels,
+                                                       rate=self.sampling_rate,
+                                                       input=True,
+                                                       output=False,
+                                                       frames_per_buffer=self._chunk_size)
+
     def close(self):
         if self._audio_stream is not None:
             self._audio_stream.stop_stream()
             self._audio_stream.close()
             self._audio_stream = None
-            
-    
+
     def read(self, size):
         if self._audio_stream is None:
             raise IOError("Stream is not open")
-        
+
         if self._audio_stream.is_active():
             data = self._audio_stream.read(size)
             if data is None or len(data) < 1:
                 return None
             return data
-        
+
         return None
-    
+
 
 class StdinAudioSource(AudioSource):
     """
     A class for an :class:`AudioSource` that reads data from standard input.
     """
-    
-    def __init__(self, sampling_rate = DEFAULT_SAMPLE_RATE,
-                 sample_width = DEFAULT_SAMPLE_WIDTH,
-                 channels = DEFAULT_NB_CHANNELS):
-    
+
+    def __init__(self, sampling_rate=DEFAULT_SAMPLE_RATE,
+                 sample_width=DEFAULT_SAMPLE_WIDTH,
+                 channels=DEFAULT_NB_CHANNELS):
+
         AudioSource.__init__(self, sampling_rate, sample_width, channels)
         self._is_open = False
-    
-    
+
     def is_open(self):
         return self._is_open
-        
+
     def open(self):
         self._is_open = True
-    
+
     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
         data = sys.stdin.read(to_read)
-        
+
         if data is None or len(data) < 1:
             return None
-        
+
         return data
-       
-           
+
+
 class PyAudioPlayer():
     """
     A class for audio playback using Pyaudio
     """
-    
-    def __init__(self, sampling_rate = DEFAULT_SAMPLE_RATE,
-                 sample_width = DEFAULT_SAMPLE_WIDTH,
-                 channels = DEFAULT_NB_CHANNELS):
+
+    def __init__(self, sampling_rate=DEFAULT_SAMPLE_RATE,
+                 sample_width=DEFAULT_SAMPLE_WIDTH,
+                 channels=DEFAULT_NB_CHANNELS):
         if not sample_width in (1, 2, 4):
             raise ValueError("Sample width must be one of: 1, 2 or 4 (bytes)")
-        
+
         self.sampling_rate = sampling_rate
         self.sample_width = sample_width
         self.channels = channels
-        
+
         import pyaudio
         self._p = pyaudio.PyAudio()
-        self.stream = self._p.open(format = self._p.get_format_from_width(self.sample_width),
-         channels = self.channels, rate = self.sampling_rate,
-         input = False, output = True)
-        
+        self.stream = self._p.open(format=self._p.get_format_from_width(self.sample_width),
+                                   channels=self.channels, rate=self.sampling_rate,
+                                   input=False, output=True)
+
     def play(self, data):
         if self.stream.is_stopped():
             self.stream.start_stream()
-        
+
         for chunk in self._chunk_data(data):
             self.stream.write(chunk)
-            
+
         self.stream.stop_stream()
-    
-    def  stop(self):
+
+    def stop(self):
         if not self.stream.is_stopped():
             self.stream.stop_stream()
         self.stream.close()
         self._p.terminate()
-    
+
     def _chunk_data(self, data):
         # make audio chunks of 100 ms to allow interruption (like ctrl+c)
         chunk_size = int((self.sampling_rate * self.sample_width * self.channels) / 10)
         start = 0
         while start < len(data):
-            yield data[start : start + chunk_size]
+            yield data[start: start + chunk_size]
             start += chunk_size
-        
+
 
 def from_file(filename):
     """
     Create an `AudioSource` object using the audio file specified by `filename`.
     The appropriate :class:`AudioSource` class is guessed from file's extension.
-    
+
     :Parameters:
-    
+
         `filename` :
             path to an audio file.
-        
+
     :Returns:
-    
+
         an `AudioSource` object that reads data from the given file.
-    
     """
-    
+
     if filename.lower().endswith(".wav"):
         return WaveAudioSource(filename)
-    
-    raise Exception("Can not create an AudioSource object from '%s'" %(filename))
+
+    raise Exception("Can not create an AudioSource object from '%s'" % (filename))
 
 
 def player_for(audio_source):
     """
     Return a :class:`PyAudioPlayer` that can play data from `audio_source`.
-    
+
     :Parameters:
-    
+
         `audio_source` : 
             an `AudioSource` object.
-    
+
     :Returns:
-    
+
         `PyAudioPlayer` that has the same sampling rate, sample width and number of channels
         as `audio_source`.
     """
-    
+
     return PyAudioPlayer(audio_source.get_sampling_rate(),
-            audio_source.get_sample_width(),
-            audio_source.get_channels())
-    
-    
-
+                         audio_source.get_sample_width(),
+                         audio_source.get_channels())