o
    Me#  ã                   @   sˆ   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Zdede	fdd	„Z
dee fd
d„Zdefdd„Zdefdd„Zdd„ ZdS )é    Né   )Úwave_backend)Úbackend)ÚListÚversionÚreturnc                 C   sZ   |   d¡}t|d ƒ}t|d ƒ}t|d ƒ}|dk rdS |dkr+|dkr+|dkr+dS dS )NÚ.r   r   é   FT)ÚsplitÚint)r   Zver_arrZv0Úv1Úv2© r   úRD:\Projects\ConvertPro\env\Lib\site-packages\paddle/audio/backends/init_backend.pyÚ_check_version   s   
r   c                  C   s„   g } zddl }W n ty   d}d ||¡}t |¡ Y nw dtjv r;|j}t|ƒdkr6d |¡}t|ƒ‚|j	 
¡ } |  d¡ | S )a#   List available backends, the backends in paddleaudio and the default backend.

    Returns:
        List[str]: The list of available backends.

    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])
            wav_path = "./test.wav"

            current_backend = paddle.audio.backends.get_current_backend()
            print(current_backend) # wave_backend, the default backend.
            backends = paddle.audio.backends.list_available_backends()
            # default backends is ['wave_backend']
            # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
            if 'soundfile' in backends:
                paddle.audio.backends.set_backend('soundfile')

            paddle.audio.save(wav_path, waveform, sample_rate)

    r   NÚpaddleaudiozÎFailed importing {}. 
only wave_banckend(only can deal with PCM16 WAV) supportted.
if want soundfile_backend(more audio type suppported),
please manually installed (usually with `pip install {} >= 1.0.2`). FzSthe version of paddleaudio installed is {},
please ensure the paddleaudio >= 1.0.2.r   )r   ÚImportErrorÚformatÚwarningsÚwarnÚsysÚmodulesÚ__version__r   ÚbackendsZlist_audio_backendsÚappend)r   r   ÚpackageZwarn_msgr   Úerr_msgr   r   r   Úlist_available_backends%   s(   ûø

þ

r   c                  C   s6   d} dt jv rddl}|j ¡ } tjj|jkr| S dS )a9   Get the name of the current audio backend

    Returns:
        str: The name of the current backend,
        the wave_backend or backend imported from paddleaudio

    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])
            wav_path = "./test.wav"

            current_backend = paddle.audio.backends.get_current_backend()
            print(current_backend) # wave_backend, the default backend.
            backends = paddle.audio.backends.list_available_backends()
            # default backends is ['wave_backend']
            # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2

            if 'soundfile' in backends:
                paddle.audio.backends.set_backend('soundfile')

            paddle.audio.save(wav_path, waveform, sample_rate)

    Nr   r   r   )r   r   r   r   Zget_audio_backendÚpaddleÚaudioÚload)Zcurrent_backendr   r   r   r   Úget_current_backend\   s    

r!   Úbackend_namec                 C   sj   | t ƒ vrtƒ ‚| dkrt}nddl}|j | ¡ |}dD ]}tt|t||ƒƒ tt	j
|t||ƒƒ qdS )ao  Set the backend by one of the list_audio_backend return.

    Args:
        backend (str): one of the list_audio_backend. "wave_backend" is the default. "soundfile" imported from paddleaudio.

    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])
            wav_path = "./test.wav"

            current_backend = paddle.audio.backends.get_current_backend()
            print(current_backend) # wave_backend, the default backend.
            backends = paddle.audio.backends.list_available_backends()
            # default backends is ['wave_backend']
            # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2

            if 'soundfile' in backends:
                paddle.audio.backends.set_backend('soundfile')

            paddle.audio.save(wav_path, waveform, sample_rate)

    r   r   N©Úsaver    Úinfo)r   ÚNotImplementedErrorr   r   r   Zset_audio_backendÚsetattrr   Úgetattrr   r   )r"   Úmoduler   Úfuncr   r   r   Úset_backend…   s   
"þr+   c                  C   s    dD ]} t t| tt| ƒƒ qd S )Nr#   )r'   r   r(   r   )r*   r   r   r   Ú_init_set_audio_backend¶   s   ÿr,   )r   r   Ú r   r   Útypingr   r   ÚstrÚboolr   r   r!   r+   r,   r   r   r   r   Ú<module>   s   7)1