Fear3d

MultiBgs

Nov 7th, 2021 (edited)
1,355
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // MultiBgs.js
  2. // Version: 1.1
  3.  
  4. /*:
  5. * @plugindesc Allows you to play multiple Background Sounds at the same time
  6. * @author Arthran
  7. *
  8. * @help
  9. *
  10. * By default, RPG Maker only allows you to play one BGS at a time. This plugin
  11. * will allow you to play multiple BGS at the same time, through plugin commands.
  12. *
  13. * ============================================================================
  14. * Plugin Commands
  15. * ============================================================================
  16. *
  17. * Plugin Command:
  18. *   MBGS play Name volume pitch pan  - Plays the specified BGS at the specified
  19. *                                      volume, pitch, and pan. The latter three
  20. *                                      arguements are optional. If omitted, they
  21. *                                      will default to 90, 100, and 0
  22. *                                      respectively. The Name arguement is
  23. *                                      required, and it needs to be the filename
  24. *                                      of the BGS, without the extension. Does
  25. *                                      not support filenames that have spaces.
  26. *                                      If you need spaces in your filename,
  27. *                                      see the script call below.
  28. *
  29. *   MBGS stop                       - Stops all BGS
  30. *
  31. * Examples:
  32. *   MBGS play River 25 100 0  - Plays River.ogg at 25% volume, 100% pitch, 0 pan
  33. *   MBGS play River 25        - Does the same thing
  34. *   MBGS play Drips           - Plays Drips.m4a at 90% volume, 100% pitch, 0 pan
  35. *
  36. * ============================================================================
  37. * Script Calls
  38. * ============================================================================
  39. *
  40. * If your filename has a space in it, the plugin command will be unable to play
  41. * it. In that case, you can use the following script call instead:
  42. *
  43. * Script Call:
  44. *   MBGS.play("Name", volume, pitch, pan);  - Plays the specified BGS at the
  45. *                                             specified parameters. Volume,
  46. *                                             pitch, and pan are optional.
  47. *
  48. *   MBGS.stop();                            - Works the same as the plugin
  49. *                                             command.
  50. *
  51. * Examples:
  52. *   MBGS.play("My BGS", 25, 100, 0);  - Plays 'My BGS.ogg' at 25% volume,
  53. *                                       100% pitch, 0 pan
  54. *   MBGS.play("My BGS", 25);          - Does the same thing
  55. *
  56. * ============================================================================
  57. * Notes
  58. * ============================================================================
  59. *
  60. * The "Play BGS..." event command still has the default behavior. That is to
  61. * say that it still only allows you to play one BGS at a time. You will need
  62. * to use the plugin command or script call in order to play multiple.
  63. *
  64. * Feel free to use this plugin in any kind of project you want. Please include
  65. * Arthran in the credits of your game. You may edit this however you want, but
  66. * please do leave my name somewhere in the annotations.
  67. */
  68.  
  69. var MBGS = {};
  70.  
  71. MBGS.play = function(aname, avolume = 90, apitch = 100, apan = 0) {
  72.     AudioManager.playBgsEx({
  73.         name: aname,
  74.         volume: avolume,
  75.         pitch: apitch,
  76.         pan: apan
  77.     });
  78. };
  79.  
  80. MBGS.stop = function() {
  81.     AudioManager.stopBgs();
  82. };
  83.  
  84. (function() {
  85.  
  86.     const _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  87.     Game_Interpreter.prototype.pluginCommand = function(command, args) {
  88.         _Game_Interpreter_pluginCommand.call(this, command, args);
  89.         if (command === 'MBGS') {
  90.             switch (args[0]) {
  91.             case 'play':
  92.                 AudioManager.playBgsEx({
  93.                     name: args[1],
  94.                     volume: Number(args[2] || 90),
  95.                     pitch: Number(args[3] || 100),
  96.                     pan: Number(args[4] || 0)
  97.                 });
  98.                 break;
  99.             case 'stop':
  100.                 AudioManager.stopBgs();
  101.                 break;
  102.             }
  103.         }
  104.     };
  105.  
  106.  
  107.     AudioManager._bgsBuffers = [];
  108.     AudioManager._currentBgsEx = [];
  109.  
  110.     AudioManager.playBgsEx = function(bgs, pos) {
  111.         let isCurrentBgs = this.isCurrentBgsEx(bgs);
  112.  
  113.         if (isCurrentBgs >= 0) {
  114.             this.updateBgsParametersEx(this._bgsBuffers[isCurrentBgs], bgs);
  115.         } else if (bgs.name) {
  116.             var buffer = this.createBuffer('bgs', bgs.name);
  117.             this.updateBgsParametersEx(buffer, bgs);
  118.             buffer.play(true, pos || 0);
  119.             this._bgsBuffers.push(buffer);
  120.             this._currentBgsEx.push(bgs);
  121.         }
  122.     };
  123.  
  124.     AudioManager.isCurrentBgsEx = function(bgs) {
  125.         if ((this._currentBgsEx.length > 0) && (this._bgsBuffers.length > 0)) {
  126.             let result = this._currentBgsEx.findIndex(function(currentBgs) {
  127.                 return (currentBgs.name == bgs.name);
  128.             });
  129.             return result;
  130.         } else {
  131.             return -1;
  132.         }
  133.     };
  134.  
  135.     AudioManager.updateBgsParametersEx = function(buffer, bgs) {
  136.         this.updateBufferParameters(buffer, this._bgsVolume, bgs);
  137.     };
  138.  
  139.     const _AudioManager_stopBgs = AudioManager.stopBgs;
  140.     AudioManager.stopBgs = function() {
  141.         _AudioManager_stopBgs.call(this);
  142.  
  143.         if (this._bgsBuffers.length > 0) {
  144.             this.stopBgsEx();
  145.         }
  146.     };
  147.  
  148.     AudioManager.stopBgsEx = function() {
  149.         this._bgsBuffers.forEach(function(buffer) {
  150.             buffer.stop();
  151.         });
  152.         this._bgsBuffers = [];
  153.         this._currentBgsEx = [];
  154.     };
  155.  
  156.     AudioManager.saveBgsEx = function() {
  157.         const bgsEx = [];
  158.         for (const bgs of this._currentBgsEx) {
  159.             bgsEx.push({
  160.                 name: bgs.name,
  161.                 volume: bgs.volume,
  162.                 pitch: bgs.pitch,
  163.                 pan: bgs.pan
  164.             });
  165.         }
  166.         return bgsEx;
  167.     };
  168.  
  169.     const _Game_System_onBeforeSave = Game_System.prototype.onBeforeSave;
  170.     Game_System.prototype.onBeforeSave = function() {
  171.         _Game_System_onBeforeSave.call(this);
  172.         this._bgsExOnSave = AudioManager.saveBgsEx();
  173.     };
  174.  
  175.     const _Game_System_onAfterLoad = Game_System.prototype.onAfterLoad;
  176.     Game_System.prototype.onAfterLoad = function() {
  177.         _Game_System_onAfterLoad.call(this);
  178.  
  179.         for (const bgs of this._bgsExOnSave) {
  180.             AudioManager.playBgsEx(bgs);
  181.         }
  182.     };
  183. })();
Advertisement
Add Comment
Please, Sign In to add comment