
    ^jf3                       d dl mZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZ e
rd dl	mZ  G d d          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Zd9dZd9dZd9dZd9dZd9dZd:d;dZ G d de          Z G d  d!e          Z G d" d#e          Z G d$ d%e          Z G d& d'e          Z G d( d)e          Z  G d* d+e          Z!	 	 d<d=d5Z"d>d8Z#dS )?    )annotationsN)uniform)TYPE_CHECKING)	AudioDataAudioFormatSource)	Generatorc                      e Zd ZdZd	dZdS )
EnvelopezBase class for SynthesisSource amplitude envelopes.

    Custom Envelopes need only provide a single `get_generator`
    method that takes the sample rate, and duration as arguments.
    sample_ratefloatdurationreturnGenerator[float]c                    t           )a%  Get a generator instance.

        Args:
            sample_rate:
                The sample rate of the Source this will be applied to.
            duration:
                The duration of the Source. This is used to calculate
                the number of bytes for some Envelopes.
        )NotImplementedError)selfr   r   s      Q/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/media/synthesis.pyget_generatorzEnvelope.get_generator   s
     "!    Nr   r   r   r   r   r   )__name__
__module____qualname____doc__r    r   r   r   r      s2         
" 
" 
" 
" 
" 
"r   r   c                  &    e Zd ZdZdddZdddZdS )FlatEnvelopezA flat envelope, providing basic amplitude setting.

    Args:
        amplitude:
            The amplitude (volume) of the wave, from 0.0 to 1.0.
            Values outside this range will be clamped.
          ?	amplituder   c                L    t          t          d|          d          | _        d S N      ?r   )maxminr    )r   r    s     r   __init__zFlatEnvelope.__init__-   s!    Si00!44r   Nr   r   r   r   c              #      K   | j         }	 |V  Nr    )r   r   r   r    s       r   r   zFlatEnvelope.get_generator0   s       N		OOO	r   r   )r    r   )NNr   r   r   r   r   r&   r   r   r   r   r   r   $   sP         5 5 5 5 5      r   r   c                  "    e Zd ZdZddZdd	Zd
S )LinearDecayEnvelopea7  A linearly decaying envelope.

    This envelope linearly decays the amplitude from the peak value
    to 0, over the length of the waveform.

    :Parameters:
        `peak` : float
            The Initial peak value of the envelope, from 0.0 to 1.0.
            Values outside this range will be clamped.
    r#   c                L    t          t          d|          d          | _        d S r"   )r$   r%   peak)r   r/   s     r   r&   zLinearDecayEnvelope.__init__B   s    C**			r   r   r   r   r   r   c              #     K   | j         }t          ||z            }t          |          D ]}||z
  |z  |z  V  	 dV  )NTr   )r/   intrange)r   r   r   r/   total_bytesis         r   r   z!LinearDecayEnvelope.get_generatorE   sf      y+011{## 	9 	9A?k1D88888	GGG	r   N)r#   r   r+   r   r   r   r-   r-   6   sF        	 	+ + + +     r   r-   c                  $    e Zd ZdZdddZddZdS )ADSREnvelopea  A four-part Attack, Decay, Suspend, Release envelope.

    This is a four part ADSR envelope. The attack, decay, and release
    parameters should be provided in seconds. For example, a value of
    0.1 would be 100ms. The sustain_amplitude parameter affects the
    sustain volume. This defaults to a value of 0.5, but can be provided
    on a scale from 0.0 to 1.0.

    Args:
        attack:
            The attack time, in seconds.
        decay:
            The decay time, in seconds.
        release:
            The release time, in seconds.
        sustain_amplitude:
            The sustain amplitude (volume), from 0.0 to 1.0.
    r   attackr   decayreleasesustain_amplitudec                v    || _         || _        || _        t          t	          d|          d          | _        d S r"   )r7   r8   r9   r$   r%   r:   )r   r7   r8   r9   r:   s        r   r&   zADSREnvelope.__init__b   s:    
!$S.?%@%@!!D!Dr   r   r   r   r   c              #    K   | j         }t          ||z            }t          || j        z            }t          || j        z            }t          || j        z            }||z
  |z
  |z
  }d|z
  |z  }	||z  }
