Exemple #1
0
    def test_force_all(self):

        items = [
            ('1', 1),
            ('2', 1),
            ('3', 1),
        ]

        r = Randomizer(items)
        r.force_all = True

        last_item = None
        for x in range(100):
            results = set()
            results.add(next(r))
            self.assertNotEqual(last_item, r.get_current())
            results.add(next(r))
            results.add(next(r))
            last_item = r.get_current()
            self.assertEqual(len(results), 3)
Exemple #2
0
class PlaylistInstance:
    """
    PlaylistInstance class represents an instance of a playlist asset.
    """

    # pylint: disable-msg=too-many-arguments
    def __init__(self, name: str, playlist: dict, track_crossfade_time: float,
                 context: Optional[str] = None, settings: Optional[dict] = None):
        """Constructor"""
        self._name = name
        if playlist is None:
            raise ValueError("Cannot create playlist instance: playlist parameter is None")

        self._context = context

        if settings is None:
            settings = {}

        # Set crossfade time that will be used for playing this instance (will either
        # use the track time or the playlist time depending upon the crossfade_mode
        # setting
        settings.setdefault('crossfade_mode', playlist['crossfade_mode'])
        if settings['crossfade_mode'] == 'use_track_setting':
            settings['crossfade_time'] = track_crossfade_time
        elif settings['crossfade_mode'] == 'use_playlist_setting':
            if playlist['crossfade_mode'] == 'use_track_setting':
                settings['crossfade_time'] = track_crossfade_time
            else:
                settings.setdefault('crossfade_time', playlist['crossfade_time'])
        else:
            settings.setdefault('crossfade_time', playlist['crossfade_time'])

        settings.setdefault('shuffle', playlist['shuffle'])
        settings.setdefault('repeat', playlist['repeat'])
        settings['sounds'] = playlist['sounds']
        settings.setdefault('events_when_played', playlist['events_when_played'])
        settings.setdefault('events_when_stopped', playlist['events_when_stopped'])
        settings.setdefault('events_when_looping', playlist['events_when_looping'])
        settings.setdefault('events_when_sound_changed', playlist['events_when_sound_changed'])
        settings.setdefault('events_when_sound_stopped', playlist['events_when_sound_stopped'])
        self._settings = settings

        self._sounds = Randomizer(self._settings['sounds'])
        self._sounds.disable_random = not self._settings['shuffle']
        self._sounds.force_all = True
        self._sounds.force_different = True
        self._sounds.loop = self._settings['repeat']

        self._current_sound_instance = None
        self._fading_sound_instance = None

    def __repr__(self):
        """String that's returned if someone prints this object"""
        return '<PlaylistInstance: {}>'.format(self.name)

    @property
    def name(self):
        return self._name

    @property
    def crossfade_time(self):
        return self._settings['crossfade_time']

    @property
    def shuffle(self):
        return self._settings['shuffle']

    @property
    def repeat(self):
        return self._settings['repeat']

    @property
    def events_when_played(self):
        return self._settings['events_when_played']

    @property
    def events_when_stopped(self):
        return self._settings['events_when_stopped']

    @property
    def events_when_looping(self):
        return self._settings['events_when_looping']

    @property
    def events_when_sound_changed(self):
        return self._settings['events_when_sound_changed']

    @property
    def events_when_sound_stopped(self):
        return self._settings['events_when_sound_stopped']

    @property
    def sounds(self):
        return self._settings['sounds']

    @property
    def end_of_playlist(self):
        if self._sounds.disable_random:
            return self._sounds.data['current_item_index'] == len(self._sounds.items)
        else:
            return len(self._sounds.data['items_sent']) == len(self._sounds.items)

    @property
    def current_sound_instance(self):
        """Return the current sound instance"""
        return self._current_sound_instance

    @current_sound_instance.setter
    def current_sound_instance(self, value):
        """Set the current sound instance"""
        self._current_sound_instance = value

    @property
    def fading_sound_instance(self):
        """Return the fading sound instance"""
        return self._fading_sound_instance

    @fading_sound_instance.setter
    def fading_sound_instance(self, value):
        """Set the fading sound instance"""
        self._fading_sound_instance = value

    @property
    def context(self):
        """The context under which this playlist was created/played."""
        return self._context

    def get_next_sound_name(self):
        """Return the name of the next sound in the playlist (advance iterator)"""
        try:
            return self._sounds.get_next()
        except StopIteration:
            return None

    def get_current_sound_name(self):
        """Return the name of the current sound in the playlist"""
        try:
            return self._sounds.get_current()
        except StopIteration:
            return None