legoEv3/ev3dev2/__pycache__/sound.cpython-35.pyc




[c@sddlZejd
kr'edddlZddlZddlZddlmZmZm    Z    ddZ
Gdd    d    eZdS)Nz"Must be using Python 3.4 or higher)check_outputPopenPIPEcCsTt}xD|D]<\}}t|}x!|jdD]}|||<q8WqW|S)zN Utility function used by Sound class for building the note frequencies table /)dictroundsplit)Znotesresnotefreqnr//usr/lib/python3/dist-packages/ev3dev2/sound.py_make_scales%s    rcn@s6eZdZdZdZdZdZdZeeefZddZ    ded    d
Z
dedd
ZddeddZdeddZ
eddZdeddZddeddZddZdddZddd Zd!d"d#d$ZedrZddddsddtdduddviZdS(wSoundab
    Support beep, play wav files, or convert text to speech.

    Note that all methods of the class spawn system processes and return
    subprocess.Popen objects. The methods are asynchronous (they return
    immediately after child process was spawned, without waiting for its
    completion), but you can call wait() on the returned result.

    Examples::

        # Play 'bark.wav':
        Sound.play('bark.wav')

        # Introduce yourself:
        Sound.speak('Hello, I am Robot')

        # Play a small song
        Sound.play_song((
            ('D4', 'e3'),
            ('D4', 'e3'),
            ('D4', 'e3'),
            ('G4', 'h'),
            ('D5', 'h')
        ))

    In order to mimic EV3-G API parameters, durations used in methods
    exposed as EV3-G blocks for sound related operations are expressed
    as a float number of seconds.
    NrcCsB||jks>td|djdd|jDfdS)Nz'Invalid play_type %s, must be one of %s,css|]}t|VqdS)N)str).0trrr    <genexpr>]sz,Sound._validate_play_type.<locals>.<genexpr>)
