
    Be                    h   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZ ddlZddlZddlmZmZ dd	lmZmZ d
dlmZ  G d de          Z G d de          Z G d de          Zg dZej         ej!        ej"        ej#        gZ$ddZ%ddZ& G d de          Z G d de          ZdS )zG
Contains Media panes including renderers for Audio and Video content.
    )annotationsN)	b64encode)BytesIO)AnyClassVarDictListMapping   )AudioVideo)isfileisurl   )	ModelPanec                      e Zd ZdZd ZdS )TensorLikeMetazCSee https://blog.finxter.com/python-__instancecheck__-magic-method/c                    t          |dd          }t          |dd          }t          |          o-t          |          ot          |          ot          |d          S )Nnumpy dimdtype)getattrboolcallablehasattr)selfinstance
numpy_attrdim_attrs       0lib/python3.11/site-packages/panel/pane/media.py__instancecheck__z TensorLikeMeta.__instancecheck__   sh    Xw33
8UB// 'Z  'X' Hg&&		
    N)__name__
__module____qualname____doc__r"    r#   r!   r   r      s)        MM
 
 
 
 
r#   r   c                      e Zd ZdZdS )
TensorLikez`A class similar to torch.Tensor. We don't want to make PyTorch a dependency of this project
    N)r$   r%   r&   r'   r(   r#   r!   r*   r*   $   s         Dr#   r*   )	metaclassc                      e Zd ZU  ej        dd          Z ej        dd          Z ej        dd          Z	 ej        dd	          Z
 ej        d
dd          Z ej        ddd          Z ej        dd          Z ej        dd          Zded<   ded<   ded<   dddZded<   g Zded<   dZded<   dZed,d#            Zd-d&Zd.d(Z fd)Zd/d+Z xZS )0
_MediaBaseFz&
        Whether the media should loopdefaultdocr   z
        The current timestamp   zK
        How frequently to sample the current playback time in millisecondsTz.
        Whether the media is currently pausedr   z/
        The media file either local or remote.)r/   
allow_Noner0   Nr   d   (
        The volume of the media player.r/   boundsr0   z
        When True, it specifies that the output will play automatically.
        In Chromium browsers this requires the user to click play once.zA
        When True, it specifies that the output should be muted.zClassVar[str]_default_mimezClassVar[List[str]]_formats_media_typevalue)sample_rateobjectz"ClassVar[Mapping[str, str | None]]_rename_rerender_paramszClassVar[bool]_updatesobjr   returnfloat | bool | Nonec                    t          t                    rHt                    r"t          fd| j        D                       rdS t          | j                  rdS t          d          rdS dS )Nc              3  H   K   | ]}                     d |z             V  dS ).N)endswith).0fmtrA   s     r!   	<genexpr>z%_MediaBase.applies.<locals>.<genexpr>W   s3      "Q"QS3<<C#8#8"Q"Q"Q"Q"Q"Qr#   TreadF)
isinstancestrr   anyr9   r   r   )clsrA   s    `r!   appliesz_MediaBase.appliesT   s    c3 	c{{ s"Q"Q"Q"QCL"Q"Q"QQQ tS#,'' t3 	4ur#   data
np.ndarrayc                    |j         }|t          j        t          j        fv r"|dz                      t          j                  }|S )Ng      @)r   npfloat32float64astypeint16)r   rQ   r   s      r!   _to_np_int16z_MediaBase._to_np_int16_   s;    
RZ,,,7N**2844Dr#   np.ndarray | TensorLikec                    t          |t                    r|                                }|                     |          }ddlm} t                      }|                    || j        |           |S )Nr   )wavfile)	rL   r*   r   rY   scipy.ior\   r   writer<   )r   rQ   r\   buffers       r!   
_to_bufferz_MediaBase._to_bufferg   sm    dJ'' 	 ::<<D  &&$$$$$$fd.555r#   c                X    t                                          |          }d|v r|d= |S )Njs_property_callbacks)super_process_property_change)r   msg	__class__s     r!   rd   z#_MediaBase._process_property_changeq   s3    gg..s33"c))+,
r#   Dict[str, Any]c                   | j         }|d}nFt          |t          j        t          f          r9d}|                     |          }t          |                                          }nt          j	        
                    |          rg|                    d          d         }t          |d          5 }|                                }d d d            n# 1 swxY w Y   t          |          }nf|                                                    d          rt!          |          S |r|d| j         d	| d
