
    \
jYL                        d dl Z d dlZd dlZd dlZd dlZddlmZmZmZ 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 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 G d de          Z G d d e          ZdS )!    N   )SourceAudioFormat	AudioData)dequec                       e Zd ZdZd ZdS )Envelopez3Base class for SynthesisSource amplitude envelopes.c                     t           NNotImplementedError)selfsample_ratedurations      P/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/media/synthesis.pyget_generatorzEnvelope.get_generator1   s    !!    N)__name__
__module____qualname____doc__r    r   r   r	   r	   /   s)        ==" " " " "r   r	   c                        e Zd ZdZddZd ZdS )FlatEnvelopezA flat envelope, providing basic amplitude setting.

    :Parameters:
        `amplitude` : float
            The amplitude (volume) of the wave, from 0.0 to 1.0.
            Values outside of this range will be clamped.
          ?c                 L    t          t          d|          d          | _        d S N      ?r   )maxmin	amplitude)r   r!   s     r   __init__zFlatEnvelope.__init__=   s!    Si00!44r   c              #       K   | j         }	 |V  r   r!   )r   r   r   r!   s       r   r   zFlatEnvelope.get_generator@   s       N		OOO	r   Nr   r   r   r   r   r"   r   r   r   r   r   r   5   sA         5 5 5 5    r   r   c                        e Zd ZdZddZd ZdS )LinearDecayEnvelopea:  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 of 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__Q   s    C**			r   c              #   z   K   | j         }t          ||z            }t          |          D ]}||z
  |z  |z  V  d S r   )r*   intrange)r   r   r   r*   total_bytesis         r   r   z!LinearDecayEnvelope.get_generatorT   s]      y+011{## 	9 	9A?k1D88888	9 	9r   N)r   r&   r   r   r   r(   r(   F   sA        	 	+ + + +9 9 9 9 9r   r(   c                        e Zd ZdZddZ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.

    :Parameters:
        `attack` : float
            The attack time, in seconds.
        `decay` : float
            The decay time, in seconds.
        `release` : float
            The release time, in seconds.
        `sustain_amplitude` : float
            The sustain amplitude (volume), from 0.0 to 1.0.
    r   c                 v    || _         || _        || _        t          t	          d|          d          | _        d S r   )attackdecayreleaser   r    sustain_amplitude)r   r3   r4   r5   r6   s        r   r"   zADSREnvelope.__init__n   s:    
!$S.?%@%@!!D!D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 S )Nr   )r6   r,   r3   r4   r5   r-   )r   r   r   r6   r.   attack_bytesdecay_bytesrelease_bytessustain_bytes
decay_steprelease_stepr/   s               r   r   zADSREnvelope.get_generatort   s`      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	9 	9r   Nr%   r&   r   r   r   r1   r1   [   sE         $E E E E9 9 9 9 9r   r1   c                        e Zd ZdZddZd ZdS )TremoloEnvelopea{  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.

    :Parameters:
        `depth` : float
            The amount of fluctuation, from 0.0 to 1.0.
        `rate` : float
            The fluctuation frequency, in seconds.
        `amplitude` : float
            The peak amplitude (volume), from 0.0 to 1.0.
    r   c                     t          t          d|          d          | _        || _        t          t          d|          d          | _        d S r   )r   r    depthrater!   )r   rA   rB   r!   s       r   r"   zTremoloEnvelope.__init__   sA    S%!,,
	Si00!44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 S )N        r      )	r,   r!   r   rA   mathpirB   r-   sin)
r   r   r   r.   periodmax_amplitudemin_amplitudestepr/   values
             r   r   zTremoloEnvelope.get_generator   s      +011x'C#
"2dn!DEE!v%	1{## 	J 	JAHTAX&&E==89MIIIII	J 	Jr   Nr%   r&   r   r   r   r?   r?      sF         "5 5 5 5
J J J J Jr   r?   c                   4    e Zd ZdZddZddZd Zd	 Zd
 ZdS )SynthesisSourceaK  Base class for synthesized waveforms.

    :Parameters:
        `duration` : float
            The length, in seconds, of audio that you wish to generate.
        `sample_rate` : int
            Audio samples per second. (CD quality is 44100).
        `sample_size` : int
            The bit precision. Must be either 8 or 16.
          Nc                    t          |          | _        t          d||          | _        d| _        || _        || _        |dz	  | _        | j        |z  | _        t          | j        | j        z            | _
        |pt          d          | _        | j                            ||          | _        | j        dk    r| xj
        dz  c_
        d S d S )	Nr   )channelssample_sizer   r      r   r$   rE       )float	_durationr   audio_format_offset_sample_rate_sample_size_bytes_per_sample_bytes_per_secondr,   _max_offsetr   enveloper   _envelope_generator)r   r   r   rT   r`   s        r   r"   zSynthesisSource.__init__   s    x'##% % %
 ''!,!1!%!7+!Et5FGG ?L3$?$?$?#'=#>#>{H#U#U !Q&&
