Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: AI programs Spider Monkey Panel (Read 1143 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

AI programs Spider Monkey Panel

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!!




Code: [Select]
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;
}

Re: AI programs Spider Monkey Panel

Reply #1
Thanks for the script. While AI may be great, that snippet works only in specific setups and has some problems:

- All coordinates are hardcoded, therefore the elements are not resized according to panel size. It may be fine if people just use it for the intended panel size though.
- As is, even if you disable it. Any track played more than 150 seconds is automatically skipped since the callback 'on_playback_time' is always called (unless I misunderstood it).
- There is no need to repaint on every minimal mouse change, it should be throttled on drag.

Possible improvements:
- There are no "on hover" effects on the button and bar.
- There is no control about what happens when current queue/playlist reaches the end.

Maybe you can reformulate your prompt to have all that into consideration.

Re: AI programs Spider Monkey Panel

Reply #2
Great use of our new robot "friends"!

All your jobs are belong to us indeed.

I was using Preview for a very similar use case, but always happy to remove another component.