
    Sh9                     
   d Z ddlmZmZ ddlZddlZg dZdZdZdZ	 G d d	          Z
 G d
 d          Z G d de
e          Z G d de
          Z G d de
          Z G d de
          Z G d d          Zd Zd ZdS )ag  
Module for low-level audio input-output operations.

Class summary
=============

.. autosummary::

        AudioSource
        Rewindable
        BufferAudioSource
        WaveAudioSource
        PyAudioSource
        StdinAudioSource
        PyAudioPlayer
        

Function summary
================

.. autosummary::

        from_file
        player_for
    )ABCMetaabstractmethodN)	AudioSource
RewindableBufferAudioSourceWaveAudioSourcePyAudioSourceStdinAudioSourcePyAudioPlayer	from_file
player_fori>        c                       e Zd ZdZeZeeefdZ	e
d             Ze
d             Ze
d             Ze
d             Zd Zd Zd	 Zd
S )r   an   
    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).
    c                     |dvrt          d          |dk    rt          d          || _        || _        || _        d S )Nr   r      .Sample width must be one of: 1, 2 or 4 (bytes)r   z$Only mono audio is currently handled)
ValueErrorsampling_ratesample_widthchannelsselfr   r   r   s       P/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/auditok/io.py__init__zAudioSource.__init__>   sQ     y((MNNNq==CDDD*(     c                     dS )z6 Return True if audio source is open, False otherwise N r   s    r   is_openzAudioSource.is_openL         r   c                     dS )z Open audio source Nr   r    s    r   openzAudioSource.openP   r"   r   c                     dS )z Close audio source Nr   r    s    r   closezAudioSource.closeT   r"   r   c                     dS )a  
        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' * 'smaple_width' * 'channels', where 'N' is:
            
            - `size` if `size` < 'left_samples'
            
            - 'left_samples' if `size` > 'left_samples' 
        
        Nr   )r   sizes     r   readzAudioSource.readX   r"   r   c                     | j         S )z9 Return the number of samples per second of audio stream )r   r    s    r   get_sampling_ratezAudioSource.get_sampling_ratel   s    !!r   c                     | j         S )z? Return the number of bytes used to represent one audio sample )r   r    s    r   get_sample_widthzAudioSource.get_sample_widthp   s      r   c                     | j         S )z4 Return the number of channels of this audio source )r   r    s    r   get_channelszAudioSource.get_channelst   s
    }r   N)__name__