* '&r   rD   c                 "   t          || j        | j        z
            }|dk    rdS t          | j                  | j        z  }t          |          | j        z  }|                     |          }| xj        |z  c_        t          ||||g           S )z'Return `num_bytes` bytes of audio data.r   N)r    r_   rZ   rW   r^   _generate_datar   )r   	num_bytescompensation_time	timestampr   datas         r   get_audio_datazSynthesisSource.get_audio_data   s    	4#3dl#BCC	>>4$,''$*@@	##d&<<""9--	!y)XrBBBr   c                      t          d          )z\Generate `num_bytes` bytes of data.

        Return data as ctypes array or string.
        abstractr   r   rd   s     r   rc   zSynthesisSource._generate_data   s    
 "*---r   c                 "   t          || j        z            | _        t          t	          | j        d          | j                  | _        | j        dk    r| xj        dz  c_        | j                            | j	        | j
                  | _        d S )Nr   rE   rV   )r,   r^   rZ   r    r   r_   r]   r`   r   r[   rX   ra   )r   rf   s     r   seekzSynthesisSource.seek   s    9t'==>> 3t|Q//1ABB !Q&&LLJ&LL#'=#>#>t?PRVR`#a#a   r   c                    |                      d           |                     | j                                                  }t	          j        ddt          |          dz   dz
  dddd| j        | j        | j	        | j
        d	t          |                    }t          |d
          5 }|                    |           |                    |           ddd           dS # 1 swxY w Y   dS )a  Save the audio to disk as a standard RIFF Wave.

        A standard RIFF wave header will be added to the raw PCM
        audio data when it is saved to disk.

        :Parameters:
            `filename` : str
                The file name to save as.

        r   z<4sI8sIHHIIHH4sIs   RIFF,      s   WAVEfmt rQ   r   s   datawbN)rm   rh   r_   get_string_datastructpacklenr[   r^   r]   r\   openwrite)r   filenamerg   headerfs        r   savezSynthesisSource.save   s    			!""4#344DDFF/$ YY^a/(!.!3!3!.$ YY( ( (D!! 	QGGFOOOGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   &+CC"%C")rP   rQ   N)rD   )	r   r   r   r   r"   rh   rc   rm   r{   r   r   r   rO   rO      sz        	 	+ + + +(C C C C. . .
