o
    Si                     @   s   d dl Z d dlmZmZ d dlmZmZmZmZm	Z	m
Z
 d dlZd dlmZmZ d dlmZmZmZ eG dd dZeG dd	 d	eZdS )
    N)asdict	dataclass)AnyDictListOptionalSequenceUnion)FeatureExtractorregister_extractor)Secondscompute_num_framesis_module_availablec                   @   s  e Zd ZU dZdZeeef ed< dZ	eeef ed< dZ
ee ed< dZeed	< dZee ed
< dZee ed< dZeeee f ed< dZeed< dZeed< dZee ed< dZeed< deeef fddZedeeef dd fddZedd ZdS )OpenSmileConfiga  
    OpenSmile configs are stored in separated txt files in its specific format.
    You can specify predefined config by setting ``feature_set`` and ``feature_level``
    class attributes with:
    (1) ``FeatureSet`` and ``FeatureLevel`` classes predefined in
    https://github.com/audeering/opensmile-python/blob/master/opensmile/core/define.py
    OR
    (2) strings refered to enum members,
    In opensmile-python You can also create your own config file and pass its path and
    corresponding feature level as documented here
    https://audeering.github.io/opensmile-python/usage.html#custom-config.
    For now custom configs are not supported in this extractor.
    ComParE_2016feature_setlldfeature_levelNoptions   loglevellogfilesampling_rater   channelsFmixdownresample   num_workersverbosereturnc                 C   s   t | S N)r   self r#   M/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/features/opensmile.pyto_dict,   s   zOpenSmileConfig.to_dictdatac                 C   s   t di | S )Nr#   )r   )r&   r#   r#   r$   	from_dict/   s   zOpenSmileConfig.from_dictc                  C   s$   t dsJ dddl} t| jjS )zf
        Returns list of strings with names of pretrained FeatureSets available in opensmile.
        	opensmileBTo use opensmile extractors, please "pip install opensmile" first.r   N)r   r(   list
FeatureSet__members__)r(   r#   r#   r$   featuresets_names3   s   z!OpenSmileConfig.featuresets_names)__name__
__module____qualname____doc__r   r	   strr   __annotations__r   r   r   dictr   intr   r   r   r   r   boolr   r   r   r   r%   staticmethodr'   r-   r#   r#   r#   r$   r      s$   
 r   c                       s   e Zd ZdZdZeZddee f fddZ	e
dee fdd	Zdefd
dZe
defddZdedefddZdee fdd	ZdejdedejfddZdejdejdedejfddZ  ZS )OpenSmileExtractorz<Wrapper for extraction of features implemented in OpenSmile.zopensmile-extractorNconfigc                    s   t  j|d tdsJ ddd l}t| jjtr#|j| jj | _n| jj| _|	| jj
| _
|j| j| j
| jj| jj| jj| jj| jj| jj| jj| jj| jjd| _d S )N)r9   r(   r)   r   )r   r   r   r   r   r   r   r   r   r   r   )super__init__r   r(   
isinstancer9   r   r2   r+   FeatureLevelr   Smiler   r   r   r   r   r   r   r   r   smileExtractor)r"   r9   r(   	__class__r#   r$   r;   G   s0   
zOpenSmileExtractor.__init__r   c                 C   s   | j jS r    r?   feature_namesr!   r#   r#   r$   rC   a   s   z OpenSmileExtractor.feature_namesc                 C   s$   ddl m} | j|ju p| j|ju S )Nr   )r=   )r(   r=   r   LowLevelDescriptorsLowLevelDescriptors_Deltas)r"   r=   r#   r#   r$   is_lld_or_lld_dee   s   
z#OpenSmileExtractor.is_lld_or_lld_dec                 C   s8   dd l }|  r| j|jj v rdS td| jj )Nr   g{Gz?zmframe_shift is not defined for Functionals feature level or for non default feature set. Defined featureset: )r(   rF   r   r+   r,   valuesNotImplementedErrorr9   )r"   r(   r#   r#   r$   frame_shiftm   s   zOpenSmileExtractor.frame_shiftr   c                 C   s
   t | jS r    )lenrC   )r"   r   r#   r#   r$   feature_dim|      
zOpenSmileExtractor.feature_dimc                 C   s
   | j  S r    rB   r!   r#   r#   r$   rC      rL   samplesc                 C   sl   | j jd ur| j j|krtd| d| j j ddd l}| jj||d }|  r2| |||}|	 S )NzGiven sampling rate (z4) mismatched with the value set in OpenSmileConfig (z).r   )r   )
r9   r   
ValueErrorr(   r?   process_signalto_numpyrF   _pad_framescopy)r"   rM   r   r(   featsr#   r#   r$   extract   s   zOpenSmileExtractor.extractrS   c                 C   s   t |d | }t|| j|t |d  }t|dkr%td|  |dkr<t j||| dddf dd}|S |dk rK|d| ddf }|S )zXAdds last diff frames to the end of feats matrix to fit lhotse.utils.compute_num_frames.r   r      z(Unusual difference in number of frames: N)axis)npshaper   rI   abswarningswarnappend)r"   rM   rS   r   durationdiffr#   r#   r$   rQ      s   "zOpenSmileExtractor._pad_framesr    )r.   r/   r0   r1   namer   config_typer   r   r;   propertyr   r2   rC   r6   rF   r   rI   r5   rK   rW   ndarrayrT   rQ   __classcell__r#   r#   r@   r$   r8   @   s,    r8   )rZ   dataclassesr   r   typingr   r   r   r   r   r	   numpyrW   lhotse.features.baser
   r   lhotse.utilsr   r   r   r   r8   r#   r#   r#   r$   <module>   s     4