http://www.perlmonks.org?node_id=696267


in reply to Music shuffling

Now, how you gonna make sure that NIN isn't followed by Elton John.

That could cause a brain hemorrhage!

Replies are listed 'Best First'.
Re^2: Music shuffling
by Hue-Bond (Priest) on Jul 09, 2008 at 07:18 UTC

    The simplest solution I found to the OP issue was to divide the track list in two halves, then play each one of them alternately:

    ## pseudocode my @list = shuffle get_tracks; my @list1 = @list[0..@list/2-1]; my @list2 = @list[@list/2..$#list]; while (1) { play @list1; play @list2; shuffle @list1; shuffle @list2; }

    Regarding this:

    Now, how you gonna make sure that NIN isn't followed by Elton John.

    I have thought about a database with style tags and a mechanism for specifying which styles are allowable after a given song:

    ## pseudocode my @style_levels = ( [ qw/noise/ ], [ qw/metal heavy/ ], [ qw/pop rock/ ], [ qw/chill_out classical/ ], ); sub get_next_song { my $cur_song = shift; my $cur_style = get_style_from_db $cur_song; ## now we have 'pop' +, 'classical'... my $index = get_index $cur_style; ## walk @style_level +s and return the index where 'pop' or 'classical' has been found my $next_index = $index + (-1,0,1)[rand 3]; ## randomly go to ne +xt, current or previous style level (TODO: bounds checking) my $next_style = ${ $style_levels[$next_index] }[ rand @{$style_le +vels[$next_index]} ]; ## choose a style of this level return get_song { style => $next_style }; ## pick some song of + this style }

    --
    David Serrano