b 
b 
b    r   rO   c                       e Zd ZdZd ZdS )SilencezA silent waveform.c                 ,    | j         dk    rd|z  S d|z  S )Nr      W    )r]   rk   s     r   rc   zSilence._generate_data  s&    !Q&&Y&&9$$r   Nr   r   r   r   rc   r   r   r   r}   r}     s)        % % % % %r   r}   c                       e Zd ZdZd ZdS )
WhiteNoisezA white noise, random waveform.c                 *    t          j        |          S r   )osurandomrk   s     r   rc   zWhiteNoise._generate_data  s    z)$$$r   Nr   r   r   r   r   r     s)        ))% % % % %r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )Sinea  A sinusoid (sine) waveform.

    :Parameters:
        `duration` : float
            The length, in seconds, of audio that you wish to generate.
        `frequency` : int
            The frequency, in Hz of the waveform you wish to produce.
        `sample_rate` : int
            Audio samples per second. (CD quality is 44100).
        `sample_size` : int
            The bit precision. Must be either 8 or 16.
      c                 X     t          t          |           j        |fi | || _        d S r   )superr   r"   	frequencyr   r   r   kwargs	__class__s       r   r"   zSine.__init__&  s3    "dD"866v666"r   c                    | j         dk    r|}d}d}t          j        |z              }n|dz	  }d}d}t          j        |z              }| j        t
          j        dz  z  | j        j        z  }| j	        }t          |          D ]?}t          t          j        ||z            |z  t          |          z  |z             ||<   @t          |          S Nr      r     rE   )r]   ctypesc_ubytec_shortr   rF   rG   rY   r   ra   r-   r,   rH   nextbytes)	r   rd   samplesbiasr!   rg   rL   r`   r/   s	            r   rc   zSine._generate_data*  s    !Q&&GDINW,//DD1nGDINW,//D~1-0A0MM+w 	R 	RA$(4!8,,y84>>IDPQQDGGT{{r   r   r   r   r   r   r"   rc   __classcell__r   s   @r   r   r     sV         # # # # # #      r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )Trianglea  A triangle waveform.

    :Parameters:
        `duration` : float
            The length, in seconds, of audio that you wish to generate.
        `frequency` : int
            The frequency, in Hz of the waveform you wish to produce.
        `sample_rate` : int
            Audio samples per second. (CD quality is 44100).
        `sample_size` : int
            The bit precision. Must be either 8 or 16.
    r   c                 X     t          t          |           j        |fi | || _        d S r   )r   r   r"   r   r   s       r   r"   zTriangle.__init__I  3    &h&x::6:::"r   c                    | j         dk    r|}d}d}d}t          j        |z              }n!|dz	  }d}d}d}t          j        |z              }||z
  dz  | j        z  | j        j        z  }| j        }t          |          D ]K}	||z  }||k    r|||z
  z
  }| }||k     r|||z
  z
  }| }t          |t          |          z            ||	<   Lt          |          S )Nr   r      r   r    rE   )r]   r   r   r   r   rY   r   ra   r-   r,   r   r   
r   rd   r   rM   maximumminimumrg   rL   r`   r/   s
             r   rc   zTriangle._generate_dataM  s   !Q&&GEGGNW,//DD1nGEGGNW,//D'!Q&7$:K:WW+w 	2 	2ATMEw57?3uw57?3u%$x..011DGGT{{r   r   r   r   s   @r   r   r   <  sV         # # # # # #      r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )Sawtootha  A sawtooth waveform.

    :Parameters:
        `duration` : float
            The length, in seconds, of audio that you wish to generate.
        `frequency` : int
            The frequency, in Hz of the waveform you wish to produce.
        `sample_rate` : int
            Audio samples per second. (CD quality is 44100).
        `sample_size` : int
            The bit precision. Must be either 8 or 16.
    r   c                 X     t          t          |           j        |fi | || _        d S r   )r   r   r"   r   r   s       r   r"   zSawtooth.__init__u  r   r   c                 |   | j         dk    r|}d}d}d}t          j        |z              }n!|dz	  }d}d}d}t          j        |z              }||z
  | j        z  | j        z  }| j        }t          |          D ]7}	||z  }||k    r|||z  z   }t          |t          |          z            ||	<   8t          |          S )Nr   r   r   r   r   r   )r]   r   r   r   r   r[   ra   r-   r,   r   r   r   s
             r   rc   zSawtooth._generate_datay  s    !Q&&GEGGNW,//DD1nGEGGNW,//D'!T^3d6GG+w 	2 	2ATMEw57?3%$x..011DGGT{{r   r   r   r   s   @r   r   r   h  sV         # # # # # #      r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )Squarea  A square (pulse) waveform.

    :Parameters:
        `duration` : float
            The length, in seconds, of audio that you wish to generate.
        `frequency` : int
            The frequency, in Hz of the waveform you wish to produce.
        `sample_rate` : int
            Audio samples per second. (CD quality is 44100).
        `sample_size` : int
            The bit precision. Must be either 8 or 16.
    r   c                 X     t          t          |           j        |fi | || _        d S r   )r   r   r"   r   r   s       r   r"   zSquare.__init__  s3    $fd$X88888"r   c                    | j         dk    r|}d}d}t          j        |z              }n|dz	  }d}d}t          j        |z              }| j        j        | j        z  dz  }| j        }d}d}	t          |          D ]=}
