Randomizer

Randomness is art. In context of an audio player and its task to play random songs most people would use a general purpose random-function provided by the programming languages they use. In more critical context like security, or if the developer wants to have “perfect” randomness, a cryptography library providing cryptographic randomness would be used. This kind of randomness is wrong for playlist/queue managers in audio software. The randomness of tools like MusicDB is not for fuzzing data or doing security relevant task, it is for the listener. The bad thing here is, that listeners are human. For humans, real randomness does not feel random. Real randomness allows playing multiple songs form the same album after each other. Humans would neither expect, nor want such behavior of a randomizer. This is why MusicDB uses a less random randomizer that feels more random.

Randy Module

The randy module provides a way to select random songs or videos that can be put into the Song/Video Queue. The selection of random music follows certain constraints.

Song Selection Algorithm

Selecting a random song is done in two stages, the Database Stage and the Blacklist Stage The first stage selects a song from a limited set of songs, the second stage checks if the song is on a blacklist. In each stage, a song can be rejected and the algorithm starts at the beginning.

Warning

The process of trying to get a good song gets repeated until a good song is found. If over constraint, this ends in an infinite loop!

Database Stage

In the first stage, a song gets chosen by the database via musicdb.lib.db.musicdb.MusicDatabase.GetRandomSong(). There are 4 parameters that define the constraints applied on set of possible songs:

  • The activated genres as maintained by the musicdb.lib.cfg.mdbstate module.

  • The flag if disabled songs shall be excluded

  • The flag if hated songs shall be excluded

  • Minimum and maximum length of a song in seconds

Some of them can be configured in the MusicDB configuration file:

[Randy]
nodisabled=True
nohated=True
minsonglen=120

Because the database only takes album tags into account, the song tags gets checked afterwards. If the song has a confirmed genre tag, and if this tag does not match the filter, the song gets rejected. Song genres that are automatically set by an algorithm (and not confirmed by the user) will be ignored because the algorithm may be wrong.

Blacklist Stage

The selected song from the first stage now gets compared to the blacklists via musicdb.mdbapi.blacklist.BlacklistInterface.CheckAllListsForSong() or musicdb.mdbapi.blacklist.BlacklistInterface.CheckSongList(). If the song, or its album or artist, is listed in one of blacklist, then the song, a song from the same album or from the same artist was played recently. So, the chosen song gets dropped and the finding-process starts again.

Is the blacklist length set to 0, the specific blacklist is disabled

Video Selection Algorithm

The selection of a video is a slightly simplified way as done for Songs. For the blacklist stage it does not considers any Artist and Album associations and therefore the corresponding blacklists.

Randy Class

class musicdb.mdbapi.randy.Randy(config, database)[source]

This class provides methods to get a random song or video under certain constraints.

Parameters
Raises

TypeError – When the arguments are not of the correct type.

GetSong()[source]

This method chooses a random song in a two-stage process as described in the module description.

Warning

Due to too hard constraints, it may be possible that it becomes impossible to find a new song. If this is the case, the method returns None. The amount of tries can be configured in the MusicDB Configuration

Returns

A song from the MusicDatabase or None if an error occurred.

GetSongFromAlbum(albumid)[source]

Get a random song from a specific album.

If the selected song is listed in the blacklist for songs, a new one will be selected. Entries in the album and artist blacklist will be ignored because the artist and album is forced by the user. But the song gets added to the blacklist for songs, as well as the album and artist gets added.

The genre of the song gets completely ignored. The user wants to have a song from the given album, so it gets one.

Warning

This is a dangerous method. An album only has a very limited set of songs.

If all the songs are listed in the blacklist, the method would get caught in an infinite loop. To avoid this, there are only a limited amount of tries to find a random song. If the limit is reached, the method returns None. The amount of tries can be configured in the MusicDB Configuration

Parameters

albumid (int) – ID of the album the song shall come from

Returns

A song from the MusicDatabase or None if an error occurred.

GetVideo()[source]

This method chooses a random video in a simplified two-stage process as described in the module description. This method will be refined in future to fully behave like the GetSong() method.

Warning

Due to too hard constraints, it may be possible that it becomes impossible to find a new song. If this is the case, the method returns None. The amount of tries can be configured in the MusicDB Configuration

Returns

A video from the MusicDatabase or None if an error occurred.