
    ujh9                         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
 d dlmZ d dlmZmZ dZd	Zd
ZdZdZdddZd Zdededeeeeeef   fdZ G d de
      Zy)    N)Path)OptionalTupleUnion)Tensor)Dataset)download_url_to_file)_extract_tar_load_waveformSpeechCommandsspeech_commands_v0.02_nohash__background_noise_i>  @743935421bb51cccdb6bdd152e04c5c70274e935c82119ad7faeec31780d811d@af14739ee7dc311471de98f5f9d2c9191b18aedfe957f4a6ff791c709868ff58)z@http://download.tensorflow.org/data/speech_commands_v0.01.tar.gzz@http://download.tensorflow.org/data/speech_commands_v0.02.tar.gzc                 Z   g }|D ]  }t         j                  j                  | |      }t        |      5 }||D cg c]M  }t         j                  j	                  t         j                  j                  | |j                                     O c}z  }d d d         |S c c}w # 1 sw Y   xY wN)ospathjoinopennormpathstrip)root	filenamesoutputfilenamefilepathfileobjlines          `/var/www/html/dev/engine/venv/lib/python3.12/site-packages/torchaudio/datasets/speechcommands.py
_load_listr"      s    F _77<<h/(^ 	_wV]^drww''T4::<(HI^^F	_ 	__ M _	_ 	_s   B!AB
B!B!!B*	r   r   returnc                    t         j                  j                  | |      }t         j                  j                  |      \  }}t         j                  j                  |      \  }}t         j                  j	                  |      \  }}t         j                  j	                  |      \  }}|j                  t
              \  }}	t        |	      }	|t        |||	fS r   )r   r   relpathsplitsplitextHASH_DIVIDERintSAMPLE_RATE)
r   r   r%   reldirr   _labelspeaker
speaker_idutterance_numbers
             r!   _get_speechcommands_metadatar1      s    ggooh-Gww}}W-FHww}}V$HAu !!(+JGQ!!'*JGQ#*==#> J +,K
4DDD    c                       e Zd ZdZeeddfdeeef   dedede	de
e   d	dfd
Zded	eeeeeef   fdZded	eeeeeef   fdZd	efdZy)SPEECHCOMMANDSa,  *Speech Commands* :cite:`speechcommandsv2` dataset.

    Args:
        root (str or Path): Path to the directory where the dataset is found or downloaded.
        url (str, optional): The URL to download the dataset from,
            or the type of the dataset to dowload.
            Allowed type values are ``"speech_commands_v0.01"`` and ``"speech_commands_v0.02"``
            (default: ``"speech_commands_v0.02"``)
        folder_in_archive (str, optional):
            The top-level directory of the dataset. (default: ``"SpeechCommands"``)
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (default: ``False``).
        subset (str or None, optional):
            Select a subset of the dataset [None, "training", "validation", "testing"]. None means
            the whole dataset. "validation" and "testing" are defined in "validation_list.txt" and
            "testing_list.txt", respectively, and "training" is the rest. Details for the files
            "validation_list.txt" and "testing_list.txt" are explained in the README of the dataset
            and in the introduction of Section 7 of the original paper and its reference 12. The
            original paper can be found `here <https://arxiv.org/pdf/1804.03209.pdf>`_. (Default: ``None``)
    FNr   urlfolder_in_archivedownloadsubsetr#   c                 >   ||dvrt        d      |dv r'd}d}t        j                  j                  |||z         }t        j                  |      }t        j                  j                  ||      | _        t        j                  j                  |      }t        j                  j                  ||      }	|j                  dd      d   }t        j                  j                  ||      }t        j                  j                  ||      | _        |rt        j                  j                  | j                        st        j                  j                  |	      s$t        j                  |d       }
t        ||	|
	       t        |	| j                         nBt        j                  j                  | j                        st!        d
