o
    2wi7E                     @   s|  d dl mZ d dlmZmZ d dlZd dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ e dd	 Ze ejd
ejdddejddejdddddejddejdddddejddejdddddejddddd
edee dee dee d ef
d!d"Ze ejd#ejdddd$dejd%ejdddejd&edd'd(ejd)d*edd+d(ejd,d-eg d.d/d0d(ejd1edd2d(d#ed%ed3ed4ee d5ed6efd7d8Ze ejd#ejdddd$dejd%ejdddejd9ed:d;d(ejd<ed=d>d(ejd?d)ed@dAd(ejd1edd2d(d#ed%edBedCedDed6efdEdFZe ejd#ejdddd$dejd%ejdddejd<ed=dGd(d#ed%edCefdHdIZe ejdJdKejddddLejd
e ddJee d
efdMdNZe ejdJdKejddddLejd
e ddJee d
efdOdPZejeddQdRejdSejdddd$dejd
ejdddejdTddUdejd)dVeddWdXejdYdZeg d[d\d]d(ejd^edd_d(dSed
ed`edaedbedcefdddeZ e ejdJdKejddddLejd
e ddJee d
efdfdgZ!e ejdSejdddd$dejd
ejdddejd)dhdedidjdSed
edkee fdldmZ"e ejdnejdddd$dejdoe ddnedoefdpdqZ#e ejdnejdddd$ddnefdrdsZ$ejeddQdRejdnejdddd$dejdtedejddue%dvdejddwedxdyd(ejdzdd{d|ejd}dd~d|ejdddd|ejdddd|dnedtedee% dededededefddZ&e ejdnejdddd$dejddde%ddjejddde%ddjdnede%dee% ddfddZ'dS )    )Path)ListOptionalN)cli)CutSetappend_cutsmix_cutsload_manifest_lazy_or_eager)Pathlikec                   C   s   dS )z)Group of commands used to create CutSets.N r   r   r   Q/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/lhotse/bin/modes/cut.pycut   s   r   output_cut_manifestT)
allow_dash)typez-rz--recording-manifestF)existsdir_okayzPOptional recording manifest - will be used to attach the recordings to the cuts.)r   helpz-fz--feature-manifestzLOptional feature manifest - will be used to attach the features to the cuts.z-sz--supervision-manifestzTOptional supervision manifest - will be used to attach the supervisions to the cuts.z--force-eagerzForce reading full manifests into memory before creating the manifests (useful when you are not sure about the input manifest sorting).)is_flagr   recording_manifestfeature_manifestsupervision_manifestforce_eagerc           	      C   sl   dd |||fD \}}}t dd |||fD r'|s'tj|||| dd dS tj|||d}||  dS )	aE  
    Create a CutSet stored in OUTPUT_CUT_MANIFEST. Depending on the provided options, it may contain any combination
    of recording, feature and supervision manifests.
    Either RECORDING_MANIFEST or FEATURE_MANIFEST has to be provided.
    When SUPERVISION_MANIFEST is provided, the cuts time span will correspond to that of the supervision segments.
    Otherwise, that time span corresponds to the one found in features, if available, otherwise recordings.

    .. hint::
        ``--force-eager`` must be used when the RECORDING_MANIFEST is not sorted by recording ID.
    c                 S   s    g | ]}|d urt |nd qS Nr	   ).0pr   r   r   
<listcomp>=   s    zsimple.<locals>.<listcomp>c                 s   s    | ]
}|d u p
|j V  qd S r   )is_lazy)r   mr   r   r   	<genexpr>C   s
    
zsimple.<locals>.<genexpr>T)
recordingssupervisionsfeaturesoutput_pathlazy)r!   r"   r#   N)allr   from_manifeststo_file)	r   r   r   r   r   supervision_setfeature_setrecording_setcut_setr   r   r   simple   s(   +
r-   cuts)r   r   r   output_cutsz(--keep-overlapping/--discard-overlappingzwhen `False`, it will discard parts of other supervisions that overlap with the
            main supervision. In the illustration, it would discard `Sup2` in `Cut1` and `Sup1` in `Cut2`.)r   defaultr   z-dz--min-durationa  An optional duration in seconds; specifying this argument will extend the cuts
            that would have been shorter than `min_duration` with actual acoustic context in the recording/features.
            If there are supervisions present in the context, they are kept when `keep_overlapping` is true.
            If there is not enough context, the returned cut will be shorter than `min_duration`.
            If the supervision segment is longer than `min_duration`, the return cut will be longer.z-cz--context-direction)centerleftrightrandomr1   zWhich direction should the cut be expanded towards to include context.
            The value of "center" implies equal expansion to left and right;
            random uniformly samples a value between "left" and "right".z,--keep-all-channels/--discard-extra-channelszIf ``True``, the output cut will have the same channels as the input cut. By default,
            the trimmed cut will have the same channels as the supervision.keep_overlappingmin_durationcontext_directionkeep_all_channelsc                 C   \   t | } t |}| j||||dD ]}|| qW d   dS 1 s'w   Y  dS )z
    Splits each input cut into as many cuts as there are supervisions.
    These cuts have identical start times and durations as the supervisions.
    When there are overlapping supervisions, they can be kept or discarded with options.
    )r5   r6   r7   r8   N)r   	from_fileopen_writertrim_to_supervisionswrite)r.   r/   r5   r6   r7   r8   writerr   r   r   r   r<   X   s   
2
"r<   z--typewordz"Alignment type to use for trimmingz--max-pauseg        z=Merge alignments separated by a pause shorter than this valuez--delimiter z@Delimiter to use for concatenating alignment symbols for mergingr   	max_pause	delimiterc                 C   r9   )a  
    Return a new CutSet with Cuts that have identical spans as the alignments of
    type `type`. An additional `max_pause` is allowed between the alignments to
    merge contiguous alignment items.

    For the case of a multi-channel cut with multiple alignments, we can either trim
    while respecting the supervision channels (in which case output cut has the same channels
    as the supervision) or ignore the channels (in which case output cut has the same channels
    as the input cut).
    )r   rA   rB   r8   N)r   r:   r;   trim_to_alignmentsr=   )r.   r/   r   rA   rB   r8   r>   r   r   r   r   rC      s   
,
"rC   zEMerge supervision groups separated by a pause shorter than this valuec                 C   sV   t | } t |}| j|dD ]}|| qW d   dS 1 s$w   Y  dS )a5  
    Return a new CutSet with Cuts that have identical spans as the supervision groups.
    An additional `max_pause` is allowed to merge contiguous supervision groups.

    A supervision group is defined as a set of supervisions that are overlapping or
    separated by a pause shorter than `max_pause`.
    )rA   N)r   r:   r;   trim_to_supervision_groupsr=   )r.   r/   rA   r>   r   r   r   r   rD      s   
"rD   cut_manifests)nargsr   c                 C   sZ   dd | D } t |}t|  D ]	}|t| qW d   dS 1 s&w   Y  dS )a  
    Create a CutSet stored in OUTPUT_CUT_MANIFEST by iterating jointly over CUT_MANIFESTS and mixing the Cuts
    on the same positions. E.g. the first output cut is created from the first cuts in each input manifest.
    The mix is performed by summing the features from all Cuts.
    If the CUT_MANIFESTS have different number of Cuts, the mixing ends when the shorter manifest is depleted.
    c                 S      g | ]}t |qS r   r   r:   r   pathr   r   r   r          z"mix_sequential.<locals>.<listcomp>N)r   r;   zipr=   r   )rE   r   wr.   r   r   r   mix_sequential   s   
"rO   c                 C   s^   ddl m} ddlm} |dd | D  }|dd |}tdd	 | D }|| d
S )z
    Create a CutSet stored in OUTPUT_CUT_MANIFEST by matching the Cuts from CUT_MANIFESTS by their recording IDs
    and mixing them together.
    r   )groupby)combinec                 S   rH   r   rI   rJ   r   r   r   r     rL   z'mix_by_recording_id.<locals>.<listcomp>c                 S   s   | j S r   )recording_id)r   r   r   r   <lambda>  s    z%mix_by_recording_id.<locals>.<lambda>c                 s   s    | ]	\}}t |V  qd S r   )r   )r   rR   r.   r   r   r   r      s    
z&mix_by_recording_id.<locals>.<genexpr>N)cytoolz.itertoolzrP   lhotse.manipulationrQ   r   	from_cutsitemsr(   )rE   r   rP   rQ   all_cutsrecording_id_to_cutsmixed_cut_setr   r   r   mix_by_recording_id   s   
r[   )show_default)context_settingscut_manifestz--preserve-idzHShould the cuts preserve IDs (by default, they will get new, random IDs)z--max-durationzCThe maximum duration in seconds of a cut in the resulting manifest.)r   requiredr   z-oz--offset-type)startendr4   r`   zWhere should the truncated cut start: "start" - at the start of the original cut, "end" - MAX_DURATION before the end of the original cut, "random" - randomly choose somewhere between "start" and "end" options.zB--keep-overflowing-supervisions/--discard-overflowing-supervisionsz_When a cut is truncated in the middle of a supervision segment, should the supervision be kept.preserve_idmax_durationoffset_typekeep_overflowing_supervisionsc                 C   s*   t | }|j||||d}|| dS )z
    Truncate the cuts in the CUT_MANIFEST and write them to OUTPUT_CUT_MANIFEST.
    Cuts shorter than MAX_DURATION will not be modified.
    )rc   rd   keep_excessive_supervisionsrb   N)r   r:   truncater(   )r^   r   rb   rc   rd   re   r,   truncated_cut_setr   r   r   rg     s   