t          d|dz             D ]	}||z  V  
t          d|dz             D ]}d||	z  z
  V  t          d|dz             D ]}|V  t          d|dz             D ]}|||
z  z
  V  	 dV  )N   Tr   )r:   r1   r7   r8   r9   r2   )r   r   r   r:   r3   attack_bytesdecay_bytesrelease_bytessustain_bytes
decay_steprelease_stepr4   s               r   r   zADSREnvelope.get_generatorh   si      2+011;455+
233K$,677#l2[@=P++{:
(=8q,*++ 	# 	#Al"""""q+/** 	' 	'Aq:~&&&&&q-!+,, 	$ 	$A#####q-!+,, 	9 	9A#q<'788888	GGG	r   Nr*   )r7   r   r8   r   r9   r   r:   r   r   r+   r   r   r   r6   r6   N   sP         &E E E E E     r   r6   c                  $    e Zd ZdZdddZddZdS )TremoloEnvelopeaY  A tremolo envelope, for modulation amplitude.

    A tremolo envelope that modulates the amplitude of the
    waveform with a sinusoidal pattern. The depth and rate
    of modulation can be specified. Depth is calculated as
    a percentage of the maximum amplitude. For example:
    a depth of 0.2 and amplitude of 0.5 will fluctuate
    the amplitude between 0.4 an 0.5.

    Args:
        depth:
            The amount of fluctuation, from 0.0 to 1.0.
        rate:
            The fluctuation frequency, in seconds.
        amplitude:
            The peak amplitude (volume), from 0.0 to 1.0.
    r   depthr   rater    c                    t          t          d|          d          | _        || _        t          t          d|          d          | _        d S r"   )r$   r%   rF   rG   r    )r   rF   rG   r    s       r   r&   zTremoloEnvelope.__init__   sA    S%!,,
	Si00!44r   r   r   r   r   c              #  .  K   t          ||z            }||z  }| j        }t          dd| j        z
  | j        z            }t          j        dz  |z  | j        z  }t          |          D ]&}t	          j        ||z            }	|	||z
  z  |z   V  '	 dV  )N        r#      Tr   )	r1   r    r$   rF   _mathpirG   r2   sin)
r   r   r   r3   periodmax_amplitudemin_amplitudestepr4   values
             r   r   zTremoloEnvelope.get_generator   s      +011x'C#
"2dn!DEE1&2{## 	J 	JAIdQh''E==89MIIIII	GGG	r   Nr*   )rF   r   rG   r   r    r   r   r+   r   r   r   rE   rE   }   sK         $5 5 5 5 5

 
 
 
 
 
r   rE   	frequencyr   r   r   r   c              #     K   	 dV  )NTrJ   r   rT   r   s     r   silence_generatorrW      s      			r   c              #  .   K   	 t          dd          V  )NT      r#   )_uniformrV   s     r   noise_generatorr[      s&      "tS!!!!!"r   c              #  t   K   dt           j        z  | z  |z  }d}	 t          j        ||z            V  |dz  }N       @rJ   Tr#   )rL   rM   rN   )rT   r   rR   r4   s       r   sine_generatorr_      sM      >I%3DAiD!!!!!	Sr   c              #  t   K   d| z  |z  }d}	 |dk    rd|dz
  z
  }| }|dk     rd|dz
  z
  }| }|V  ||z  },)Ng      @rJ   Tr#   rY   r   )rT   r   rR   rS   s       r   triangle_generatorra      sq      ?[(DE3;;53;'E5D4<<EDL)E5Dr   c              #  f   K   t          || z            }d| z  |z  }d}	 |||z  z  dz
  V  |dz  }r]   r1   )rT   r   period_lengthrR   r4   s        r   sawtooth_generatorre      sY      i/00M?[(DAa-'(3....	Sr         I@
duty_cyclec              #     K   t          || z            }t          ||z  dz            }d}	 t          ||z  |k               dz  dz
  V  |dz  }$)Nd   rJ   Tr^   r#   rc   )rT   r   rg   rd   r4   s        r   pulse_generatorrj      so      i/00MZ-/#566JA!m#j011C7#====	Sr   c                  6    e Zd ZdZdddZdddZddZddZdS )SynthesisSourcea  Base class for synthesized waveforms.

    Args:
        generator:
            A waveform generator that produces a stream of floats from (-1.0, 1.0)
        duration:
            The length, in seconds, of audio that you wish to generate.
        sample_rate:
            Audio samples per second. (CD quality is 44100).
        envelope:
            An optional Envelope to apply to the waveform.
       N	generatorr	   r   r   r   r1   envelopeEnvelope | Nonec                   || _         || _        t          dd|          | _        |pt	          d          | _        | j                            ||          | _        |dz  | _        t          | j        |z            dz  | _
        d| _        d S )	Nr=      )channelssample_sizer   r#   r)   rK       r   )
