
    Li                     *   d Z ddlZddl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 	 ddlmZ ddlmZ  ej.                  d      Z G d d      Z G d dej4                        Zy# e$ r ddlmZ ddlmZ Y Ew xY w)z%[Experimental] Live Music API client.    N)AsyncIterator   )_api_module)_common)_live_converters)_transformers)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y)AsyncMusicSessionz![Experimental] AsyncMusicSession.
api_client	websocketc                      || _         || _        y )N)_api_client_ws)selfr   r   s      U/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/google/genai/live_music.py__init__zAsyncMusicSession.__init__-   s    !DDH    promptsreturnNc                   K   | j                   j                  rt        d      d|D cg c]  }t        j                  |d       c}i}| j
                  j                  t        j                  d|i             d {    y c c}w 7 
w)N4Live music generation is not supported in Vertex AI.weightedPromptsTconvert_keysclientContent	r   vertexaiNotImplementedErrorr   convert_to_dictr   sendjsondumps)r   r   promptclient_content_dicts       r   set_weighted_promptsz&AsyncMusicSession.set_weighted_prompts1   s        
@ 
 # %%f4@ ((--

O5H#IJ
KKK Ls   'BB7B>B	?Bconfigc                    K   | j                   j                  rt        d      t        j                  |d      }| j
                  j                  t        j                  d|i             d {    y 7 w)Nr   Tr   musicGenerationConfigr!   )r   r+   config_dicts      r   set_music_generation_configz-AsyncMusicSession.set_music_generation_configB   s`        
@  ++FFk
((--

$;[#IJ
KKKs   A,A6.A4/A6playback_controlc                    K   | j                   j                  rt        d      d|j                  i}| j                  j                  t        j                  |             d {    y 7 w)Nr   playbackControl)r   r"   r#   valuer   r%   r&   r'   )r   r0   playback_control_dicts      r   _send_control_signalz&AsyncMusicSession._send_control_signalM   s[        
@   12B2H2HIHHMM$**%:;<<<s   A!A+#A)$A+c                 p   K   | j                  t        j                  j                         d{   S 7 w)z0Sends playback signal to start the music stream.N)r5   r	   LiveMusicPlaybackControlPLAYr   s    r   playzAsyncMusicSession.playX   s)     **5+I+I+N+NOOOO   -646c                 p   K   | j                  t        j                  j                         d{   S 7 w)z2Sends a playback signal to pause the music stream.N)r5   r	   r7   PAUSEr9   s    r   pausezAsyncMusicSession.pause\   s)     **5+I+I+O+OPPPPr;   c                 p   K   | j                  t        j                  j                         d{   S 7 w)zSends a playback signal to stop the music stream.

    Resets the music generation context while retaining the current config.
    N)r5   r	   r7   STOPr9   s    r   stopzAsyncMusicSession.stop`   s+     
 **5+I+I+N+NOOOOr;   c                 p   K   | j                  t        j                  j                         d{   S 7 w)zKReset the context (prompts retained) without stopping the music generation.N)r5   r	   r7   RESET_CONTEXTr9   s    r   reset_contextzAsyncMusicSession.reset_contextg   s2     **&&44   r;   c                   K   | j                          d{   x}r!| | j                          d{   x}r yy7 (7 w)zbReceive model responses from the server.

    Yields:
      The audio chunks from the server.
    N)_receive)r   results     r   receivezAsyncMusicSession.receivem   s>      --/)
)&
)l --/)
)&
)))s#   A?AAAAAc                   K   t        j                         }	 | j                  j                  d       d {   }|r	 t        j                  |      }ni }| j                  j                  rt        d      |}t         j                  j                  ||j                               S 7 q# t        $ r& | j                  j                          d {  7  }Y w xY w# t
        j                  j                  $ r t        d|      w xY ww)NFdecodezFailed to parse response: r   )responsekwargs)r	   LiveMusicServerMessager   recv	TypeErrorr&   loadsdecoderJSONDecodeError
