o
    Me                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZm	Z	 ddl
mZ dd Zded	efd
dZ	 			dde	eef dedededed	ee jef fddZ			ddede jdededee dee fddZdS )    N)Path)OptionalTupleUnion   )	AudioInfoc                  C   s   d} d | }|S )NZpaddleaudiozonly PCM16 WAV supportted. 
if want support more other audio types, please manually installed (usually with `pip install {}`). 
 and use paddle.audio.backends.set_backend('soundfile') to set audio backend)format)packageZwarn_msg r
   RD:\Projects\ConvertPro\env\Lib\site-packages\paddle/audio/backends/wave_backend.py_error_message   s
   r   filepathreturnc           	      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, 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frame_offset
num_frames	normalizechannels_firstc                 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 )
as  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)

    Exampels:
        .. 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, 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   loadW   s8   
&




r2      srcr    r"   r!   c           
      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 infomation
            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, 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   )Nr3   z+Invalid bits_per_sample, only supprt 16 bitr   r*   z<hwN)ndimnumpyr,   r0   shape
ValueErrorintr)   r/   r.   r-   r   r   ZsetnchannelsZsetsampwidthZsetframerateZwriteframestobytes)
r   r4   r    r(   r"   r!   Zaudio_numpyr   Zsample_widthfr
   r
   r   save   s    '




"r>   )r   r$   TT)TNr3   )r/   r   r8   r,   pathlibr   typingr   r   r   backendr   r   strr#   r;   boolZTensorr2   r>   r
   r
   r
   r   <module>   sN   4
Q