_generator	_durationr   audio_formatr   	_enveloper   _envelope_generator_bytes_per_secondr1   _max_offset_offset)r   rn   r   r   ro   s        r   r&   zSynthesisSource.__init__   s    #!'P[\\\!@\C%@%@%@#'>#?#?X#V#V  "-qt5@AAJNr   rJ   	num_bytescompensation_timer   AudioData | Nonec                P   t          || j        | j        z
            }|dk    rdS | j        | j        z  }|| j        z  }| xj        |z  c_        |dz	  }| j        | j        fdt          |          D             }t          j        | dg|R  }t          ||||g           S )z)Return ``num_bytes`` bytes of audio data.r   Nr=   c              3  v   K   | ]3}t          t                    t                    z  d z            V  4dS )i  N)r1   next).0_ro   rn   s     r   	<genexpr>z1SynthesisSource.get_audio_data.<locals>.<genexpr>  sA      WW1DOOd8nn4v=>>WWWWWWr   h)
r%   r|   r}   r{   rv   rz   r2   _structpackr   )	r   r~   r   	timestampr   samplesdataro   rn   s	          @@r   get_audio_datazSynthesisSource.get_audio_data   s    	4#3dl#BCC	>>4L4#99	t55	! q.O	+WWWWWgWWW|wMMM1D111y)XrBBBr   r   Nonec                    t          || j        z            }t          t          |d          | j                  dz  | _        | j                            | j        j	        | j
                  | _        d S )Nr   ru   )r1   r{   r%   r$   r|   r}   ry   r   rx   r   rw   rz   )r   r   offsets      r   seekzSynthesisSource.seek  sb    Y!77883vq>>4+;<<zI#'>#?#?@Q@]_c_m#n#n   r   boolc                    dS )NTr   )r   s    r   