__module____qualname____doc__r   __metaclass__DEFAULT_SAMPLE_RATEDEFAULT_SAMPLE_WIDTHDEFAULT_NB_CHANNELSr   r   r!   r$   r&   r)   r+   r-   r/   r   r   r   r   r   '   s         ( M': 4/! ! ! ! E E ^E " " ^" # # ^#   ^&" " "! ! !    r   r   c                       e Zd ZdZeZed             Zed             Zed             Z	ed             Z
ed             ZdS )r   z
    Base class for rewindable audio streams.
    Subclasses should implement methods to return to the beginning of an
    audio stream as well as method to move to an absolute audio position
    expressed in time or in number of samples. 
    c                     dS )z* Go back to the beginning of audio stream Nr   r    s    r   rewindzRewindable.rewind   s	     	r   c                     dS )z1 Return the total number of already read samples Nr   r    s    r   get_positionzRewindable.get_position   r"   r   c                     dS )z; Return the total duration in seconds of already read data Nr   r    s    r   get_time_positionzRewindable.get_time_position   r"   r   c                     dS )z Move to an absolute position 
        
        :Parameters:
        
            `position` : int
                number of samples to skip from the start of the stream
        Nr   r   positions     r   set_positionzRewindable.set_position   r"   r   c                     dS )z Move to an absolute position expressed in seconds
        
        :Parameters:
        
            `time_position` : float
                seconds to skip from the start of the stream
        Nr   )r   time_positions     r   set_time_positionzRewindable.set_time_position   s	     	r   N)r0   r1   r2   r3   r   r4   r   r:   r<   r>   rB   rE   r   r   r   r   r   z   s          M  ^ @ @ ^@ J J ^J   ^   ^  r   r   c                   h    e Zd ZdZeeefdZd Zd Z	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd ZdS )r   z
    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`.
    c                     t          |          ||z  z  dk    rt          d          t                              | |||           || _        d| _        | j        dnt          | j                  | _        d| _        d S )Nr   Elength of data_buffer must be a multiple of (sample_width * channels)F)lenr   r   r   _buffer_index_left_is_open)r   data_bufferr   r   r   s        r   r   zBufferAudioSource.__init__   s    
 {|h67!;;deeeT=,III",.QQC4E4E
r   c                     | j         S NrM   r    s    r   r!   zBufferAudioSource.is_open   
    }r   c                     d| _         d S NTrQ   r    s    r   r$   zBufferAudioSource.open       r   c                 <    d| _         |                                  d S NF)rM   r:   r    s    r   r&   zBufferAudioSource.close   s    r   c                    | j         st          d          | j        dk    rb|| j        z  | j        z  }|| j        k    r| j        }| j        | j        | j        |z            }| xj        |z  c_        | xj        |z  c_        |S d S )NStream is not openr   )rM   IOErrorrL   r   r   rJ   rK   r   r(   to_readdatas       r   r)   zBufferAudioSource.read   s    } 	0.///:>>T..>G##*<T[7-B BCDKK7"KKJJ'!JJKtr   c                     | j         S )z- Return all audio data as one string buffer. )rJ   r    s    r   get_data_bufferz!BufferAudioSource.get_data_buffer   s
    |r   c                     t          |          | j        | j        z  z  dk    rt          d          || _        d| _        | j        dnt          | j                  | _        dS )z Set new data for this audio stream. 
        
        :Parameters:
        
            `data_buffer` : str, basestring, Bytes
                a string buffer with a length multiple of (sample_width * channels)
        r   rH   N)rI   r   r   r   rJ   rK   rL   r   rN   s     r   set_datazBufferAudioSource.set_data   sc     {t04=@AAEEdeee",.QQC4E4E


r   c                     t          |          | j        | j        z  z  dk    rt          d          | xj        |z  c_        | xj        t          |          z  c_        dS )z Append data to this audio stream
        
        :Parameters:
        
            `data_buffer` : str, basestring, Bytes
                a buffer with a length multiple of (sample_width * channels)
        r   rH   N)rI   r   r   r   rJ   rL   ra   s     r   append_datazBufferAudioSource.append_data   sa     {t04=@AAEEdeee#

c+&&&



r   c                 0    |                      d           d S Nr   )rB   r    s    r   r:   zBufferAudioSource.rewind   s    !r   c                      | j         | j        z  S rP   )rK   r   r    s    r   r<   zBufferAudioSource.get_position   s    {T...r   c                 J    t          | j                  | j        | j        z  z  S rP   )floatrK   r   r   r    s    r   r>   z#BufferAudioSource.get_time_position   s"    T[!!T%69K%KLLr   c                    |dk     rt          d          | j        d| _        d| _        d S || j        z  }|t          | j                  k     r|nt          | j                  | _        t          | j                  | j        z
  | _        d S )Nr   zposition must be >= 0)r   rJ   rK   rL   r   rI   r@   s     r   rB   zBufferAudioSource.set_position  s    a<<4555<DKDJFD%%"*S->->">">hhCDUDU&&4


r   c                 ^    t          | j        |z            }|                     |           d S rP   )intr   rB   )r   rD   rA   s      r   rE   z#BufferAudioSource.set_time_position  s1    t)M9::(#####r   N)r0   r1   r2   r3   r5   r6   r7   r   r!   r$   r&   r)   r_   rb   rd   r:   r<   r>   rB   rE   r   r   r   r   r      s          "5 4/           $  F F F' ' '   / / /M M M5 5 5$ $ $ $ $r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )r   z
    A class for an `AudioSource` that reads data from a wave file.
    
    :Parameters:
        
        `filename` :
            path to a valid wave file
    c                 "   || _         d | _        t          j        | j                   }t                              | |                                |                                |                                           |	                                 d S rP   )
	_filename_audio_streamwaver$   r   r   getframerategetsampwidthgetnchannelsr&   )r   filenamestreams      r   r   zWaveAudioSource.__init__!  s{    !!4>**T6#6#6#8#8#)#6#6#8#8#)#6#6#8#8	: 	: 	: 	r   c                     | j         d uS rP   rp   r    s    r   r!   zWaveAudioSource.is_open-      !--r   c                 T    | j          t          j        | j                  | _         d S d S rP   )rp   rq   r$   ro   r    s    r   r$   zWaveAudioSource.open0  s-    %!%4>!:!:D &%r   c                 X    | j         "| j                                          d | _         d S d S rP   )rp   r&   r    s    r   r&   zWaveAudioSource.close5  s6    )$$&&&!%D *)r   c                     | j         t          d          | j                             |          }|t          |          dk     rd S |S NrY   r   )rp   rZ   
readframesrI   r   r(   r]   s      r   r)   zWaveAudioSource.read;  sM    %.///%0066D|s4yy1}}tKr   N)	r0   r1   r2   r3   r   r!   r$   r&   r)   r   r   r   r   r     si         	 	 	. . .; ; ;
& & &    r   r   c                   :    e Zd ZdZeeedfdZd Zd Z	d Z
d ZdS )	r	   z^
    A class for an `AudioSource` that reads data the built-in microphone using PyAudio. 
    i   c                     t                               | |||           || _        dd l}|                                | _        | j                            | j                  | _        d | _	        d S rf   )
r   r   _chunk_sizepyaudioPyAudio_pyaudio_objectget_format_from_widthr   _pyaudio_formatrp   )r   r   r   r   frames_per_bufferr   s         r   r   zPyAudioSource.__init__J  sk     	T=,III,&00#3II$J[\\!r   c                     | j         d uS rP   rx   r    s    r   r!   zPyAudioSource.is_openY  ry   r   c                 x    | j                             | j        | j        | j        dd| j                  | _        d S )NTF)formatr   rateinputoutputr   )r   r$   r   r   r   r   rp   r    s    r   r$   zPyAudioSource.open\  sF    !166@T>Bm:>:L;?<AGKGW 7 Y Yr   c                     | j         ;| j                                          | j                                          d | _         d S d S rP   )rp   stop_streamr&   r    s    r   r&   zPyAudioSource.closee  sK    )**,,,$$&&&!%D *)r   c                     | j         t          d          | j                                         r3| j                             |          }|t	          |          dk     rd S |S d S r}   )rp   rZ   	is_activer)   rI   r   s      r   r)   zPyAudioSource.readl  si    %.///'')) 	%**400D|s4yy1}}tKtr   Nr0   r1   r2   r3   r5   r6   r7   r   r!   r$   r&   r)   r   r   r   r	   r	   E  s          (; 4/%)" " " ". . .Y Y Y& & &
 
 
 
 
r   r	   c                   8    e Zd ZdZeeefdZd Zd Z	d Z
d ZdS )r
   zR
    A class for an :class:`AudioSource` that reads data from standard input.
    c                 N    t                               | |||           d| _        d S rW   )r   r   rM   r   s       r   r   zStdinAudioSource.__init__~  s)     	T=,IIIr   c                     | j         S rP   rQ   r    s    r   r!   zStdinAudioSource.is_open  rR   r   c                     d| _         d S rT   rQ   r    s    r   r$   zStdinAudioSource.open  rU   r   c                     d| _         d S rW   rQ   r    s    r   r&   zStdinAudioSource.close  s    r   c                     | j         st          d          || j        z  | j        z  }t          j                            |          }|t          |          dk     rd S |S r}   )rM   rZ   r   r   sysstdinr)   rI   r[   s       r   r)   zStdinAudioSource.read  s_    } 	0.///**T]:y~~g&&<3t99q==4r   Nr   r   r   r   r
   r
   y  sy          (; 4/         
 
 
 
 
r   r
   c                   2    e Zd ZdZeeefdZd Zd Z	d Z
dS )r   z2
    A class for audio playback using Pyaudio
    c                 &   |dvrt          d          || _        || _        || _        dd l}|                                | _        | j                            | j                            | j                  | j        | j        dd          | _	        d S )Nr   r   r   FT)r   r   r   r   r   )
r   r   r   r   r   r   _pr$   r   rv   )r   r   r   r   r   s        r   r   zPyAudioPlayer.__init__  s     y((MNNN*( //##gllDG,I,I$J[,\,\M$*< # ' 'r   c                     | j                                         r| j                                          |                     |          D ]}| j                             |           | j                                          d S rP   )rv   
is_stoppedstart_stream_chunk_datawriter   )r   r]   chunks      r   playzPyAudioPlayer.play  s|    ;!!## 	'K$$&&&%%d++ 	% 	%EKe$$$$!!!!!r   c                     | j                                         s| j                                          | j                                          | j                                         d S rP   )rv   r   r   r&   r   	terminater    s    r   stopzPyAudioPlayer.stop  sZ    {%%'' 	&K##%%%r   c              #      K   t          | j        | j        z  | j        z  dz            }d}|t	          |          k     r)||||z            V  ||z  }|t	          |          k     'd S d S )N
   r   )rl   r   r   r   rI   )r   r]   
chunk_sizestarts       r   r   zPyAudioPlayer._chunk_data  s      $,t/@@4=PTVVWW
c$iiuuz112222ZE c$iir   N)r0   r1   r2   r3   r5   r6   r7   r   r   r   r   r   r   r   r   r     sj          (; 4/' ' ' ' " " "           r   r   c                     |                                                      d          rt          |           S t          d| z            )a`  
    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.
    
    z.wavz.Can not create an AudioSource object from '%s')lowerendswithr   	Exception)ru   s    r   r   r     sF      ~~  (( )x(((
DxP
Q
QQr   c                     t          |                                 |                                 |                                           S )a;  
    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`.
    )r   r+   r-   r/   )audio_sources    r   r   r     sB     7799))++%%'') ) )r   )r3   abcr   r   rq   r   __all__r5   r6   r7   r   r   r   r   r	   r
   r   r   r   r   r   r   <module>r      s   4 ( ' ' ' ' ' ' '  



\ \ \    O O O O O O O Of* * * * * * * *\k$ k$ k$ k$ k$Z k$ k$ k$^+ + + + +k + + +\1 1 1 1 1K 1 1 1h         {      F*  *  *  *  *  *  *  * ZR R R,) ) ) ) )r   