Playing random sound when starting new game

Hello there!
Soo I have a new thing I wanna add into a game.
Now I'm pretty sure this needs a plugin, but I don't know if it exists yet.
Basically, what I want to do is, when you start the game, get to the menu and press "New game" I want it to play a sound file. But since it is a community project and many people are going to work on it, I want it to play a random soundfile out of a specific assortment.
Example: I want the game to randomly choose out of the files "Dana_Start.ogg", "Larry_Start.ogg" and "Patrick_Start.ogg" which one it will play when you press start. And every time you start the game again, it should randomly choose a soundfile out of these 3 again.
Ehh, I hope this wasn't too complicated now, it's kinda hard to explain... :elswt:
So I've been searching the web for this kind of plugin already but just found some that play one specific sound at the start, and don't have the function to randomly choose between several.

If someone could help me, that'd be great. If it's not possible, then that's fine too.
Have a nice day. 🙂
I created a little plugin to handle this for you, if you have any problems be sure to let me know, i think i made it rather simple to use. Just follow the instructions in the help.

Download the attached plugin file or copy/paste the code below and follow the instructions 🙂

Name this file: NewGame_RandomSE.js

Place this file in your js/plugins folder.

Set up the parameter with the filenames you wish to have added to the random selection and you're good to go.

Setup Volume, Pitch, and Pan parameters.

and you should be good to go.

JavaScript:
/*:
*
* @plugindesc Play Random SE on New Game.
*
* @author Ritter
*
*
* @param SE Filename List
* @desc List of SE filenames. ex: Cat, Cow, Crow
*
* @param SE Volume
* @type number
* @max 100
* @min 0
* @desc Volume of sound effect.
* 100 = Max volume. 0 = Sweet silence.
* 
*
* @param SE Pitch
* @desc Pitch of sound effect.
* 100 recommended.
* @type number
* @max 150
* @min 50
* 
*
* @param SE Pan
* @desc Pan of sound effect.
* 0 recommended.
* @type number
* @max 100
* @min -100
* 
*
*
* @help
*
* Play a random sound effect every time you hit new game on title screen!
* Just place a list of all Sound Effects you wish to randomly choose from
* in the plugin parameter separated as shown in the description example.
*
* Do not include the .ogg just the filename.
*
*
* Example: Cat, Cow, Crow
*
*
* Be sure to separate each with a comma.
*
* You can add as many sound effects to this list as you wish.
*/

// -----------------------------------------------------------------
// CODE STUFF
// -----------------------------------------------------------------

var Imported = Imported || {};
Imported.NewGame_RandomSE = true;

var Ritter = Ritter || {};          // Ritter main object
Ritter.Params = Ritter.Params || {};        // Ritter stuff

var parameters = PluginManager.parameters('NewGame_RandomSE');
Ritter.Params.filenames = String(parameters["SE Filename List"]);
Ritter.Params.SEvolume = Number(parameters["SE Volume"]);
Ritter.Params.SEpitch = Number(parameters["SE Pitch"]);
Ritter.Params.SEpan = Number(parameters["SE Pan"]);

(function() {



Window_Selectable.prototype.processOk = function() {
    if (this.isCurrentItemEnabled()) {
        if (Window_TitleCommand._lastCommandSymbol == 'newGame' && SceneManager._scene instanceof Scene_Title) {
            var filename = Ritter.randomSe();
            AudioManager.playSe({name: filename, volume: Ritter.Params.SEvolume, pitch: Ritter.Params.SEpitch, pan: Ritter.Params.SEpan});
        } else {
        this.playOkSound();
        }
        this.updateInputData();
        this.deactivate();
        this.callOkHandler();
    } else {
        this.playBuzzerSound();
    }
};

Ritter.randomSe = function() {
    var filenames = Ritter.Params.filenames.split(', ');
    var min = 0;
    var max = filenames.length;
    
    return filenames[Math.floor(Math.random() * max)];

};


})();
Hi, I also did a mini plugin similar as the above that will satisfy your need. But in this plugin, you can set volume, pitch and pan on your own. The way to do this can be found in the plugin help area. You can simply copy the code or download it directly.

Hope this can solve your problems. Thanks.

JavaScript:
//=============================================================================
//  New Game Random Sound
//=============================================================================

var Imported = Imported || {};
Imported.RandomSound = true;
var Sound = Sound || {};

