o
    Th;                     @   s
  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 zddlmZ ddlmZ W n eym   ddlmZ ddlmZ Y nw edZG dd dZG dd dejZdS )z%[Experimental] Live Music API client.    N)AsyncIterator   )_api_module)_common)_transformers)types)BaseApiClient)set_value_by_path)_live_converters)_Content_to_mldev)_Content_to_vertex)ClientConnection)connectzgoogle_genai.live_musicc                   @   s   e Zd ZdZdedefddZdeej	 ddfd	d
Z
dejddfddZdejddfddZdddZdddZdddZdddZdeej fddZdejfddZdddZdS ) AsyncMusicSessionz![Experimental] AsyncMusicSession.
api_client	websocketc                 C   s   || _ || _d S )N)_api_client_ws)selfr   r    r   Z/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/google/genai/live_music.py__init__/   s   
zAsyncMusicSession.__init__promptsreturnNc                    sB   | j jr	tdtjd|id}| jtd|iI d H  d S )N4Live music generation is not supported in Vertex AI.weighted_promptsfrom_objectclientContent)	r   vertexaiNotImplementedErrorlive_converters _LiveMusicClientContent_to_mldevr   sendjsondumps)r   r   client_content_dictr   r   r   set_weighted_prompts5   s    z&AsyncMusicSession.set_weighted_promptsconfigc                    s>   | j jr	tdtj|d}| jtd|iI d H  d S )Nr   r   musicGenerationConfig)	r   r   r    r!   #_LiveMusicGenerationConfig_to_mldevr   r#   r$   r%   )r   r(   config_dictr   r   r   set_music_generation_configA   s    z-AsyncMusicSession.set_music_generation_configplayback_controlc                    s>   | j jr	tdtjd|id}| jt|I d H  d S )Nr   r-   r   )	r   r   r    r!    _LiveMusicClientMessage_to_mldevr   r#   r$   r%   )r   r-   playback_control_dictr   r   r   _send_control_signalM   s   z&AsyncMusicSession._send_control_signalc                       |  tjjI dH S )z0Sends playback signal to start the music stream.N)r0   r   LiveMusicPlaybackControlPLAYr   r   r   r   playY      zAsyncMusicSession.playc                    r1   )z2Sends a playback signal to pause the music stream.N)r0   r   r2   PAUSEr4   r   r   r   pause]   r6   zAsyncMusicSession.pausec                    r1   )zSends a playback signal to stop the music stream.

    Resets the music generation context while retaining the current config.
    N)r0   r   r2   STOPr4   r   r   r   stopa   s   zAsyncMusicSession.stopc                    r1   )zKReset the context (prompts retained) without stopping the music generation.N)r0   r   r2   RESET_CONTEXTr4   r   r   r   reset_contexth   s   
zAsyncMusicSession.reset_contextc                 C  s4   |   I dH  }r|V  |   I dH  }s
dS dS )zbReceive model responses from the server.

    Yields:
      The audio chunks from the server.
    N)_receive)r   resultr   r   r   receiven   s   zAsyncMusicSession.receivec                    s   t  }z| jjddI d H }W n ty"   | j I d H }Y nw |r=zt|}W n tjjy<   t	d|w i }| j
jrGtdt|}t jj|| dS )NFdecodezFailed to parse response: r   )responsekwargs)r   LiveMusicServerMessager   recv	TypeErrorr$   loadsdecoderJSONDecodeError
ValueErrorr   r   r    r!   "_LiveMusicServerMessage_from_mldev_from_response
model_dump)r   parameter_modelraw_responserB   response_dictr   r   r   r=   x   s,   zAsyncMusicSession._receivec                    s   | j  I dH  dS )z<Closes the bi-directional stream and terminates the session.N)r   closer4   r   r   r   rQ      r6   zAsyncMusicSession.close)r   N)__name__
__module____qualname____doc__r   r   r   listr   WeightedPromptr'   LiveMusicGenerationConfigr,   r2   r0   r5   r8   r:   r<   r   rD   r?   r=   rQ   r   r   r   r   r   ,   s:    








r   c                   @   s6   e Zd ZdZedejdede	e
 fd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                C  s  | j  }t|tr|d}t| j |}| j jrR| j j}| j jj	}| d| d| }| j jj
}ttjtj|djddd}t|dd	g| t|}	ntd
zAt||d4 I dH )}
|
|	I dH  t|
jddI dH  t| j |
dV  W d  I dH  W dS 1 I dH sw   Y  W dS  ty   t||d4 I dH '}
|
|	I dH  t|
 I dH  t| j |
dV  W d  I dH  Y dS 1 I dH sw   Y  Y dS w )z0[Experimental] Connect to the live music server.zutf-8z!/ws/google.ai.generativelanguage.z).GenerativeService.BidiGenerateMusic?key=)rZ   T)exclude_noner   setuprZ   r   )additional_headersNFr@   )r   r   )extra_headers)r   _websocket_base_url
isinstancebytesrA   tt_modelapi_key_http_optionsapi_versionheadersr   convert_to_dictr!   $_LiveMusicConnectParameters_to_mldevr   LiveMusicConnectParametersrM   setvr$   r%   r    r   r#   loggerinforE   r   rF   )r   rZ   base_urltransformed_modelrd   versionurirg   request_dictrequestwsr   r   r   r      sF   




22zAsyncLiveMusic.connectN)rR   rS   rT   rU   r   experimental_warning
contextlibasynccontextmanagerstrr   r   r   r   r   r   r   rY      s    rY   )rU   rv   r$   loggingtypingr    r   r   r   rb   r   r   r   r	   rk   r
   r!   modelsr   r   websockets.asyncio.clientr   r   ModuleNotFoundErrorwebsockets.client	getLoggerrl   r   
BaseModulerY   r   r   r   r   <module>   s0   
j