o
    pi                     @  s   d dl mZ d dlZd dlmZmZ d dlZd dlZddl	m
Z
 er.d dlmZ d dlmZ dd	 Zd(ddZ	 			d)d*ddZ			d+d,d&d'ZdS )-    )annotationsN)TYPE_CHECKINGBinaryIO   )	AudioInfo)Path)Tensorc                  C  s   d} d|  d}|S )NZpaddleaudiozxonly PCM16 WAV supported. 
if want support more other audio types, please manually installed (usually with `pip install zQ`). 
 and use paddle.audio.backends.set_backend('soundfile') to set audio backend )packageZwarn_msgr	   r	   i/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/audio/backends/wave_backend.py_error_message    s   r   filepathstr | BinaryIOreturnr   c           	      C  s   t | dr| }nt| d}zt|}W n tjy,   |d |  t }t|w | }|	 }|
 }| d }d}|  t|||||S )a  Get signal information of input audio file.

    Args:
        filepath: audio path or file object.

    Returns:
        AudioInfo: info of the given audio.

    Example:
        .. code-block:: python

            >>> import os
            >>> import paddle

            >>> sample_rate = 16000
            >>> wav_duration = 0.5
            >>> num_channels = 1
            >>> num_frames = sample_rate * wav_duration
            >>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
            >>> waveform = wav_data.tile([num_channels, 1])
            >>> base_dir = os.getcwd()
            >>> filepath = os.path.join(base_dir, "test.wav")

            >>> paddle.audio.save(filepath, waveform, sample_rate)
            >>> wav_info = paddle.audio.info(filepath)
    readrbr      ZPCM_S)hasattropenwaveErrorseekcloser   NotImplementedErrorgetnchannelsgetframerate
getnframesZgetsampwidthr   )	r   file_objfile_err_msgchannelssample_rateZsample_framesbits_per_sampleencodingr	   r	   r   info+   s(   



r$   T
str | Pathframe_offsetint
num_frames	normalizeboolchannels_firsttuple[Tensor, int]c                 C  s
  t | dr| }nt| d}zt|}W n tjy,   |d |  t }t|w | }|	 }	|
 }
||
}|  tj|tjd}|tj}|rW|d }n|}t||
|f}|dkrq|||| ddf }t|}|rtj|ddgd	}||	fS )
a  Load audio data from file. load the audio content start form frame_offset, and get num_frames.

    Args:
        frame_offset: from 0 to total frames,
        num_frames: from -1 (means total frames) or number frames which want to read,
        normalize:
            if True: return audio which norm to (-1, 1), dtype=float32
            if False: return audio with raw data, dtype=int16

        channels_first:
            if True: return audio with shape (channels, time)

    Return:
        Tuple[paddle.Tensor, int]: (audio_content, sample rate)

    Examples:
        .. code-block:: python

            >>> import os
            >>> import paddle

            >>> sample_rate = 16000
            >>> wav_duration = 0.5
            >>> num_channels = 1
            >>> num_frames = sample_rate * wav_duration
            >>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
            >>> waveform = wav_data.tile([num_channels, 1])
            >>> base_dir = os.getcwd()
            >>> filepath = os.path.join(base_dir, "test.wav")

            >>> paddle.audio.save(filepath, waveform, sample_rate)
            >>> wav_data_read, sr = paddle.audio.load(filepath)
    r   r   r   )dtype   r%   Nr   )perm)r   r   r   r   r   r   r   r   r   r   r   Z
readframesnpZ
frombufferZint16astypefloat32ZreshapepaddleZ	to_tensor	transpose)r   r'   r)   r*   r,   r   r   r   r    r!   framesZaudio_contentZaudio_as_np16Zaudio_as_np32Z
audio_normZwaveformr	   r	   r   load_   s8   
(




r7      strsrcr   r!   r#   
str | Noner"   
int | NoneNonec           
      C  s   |j dks	J d| }|rt|}|jd }|dvr!tdt|d }|jtj	kr4|d 
d}t| d	}	|	| |	| |	| |	|  W d
   d
S 1 s\w   Y  d
S )a  
    Save audio tensor to file.

    Args:
        filepath: saved path
        src: the audio tensor
        sample_rate: the number of samples of audio per second.
        channels_first: src channel information
            if True, means input tensor is (channels, time)
            if False, means input tensor is (time, channels)
        encoding: audio encoding format, wave_backend only support PCM16 now.
        bits_per_sample: bits per sample, wave_backend only support 16 bits now.

    Returns:
        None

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> sample_rate = 16000
            >>> wav_duration = 0.5
            >>> num_channels = 1
            >>> num_frames = sample_rate * wav_duration
            >>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
            >>> waveform = wav_data.tile([num_channels, 1])
            >>> filepath = "./test.wav"

            >>> paddle.audio.save(filepath, waveform, sample_rate)
       zExpected 2D tensorr   )Nr8   z,Invalid bits_per_sample, only support 16 bitr   r/   z<hwN)ndimnumpyr1   r5   shape
ValueErrorr(   r.   r4   r3   r2   r   r   ZsetnchannelsZsetsampwidthZsetframerateZwriteframestobytes)
r   r:   r!   r,   r#   r"   Zaudio_numpyr    Zsample_widthfr	   r	   r   save   s    '




"rF   )r   r   r   r   )r   r%   TT)r   r&   r'   r(   r)   r(   r*   r+   r,   r+   r   r-   )TNr8   )r   r9   r:   r   r!   r(   r,   r+   r#   r;   r"   r<   r   r=   )
__future__r   r   typingr   r   rA   r1   r4   backendr   pathlibr   r   r   r$   r7   rF   r	   r	   r	   r   <module>   s(   
6S