ValueErrorr   r"   r#   _from_response
model_dump)r   parameter_modelraw_responserL   response_dicts        r   rF   zAsyncMusicSession._receivew   s     224O+88===66l H::l+ h   VWWm''66'A'A'C 7  ! 7 +88==?**l+
 \\)) H5l5EFGGHs[   DB+ B)B+ DC AD)B+ +&CCCDCD,D		Dc                 T   K   | j                   j                          d{    y7 w)z<Closes the bi-directional stream and terminates the session.N)r   closer9   s    r   r[   zAsyncMusicSession.close   s     
((..
s   (&()r   N)__name__
__module____qualname____doc__r
   r   r   listr	   WeightedPromptr*   LiveMusicGenerationConfigr/   r7   r5   r:   r>   rA   rD   r   rN   rH   rF   r[    r   r   r   r   *   s    ) ;K L%../LL"	L33	L	L	=#<<	=	=PQP]5+G+GH e:: .r   r   c                   n    e Zd ZdZ ej
                  d      ej                  dede	e
   fd              Zy)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               J  K   | j                   j                         }t        |t              r|j	                  d      }t        j                  | j                   |      }| j                   j                  r| j                   j                  }| j                   j                  j                  }| d| d| }| j                   j                  j                  }t        j                  t        j                  t        j                   |      j#                  d                  }t%        |dd	g|       t'        j(                  |      }	nt+        d
      	 t-        ||      4 d{   }
|
j/                  |	       d{    t0        j3                  |
j5                  d       d{          t7        | j                   |
       ddd      d{    y7 u7 ^7 77 # 1 d{  7  sw Y   yxY w# t8        $ r t-        ||      4 d{  7  }
|
j/                  |	       d{  7   t0        j3                  |
j5                          d{  7         t7        | j                   |
       ddd      d{  7   Y y# 1 d{  7  sw Y   Y yxY ww xY ww)z0[Experimental] Connect to the live music server.zutf-8z!/ws/google.ai.generativelanguage.z).GenerativeService.BidiGenerateMusic?key=)rf   T)exclude_none)from_objectsetuprf   r   )additional_headersNFrJ   )r   r   )extra_headers)r   _websocket_base_url
isinstancebytesrK   tt_modelapi_key_http_optionsapi_versionheadersr   r$   live_converters$_LiveMusicConnectParameters_to_mldevr	   LiveMusicConnectParametersrV   setvr&   r'   r#   r   r%   loggerinforO   r   rP   )r   rf   base_urltransformed_modelrr   versionuriru   request_dictrequestwss              r   r   zAsyncLiveMusic.connect   s3     335H(E")h		$"2"2E:  ((g  ..::gj9'Bklsktuc  ..66g ,,

>
>::'

-l <'7+->?

<(g VWWK9 K KRggg"'''//04+;+;rJJ	K K K/K K K K
  KG4 K Kggg"'')OO$4+;+;rJJ	K K K K KKs   EJ#G6 #G$G6 'G!<G=(G!%G
&"G!G6 GG6 J#G6 G!G!G6 !G3'G*(G3/G6 2J#3G6 6J HJ J	*H-+'J	I#J	6J JJ J#	J	JJ	J J#J  J#N)r\   r]   r^   r_   r   experimental_warning
contextlibasynccontextmanagerstrr   r   r   rc   r   r   re   re      sS    
  7T !!(KC (KM:K,L (K "(Kr   re   )r_   r   r&   loggingtypingr    r   r   r   rv   r   rp   r	   r   r
   r   ry   websockets.asyncio.clientr   r   ModuleNotFoundErrorwebsockets.client	getLoggerrz   r   
BaseModulere   rc   r   r   <module>r      s     ,        1    & .(8/
 
		4	5f fR2K[++ 2Ka  (0'(s    A> >BB