o
    ٷi*                     @   s   d dl mZ d dlZd dlmZ d dlmZmZ zd dl	Z	W n e
y)   dZ	Y nw zd dlZW n e
y;   dZY nw eeZG dd dZdS )    )BytesION)init_logger)AudioResponseCreateAudioc                   @   s:   e Zd ZdZdedefddZdejde	de
fd	d
ZdS )
AudioMixinz+Mixin class to add audio-related utilities.	audio_objreturnc              	   C   sV  |j }|j}|j }|j}|j}|j}|dkrtd| tdu r't	d|j
dkr5td|j
 d| |||\}}dd	i fd
dddifddi fddi fddi fddddifd}||vrltd| d d}|| \}	}
}t }tj|||fd|	i| | }W d   n1 sw   Y  |rddl}||d}t||
dS )z6Convert audio tensor to bytes in the specified format.audiozUnsupported stream format: NzYsoundfile is required for audio generation. Please install it with: pip install soundfile   z$Unsupported audio tensor dimension: z/. Only mono (1D) and stereo (2D) are supported.WAVz	audio/wavRAWz	audio/pcmsubtypePCM_16FLACz
audio/flacMP3z
audio/mpegAACz	audio/aacOGGz	audio/oggOPUS)wavpcmflacmp3aacopuszUnsupported response format 'z', defaulting to 'wav'.r   formatr   zutf-8)
audio_data
media_type)audio_tensorsample_rateresponse_formatlowerstream_formatbase64_encodespeed
ValueError	soundfileImportErrorndim_apply_speed_adjustmentloggerwarningr   writegetvaluebase64	b64encodedecoder   )selfr   r   r   r   r!   r"   r#   supported_formatssoundfile_formatr   kwargsbufferr   r-    r5   b/home/ubuntu/.local/lib/python3.10/site-packages/vllm_omni/entrypoints/openai/audio_utils_mixin.pycreate_audio   sH   

	
zAudioMixin.create_audior   r#   r   c              
   C   s   |dkr||fS t du rtdzt|jtjs|tj}t jj	||d}||fW S  t
yD } ztd|  td|d}~ww )zBApply speed adjustment to the audio tensor while preserving pitch.g      ?NzUlibrosa is required for speed adjustment. Please install it with: pip install librosa)yratez+An error occurred during speed adjustment: z!Failed to apply speed adjustment.)librosar&   np
issubdtypedtypefloatingastypefloat32effectstime_stretch	Exceptionr)   errorr$   )r0   r   r#   r   stretched_audioer5   r5   r6   r(   L   s   

z"AudioMixin._apply_speed_adjustmentN)__name__
__module____qualname____doc__r   r   r7   r;   ndarrayfloatintr(   r5   r5   r5   r6   r      s    4r   )ior   numpyr;   vllm.loggerr   +vllm_omni.entrypoints.openai.protocol.audior   r   r%   r&   r:   rG   r)   r   r5   r5   r5   r6   <module>   s     