//==============================================================================
/*:
 * @author AnN
 *
 * @plugindesc Randomly play se when player press the "New Game" button
 *
 * @param Sound Files
 *
 * @desc List all sound files here you wish to play randomly. Seperate each sound file with ";".
 *
 * @help
 * This plugin only has one parameter, which is sound list that will be played
 * randomly when player press the "New Game" button.
 *
 *                 **The form of the parameter**
 * FileName1, volume, pitch, pan; FileName2, volume, pitch, pan
 * E.g. Attack1,100,100,0; Attack2,90,120,0
 *
 *                 Spaces are allowed but NO TRAILING ";"
 */

(function() {
    //=============================================================================
    //    Read Files and Initialsing
    //=============================================================================
    Sound.Param = PluginManager.parameters('RandomSound');
    sounds = String(Sound.Param['Sound Files']);
    sounds = sounds.split(';');
    
    for (i = 0; i < sounds.length; i++) {
        var property = new Object(sounds[i].split(','));
        sounds[i] = {};
        sounds[i].name = String(property[0].trim());
        sounds[i].volume = Number(property[1].trim());
        sounds[i].pitch = Number(property[2].trim());
        sounds[i].pan = Number(property[3].trim());
    };

    //=============================================================================
    //    Override the new game function
    //=============================================================================
    var randomSound_NewGame = Window_TitleCommand.prototype.processOk;
    Window_TitleCommand.prototype.processOk = function() {
        randomSound_NewGame.call(this);

        if (this.isCurrentItemEnabled() && Window_TitleCommand._lastCommandSymbol == 'newGame') {
            this.playRandomSound();
        };
    };

    //=============================================================================
    //    Algorithm for randomly playing sound
    //=============================================================================
    Window_TitleCommand.prototype.playRandomSound = function() {
        var maxRate = 0;
        
        if (sounds.length > 1) {
            for (i = 0; i < sounds.length; i++) {
                sounds[i].rate = 0;
                sounds[i].rate = Math.floor(Math.random() * 11);
                if (sounds[i].rate > maxRate) maxRate = sounds[i].rate;
            };
        } else if (sounds.length === 1) {
            AudioManager.playSe( {"name":sounds[0].name, "volume":sounds[0].volume, "pitch":sounds[0].pitch, "pan":sounds[0].pan} );
        };
        
        for (i = 0; i < sounds.length; i++) {
            if (sounds[i].rate === maxRate) {
                AudioManager.playSe( {"name":sounds[i].name, "volume":sounds[i].volume, "pitch":sounds[i].pitch, "pan":sounds[i].pan} );
                break;
            };
        };
    };

})();
Thank both of you! It works perfectly! Exactly what I was looking for. 🙂

Edit: I hope I don't sound greedy now, but is it possible to achieve the same thing for the "continue" option? If it's too much work, just ignore this!
it would be an incredibly easy edit of either one of our tiny plugins xD

if you used mine edit this line

if (Window_TitleCommand._lastCommandSymbol == 'newGame' && SceneManager._scene instanceof Scene_Title) {

to look like this

if ((Window_TitleCommand._lastCommandSymbol == 'newGame' || Window_TitleCommand._lastCommandSymbol == 'continue') && SceneManager._scene instanceof Scene_Title)

Or if you used AnN's edit this line

if (this.isCurrentItemEnabled() && Window_TitleCommand._lastCommandSymbol == 'newGame')

to look like this

if (this.isCurrentItemEnabled() && (Window_TitleCommand._lastCommandSymbol == 'newGame' || Window_TitleCommand._lastCommandSymbol == 'continue'))

be sure not to delete/remove the { which follows at the end of each line, its important.

I can also look into doing it for when you select a save file as well in the save scene if you'd like 🙂
it would be an incredibly easy edit of either one of our tiny plugins xD

if you used mine edit this line

if (Window_TitleCommand._lastCommandSymbol == 'newGame' && SceneManager._scene instanceof Scene_Title) {

to look like this

if ((Window_TitleCommand._lastCommandSymbol == 'newGame' || Window_TitleCommand._lastCommandSymbol == 'continue') && SceneManager._scene instanceof Scene_Title)

Or if you used AnN's edit this line

if (this.isCurrentItemEnabled() && Window_TitleCommand._lastCommandSymbol == 'newGame')

to look like this

if (this.isCurrentItemEnabled() && (Window_TitleCommand._lastCommandSymbol == 'newGame' || Window_TitleCommand._lastCommandSymbol == 'continue'))

be sure not to delete/remove the { which follows at the end of each line, its important.

I can also look into doing it for when you select a save file as well in the save scene if you'd like 🙂

Aaa, I seriously can't thank you enough! It's perfect. 😀
And no need for the save scene, this is all I needed, thank you! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/playing-random-sound-when-starting-new-game.121574/