Posts tagged Randomizer

Music Randomizer

Music on a website is fine so long as you do not have to listen to the same song over and over as some unfortunately do. Randomization makes short work of that. Moreover, this MIDI player will switch tracks if the same song has been playing too long.

<script language=”JavaScript”>
<!–
function scrollmidire()
{
midi.songname.value = midi.songname.value.substring(1,midi.songname.value.length) + midi.songname.value.charAt(0);
midiretimer = setTimeout(“scrollmidire()”, midirescrollspeed);
}
function stopmidire()
{
clearTimeout(midiretimer);
clearTimeout(midiretimeout);
midire.src = “”;
midi.songname.value = ” – Stopped – “;
scrollmidire();
setcookiemidire(“midire”, “stop”);
}
function setmidire(file, name)
{
midire.src = midiredirectory + “/” + file;
midi.songname.value = ” You are listening to ” + name;
}
function nextmidire()
{
clearTimeout(midiretimer);
clearTimeout(midiretimeout);
var randommusic = Math.round((Math.random() * 1000 + Math.random() * 500) % midirenum); // JavaScript is not the most random of languages; two randomizations helps greatly
switch(randommusic)
{
// CHANGE
case 0: setmidire(‘midi1.mid’, ‘First MIDI title’); break;
case 1: setmidire(‘midi2.mid’, ‘Second MIDI title’); break;
case 2: setmidire(‘midi3.mid’, ‘Third MIDI title’); break;
default: setmidire(‘midi.mid’, ‘Default MIDI title’); break; // this should never be reached, but just in case it is….
}
scrollmidire();
midiretimeout = setTimeout(“nextmidire()”, midirewait); // Milliseconds until track switches
setcookiemidire(“midire”, ” “);
}
function setcookiemidire(name,value)
{
var next = new Date();
next.setYear(next.getYear() + 1);
document.cookie = name + ‘=’ + value + ‘; expires=’ + next.toGMTString() + ‘;’;
}
function getcookiemidire(name)
{
var cookieFound = false;
var start = 0;
var end = 0;
var cookieString = document.cookie;
var i = 0;
while (i <= cookieString.length)
{
start = i;
end = start + name.length;
if (cookieString.substring(start,end) == name)
{
cookieFound = true;
break;
}
i++;
}
if(cookieFound)
{
start = end + 1;
end = document.cookie.indexOf(“;”,start);
if (end < start)
end = document.cookie.length;
return document.cookie.substring(start,end);
}
return “”;
}
// CHANGE
var midirenum = 3; // Number of MIDIs; update every time a new one is added
var midirewait = 300000; // Milliseconds the same song will play for before another track begins
var midirescrollspeed = 100; // Low numbers make it scroll faster; large numbers, slower
var midiredirectory = “midis”; // Directory where your MIDIs are located
var midiretimer, midiretimeout; // timer declarations; leave them alone
document.write(‘<form name=”midi”>\n’);
document.write(‘<bgsound src=”" loop=”infinite” id=”midire”></bgsound>\n’);
document.write(‘<input type=”text” name=”songname” value=”You are listening to… ” readonly><br>\n’);
document.write(‘<input type=”button” name=”stop” title=”Stop” value=”•” onClick=”stopmidire()”>\n’);
document.write(‘<input type=”button” name=”next” title=”Next Track” value=”>” onClick=”nextmidire()”></form>\n’);
if(getcookiemidire(“midire”) == “stop”) stopmidire();
else nextmidire();
//–>
</script>

You should go through the script and change the script to meet your specialized needs. Just look for the conveniently placed CHANGE comments located in the script; there are only two of them, one at the bottom and one in the song list.
The entire thing is one large JavaScript, so it will easily fit into one imported script. If your song list ever becomes large, I recommend using it as an imported script for the sake of load speed.

http://www.realitysend.com/stuff/code.shtml

Comments (2) »