k    rd}nt%          d| j         d          d| j         d	| d
|                    d           }t!          |          S )Nr#   wavrF   rbhttp)r=   zdata:/z;base64,z"Object should be either path to a z file or numpy array.zutf-8)r8   rL   rT   ndarrayr*   r`   r   getvalueospathr   splitopenrK   lower
startswithdictr:   
ValueErrordecode)r   rA   rI   rQ   r_   fb64s          r!   _transform_objectz_MediaBase._transform_objectw   s    ;DDbj*566 	kC__S))FV__..//DDW^^C   
	k))C..$Cc4  Avvxx                             T??DDYY[[##F++ 	ks#### 	kGt'7GG#GGGGGDDi$BRiiijjjLd&LLLLdkk'6J6JLL3s   2CCCrA   r   rB   rC   )rQ   rR   )rQ   rZ   )rA   r   rB   rg   )r$   r%   r&   paramBooleanloopNumbertimeIntegerthrottlepausedStringr=   volumeautoplaymuted__annotations__r>   r?   r@   _MediaBase__abstractclassmethodrP   rY   r`   rd   r{   __classcell__rf   s   @r!   r-   r-   )   s!        5= -) * * *D 5< (! " " "D u}S /N O O OH U]4 .1 2 2 2F U\" <2 3 3 3F U\$x >+ , , ,F u}U 1K L L LH EM% .D E E EE !   !!!! w3 3G     -/....#H####J   [                         r#   r-   )ztorch.shortztorch.int16z
torch.halfztorch.float16ztorch.floatztorch.float32ztorch.doubleztorch.float64rA   r   rB   r   c                    t          | t                    o2|                                 dk    ot          | j                  t
          v S Nr   )rL   r*   r   rM   r   _VALID_TORCH_DTYPES_FOR_AUDIOrA   s    r!   _is_1dim_int_or_float_tensorr      s>    3
## 	8		1	8CI77r#   c                h    t          | t          j                  o| j        dk    o| j        t
          v S r   )rL   rT   rn   ndimr   _VALID_NUMPY_DTYPES_FOR_AUDIOr   s    r!   _is_1dim_int_or_float_ndarrayr      s4    3
## 	3!	3	22r#   c                       e Zd ZdZ ej        deej        e	fdd          Z
 ej        dd          ZeZd	Zg d
ZdZed fd            Z xZS )r   a  
    The `Audio` pane displays an audio player given a local or remote audio
    file, a NumPy Array or Torch Tensor.

    The pane also allows access and control over the player state including
    toggling of playing/paused and loop state, the current time, and the
    volume.

    The audio player supports ogg, mp3, and wav files

    If SciPy is installed, 1-dim Numpy Arrays and 1-dim
    Torch Tensors are also supported. The dtype must be one of the following

    - numpy: np.int16, np.uint16, np.float32, np.float64
    - torch: torch.short, torch.int16, torch.half, torch.float16, torch.float, torch.float32,
    torch.double, torch.float64

    The array or Tensor input will be downsampled to 16bit and converted to a wav file by SciPy.

    Reference: https://panel.holoviz.org/reference/panes/Audio.html

    :Example:

    >>> Audio('http://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3', name='Audio')
    r   Tz^
        The audio file either local or remote, a 1-dim NumPy ndarray or a 1-dim Torch Tensor.)r/   class_r2   r0   iD  zO
        The sample_rate of the audio when given a NumPy array or Torch tensor.r.   ri   )mp3ri   oggaudiorA   r   rB   rC   c                    t                                          |          pt          |          pt          |          S )N)rc   rP   r   r   )rO   rA   rf   s     r!   rP   zAudio.applies   s;    $$ 1,S111+C00	
r#   r|   )r$   r%   r&   r'   r}   ClassSelectorrM   rT   rn   r*   r=   r   r<   _BkAudio_bokeh_modelr8   r9   r:   r   rP   r   r   s   @r!   r   r      s         4 !U S"*j4Q,0 7ab b bF  %- 4R S S SK LM$$$HK
 
 
 
 
 [
 
 
 
 
r#   r   c                  L    e Zd ZdZ ej        ddd          ZeZdZ	g dZ
dZd	S )
r   al  
    The `Video` Pane displays a video player given a local or remote video
    file.

    The widget also allows access and control over the player state including
    toggling of playing/paused and loop state, the current time, and the
    volume.

    Depending on the browser the video player supports mp4, webm, and ogg
    containers and a variety of codecs.

    Reference: https://panel.holoviz.org/reference/panes/Video.html

    :Example:

    >>> Video(
    ...     'https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_640_3MG.mp4',
    ...     width=640, height=360, loop=True
    ... )
    r4   r3   r5   r6   mp4)r   webmr   videoN)r$   r%   r&   r'   r}   r   r   _BkVideor   r8   r9   r:   r(   r#   r!   r   r      sW         ( U]3x >+ , , ,F LM%%%HKKKr#   r   )rA   r   rB   r   )'r'   
__future__r   rp   base64r   ior   typingr   r   r   r	   r
   r   rT   r}   modelsr   r   r   r   utilr   r   baser   typer   r*   r-   r   rX   uint16rU   rV   r   r   r   r(   r#   r!   <module>r      s-    # " " " " " 				                               9 9 9 9 9 9 9 9                      

 

 

 

 

T 

 

 

	 	 	 	 	> 	 	 	 	
b  b  b  b  b  b  b  b H! ! !  "$29bj"* M       /
 /
 /
 /
 /
J /
 /
 /
d    J     r#   