,rg   c                 C   sZ   dd | D }t |}t| D ]	}|t| qW d   dS 1 s&w   Y  dS )a{  
    Create a new CutSet by appending the cuts in CUT_MANIFESTS. CUT_MANIFESTS are iterated position-wise (the
    cuts on i'th position in each manfiest are appended to each other).
    The cuts are appended in the order in which they appear in the
    input argument list.
    If CUT_MANIFESTS have different lengths, the script stops once the shortest CutSet is depleted.
    c                 S   rH   r   rI   rJ   r   r   r   r   R  rL   zappend.<locals>.<listcomp>N)r   r;   rM   r=   r   )rE   r   cut_setsrN   r.   r   r   r   appendD  s   "rj   z
--durationzDesired duration of cuts after padding. Cuts longer than this won't be affected. By default, pad to the longest cut duration found in CUT_MANIFEST.)r0   r   r   durationc                 C   s$   t | }|j|d}|| dS )z
    Create a new CutSet by padding the cuts in CUT_MANIFEST. The cuts will be right-padded, i.e. the padding
    is placed after the signal ends.
    )rk   N)r   r:   padr(   )r^   r   rk   r,   padded_cut_setr   r   r   rl   X  s   
rl   cutsetoutputc                 C   s   t | jt|dd dS )a  
    
    Decompose CUTSET into:
        * recording set (recordings.jsonl.gz)
        * feature set (features.jsonl.gz)
        * supervision set (supervisions.jsonl.gz)

    If any of these are not preset in any of the cuts,
    the corresponding file for them will be empty.
    T)
