
    +jx                     N   d Z ddlZddlZddl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 ddlmZ dd	lmZ dd
lmZ ddlmZ ddl	mZ ej        Z	 ddlmZ ddlmZ n# e$ r ddlmZ ddlmZ Y nw xY w ej        d          Z G d d          Z G d dej                  ZdS )z%[Experimental] Live Music API client.    N)AsyncIterator   )_api_module)_common)_live_converters)_transformers)errors)types)BaseApiClient)set_value_by_path)ClientConnection)connectzgoogle_genai.live_musicc                       e Zd ZdZdedefdZdeej	                 ddfdZ
d	ej        ddfd
Zdej        ddfdZddZddZddZddZdeej                 fdZdej        fdZddZdS )AsyncMusicSessionz![Experimental] AsyncMusicSession.
api_client	websocketc                 "    || _         || _        d S )N)_api_client_ws)selfr   r   s      [/home/wildlama/visual-decline/.venv/lib/python3.11/site-packages/google/genai/live_music.py__init__zAsyncMusicSession.__init__0   s    !DDHHH    promptsreturnNc                    K   | j         j        rt          d          dd |D             i}| j                            t          j        d|i                     d {V  d S )N4Live music generation is not supported in Vertex AI.weightedPromptsc                 :    g | ]}t          j        |d           S )Tconvert_keys)r   convert_to_dict).0prompts     r   
<listcomp>z:AsyncMusicSession.set_weighted_prompts.<locals>.<listcomp>=   s8        %f4@@@  r   clientContent)r   vertexaiNotImplementedErrorr   sendjsondumps)r   r   client_content_dicts      r   set_weighted_promptsz&AsyncMusicSession.set_weighted_prompts4   s         

@  
   #   (--
O5H#IJJ
K
KKKKKKKKKKr   configc                    K   | j         j        rt          d          t          j        |d          }| j                            t          j        d|i                     d {V  d S )Nr   Tr    musicGenerationConfig)	r   r'   r(   r   r"   r   r)   r*   r+   )r   r.   config_dicts      r   set_music_generation_configz-AsyncMusicSession.set_music_generation_configE   s         G
@   +FFFFk
(--
$;[#IJJ
K
KKKKKKKKKKr   playback_controlc                    K   | j         j        rt          d          d|j        i}| j                            t          j        |                     d {V  d S )Nr   playbackControl)r   r'   r(   valuer   r)   r*   r+   )r   r3   playback_control_dicts      r   _send_control_signalz&AsyncMusicSession._send_control_signalP   ss         =
@    12B2HIHMM$*%:;;<<<<<<<<<<<r   c                 Z   K   |                      t          j        j                   d{V S )z0Sends playback signal to start the music stream.N)r8   r
   LiveMusicPlaybackControlPLAYr   s    r   playzAsyncMusicSession.play[   s2      **5+I+NOOOOOOOOOr   c                 Z   K   |                      t          j        j                   d{V S )z2Sends a playback signal to pause the music stream.N)r8   r
   r:   PAUSEr<   s    r   pausezAsyncMusicSession.pause_   s2      **5+I+OPPPPPPPPPr   c                 Z   K   |                      t          j        j                   d{V S )zSends a playback signal to stop the music stream.

    Resets the music generation context while retaining the current config.
    N)r8   r
   r:   STOPr<   s    r   stopzAsyncMusicSession.stopc   s4      
 **5+I+NOOOOOOOOOr   c                 Z   K   |                      t          j        j                   d{V S )zKReset the context (prompts retained) without stopping the music generation.N)r8   r
   r:   RESET_CONTEXTr<   s    r   reset_contextzAsyncMusicSession.reset_contextj   sE      **&4        r   c                   K   |                                   d{V x}r#|W V  |                                   d{V x}!dS dS )zbReceive model responses from the server.

    Yields:
      The audio chunks from the server.
    N)_receive)r   results     r   receivezAsyncMusicSession.receivep   sw       --//))))))
)& llll --//))))))
)&     r   c                 \  K   t          j                    }	 | j                            d           d {V }n# t          $ r" | j                                         d {V }Y n~t
          $ rr}|j        r|j        j        }|j        j        }n'd}t          j
        j                            |d          }t          j                            ||d            Y d }~nd }~ww xY w|r@	 t!          j        |          }n,# t           j        j        $ r t)          d|          w xY wi }| j        j        rt/          d          |}|s6|r4t          j                            |                    d          |d            t           j                            ||                                          S )	NFdecodei  zAbnormal closure.zFailed to parse response: r   code)responsekwargs)r
   LiveMusicServerMessager   recv	TypeErrorConnectionClosedrcvdrN   reason
