On the topic of Custom Music on PS

Pikachuun

the entire waruda machine
is a Pre-Contributor
#1
This is a really popular suggestion that I've seen like 50 billion times now (exaggeration I know but it gets the point across). It will never be implemented officially due to the costs associated with hosting the music files themselves on the simulator.

But unofficially, thanks to the power of based TamperMonkey, I implemented it.

You're probably asking "butt choon, hao dos i da custum moosic?" to which I reply "Use better English." I think I see what you mean though, so here's a simple way to use this script.

Step 1: Get TamperMonkey (the stable one, it's the black and white icon) and reopen your browser if you have to, then enable it by clicking the shiny new icon that will appear on the upper-right corner of your browser.
Step 2: Same icon, but this time click the "Add a new script button." If a new tab opens with "My Fancy New Userscript" on it, you did it right. I am very proud of you.
Step 3: In the box with all of the "My Fancy New Userscript" text, delete all the things! When you have done this, grab the script and place it in the box [If you can't use pastebin I'll include the code at the bottom of this post]. Then save the script.
Step 4: If something pops up with a box on it, check it. If a drop-down menu appears with something asking for you to choose an option, enable it.
Step 5: ???
Step 100: Profit.

I've included "3" examples for demonstration purposes. The quotes around the 3 are due to the easter egg case being there. Don't edit the thing below the line with all caps on it. That's pretty much needed for the thing to work. The rest you can feel free to edit as you wish to add your own custom music; I even left a bunch of notes so that way you don't get confused part-way in.

Thanks for reading, and happy custum moosicing! (.3.)/

EDIT: Ninety Nine has asked about how to get music files directly. If you're that kind of guy who reads only the OP (even though it's literally right below this post), here's basically what you have to do: In the case of most music files, you just need to view the source code of the website and find the mp3/wav/whatever file (This is how I got the second example). Some music files allow you to also view them in Resources (JS Console) but those are rarer by comparison. Some other music files allow you to simply copy the download link for you to get them as well (1st example). Just pick one of these 3 methods and you should be good to go. If one doesn't work, you have the other two.

Code:
// ==UserScript==
// @ name         Custom Music PS Edition
// @ namespace    http://www.smogon.com/forums/members/pikachuun.171701/
// @ version      The only version
// @ description  Adds your own custom music for PS by replacing the preloadBGM script.
//                Note that the music selection is entirely up to you; I provided "3" examples so you get the jist of it.
// @ author       Based Choon
// @ match        http://play.pokemonshowdown.com/*
// @ grant        http://www.slate.com/content/dam/slate/articles/news_and_politics/history/2012/02/120229_HISTORY_grant.jpg.CROP.original-original.jpg
// ==/UserScript==
Battle.prototype.preloadBgm = function () {
    var bgmNum = Math.floor(Math.random() * ( 2 )); //The number I put in parentheses for emphasis is the number of audio files you'll be replacing (I'll call it n).
    if (window.forceBgm || window.forceBgm === 0) bgmNum = window.forceBgm;
    window.bgmNum = bgmNum;
    switch (bgmNum) {
    case -1: //The special easter egg case. I have no idea what to do with this so I'm just leaving it here~
        BattleSound.loadBgm('audio/bw2-homika-dogars.mp3', 1661, 68131);
        this.bgm = 'audio/bw2-homika-dogars.mp3';
        //A note for Pokemon Showdown's vanilla audio: You can still use the format shown above if you wanted to. It's actually functional.
        break;
    case 0: //Due to how Math.random() works, your possible numbers for bgmNum are 0 to (n-1). Therefore, your case numbers should be 0 to (n-2).
        BattleSound.loadBgm('http://picosong.com/cdn/48707ff17f0fa5285c675649d64f5806.mp3', 0, 418926);
        this.bgm = 'http://picosong.com/cdn/48707ff17f0fa5285c675649d64f5806.mp3';
        break;
    //Start adding cases here in the same format as above, increasing the number by 1 each time.
    default: //This is your last case. Always have "default:" as your last case, regardless of what you set for n.
        BattleSound.loadBgm('http://audio.ngfiles.com//272000//272755_Elite_Airflow__NG_.mp3', 0, 278152);
        this.bgm = 'http://audio.ngfiles.com//272000//272755_Elite_Airflow__NG_.mp3';
        break;
    }
}
//The above code can be edited. Go nuts.
//A note for all of you people who are like "wtf is this code":
//BattleSound.loadBgm('url', loopstart, loopend):
//    url is the url of your music. NOTE THAT THIS IS THE URL OF THE FILE ITSELF, NOT THE WEBSITE THAT HOSTS IT.
//    loopstart is the start point of your loop (If I'm not mistaken this is in milliseconds). In my case I just put 0 which is the beginning.
//    loopend is the end point of your loop (same units as loopstart). In my case I calculated the end's time in milliseconds.
//this.bgm = 'url'
//    url is still the url of your music. ONCE AGAIN NOTE THAT THIS IS THE URL OF THE FILE ITSELF.
//If your music doesn't load, you probably put in the wrong site. Try finding the raw audio file (probably can be found through source code).


//THE BELOW CODE, HOWEVER, CANNOT BE EDITED. PLEASE KEEP THIS EXACTLY AS IS.
BattleSoundLibrary.prototype.loadBgm = function (url, loopstart, loopend) {
    if (this.bgmCache[url]) {
        if (this.bgmCache[url] !== this.soundPlaceholder || loopstart === undefined) {
            return this.bgmCache[url];
        }
    }
    try {
        this.bgmCache[url] = soundManager.createSound({
            id: url,
            url: url,
            volume: this.bgmVolume
        });
    } catch (e) {}
    if (!this.bgmCache[url]) {
        // couldn't load
        // suppress crash
        return this.bgmCache[url] = this.soundPlaceholder;
    }
    this.bgmCache[url].onposition(loopend, function () {
        this.setPosition(loopstart);
    });
    return this.bgmCache[url];
};
 
Last edited:
#2
Great work! Could you also include a guide on how to obtain the URL for the actual song instead of the website that hosts it. I assume that'd be the biggest issue with something like this. Other than that, amazing stuff :]
 

Pikachuun

the entire waruda machine
is a Pre-Contributor
#3
Great work! Could you also include a guide on how to obtain the URL for the actual song instead of the website that hosts it. I assume that'd be the biggest issue with something like this. Other than that, amazing stuff :]
In the case of most music files, you just need to view the source code of the website and find the mp3/wav/whatever file (This is how I got the second example). Some music files allow you to also view them in Resources (JS Console) but those are rarer by comparison. Some other music files allow you to simply copy the download link for you to get them as well (1st example). Really, it's just a matter of using one of these 3 methods, but I've found the first one is the most reliable.
 
#4
I know this is an old post, but I've been trying to get this to work recently with no luck. It says the variables 'Battle', 'BattleSound', 'BattleSoundLibrary' and 'SoundManager' are not defined. It also complains that the return statement should not contain assignment on line 51 of the code.
 

Users Who Are Viewing This Thread (Users: 1, Guests: 0)

Top