output_dirverboseN)r   r:   	decomposer   )rn   ro   r   r   r   rr   r  s   rr   c                 C   s   t |   dS )zZ
    Describe some statistics of CUTSET, such as the total speech and audio duration.
    N)r   r:   describe)rn   r   r   r   rs     s   rs   
wspecifierz--shard-sizez<Number of cuts per shard (sharding disabled if not defined).z--audio-formatflaczIFormat in which the audio is encoded (uses torchaudio available formats).z--audio/--no-audioz"Should we load and add audio data.)r0   r   z--features/--no-featuresz$Should we load and add feature data.z--custom/--no-customz#Should we load and add custom data.z--fault-tolerant/--stop-on-failzMShould we omit the cuts for which loading data failed, or stop the execution.
shard_sizeaudio_formataudior#   customfault_tolerantc           
   
   C   sV   ddl m} t| }	t|	tsJ dt|	 d|  d||	|||||||d dS )a  
    Export CUTS into a WebDataset tarfile, or a collection of tarfile shards, as specified by
    WSPECIFIER.

    
    WSPECIFIER can be:
    - a regular path (e.g., "data/cuts.tar"),
    - a path template for sharding (e.g., "data/shard-06%d.tar"), or
    - a "pipe:" expression (e.g., "pipe:gzip -c > data/shard-06%d.tar.gz").

    The resulting CutSet contains audio/feature data in addition to metadata, and can be read in
    Python using 'CutSet.from_webdataset' API.

    This function is useful for I/O intensive applications where random reads are too slow, and
    a one-time lengthy export step that enables fast sequential reading is preferable.

    See the WebDataset project for more information: https://github.com/webdataset/webdataset
    r   )export_to_webdatasetz7Only CutSet can be exported to WebDataset format (got: z from 'z'))r.   r$   rv   rw   
load_audioload_featuresload_customrz   N)lhotse.dataset.webdatasetr{   r   r:   
isinstancer   )
rn   rt   rv   rw   rx   r#   ry   rz   export_r.   r   r   r   r{     s"   @

r{   z-bz--num-buckets   zDesired number of buckets.z--samplezMHow many samples to use for estimation (first N, by default use full cutset).num_bucketssamplereturnc                 C   sB   ddl m} t| td}|dur|j|d}t|||d dS )a  
    Estimate duration bins for dynamic bucketing.
    Prints a Python list of num_buckets-1 floats (seconds) which constitute the boundaries between buckets.
    The bins are estimated in such a way so that each bucket has a roughly equal total duration of data.
    r   )estimate_duration_buckets)manifest_clsN)first)r   ))lhotse.dataset.sampling.dynamic_bucketingr   r
   r   subsetclickecho)rn   r   r   r   r.   r   r   r   estimate_bucket_bins  s
   r   )(pathlibr   typingr   r   r   lhotse.bin.modes.cli_baser   
lhotse.cutr   r   r   lhotse.serializationr
   lhotse.utilsr   groupr   commandargumentoptionboolr-   floatChoicestrr<   rC   rD   rO   r[   dictrg   rj   rl   rr   rs   intr{   r   r   r   r   r   <module>   sX   
,			/
