//=============================================================================
// System Sound Changer
// by Agoaj
// Date: 5/27/2017
//=============================================================================
/*:
* @plugindesc Change the system sounds at run time. (More info in Help)
* @author Agoaj
* @help This plugin adds two plugin commands. Changes to system SE will save in the player's save file.
*
* ChangeSound lets you change a system sound effect and adjust it's pan/pitch/volume
* ChangeSound [System SE Name] [New SE Filename] (Optional)[pan/pitch/volume]=[value]
* Example: ChangeSound Cursor Cursor1
* (This will change your Cursor to use the Cursor1 SE)
* Example: ChangeSound Cursor Cursor1 volume=100 pitch=60
* (This will change your Cursor to use the Cursor1 SE, and adjust the volume and pitch.)
*
* RevertSound will revert the SE to it's original settings from the database.
* RevertSound [System SE Name]
*/
(function() {
var parameters = PluginManager.parameters('SystemSoundChanger');
/*
* The system sounds are stored in an array, this maps the same name you see in the editor
* to the index the sound is stored.
* If you're using a different language than English, you can change these strings
* and that will change what you'll need to type into the plugin command.
*/
var SoundFXMap = {
"Cursor" : 0,
"OK" : 1,
"Cancel" : 2,
"Buzzer" : 3,
"Equip" : 4,
"Save" : 5,
"Load" : 6,
"Battle Start" : 7,
"Escape" : 8,
"Enemy Attack" : 9,
"Enemy Damage" : 10,
"Enemy Collapse" : 11,
"Boss Collapse 1" : 12,
"Boss Collapse 2" : 13,
"Actor Damage" : 14,
"Actor Collapse" : 15,
"Recovery" : 16,
"Miss" : 17,
"Evasion" : 18,
"Magic Evasion" : 19,
"Magic Reflection" : 20,
"Shop" : 21,
"Use Item" : 22,
"Use Skill" : 23
};
//New mappings that will be saved
var SavedData = {};
//A copy of the sound mapping from System.json
var OriginalSounds = [];
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
if(command == "ChangeSound")
{
if(args.length > 0)
{
/*
var soundName = args[0];
var soundIndex = -1;
var i = 1;
for(i = 1; i < args.length; i++)
{
if(!(soundName in SoundFXMap)
{
soundName += " " + args[i];
}
else
{
soundIndex = SoundFXMap[soundName];
break;
}
}
if(soundIndex == -1)
{
//Todo: Get event name and line for error
//Could not find figure out system sound name, throw error.
console.error("Cannot find system sound to replace by name of: " + soundName + "{" + args +"}");
return;
}
var replacementFileName = args[i];
for(i = i+1; i < args.length; i++)
{
replacementFileName += " " + args[i];
}
//Replace it in dataSystem.
$dataSystem.sounds[soundIndex].name = replacementFileName;
//Add it to our saved data
SavedData[soundIndex] = data.replacementFileName;
return;
*/
var data = GetSoundNameAndFileNameFromArgs(args);
if(data != null && data.soundIndex != -1 && data.replacementFileName != null)
{
//Replace it in dataSystem.
ReplaceSound(data);
}
return;
}
return;
}
else if(command = "RevertSound")
{
var data = GetSoundNameAndFileNameFromArgs(args);
if(data != null && data.soundIndex != -1)
{
RevertSound(data.soundIndex);
}
return;
}
return _Game_Interpreter_pluginCommand(command, args);
}
var GetSoundNameAndFileNameFromArgs = function(args)
{
var ret = {};
ret.soundName = null;
ret.soundIndex = -1;
ret.replacementFileName = null;
ret.pan = null;
ret.pitch = null;
ret.volume = null;
var soundName = args[0];
var replacementFileName = null;
var soundIndex = -1;
var i = 1;
if(soundName in SoundFXMap)
{
soundIndex = SoundFXMap[soundName];
}
else
{
for(i = 1; i < args.length; i++)
{
if(args[i].includes("="))
{
ParseExtraParam(args[i],ret);
continue;
}
if(!(soundName in SoundFXMap))
{
soundName += " " + args[i];
}
else
{
soundIndex = SoundFXMap[soundName];
break;
}
}
}
if(soundIndex == -1)
{
//Todo: Get event name and line for error
//Could not find figure out system sound name, throw error.
console.error("Cannot find system sound to replace by name of: " + soundName + "{" + args +"}");
return null;
}
if(i < args.length)
{
replacementFileName = args[i];
for(i = i+1; i < args.length; i++)
{
if(args[i].includes("="))
{
ParseExtraParam(args[i],ret);
continue;
}
replacementFileName += " " + args[i];
}
}
ret.soundName = soundName;
ret.soundIndex = soundIndex;
ret.replacementFileName = replacementFileName;
return ret;
}
var ParseExtraParam = function(arg, ret)
{
if(arg.startsWith("pan="))
{
var si = "pan=".length;
ret.pan = parseInt(arg.substring(si));
}
else if(arg.startsWith("pitch="))
{
var si = "pitch=".length;
ret.pitch = parseInt(arg.substring(si));
}
else if(arg.startsWith("volume="))
{
var si = "volume=".length;
ret.volume = parseInt(arg.substring(si));
}
else
{
//Bad argument
}
}
var ReplaceSound = function(data)
{
var oldSoundInfo = $dataSystem.sounds[data.soundIndex];
var newSoundInfo = {};
newSoundInfo.name = data.replacementFileName;
if(data.pan == null)
newSoundInfo.pan = oldSoundInfo.pan;
else
newSoundInfo.pan = data.pan;
if(data.pitch == null)
newSoundInfo.pitch = oldSoundInfo.pitch;
else
newSoundInfo.pitch = data.pitch;
if(data.volume == null)
newSoundInfo.volume = oldSoundInfo.volume;
else
newSoundInfo.volume = data.volume;
//Replace it in dataSystem.
$dataSystem.sounds[data.soundIndex] = newSoundInfo;
//Add it to our saved data
SavedData[data.soundIndex] = newSoundInfo;
}
var RevertSound = function(soundIndex)
{
if(soundIndex in SavedData)
{
//Remove from save file
delete SavedData[soundIndex];
}
//Restore Original
$dataSystem.sounds[soundIndex] = JsonEx.makeDeepCopy(originalSounds[soundIndex]);
}
//Add our saved data.
var DataManager_makeSaveContents_Alias = DataManager.makeSaveContents;
DataManager.makeSaveContents = function() {
var contents = DataManager_makeSaveContents_Alias.call(this);
contents.SystemSoundChanger = SavedData;
return contents;
}
//Get our saved data
var DataManager_extractSaveContents_Alias = DataManager.extractSaveContents;
DataManager.extractSaveContents = function(contents) {
DataManager_extractSaveContents_Alias.call(this,contents);
SavedData = contents.SystemSoundChanger
UpdateSoundMapping();
}
var ResetAllSoundChanges = function()
{
if(Object.keys(SavedData).length > 0)
{
SavedData = {};
$dataSystem.sounds = JsonEx.makeDeepCopy(originalSounds);
}
}
//Wipe sound changes when starting new game.
var DataManager_setupNewGame_Alias = DataManager.setupNewGame;
DataManager.setupNewGame = function() {
DataManager_setupNewGame_Alias.call(this);
ResetAllSoundChanges();
}
//Wipe sound changes when ending game
var Scene_Menu_prototype_commandGameEnd_Alias = Scene_Menu.prototype.commandGameEnd;
Scene_Menu.prototype.commandGameEnd = function() {
Scene_Menu_prototype_commandGameEnd_Alias.call(this);
ResetAllSoundChanges();
}
//Wipe sound changes when game over
var Scene_Gameover_start_Alias = Scene_Gameover.prototype.start;
Scene_Gameover.prototype.start = function() {
Scene_Gameover_start_Alias.call(this);
ResetAllSoundChanges();
};
//Wipe sound changes when at the title screen
var Scene_Title_start_Alias = Scene_Title.prototype.start;
Scene_Title.prototype.start = function() {
Scene_Title_start_Alias.call(this);
ResetAllSoundChanges();
};
//Grab a copy of the original sound mapping
var DataManager_onLoad_Alias = DataManager.onLoad;
DataManager.onLoad = function(object) {
DataManager_onLoad_Alias.call(this, object);
if(object === $dataSystem) {
originalSounds = JsonEx.makeDeepCopy(object.sounds);
UpdateSoundMapping();
}
}
//Copy our new filenames into the rpg maker dataSystem
var UpdateSoundMapping = function()
{
for(var index in SavedData)
{
$dataSystem.sounds[index] = SavedData[index];
SoundManager.loadSystemSound(index);
}
}
})();