U
    ,-e!                     @   sH   d Z ddlmZmZ ddlZddlmZ ddlm	Z	 G dd deZ
dS )	z)
Text/audio processor class for MusicGen
    )ListOptionalN   )ProcessorMixin)to_numpyc                       sb   e Zd ZdZdZdZ fddZddd	Zd
d Zdd Z	dd Z
deeej dddZ  ZS )MusicgenProcessoral  
    Constructs a MusicGen processor which wraps an EnCodec feature extractor and a T5 tokenizer into a single processor
    class.

    [`MusicgenProcessor`] offers all the functionalities of [`EncodecFeatureExtractor`] and [`TTokenizer`]. See
    [`~MusicgenProcessor.__call__`] and [`~MusicgenProcessor.decode`] for more information.

    Args:
        feature_extractor (`EncodecFeatureExtractor`):
            An instance of [`EncodecFeatureExtractor`]. The feature extractor is a required input.
        tokenizer (`T5Tokenizer`):
            An instance of [`T5Tokenizer`]. The tokenizer is a required input.
    ZEncodecFeatureExtractor)ZT5TokenizerZT5TokenizerFastc                    s    t  || | j| _d| _d S )NF)super__init__feature_extractorcurrent_processor_in_target_context_manager)selfr
   	tokenizer	__class__ q/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/transformers/models/musicgen/processing_musicgen.pyr	   +   s    zMusicgenProcessor.__init__NTc                 C   s   | j j|||dS )N)tasklanguageno_timestamps)r   get_decoder_prompt_ids)r   r   r   r   r   r   r   r   0   s    z(MusicgenProcessor.get_decoder_prompt_idsc                 O   s   | j r| j||S |dd}|dd}|dd}t|dkrV|d }|dd }|dkrn|dkrntd|dk	r| j|f|}|dk	r| j|f|d|i|}|dkr|S |dkr|S |d |d< d	|kr|d	 |d	< |S dS )
a  
        Forwards the `audio` argument to EncodecFeatureExtractor's [`~EncodecFeatureExtractor.__call__`] and the `text`
        argument to [`~T5Tokenizer.__call__`]. Please refer to the doctsring of the above two methods for more
        information.
        audioNsampling_ratetextr      zAYou need to specify either an `audio` or `text` input to process.Zinput_valuespadding_mask)r   r   poplen
ValueErrorr   r
   )r   argskwargsr   r   r   inputsZaudio_inputsr   r   r   __call__3   s,    zMusicgenProcessor.__call__c                 O   s`   | dd}| dd}t|dkr8|d }|dd }|dk	rN| j||dS | jj||S dS )ai  
        This method is used to decode either batches of audio outputs from the MusicGen model, or batches of token ids
        from the tokenizer. In the case of decoding token ids, this method forwards all its arguments to T5Tokenizer's
        [`~PreTrainedTokenizer.batch_decode`]. Please refer to the docstring of this method for more information.
        r   Nr   r   r   )r   )r   r   _decode_audior   batch_decode)r   r   r    audio_valuesr   r   r   r   r$   Y   s    zMusicgenProcessor.batch_decodec                 O   s   | j j||S )z
        This method forwards all its arguments to T5Tokenizer's [`~PreTrainedTokenizer.decode`]. Please refer to the
        docstring of this method for more information.
        )r   decode)r   r   r    r   r   r   r&   k   s    zMusicgenProcessor.decode)r   returnc           
      C   s   t |}|j\}}}|dkr$t|S t |}||jd  }d| jj }tj|dd|ffd|d}| }t|D ]>}t	|| || dddf | jjk }	|	
|d||< qp|S )zn
        This method strips any padding from the audio values to return a list of numpy audio arrays.
        Nr   )r   r   r   Zconstant)Zconstant_values)r   shapelistr
   padding_valuenppadtolistrangeZasarrayZreshape)
r   r%   r   ZbszZchannelsZseq_len
differencer+   iZsliced_audior   r   r   r#   r   s    zMusicgenProcessor._decode_audio)NNT)N)__name__
__module____qualname____doc__Zfeature_extractor_classZtokenizer_classr	   r   r"   r$   r&   r   r   r,   Zndarrayr#   __classcell__r   r   r   r   r      s   
&r   )r5   typingr   r   numpyr,   Zprocessing_utilsr   utilsr   r   r   r   r   r   <module>   s
   