ó
ÔŒ\c           @  sS  d  d l  m Z d  d l Z d  d l m Z m Z d  d l m Z m Z e e d d d e
 d d e
 d „ Z d d e d d d „ Z d d	 „ Z d d d
 „ Z e
 d „ Z d „  Z d d „ Z d d „ Z d d „ Z d d „ Z d d „ Z d d d „ Z d d „ Z d d „ Z d d „ Z d d „ Z e
 d „ Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   print_functionN(   t   get_channelt
   get_serial(   t   register_channelt   alias_channelt   musici    c         C  sõ  t  j j ƒ  j r! t d ƒ ‚ n  |  d k r1 d St |  t ƒ rL |  g }  n  t  j j j	 ”yst
 | ƒ } | j }	 | d k rŒ | j } n  | d k r­ t  j j r­ | } n  | j |  k }
 | j ƒ  | d k rá t  j j } n  | r| j ƒ  |  k rd } |
 } n | j | ƒ t } t  j j rCt  j j rC| rCt } n t } | r}| j |  d | d | d | d | d | ƒn  t ƒ  } | |	 _ | | _ | r³|  |	 _ | |	 _ n g  |	 _ t |	 _ t |	 _ Wn t  j j rë‚  qën XWd QXd S(	   s‘  
    :doc: audio

    This stops the music currently playing on the numbered channel, dequeues
    any queued music, and begins playing the specified file or files.

    `filenames`
        This may be a single file, or a list of files to be played.

    `channel`
        The channel to play the sound on.

    `loop`
        If this is True, the tracks will loop while they are the last thing
        in the queue.

    `fadeout`
        If not None, this is a time in seconds to fade for. Otherwise the
        fadeout time is taken from config.fade_music.

    `synchro_start`
        Ren'Py will ensure that all channels of with synchro_start set to true
        will start playing at exactly the same time. Synchro_start should be
        true when playing two audio files that are meant to be synchronized
        with each other.

    `fadein`
        This is the number of seconds to fade the music in for, on the
        first loop only.

    `tight`
        If this is True, then fadeouts will span into the next-queued sound. If
        None, this is true when loop is True, and false otherwise.

    `if_changed`
        If this is True, and the music file is currently playing,
        then it will not be stopped/faded out and faded back in again, but
        instead will be kept playing. (This will always queue up an additional
        loop of the music.)

    This clears the pause flag for `channel`.
    s#   Can't play music during init phase.Ni    t   loopt   synchro_startt   fadeint   tightt	   loop_only(   t   renpyt   gamet   contextt
   init_phaset	   Exceptiont   Nonet
   isinstancet
   basestringt   audiot   lockR   t   default_loopt   configt   tight_loop_defaultR   t   dequeuet
   fade_musict   get_playingt   fadeoutt   Falset   skip_soundst   skippingt   Truet   enqueueR   t   last_changedt   last_filenamest
   last_tightt   pauset   debug_sound(   t	   filenamest   channelR   R   R   R   R	   t
   if_changedt   ct   ctxt   loop_is_filenamesR
   R    t   t(    (    s   renpy/audio/music.pyt   play#   sP    ,		
		.						c   
      C  s»  t  j j ƒ  j r! t d ƒ ‚ n  |  d k r< g  }  t } n  t |  t ƒ rW |  g }  n  t  j	 j
 d k rv t | ƒ n  t  j j j 0yt | ƒ } | j } | d k r¶ | j } n  | d k r× t  j	 j r× | } n  | rí | j t ƒ n  t  j	 j rt  j	 j
 r| rt } n t } | rC| j |  d | d | d | ƒn  t ƒ  }	 |	 | _ |	 | _ | ry|  | _ | | _ n g  | _ t | _ t | _ Wn t  j	 j r±‚  q±n XWd QXd S(   sÏ  
    :doc: audio

    This queues the given filenames on the specified channel.

    `filenames`
        This may be a single file, or a list of files to be played.

    `channel`
        The channel to play the sound on.

    `loop`
        If this is True, the tracks will loop while they are the last thing
        in the queue.

    `clear_queue`
        If True, then the queue is cleared, making these files the files that
        are played when the currently playing file finishes. If it is False,
        then these files are placed at the back of the queue. In either case,
        if no music is playing these files begin playing immediately.

    `fadein`
        This is the number of seconds to fade the music in for, on the
        first loop only.

    `tight`
        If this is True, then fadeouts will span into the next-queued sound. If
        None, this is true when loop is True, and false otherwise.

    This clears the pause flag for `channel`.
    s#   Can't play music during init phase.t   fastR   R   R	   N(   R   R   R   R   R   R   R   R   R   R   R   t   stopR   R   R   R   R   R   R   R   R    R   R!   R"   R#   R$   R%   (
   R&   R'   R   t   clear_queueR   R	   R)   R*   R    R,   (    (    s   renpy/audio/music.pyt   queueŒ   sH    !				"						c         C  sE   t  | ƒ } | j |  t ƒ \ }  } } t j j | j |  | j ƒ S(   s¡   
    Return true if the given filename is playable on the channel. This
    takes into account the prefix and suffix, and ignores a preceding
    specifier.
    (   R   t   split_filenameR   R   t   loadert   loadablet   file_prefixt   file_suffix(   t   filenameR'   R)   t   _(    (    s   renpy/audio/music.pyt   playableä   s    c         C  sº   t  j j ƒ  j r d St  j j j  yn t |  ƒ } | j } | d k rY t  j j	 } n  | j
 | ƒ t ƒ  } | | _ | | _ g  | _ t | _ Wn t  j j r° ‚  q° n XWd QXd S(   së  
    :doc: audio

    This stops the music that is currently playing, and dequeues all
    queued music. If fadeout is None, the music is faded out for the
    time given in config.fade_music, otherwise it is faded for fadeout
    seconds.

    This sets the last queued file to None.

    `channel`
        The channel to stop the sound on.

    `fadeout`
        If not None, this is a time in seconds to fade for. Otherwise the
        fadeout time is taken from config.fade_music.


    N(   R   R   R   R   R   R   R   R   R   R   R   R   R!   R"   R   R#   R%   (   R'   R   R)   R*   R,   (    (    s   renpy/audio/music.pyR/   ò   s"    					c         C  s5   t  |  ƒ } | r | j r d S| | _ t | _ d S(   s5   
    Determines if channel will loop by default.
    N(   R   t   default_loop_setR   R   (   R'   t   flagt   defaultR)   (    (    s   renpy/audio/music.pyt	   set_music   s
    	c         C  s   t  |  ƒ } | j S(   s9   
    Returns true if "channel" will loop by default.
    (   R   R   (   R'   R)   (    (    s   renpy/audio/music.pyt   is_music.  s    c         C  su   yT t  j j j | ƒ } | j ƒ  } | s7 | d k  r; d S| |  k rK d S|  | SWn t  j j rl ‚  n  d SXd S(   sS   
    Returns the number of seconds left until the given time in the
    music.
    i    N(   R   R   R   t   get_posR   R   R%   (   t   timeR'   R)   R,   (    (    s   renpy/audio/music.pyt	   get_delay7  s    c         C  se   yD t  j j j |  ƒ } | j ƒ  } | s7 | d k  r; d S| d SWn t  j j r\ ‚  n  d SXd S(   sU  
    :doc: audio

    Returns the current position of the audio or video file on `channel`, in
    seconds. Returns None if no audio is playing on `channel`.

    As this may return None before a channel starts playing, or if the audio
    channel involved has been muted, callers of this function should
    always handle a None value.
    i    g     @@N(   R   R   R   R?   R   R   R%   (   R'   R)   R,   (    (    s   renpy/audio/music.pyR?   P  s    c         C  sD   y# t  j j j |  ƒ } | j ƒ  SWn t  j j r; ‚  n  d SXd S(   sŒ   
    :doc: audio

    Returns the duration of the audio or video file on `channel`. Returns
    0.0 if no file is playing on `channel`.
    N(   R   R   R   t   get_durationR   R%   R   (   R'   R)   (    (    s   renpy/audio/music.pyRB   l  s    c         C  sD   y# t  j j j |  ƒ } | j ƒ  SWn t  j j r; ‚  n  d SXd S(   sw   
    :doc: audio

    If the given channel is playing, returns the playing file name.
    Otherwise, returns None.
    N(   R   R   R   R   R   R%   R   (   R'   R)   (    (    s   renpy/audio/music.pyR     s    c         C  s   t  d |  ƒ d k	 S(   s’   
    :doc: audio

    Returns True if the channel is currently playing a sound, False if
    it is not, or if the sound system isn't working.
    R'   N(   R   R   (   R'   (    (    s   renpy/audio/music.pyt
   is_playing‘  s    c         C  sI   y) t  j j j | ƒ } | j |  | ƒ Wn t  j j rE ‚  qE n Xd S(   sï  
    :doc: audio

    Sets the volume of this channel, as a fraction of the volume of the
    mixer controlling the channel.

    `volume`
        This is a number between 0.0 and 1.0, and is interpreted as a fraction
        of the mixer volume for the channel.

    `delay`
        It takes delay seconds to change/fade the volume from the old to
        the new value. This value is persisted into saves, and participates
        in rollback.

    `channel`
        The channel to be set
    N(   R   R   R   t   set_secondary_volumeR   R%   (   t   volumet   delayR'   R)   (    (    s   renpy/audio/music.pyt
   set_volumeœ  s    c         C  sI   y) t  j j j | ƒ } | j |  | ƒ Wn t  j j rE ‚  qE n Xd S(   s;  
    :doc: audio

    Sets the pan of this channel.

    `pan`
        A number between -1 and 1 that control the placement of the audio.
        If this is -1, then all audio is sent to the left channel.
        If it's 0, then the two channels are equally balanced. If it's 1,
        then all audio is sent to the right ear.

    `delay`
        The amount of time it takes for the panning to occur.

    `channel`
        The channel the panning takes place on. This can be a sound or a music
        channel. Often, this is channel 7, the default music channel.
    N(   R   R   R   t   set_panR   R%   (   t   panRF   R'   R)   (    (    s   renpy/audio/music.pyRH   ¸  s    c         C  sB   y" t  j j j | ƒ } |  | _ Wn t  j j r> ‚  q> n Xd S(   sÌ  
    :doc: audio

    This sets a callback that is called when the queue is empty. This
    callback is called when the queue first becomes empty, and at
    least once per interaction while the queue is empty.

    The callback is called with no parameters. It can queue sounds by
    calling renpy.music.queue with the appropriate arguments. Please
    note that the callback may be called while a sound is playing, as
    long as a queue slot is empty.
    N(   R   R   R   t   callbackR   R%   (   RJ   R'   R)   (    (    s   renpy/audio/music.pyt   set_queue_empty_callbackÔ  s    c         C  sE   y% t  j j j | ƒ } |  | j _ Wn t  j j rA ‚  qA n Xd S(   sŽ   
    :doc: audio

    Sets the pause flag for `channel` to `value`. If True, the channel
    will pause, otherwise it will play normally.
    N(   R   R   R   R   R$   R   R%   (   t   valueR'   R)   (    (    s   renpy/audio/music.pyt	   set_pauseé  s    c         C  s2   y# t  j j j |  ƒ } | j j SWn t SXd S(   s@   
    :doc: audio

    Returns the pause flag for `channel`.
    N(   R   R   R   R   R$   R   (   R'   R)   (    (    s   renpy/audio/music.pyt	   get_pauseø  s
    c         C  s[   y; t  j j j |  ƒ } | s. | j d k r: | | _ n  Wn t  j j rW ‚  qW n Xd S(   s¬  
    This sets the name of the mixer associated with a given
    channel. By default, there are two mixers, 'sfx' and
    'music'. 'sfx' is on channels 0 to 3, and 'music'
    on 3 to 7. The voice module calls this function to set channel 2 to voice.
    You can create your own mixer, but will need to add a preference if you
    wish to allow the user to set it.

    This function should only be called in an init block.
    N(   R   R   R   t   mixerR   R   R%   (   R'   RO   R<   R)   (    (    s   renpy/audio/music.pyt	   set_mixer  s    c          C  s=   t  ƒ  }  x' t j j j D] } |  j | j ƒ q Wt |  ƒ S(   s&   
    This gets all mixers in use.
    (   t   setR   R   t   all_channelst   addRO   t   list(   t   rvt   i(    (    s   renpy/audio/music.pyt   get_all_mixers  s    	c         C  s*   y t  j j j |  ƒ t SWn t SXd S(   sA   
    Returns True if the channel exists, or False otherwise.
    N(   R   R   R   R   R   (   R'   (    (    s   renpy/audio/music.pyt   channel_defined*  s
    (   t
   __future__R    t   renpy.audioR   t   renpy.audio.audioR   R   R   R   R   R   R-   R   R1   R9   R/   R=   R>   RA   R?   RB   R   RC   RG   RH   RK   RM   RN   RP   RW   RX   (    (    (    s   renpy/audio/music.pyt   <module>   s0    iX.			