Beispiel #1
0
    def sched_agent(self, ag, delay=0, chan=None, handle=None):
        """sched_agent(agent, delay=0, chan=self.channel, handle=self.run)

        Schedule an agent to run. This may be the current agent (self) or
        a newly-created agent. The delay is a time (in seconds) to delay
        before the agent runs. The channel, if None or not supplied,
        defaults to the same channel that self is running in. The agent's
        run() method will be called, unless you specify a different
        handle function.
        """

        if not isinstance(ag, Agent):
            raise generator.ScheduleError('not an Agent instance')
        if not ag.inited:
            raise generator.ScheduleError('agent is uninitialized')
        if self.generator is None or self.channel is None:
            raise generator.ScheduleError('scheduler has never been scheduled')
        if chan is None:
            chan = self.channel
        if not chan.active:
            raise generator.ChannelError(
                'cannot schedule agent to inactive channel')
        if handle is None:
            handle = ag.run
        gen = self.generator

        starttime = gen.select_time(delay)
        ag.origdelay = delay
        gen.addagent(ag, chan, starttime, handle)
Beispiel #2
0
    def send_event(self, evname, *args, **kwargs):
        """send_event(event, ..., chan=self.channel)

        Send an event. The event consists of the given name, followed by
        zero or more arguments (which may be any Python object). The
        event is sent on the given channel, or (if none given) on the
        agent's own channel.
        """

        chan = kwargs.pop('chan', None)
        if kwargs:
            raise TypeError('invalid keyword argument for this function')

        if self.generator is None or self.channel is None:
            raise generator.ScheduleError('sender has never been scheduled')
        if chan is None:
            chan = self.channel
        if not chan.active:
            raise generator.ChannelError(
                'cannot send event to inactive channel')
        gen = self.generator

        evname = check_prop_name(evname)
        ev = (evname, ) + args

        gen.sendevent(ev, chan)
Beispiel #3
0
    def new_channel(self, startvolume=1.0, parent=None):
        """new_channel(startvolume=1, parent=self.channel) -> channel

        Create a new channel. The startvolume is the volume the channel
        is initially set to; this will affect all sounds played in the
        channel and any subchannels. The new channel will be a subchannel
        of parent -- if None or not supplied, it will be a subchannel of
        the channel that the agent (self) is running in.
        """

        if self.channel is None:
            raise generator.ChannelError('creator is not in a channel')
        if parent is None:
            parent = self.channel
        chan = generator.Channel(parent, self.generator, self, startvolume,
                                 stereo.default())
        return chan
Beispiel #4
0
    def sched_note_duration_pan(self,
                                samp,
                                duration,
                                pan=None,
                                pitch=1.0,
                                volume=1.0,
                                delay=0,
                                chan=None):
        """sched_note_duration_pan(sample, duration, pan=0, pitch=1, volume=1,
            delay=0, chan=self.channel) -> duration

        Schedule a note to play, panning the stereo origin of the sound.
        The pan value defaults to 0, meaning no shift in origin;
        -1 means directly to the left; 1 means directly to the right. The
        value may also be an object created by the stereo module.

        The sound is loaded from samp (which can be a filename, File,
        or Sample object). The pitch is given as a multiple of the
        sound's original frequency; the volume is given as a fraction
        of the sound's original volume. The delay is a time (in seconds)
        to delay before the note is played. The channel, if None or not
        supplied, defaults to the same channel the agent is running in.

        This extends the original sound sample to a longer period of time.
        The duration is given in seconds. This returns the expected duration
        of the sound, in seconds. Due to the way sounds are looped, this may
        be slightly longer than the given duration.
        """

        if self.generator is None or self.channel is None:
            raise generator.ScheduleError('scheduler has never been scheduled')
        if chan is None:
            chan = self.channel
        if not chan.active:
            raise generator.ChannelError(
                'cannot schedule note to inactive channel')
        gen = self.generator
        samp = sample.get(samp)

        starttime = gen.select_time(delay)
        fduration = gen.select_duration(duration)

        pan = stereo.cast(pan)
        dur = samp.queue_note_duration(pitch, volume, pan, starttime,
                                       fduration, chan)
        return float(dur) / float(cboodle.framespersec())
Beispiel #5
0
    def new_channel_pan(self, pan=None, startvolume=1.0, parent=None):
        """new_channel_pan(pan=0, startvolume=1, parent=self.channel) -> channel

        Create a new channel, panning the stereo origin of its sounds.
        (See the stereo module.) The startvolume is the volume the channel
        is initially set to; this will affect all sounds played in the
        channel and any subchannels. The new channel will be a subchannel
        of parent -- if None or not supplied, it will be a subchannel of
        the channel that the agent (self) is running in.
        """

        if self.channel is None:
            raise generator.ChannelError('creator is not in a channel')
        if parent is None:
            parent = self.channel

        pan = stereo.cast(pan)
        chan = generator.Channel(parent, self.generator, self, startvolume,
                                 pan)
        return chan
Beispiel #6
0
    def listen(self, event=None, handle=None, hold=None, chan=None):
        """listen(event=self.selected_event, handle=self.receive, hold=None,
            chan=self.channel) -> Handler

        Begin listening for events. The event should be a string, or a
        function which returns a string. (If no event is given, the
        agent.selected_event field will be consulted.) The agent will
        listen on the given channel, or (if none is given) on the
        agent's own channel.

        The agent will react whenever a matching event is seen on the
        channel. An event matches if it is equal to the selected event
        string, or begins with it; and if it is in the listening channel,
        or a subchannel of it. (So event "foo.bar" will trigger agents
        listening for event "foo.bar", "foo", or "".)

        When an agent is triggered, its receive() method is run. (If you
        pass a different function as handle, that function will be run.)

        The hold value indicates whether the agent's channel will be kept
        alive for as long as it listens. If this is False/None, the channel
        will follow the usual rule and expire as soon as nothing is scheduled
        on it. (A listening agent does not automatically count as scheduled!)
        If the listening channel is not the same as the agent's own channel,
        you may pass one of the constants HoldRun or HoldListen, to keep
        just one of them alive. A True value will keep both.

        The listen() method returns a Handler object. You may store this
        for later use; it has a cancel() method which may be used to stop
        listening.
        """

        if not self.inited:
            raise generator.ScheduleError('agent is uninitialized')
        if self.generator is None or self.channel is None:
            raise generator.ScheduleError('listener has never been scheduled')
        if chan is None:
            chan = self.channel
        if not chan.active:
            raise generator.ChannelError('cannot listen to inactive channel')

        if event is None:
            event = self.selected_event
        if event is None:
            raise generator.ScheduleError('must specify event to listen for')
        if callable(event):
            event = event()
            if event is None:
                raise generator.ScheduleError(
                    'must return event to listen for')
        if event != '':
            event = check_prop_name(event)

        if handle is None:
            handle = self.receive

        gen = self.generator
        han = Handler(self, handle, event, chan, hold)
        gen.addhandler(han)

        return han