websocketsframesCLOSE_CODE_EXPLANATIONSgetr	   APIErrorraise_errorr*   loadsdecoderJSONDecodeError
ValueErrorr   r'   r(   _from_response
model_dump)r   parameter_modelraw_responseerN   rV   rO   response_dicts           r   rH   zAsyncMusicSession._receivez   s     244O68===66666666ll + + +8==??******lll 6 6 6	
 Zv{":>>tEXYYo!!$555555556  H:l++\) H H HFlFFGGGH h   VWWWm HX Ho!!(,,v"6"6$GGG'66'A'A'C'C 7   s(   !9 )C$	C-A(CC%C: :'D!c                 H   K   | j                                          d{V  dS )z<Closes the bi-directional stream and terminates the session.N)r   closer<   s    r   rh   zAsyncMusicSession.close   s0      
(..

r   )r   N)__name__
__module____qualname____doc__r   r   r   listr
   WeightedPromptr-   LiveMusicGenerationConfigr2   r:   r8   r=   r@   rC   rF   r   rQ   rJ   rH   rh    r   r   r   r   -   su       )) ;K    L%./LL L L L"	L3	L	L 	L 	L 	L	=#<	=	= 	= 	= 	=P P P PQ Q Q QP P P P   ]5+GH    e:    B     r   r   c                   t    e Zd ZdZ ej        d          ej        dede	e
         fd                        ZdS )AsyncLiveMusicz_[Experimental] Live music module.

  Live music can be accessed via `client.aio.live.music`.
  zLRealtime music generation is experimental and may change in future versions.modelr   c                 K   | j                                         }t          |t                    r|                    d          }t          j        | j         |          }| j         j        r| j         j        }| j         j        j	        }| d| d| }| j         j        j
        }t          j        t          j        t          j        |                              d                              }t%          |dd	g|           t'          j        |          }	nt+          d
          	 t-          ||          4 d{V }
|
                    |	           d{V  t0                              |
                    d           d{V            t7          | j         |
          W V  ddd          d{V  dS # 1 d{V swxY w Y   dS # t8          $ r t-          ||          4 d{V }
|
                    |	           d{V  t0                              |
                                 d{V            t7          | j         |
          W V  ddd          d{V  Y dS # 1 d{V swxY w Y   Y dS w xY w)z0[Experimental] Connect to the live music server.zutf-8z!/ws/google.ai.generativelanguage.z).GenerativeService.BidiGenerateMusic?key=)rs   T)exclude_none)from_objectsetuprs   r   )additional_headersNFrL   )r   r   )extra_headers)r   _websocket_base_url
isinstancebytesrM   tt_modelapi_key_http_optionsapi_versionheadersr   r"   live_converters$_LiveMusicConnectParameters_to_mldevr
   LiveMusicConnectParametersrb   setvr*   r+   r(   r   r)   loggerinforR   r   rS   )r   rs   base_urltransformed_modelr   versionurir   request_dictrequestwss              r   r   zAsyncLiveMusic.connect   s'      3355H(E"" *))h	$"2E:: X (g .:guu'uulsuuc .6g ,

>:'  

--   l <'7+->???
<((gg VWWWK999 K K K K K K KRggg"'''////////0004+;rJJJJJJJ	K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K
  K K KG444 K K K K K K Kggg"''))OOOOOO$$$4+;rJJJJJJJ	K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K KKs\   *G A)F=*G =
GG 
GG !J1A'I,J,
I6	6J9I6	:J JN)ri   rj   rk   rl   r   experimental_warning
contextlibasynccontextmanagerstrr   r   r   rp   r   r   rr   rr      s         
  7T  !(KC (KM:K,L (K (K (K "! (K (K (Kr   rr   )rl   r   r*   loggingtypingr   rW    r   r   r   r   r   r}   r	   r
   r   r   r   r   rT   websockets.asyncio.clientr   r   ModuleNotFoundErrorwebsockets.client	getLoggerr   r   
BaseModulerr   rp   r   r   <module>r      s    , +                                   1 1 1 1 1 1                         & & & & & & . . . . . .. (888888/////// ( ( (000000''''''''( 
	4	5	5p p p p p p p pf2K 2K 2K 2K 2K[+ 2K 2K 2K 2K 2Ks   A A21A2