
    v-j                        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 Zd$dZ	 	 	 	 d%d&dZ	 	 	 d'd(d#ZdS ))    )annotationsN)TYPE_CHECKINGBinaryIO   )	AudioInfo)Path)Tensorc                     d} d|  d}|S )N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 )packagewarn_msgs     b/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/audio/backends/wave_backend.py_error_messager       s.    G	V9@	V 	V 	V  O    filepathstr | BinaryIOreturnr   c                   t          | d          r| }nt          | d          }	 t          j        |          }nY# t          j        $ rG |                    d           |                                 t                      }t          |          w xY w|                                }|	                                }|
                                }|                                dz  }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      PCM_S)hasattropenwaveErrorseekcloser   NotImplementedErrorgetnchannelsgetframerate
getnframesgetsampwidthr   )	r   file_objfile_err_msgchannelssample_ratesample_framesbits_per_sampleencodings	            r   infor-   +   s   8 x   ($''+	(##: + + +a ""!'***	+ !!##H$$&&K$$&&M((**Q.OHNN]Hox  
   : ABT
str | Pathframe_offsetint
num_frames	normalizeboolchannels_firsttuple[Tensor, int]c                B   t          | d          r| }nt          | d          }	 t          j        |          }nY# t          j        $ rG |                    d           |                                 t                      }t          |          w xY w|                                }|	                                }	|
                                }
|                    |
          }|                                 t          j        |t          j                  }|                    t          j                  }|r|dz  }n|}t          j        ||
|f          }|dk    r||||z   ddf         }t%          j        |          }|rt%          j        |ddg	          }||	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#   
readframesnp
frombufferint16astypefloat32reshapepaddle	to_tensor	transpose)r   r1   r3   r4   r6   r%   r&   r'   r(   r)   framesaudio_contentaudio_as_np16audio_as_np32
audio_normwaveforms                   r   loadrL   _   s   P x   ($''+	(##: + + +a ""!'***	+ !!##H$$&&KF$$V,,MNN M-rx@@@M!((44M #"e,

 #
z*vx&899HRL<*+DDaaaGH))H ;#HAq6:::[  r.      strsrcr	   r)   r,   
str | Noner+   
int | NoneNonec                j   |j         dk    s
J d            |                                }|rt          j        |          }|j        d         }|dvrt          d          t          |dz            }|j        t          j	        k    r|dz  
                    d          }t          j        | d	          5 }	|	                    |           |	                    |           |	                    |           |	                    |                                           d
d
d
           d
S # 1 swxY 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   )NrM   z,Invalid bits_per_sample, only support 16 bitr   r:   z<hwN)ndimnumpyr=   rE   shape
ValueErrorr2   r9   rC   rA   r@   r   r   setnchannelssetsampwidthsetframeratewriteframestobytes)
r   rO   r)   r6   r,   r+   audio_numpyr(   sample_widthfs
             r   saverb      sj   N 8q===.===))++K  0l;// #H j((GHHH*++L
yFN"""e,44T::	8S	!	! -Q	x   	|$$$	{###	k))++,,,	- - - - - - - - - - - - - - - - - -s   4A'D((D,/D,)r   r   r   r   )r   r/   TT)r   r0   r1   r2   r3   r2   r4   r5   r6   r5   r   r7   )TNrM   )r   rN   rO   r	   r)   r2   r6   r5   r,   rP   r+   rQ   r   rR   )
__future__r   r   typingr   r   rW   r=   rC   backendr   pathlibr   r	   r   r-   rL   rb   r   r   r   <module>rg      s   # " " " " "  * * * * * * * *              1 1 1 1l L! L! L! L! L!f  "$>- >- >- >- >- >- >-r   