4
Issue: When I play music in the entire house I mix all our playlists together and then use the PREVIEW feature to listen to the first 3 minutes of each song. That way people get less irritated putting up with songs they might not like.
What I don't like is music AFTER the 3 minute mark is never heard by anyone... LAME.
I asked AI (Chat-GPTo1 with Canvas) that I want foobar2000 to play 3 minutes of every song but don't start at the beginning. It suggested I use Spider Monkey panel because it's gives a window into foobar functions using javascript. It told me to install the component and add a panel, click the panel and paste the code below.
PERFECT - songs that are under 3 minutes (or however long I select on the slider) play from the beginning. All other songs start in a random position that has at least 3 minutes remaining to play. It's like listening to new music.
Thank you Master AI!!
let randomStart = 0;
let enableRandomPlayback = true;
let playDuration = 150; // Default playback duration in seconds
function playRandomSegment() {
if (!enableRandomPlayback) return; // Exit if random playback is disabled
let track = fb.GetNowPlaying();
if (!track) return; // Exit if no track is playing
let duration = track.Length; // Get the track length in seconds
if (duration > playDuration) { // If the track is longer than the selected duration
randomStart = Math.random() * (duration - playDuration);
fb.PlaybackTime = randomStart; // Set playback time to the random start point
} else {
randomStart = 0; // Start from the beginning if the track is shorter
}
}
// Playback time event handler - runs every second
function on_playback_time(time) {
if (time >= randomStart + playDuration) { // Check if selected time has elapsed since randomStart
fb.Next(); // Move to the next track
}
}
// Event listener for track change
function on_playback_new_track(metadb) {
playRandomSegment(); // Trigger the function when a new track starts
}
// Paint event handler for drawing UI elements
function on_paint(gr) {
// Draw the checkbox
gr.FillSolidRect(10, 10, 15, 15, enableRandomPlayback ? RGB(0, 128, 0) : RGB(128, 128, 128));
gr.DrawRect(10, 10, 15, 15, 1, RGB(0, 0, 0));
gr.GdiDrawText("Enable Random Playback", gdi.Font("Segoe UI", 12), RGB(0, 0, 0), 30, 10, 200, 20);
// Draw the slider on the same line as the checkbox label
gr.FillSolidRect(200, 15, 200, 5, RGB(150, 150, 150)); // Slider track
let sliderPos = 200 + ((playDuration - 30) / (300 - 30)) * 200; // Calculate position based on playDuration
gr.FillSolidRect(sliderPos - 5, 10, 10, 15, RGB(0, 0, 255)); // Slider handle
gr.GdiDrawText("Playback Duration: " + playDuration + " sec", gdi.Font("Segoe UI", 12), RGB(0, 0, 0), 410, 10, 200, 20);
}
// Mouse click event handler for checkbox and slider
function on_mouse_lbtn_up(x, y) {
// Handle checkbox click
if (x >= 10 && x <= 25 && y >= 10 && y <= 25) {
enableRandomPlayback = !enableRandomPlayback;
window.Repaint(); // Repaint to update the checkbox state
}
// Handle slider click or drag
if (x >= 200 && x <= 400 && y >= 10 && y <= 25) {
let newValue = Math.round(30 + ((x - 200) / 200) * (300 - 30));
playDuration = newValue;
window.Repaint(); // Repaint to update the slider position
}
}
// Helper function to define RGB color
function RGB(r, g, b) {
return 0xFF000000 | (r << 16) | (g << 8) | b;
}