PLAY_TYPESAssertionErrorjoin)self    play_typerrr_validate_play_type[szSound._validate_play_typecCs`ttjdG}ttjd|d|}|tjkrQ|jdS|SWdQRXdS)a
        Call beep command with the provided arguments (if any).
        See `beep man page`_ and google `linux beep music`_ for inspiration.

        :param string args: Any additional arguments to be passed to ``beep`` (see the `beep man page`_ for details)
        
        :param play_type: The behavior of ``beep`` once playback has been initiated
        :type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE`` or  ``Sound.PLAY_NO_WAIT_FOR_COMPLETE``

        :return: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise

        .. _`beep man page`: https://linux.die.net/man/1/beep
        .. _`linux beep music`: https://www.google.com/search?q=linux+beep+music
        wz/usr/bin/beep %sstdoutN)    openosdevnullrshlexr
rPLAY_WAIT_FOR_COMPLETEwait)rargsrr
subprocessrrrbeep_s
z
Sound.beeprcsfdd}t|dkr5||dSt|dkrb||d|dfgStdtt|dS)a
        .. rubric:: tone(tone_sequence)

        Play tone sequence.

        Here is a cheerful example::

            my_sound = Sound()
            my_sound.tone([
                (392, 350, 100), (392, 350, 100), (392, 350, 100), (311.1, 250, 100),
                (466.2, 25, 100), (392, 350, 100), (311.1, 250, 100), (466.2, 25, 100),
                (392, 700, 100), (587.32, 350, 100), (587.32, 350, 100),
                (587.32, 350, 100), (622.26, 250, 100), (466.2, 25, 100),
                (369.99, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 700, 100),
                (784, 350, 100), (392, 250, 100), (392, 25, 100), (784, 350, 100),
                (739.98, 250, 100), (698.46, 25, 100), (659.26, 25, 100),
                (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200), (554.36, 350, 100),
                (523.25, 250, 100), (493.88, 25, 100), (466.16, 25, 100), (440, 25, 100),
                (466.16, 50, 400), (311.13, 25, 200), (369.99, 350, 100),
                (311.13, 250, 100), (392, 25, 100), (466.16, 350, 100), (392, 250, 100),
                (466.16, 25, 100), (587.32, 700, 100), (784, 350, 100), (392, 250, 100),
                (392, 25, 100), (784, 350, 100), (739.98, 250, 100), (698.46, 25, 100),
                (659.26, 25, 100), (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200),
                (554.36, 350, 100), (523.25, 250, 100), (493.88, 25, 100),
                (466.16, 25, 100), (440, 25, 100), (466.16, 50, 400), (311.13, 25, 200),
                (392, 350, 100), (311.13, 250, 100), (466.16, 25, 100),
                (392.00, 300, 150), (311.13, 250, 100), (466.16, 25, 100), (392, 700)
                ])

        Have also a look at :py:meth:`play_song` for a more musician-friendly way of doing, which uses
        the conventional notation for notes and durations.

        :param list[tuple(float,float,float)] tone_sequence: The sequence of tones to play. The first number of each tuple is frequency in Hz, the second is duration in milliseconds, and the third is delay in milliseconds between this and the next tone in the sequence.

        :param play_type: The behavior of ``tone`` once playback has been initiated
        :type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_NO_WAIT_FOR_COMPLETE``

        :return: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise

        .. rubric:: tone(frequency, duration)

        Play single tone of given frequency and duration.

        :param float frequency: The frequency of the tone in Hz
        :param float duration: The duration of the tone in milliseconds

        :param play_type: The behavior of ``tone`` once playback has been initiated
        :type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_NO_WAIT_FOR_COMPLETE``

        :return: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise
        csDdddddjdjfdd|DdS)NcSsXd}|dk    r |d|7}|dk    r:|d|7}|dk    rT|d|7}|S)Nr z-f %s z-l %s z-D %s r)    frequencydurationdelayr)rrr    beep_argssz9Sound.tone.<locals>.play_tone_sequence.<locals>.beep_argsz -n csg|]}|qSrr)rr)r/rr
<listcomp>s    z:Sound.tone.<locals>.play_tone_sequence.<locals>.<listcomp>r)r+r)Z
tone_sequence)rr)r/rplay_tone_sequencesz&Sound.tone.<locals>.play_tone_sequencerrrzGUnsupported number of parameters in Sound.tone(): expected 1 or 2, got N)len    Exceptionr)rrr)r1r)rrrtonews4z
Sound.tonegdcCs|j||dkr)td||dkrEtd|d|ko\dknsqtd||j|t|d}t|d}|j|||fgd|dS)    a Play a single tone, specified by its frequency, duration, volume and final delay.

        :param int frequency: the tone frequency, in Hertz
        :param float duration: Tone duration, in seconds
        :param float delay: Delay after tone, in seconds (can be useful when chaining calls to ``play_tone``)
        :param int volume: The play volume, in percent of maximum volume

        :param play_type: The behavior of ``play_tone`` once playback has been initiated
        :type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``

        :return: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the PID of the underlying beep command; ``None`` otherwise

        :raises ValueError: if invalid parameter
        rzinvalid duration (%s)zinvalid delay (%s)r5zinvalid volume (%s)irN)r
ValueError
set_volumeintr4)rr,r-r.volumerduration_msdelay_msrrr    play_tones

zSound.play_tonecCs|j|y|j|j}Wn"tk
rHtd|YnX|dkretd|d|ko|dknstd||j|d|d|d|S)    a Plays a note, given by its name as defined in ``_NOTE_FREQUENCIES``.

        :param string note: The note symbol with its octave number
        :param float duration: Tone duration, in seconds
        :param int volume: The play volume, in percent of maximum volume

        :param play_type: The behavior of ``play_note`` once playback has been initiated
        :type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``

        :return: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the PID of the underlying beep command; ``None`` otherwise

        :raises ValueError: is invalid parameter (note, duration,...)
        zinvalid note (%s)rzinvalid duration (%s)r5zinvalid volume (%s)r-r9r)r_NOTE_FREQUENCIESupperKeyErrorr6r<)rrr-r9rr
rrr    play_notes

zSound.play_notecCs|j|ttjd}|tjkr]ttjd|d|}|j    nk|tj
krttjd|d|S|tjkrx-ttjd|d|}|j    qWWdQRXdS)a Play a sound file (wav format).

        :param string wav_file: The sound file path

        :param play_type: The behavior of ``play`` once playback has been initiated
        :type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``

        :returns: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise
        r!z/usr/bin/aplay -q "%s"r"N)rr#r$r%rr'rr&r
r(PLAY_NO_WAIT_FOR_COMPLETE    PLAY_LOOP)rwav_filerrpidrrrplays


z
Sound.playcCs!|j||j||dS)a' Play a sound file (wav format) at a given volume.

        
        :param string wav_file: The sound file path
        :param int volume: The play volume, in percent of maximum volume

        :param play_type: The behavior of ``play_file`` once playback has been initiated
        :type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``

        :returns: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise
        N)r7rE)rrCr9rrrr    play_files
zSound.play_filez
-a 200 -s 130c

CsB|j||j|ttjd}ddgtj|tj|g}tjd}|tj    krt
|dt}t
|d|jd|}    |    j
n|tjkrt
|dt}t
|d|jd|S|tjkr7x;t
|dt}t
|d|jd|}    |    j
qWWdQRXdS)a| Speak the given text aloud.

        Uses the ``espeak`` external command.

        :param string text: The text to speak
        :param string espeak_opts: ``espeak`` command options (advanced usage)
        :param int volume: The play volume, in percent of maximum volume

        :param play_type: The behavior of ``speak`` once playback has been initiated
        :type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``

        :returns: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise
        r!z/usr/bin/espeakz--stdoutz/usr/bin/aplay -qr"stdinN)rr7r#r$r%r&r
Zquoterr'rrr"r(rArB)
rtextZespeak_optsr9rrcmd_lineZaplay_cmd_lineZespeakrErrrspeak$s"

)
zSound.speakcCsd|jdkr]tddgj}tjd|}|rT|jd|_n    d|_|jS)zM
        :returns: The detected sound channel
        :rtype: string
        NamixerZ    scontrolsz'(?P<channel>[^']+)'channelZPlayback)rLrdecoderesearchgroup)routmrrr_get_channelHs    zSound._get_channelcCsG|dkr|j}dj||}ttj|jdS)a_
        Sets the sound volume to the given percentage [0-100] by calling
        ``amixer -q set <channel> <pct>%``.
        If the channel is not specified, it tries to determine the default one
        by running ``amixer scontrols``. If that fails as well, it uses the
        ``Playback`` channel, as that is the only channel on the EV3.
        Nz!/usr/bin/amixer -q set {0} {1:d}%)rSformatrr&r
r()rZpctrLrIrrrr7]s    zSound.set_volumecCsw|dkr|j}tdd|gj}tjd|}|r^t|jdStdj|dS)aK
        Gets the current sound volume by parsing the output of
        ``amixer get <channel>``.
        If the channel is not specified, it tries to determine the default one
        by running ``amixer scontrols``. If that fails as well, it uses the
        ``Playback`` channel, as that is the only channel on the EV3.
        NrKgetz\[(?P<volume>\d+)%\]r9z)Failed to parse output of `amixer get {}`)    rSrrMrNrOr8rPr3rT)rrLrQrRrrr
get_volumels    zSound.get_volumexg?cs|dkrtd||dkr8td|t|dd|dfddy-jd    jfd
d|DSWn5tk
r}ztd|WYd
d
}~XnXd
S)a
 Plays a song provided as a list of tuples containing the note name and its
        value using music conventional notation instead of numerical values for frequency
        and duration.

        It supports symbolic notes (e.g. ``A4``, ``D#3``, ``Gb5``) and durations (e.g. ``q``, ``h``).

        For an exhaustive list of accepted note symbols and values, have a look at the ``_NOTE_FREQUENCIES``
        and ``_NOTE_VALUES`` private dictionaries in the source code.

        The value can be suffixed by modifiers:

        - a *divider* introduced by a ``/`` to obtain triplets for instance
          (e.g. ``q/3`` for a triplet of eight note)
        - a *multiplier* introduced by ``*`` (e.g. ``*1.5`` is a dotted note).

        Shortcuts exist for common modifiers:

        - ``3`` produces a triplet member note. For instance `e3` gives a triplet of eight notes,
          i.e. 3 eight notes in the duration of a single quarter. You must ensure that 3 triplets
          notes are defined in sequence to match the count, otherwise the result will not be the
          expected one.
        - ``.`` produces a dotted note, i.e. which duration is one and a half the base one. Double dots
          are not currently supported.

        Example::

            >>> # A long time ago in a galaxy far,
            >>> # far away...
            >>> Sound.play_song((
            >>>     ('D4', 'e3'),      # intro anacrouse
            >>>     ('D4', 'e3'),
            >>>     ('D4', 'e3'),
            >>>     ('G4', 'h'),       # meas 1
            >>>     ('D5', 'h'),
            >>>     ('C5', 'e3'),      # meas 2
            >>>     ('B4', 'e3'),
            >>>     ('A4', 'e3'),
            >>>     ('G5', 'h'),
            >>>     ('D5', 'q'),
            >>>     ('C5', 'e3'),      # meas 3
            >>>     ('B4', 'e3'),
            >>>     ('A4', 'e3'),
            >>>     ('G5', 'h'),
            >>>     ('D5', 'q'),
            >>>     ('C5', 'e3'),      # meas 4
            >>>     ('B4', 'e3'),
            >>>     ('C5', 'e3'),
            >>>     ('A4', 'h.'),
            >>> ))

        .. important::

            Only 4/4 signature songs are supported with respect to note durations.

        :param iterable[tuple(string, string)] song: the song
        :param int tempo: the song tempo, given in quarters per minute
        :param float delay: delay between notes (in seconds)

        :return: the spawn subprocess from ``subprocess.Popen``

        :raises ValueError: if invalid note in song or invalid play parameters
        rzinvalid tempo (%s)zinvalid delay (%s)ii`rcs%j|j}d|krR|jd\}}j|t|}nd|kr|jd\}}j|t|}n|jdr|dd}j|d}nL|jdr|dd}j|dd    }nj|}d
||fS)
aI Builds the arguments string for producing a beep matching
            the requested note and value.

            Args:
                note (str): the note note and octave
                value (str): the note value expression
            Returns:
                str: the arguments to be passed to the beep command
            r*.Nrg?3rrz-f %d -l %d -D %dr[)r=r>r
_NOTE_VALUESfloatendswith)rvaluer
baseZfactorr:)r;meas_duration_msrrrr/s
z"Sound.play_song.<locals>.beep_argsz -n cs%g|]\}}||qSrr)rrr_)r/rrr0s    z#Sound.play_song.<locals>.<listcomp>zinvalid note (%s)N)r6r8r+rr?)rZsongZtempor.er)r/r;rarr    play_songs?!zSound.play_songC0皙Y0@C#0/Db0RQ1@D0皙Y2@D#0/Eb033333s3@E0皙4@F0Gz5@F#0/Gb0Q7@G08@G#0/Ab0(\9@A0;@A#0/Bb0p=
#=@B0Q>@C1皙Y@@C#1/Db133333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