Script Dump: Bogosort Musical Notes

I’m done playing with this so grab some notes in MP3 format and run the script. Do adapt it to your own likings and have fun!

<html>
<head>
    <title>Music Bogosort</title>
    <script>
        function bogosortAndPlay() {
            var notes = ["C", "D", "E", "F", "G", "A", "B"];
            var sortedNotes = [];
            while (sortedNotes.length < 6) {
                var randomIndex = Math.floor(Math.random() * notes.length);
                var randomNote = notes[randomIndex];
                if (!sortedNotes.includes(randomNote)) {
                    sortedNotes.push(randomNote);
                }
            }
            document.getElementById("unsorted_notes").innerHTML = "Unsorted notes: " + sortedNotes.join(", ");

            // Bogosort (this is extremely inefficient)
            while (!isSorted(sortedNotes)) {
                shuffleArray(sortedNotes);
            }

            document.getElementById("sorted_notes").innerHTML = "Sorted notes: " + sortedNotes.join(", ");
            playNotes(sortedNotes);
        }

        function isSorted(array) {
            for (var i = 0; i < array.length - 1; i++) {
                if (array[i] > array[i + 1]) {
                    return false;
                }
            }
            return true;
        }

        function shuffleArray(array) {
            for (var i = array.length - 1; i > 0; i--) {
                var j = Math.floor(Math.random() * (i + 1));
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }

        function playNotes(notes) {
            var index = 0;
            function playNextNote() {
                if (index < notes.length) {
                    var audio = new Audio("notes/" + notes[index] + ".mp3");
                    audio.play();
                    index++;
                    // Delay between notes (adjust as needed)
                    setTimeout(playNextNote, 1000); // Delay of 1 second between notes
                }
            }
            playNextNote();
        }
    </script>
</head>
<body>
    <h1>Music Bogosort</h1>
    <button onclick="bogosortAndPlay()">Sort and Play</button>
    <p id="unsorted_notes"></p>
    <p id="sorted_notes"></p>
</body>
</html>

 

Loading

[Archiving] Software Heritage

Added here as a public bookmark!

We collect and preserve software in source code form, because software embodies our technical and scientific knowledge and humanity cannot afford the risk of losing it.

Software is a precious part of our cultural heritage. We curate and make accessible all the software we collect, because only by sharing it we can guarantee its preservation in the very long term.

Home Page

Loading