o
    'Wg                     @   s   d Z ddlZ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
mZ G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZdS )aO  
Each generator will return float samples from -1.0 to 1.0, which can be 
converted to actual audio with 8, 16, 24, or 32 bit depth using the
SiganlGenerator.to_audio_segment() method (on any of it's subclasses).

See Wikipedia's "waveform" page for info on some of the generators included 
here: http://en.wikipedia.org/wiki/Waveform
    N   )AudioSegment)db_to_floatget_frame_widthget_array_typeget_min_max_valuec                   @   s(   e Zd ZdddZdddZd	d
 ZdS )SignalGeneratorD     c                 C   s   || _ || _d S N)sample_rate	bit_depth)selfr   r    r   O/var/www/html/audio_editor/env/lib/python3.10/site-packages/pydub/generators.py__init__   s   
zSignalGenerator.__init__     @@        c           	         s   t | j\}t| j}t| j}t| t| j|d  } fdd|  D }t	|d|}t

||}z| }W n   | }Y t|d|| j|ddS )z
        Duration in milliseconds
            (default: 1 second)
        Volume in DB relative to maximum amplitude
            (default 0.0 dBFS, which is the maximum value)
        r   c                 3   s     | ]}t |   V  qd S r   )int).0valgainmaxvalr   r   	<genexpr>+   s    z3SignalGenerator.to_audio_segment.<locals>.<genexpr>r   r   )channelssample_width
frame_rateframe_width)datametadata)r   r   r   r   r   r   r   generate	itertoolsislicearraytobytestostringr   )	r   durationvolumeminvalr   
array_typesample_countsample_datar   r   r   r   to_audio_segment   s$   



z SignalGenerator.to_audio_segmentc                 C   s   t d)NzuSignalGenerator subclasses must implement the generate() method, and *should not* call the superclass implementation.)NotImplementedErrorr   r   r   r   r!   <   s   zSignalGenerator.generateN)r	   r
   )r   r   )__name__
__module____qualname__r   r-   r!   r   r   r   r   r      s    

r   c                       s$   e Zd Z fddZdd Z  ZS )Sinec                    s    t t| jdi | || _d S Nr   )superr3   r   freqr   r6   kwargs	__class__r   r   r   B   s   
zSine.__init__c                 c   s8    | j d tj | j }d}	 t|| V  |d7 }q)N   r   Tr   )r6   mathpir   sin)r   sine_ofsample_nr   r   r   r!   F   s   zSine.generater0   r1   r2   r   r!   __classcell__r   r   r9   r   r3   A   s    r3   c                       &   e Zd Zd fdd	Zdd Z  ZS )Pulse      ?c                    &   t t| jdi | || _|| _d S r4   )r5   rD   r   r6   
duty_cycler   r6   rG   r8   r9   r   r   r   P      
zPulse.__init__c                 c   sF    d}| j t| j }|| j }	 || |k rdV  ndV  |d7 }q)Nr   T      ?g      r   r   floatr6   rG   )r   r@   cycle_lengthpulse_lengthr   r   r   r!   U   s   
zPulse.generate)rE   rA   r   r   r9   r   rD   O       rD   c                          e Zd Z fddZ  ZS )Squarec                    $   d|d< t t| j|fi | d S NrE   rG   )r5   rQ   r   r7   r9   r   r   r   f      zSquare.__init__r0   r1   r2   r   rB   r   r   r9   r   rQ   e       rQ   c                       rC   )SawtoothrJ   c                    rF   r4   )r5   rW   r   r6   rG   rH   r9   r   r   r   m   rI   zSawtooth.__init__c                 c   sr    d}| j t| j }|| j }|}|| }	 || }||k r)d| | d V  ndd||  |  V  |d7 }q)Nr   Tr;   rJ   r   rK   )r   r@   rM   midpointascend_lengthdescend_lengthcycle_positionr   r   r   r!   r   s   
zSawtooth.generate)rJ   rA   r   r   r9   r   rW   l   rO   rW   c                       rP   )Trianglec                    rR   rS   )r5   r\   r   r7   r9   r   r   r      rT   zTriangle.__init__rU   r   r   r9   r   r\      rV   r\   c                   @   s   e Zd Zdd ZdS )
WhiteNoisec                 c   s    	 t   d d V  q)NTr;   rJ   )randomr/   r   r   r   r!      s   zWhiteNoise.generateN)r0   r1   r2   r!   r   r   r   r   r]      s    r]   )__doc__r<   r$   r"   r^   audio_segmentr   utilsr   r   r   r   objectr   r3   rD   rQ   rW   r\   r]   r   r   r   r   <module>   s    		)