|	|k    r| }|	|z  }	|	dz  }	t          ||z  t          |          z  |z             ||
<   >t          |          S r   )r]   r   r   r   rY   r   r   ra   r-   r,   r   r   )r   rd   r   r   r!   rg   half_periodr`   rM   countr/   s              r   rc   zSquare._generate_data  s    !Q&&GDINW,//DD1nGDINW,//D'3dnDqH+w 	E 	EA##$QJE%)+d8nn<tCDDDGGT{{r   r   r   r   s   @r   r   r     sV         # # # # # #
      r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )FMa  A simple FM waveform.

    This is a simplistic frequency modulated waveform, based on the
    concepts by John Chowning. Basic sine waves are used for both
    frequency carrier and modulator inputs, of which the frequencies can
    be provided. The modulation index, or amplitude, can also be adjusted.

    :Parameters:
        `duration` : float
            The length, in seconds, of audio that you wish to generate.
        `carrier` : int
            The carrier frequency, in Hz.
        `modulator` : int
            The modulator frequency, in Hz.
        `mod_index` : int
            The modulation index.
        `sample_rate` : int
            Audio samples per second. (CD quality is 44100).
        `sample_size` : int
            The bit precision. Must be either 8 or 16.
    r   r   c                 t     t          t          |           j        |fi | || _        || _        || _        d S r   )r   r   r"   carrier	modulator	mod_index)r   r   r   r   r   r   r   s         r   r"   zFM.__init__  s?     b$ 44V444""r   c                    | j         dk    r|}d}d}t          j        |z              }n|dz	  }d}d}t          j        |z              }dt          j        z  | j        z  }dt          j        z  | j        z  }| j        }| j	        }	| j
        }
t          j        }t          |          D ]M}||	z  }t           |||z  | |||z            z  z             |z  t          |
          z  |z             ||<   Nt          |          S r   )r]   r   r   r   rF   rG   r   r   r   r[   ra   rH   r-   r,   r   r   )r   rd   r   r   r!   rg   car_stepmod_stepr   r   r`   rH   r/   	increments                 r   rc   zFM._generate_data  s"   !Q&&GDINW,//DD1nGDINW,//Dtw;-tw;/N	'+hw 	? 	?AKI##h2YXPYEYAZAZ5ZZ[[%&(,X79=> ? ?DGGT{{r   )r   r   r   r   r   s   @r   r   r     sV         *# # # # # #      r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )Digitara3  A guitar-like waveform.

    A guitar-like waveform, based on the Karplus-Strong algorithm.
    The sound is similar to a plucked guitar string. The resulting
    sound decays over time, and so the actual length will vary
    depending on the frequency. Lower frequencies require a longer
    `length` parameter to prevent cutting off abruptly.

    :Parameters:
        `duration` : float
            The length, in seconds, of audio that you wish to generate.
        `frequency` : int
            The frequency, in Hz of the waveform you wish to produce.
        `decay` : float
            The decay rate of the effect. Defaults to 0.996.
        `sample_rate` : int
            Audio samples per second. (CD quality is 44100).
        `sample_size` : int
            The bit precision. Must be either 8 or 16.
    r   Zd;?c                      t          t          |           j        |fi | || _        || _        t          | j        | j        z            | _        d S r   )r   r   r"   r   r4   r,   r[   rI   )r   r   r   r4   r   r   s        r   r"   zDigitar.__init__  sS    %gt%h99&999"
$+dn<==r   c                    | j         dk    r|}d}d}t          j        |z              }n|dz	  }d}d}t          j        |z              }t	          j        d           | j        }t          d t          |          D             |          }| j	        }t          |          D ]J}	t          |d         |z  |z             ||	<   |                    ||d         |d         z   z  dz             Kt          |          S )	Nr   r   r   r   
   c                 8    g | ]}t          j        d d          S )r   )randomuniform).0_s     r   
<listcomp>z*Digitar._generate_data.<locals>.<listcomp>  s$    JJJqV^B22JJJr   )maxlenrE   )r]   r   r   r   r   seedrI   r   r-   r4   r,   appendr   )
r   rd   r   r   r!   rg   rI   ring_bufferr4   r/   s
             r   rc   zDigitar._generate_data
  s   !Q&&GDINW,//DD1nGDINW,//DBJJE&MMJJJSYZZZ
w 	N 	NA+a.94t;<<DGuAQ(GH1LMMMMT{{r   )r   r   r   r   s   @r   r   r     sV         (> > > > > >      r   r   )r   rF   rs   r   r   codecs.baser   r   r   collectionsr   r	   r   r(   r1   r?   rO   r}   r   r   r   r   r   r   r   r   r   r   <module>r      s  H 
			     7 7 7 7 7 7 7 7 7 7      " " " " " " " "    8   "9 9 9 9 9( 9 9 9*)9 )9 )9 )9 )98 )9 )9 )9XJ J J J Jh J J JD\ \ \ \ \f \ \ \~% % % % %o % % %% % % % % % % %         ?      F) ) ) ) ) ) ) )X% % % % % % % %P' ' ' ' '_ ' ' 'T2 2 2 2 2 2 2 2j- - - - -o - - - - -r   