| j                   d      |dk(  rt#        | j                  d      | _        y |dk(  rt#        | j                  d      | _        y |dk(  rt'        t#        | j                  dd            }t)        d t+        | j                        j-                  d      D              }|D cg c]5  }t.        |v r+t0        |vr#t        j                  j3                  |      |vr|7 c}| _        y t)        d t+        | j                        j-                  d      D              }|D cg c]  }t.        |v st0        |vs| c}| _        y c c}w c c}w )N)training
validationtestingzSWhen `subset` is not None, it must be one of ['training', 'validation', 'testing'].)zspeech_commands_v0.01r   z$http://download.tensorflow.org/data/z.tar.gz.   r   )hash_prefixz	The path zT doesn't exist. Please check the ``root`` path or set `download=True` to download itr;   zvalidation_list.txtr<   ztesting_list.txtr:   c              3   2   K   | ]  }t        |        y wr   str.0ps     r!   	<genexpr>z*SPEECHCOMMANDS.__init__.<locals>.<genexpr>|        MqCFM   z*/*.wavc              3   2   K   | ]  }t        |        y wr   rA   rC   s     r!   rF   z*SPEECHCOMMANDS.__init__.<locals>.<genexpr>   rG   rH   )
ValueErrorr   r   r   fspath_archivebasenamersplit_pathisdirisfile
_CHECKSUMSgetr	   r
   existsRuntimeErrorr"   _walkersetsortedr   globr(   EXCEPT_FOLDERr   )selfr   r5   r6   r7   r8   base_urlext_archiverM   archivechecksumexcludeswalkerws                 r!   __init__zSPEECHCOMMANDS.__init__H   s{    &0U"Urss 
 
 >H#K'',,x{):;C yyT+<=77##C('',,tX.??3*1-GGLL):HEWW\\$(9:
77==,ww~~g.)~~c48H(g8LWdjj177>>$**-"

| ,[ [ 
 \!%djj2GHDLy %djj2DEDLz!:djj2GI[\]HMD,<,A,A),LMMF  1$a)?BGGDTDTUVDW_gDg DL MD,<,A,A),LMMF'-^!1B}\]G]A^DL _s   6:L1L?LLnc                 L    | j                   |   }t        || j                        S )a  Get metadata for the n-th sample from the dataset. Returns filepath instead of waveform,
        but otherwise returns the same fields as :py:func:`__getitem__`.

        Args:
            n (int): The index of the sample to be loaded

        Returns:
            Tuple of the following items;

            str:
                Path to the audio
            int:
                Sample rate
            str:
                Label
            str:
                Speaker ID
            int:
                Utterance number
        )rV   r1   rL   )r[   rd   fileids      r!   get_metadatazSPEECHCOMMANDS.get_metadata   s"    * a+FDMMBBr2   c                 p    | j                  |      }t        | j                  |d   |d         }|f|dd z   S )a  Load the n-th sample from the dataset.

        Args:
            n (int): The index of the sample to be loaded

        Returns:
            Tuple of the following items;

            Tensor:
                Waveform
            int:
                Sample rate
            str:
                Label
            str:
                Speaker ID
            int:
                Utterance number
        r      N)rg   r   rL   )r[   rd   metadatawaveforms       r!   __getitem__zSPEECHCOMMANDS.__getitem__   sA    ( $$Q'!$--!hqkJ{Xab\))r2   c                 ,    t        | j                        S r   )lenrV   )r[   s    r!   __len__zSPEECHCOMMANDS.__len__   s    4<<  r2   )__name__
__module____qualname____doc__URLFOLDER_IN_ARCHIVEr   rB   r   boolr   rc   r)   r   rg   r   rl   ro    r2   r!   r4   r4   2   s    0 !2 $<_CI<_ <_ 	<_
 <_ <_ 
<_|Cc CeCc3,C&D C0*S *U63S#+E%F *0! !r2   r4   )r   pathlibr   typingr   r   r   torchr   torch.utils.datar   torchaudio._internalr	   torchaudio.datasets.utilsr
   r   ru   rt   r(   rZ   r*   rR   r"   rB   r)   r1   r4   rw   r2   r!   <module>r~      s    	  ) )  $ 5 B$ $ IK IK
E3 Ec EeCcSVX[D[>\ E(E!W E!r2   