is_precisezSynthesisSource.is_precise  s    tr   )rm   N)rn   r	   r   r   r   r1   ro   rp   )rJ   )r~   r1   r   r   r   r   )r   r   r   r   )r   r   )r   r   r   r   r&   r   r   r   r   r   r   rl   rl      s             C C C C C&o o o o     r   rl   c                  "     e Zd Zdd fdZ xZS )Silence  rm   Nr   r   rT   r1   r   ro   r   c                l    t                                          t          ||          |||           dS )zCreate a Silent waveform.N)superr&   rW   r   r   rT   r   ro   	__class__s        r   r&   zSilence.__init__  s3    *9kBBHk[cdddddr   r   rm   Nr   r   rT   r1   r   r1   ro   r   r   r   r   r&   __classcell__r   s   @r   r   r     sM        e e e e e e e e e e er   r   c                  "     e Zd Zdd fdZ xZS )
WhiteNoiser   rm   Nr   r   rT   r1   r   ro   r   c                l    t                                          t          ||          |||           dS )z%Create a random white noise waveform.N)r   r&   r[   r   s        r   r&   zWhiteNoise.__init__  2    K@@(KYabbbbbr   r   r   r   r   s   @r   r   r     M        c c c c c c c c c c cr   r   c                  "     e Zd Zdd fdZ xZS )Siner   rm   Nr   r   rT   r1   r   ro   r   c                l    t                                          t          ||          |||           dS )z"Create a sinusoid (sine) waveform.N)r   r&   r_   r   s        r   r&   zSine.__init__  s2    	;??;X`aaaaar   r   r   r   r   s   @r   r   r     sM        b b b b b b b b b b br   r   c                  "     e Zd Zdd fdZ xZS )Squarer   rm   Nr   r   rT   r1   r   ro   r   c                l    t                                          t          ||          |||           dS )z!Create a Square (pulse) waveform.N)r   r&   rj   r   s        r   r&   zSquare.__init__#  r   r   r   r   r   r   s   @r   r   r   "  r   r   r   c                  "     e Zd Zdd fdZ xZS )Triangler   rm   Nr   r   rT   r1   r   ro   r   c                l    t                                          t          ||          |||           dS )zCreate a Triangle waveform.N)r   r&   ra   r   s        r   r&   zTriangle.__init__)  3    +I{CCX{\deeeeer   r   r   r   r   s   @r   r   r   (  M        f f f f f f f f f f fr   r   c                  "     e Zd Zdd fdZ xZS )Sawtoothr   rm   Nr   r   rT   r1   r   ro   r   c                l    t                                          t          ||          |||           dS )zCreate a Sawtooth waveform.N)r   r&   re   r   s        r   r&   zSawtooth.__init__/  r   r   r   r   r   r   s   @r   r   r   .  r   r   r   rm   r   r=   r1   index	modulatorGenerator | Nonero   rp   c              #  X  K   |p t          d                                          }t          j        }dt          j        z  |z  | z  }d}|r:	  |||z  |t          |          z  z             t          |          z  V  |dz  }9	  |||z            t          |          z  V  |dz  }&)a  A sine wave generator that can be optionally modulated with another generator.

    This generator represents a single FM Operator. It can be used by itself as a
    simple sine wave, or modulated by another waveform generator. Multiple operators
    can be linked together in this way. For example::

        operator1 = sine_operator(samplerate=44800, frequency=1.22)
        operator2 = sine_operator(samplerate=44800, frequency=99, modulator=operator1)
        operator3 = sine_operator(samplerate=44800, frequency=333, modulator=operator2)
        operator4 = sine_operator(samplerate=44800, frequency=545, modulator=operator3)

    Args:
        sample_rate:
            Audio samples per second. (CD quality is 44100).
        frequency:
            The frequency, in Hz, of the waveform you wish to generate.
        index:
            The modulation index. Defaults to 1
        modulator:
            An optional operator to modulate this one.
        envelope:
            An optional Envelope to apply to the waveform.
    r#   r^   rJ   )r   r   rL   rN   rM   r   )r   rT   r   r   ro   rN   rR   r4   s           r   sine_operatorr   8  s      4 <<,,::<<H
)C>I%3DA 	#a$hi!8899DNNJJJJHA		#a$h--$x..0000HA	r   	operatorsr	   c                 (    d t          |  D             S )zCombine the output from multiple generators.

    This does a simple sum & division of the output of
    two or more generators. A new generator is returned.
    c              3  T   K   | ]#}t          |          t          |          z  V  $d S r(   )sumlen)r   r   s     r   r   z%composite_operator.<locals>.<genexpr>f  s3      GGGCLL3w<<'GGGGGGr   )zip)r   s    r   composite_operatorr   `  s     HGsIGGGGr   )rT   r   r   r   r   r   )rf   )rT   r   r   r   rg   r   r   r   )rm   r   r=   NN)r   r1   rT   r   r   r   r   r   ro   rp   r   r   )r   r	   r   r	   )$
__future__r   mathrL   structr   randomr   rZ   typingr   pyglet.media.codecs.baser   r   r   r	   r   r   r-   r6   rE   rW   r[   r_   ra   re   rj   rl   r   r   r   r   r   r   r   r   r   r   r   <module>r      sy   " " " " " "         & & & & & &             C C C C C C C C C C !      
" " " " " " " "(    8   $    (   0, , , , ,8 , , ,^" " " " "h " " "N   
" " " "
             5 5 5 5 5f 5 5 5pe e e e eo e e ec c c c c c c cb b b b b? b b bc c c c c_ c c cf f f f f f f ff f f f f f f f TURV% % % % %PH H H H H Hr   