Advertisement
Guest User

Jay_VariableMix.js

a guest
Sep 4th, 2016
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Variable Mix Manager
  3. // Jay_VariableMix.js
  4. // Version 1.0.2
  5. //=============================================================================
  6.  
  7. var Imported = Imported || {};
  8. Imported.Jay_VariableMix = true;
  9.  
  10. var Jay = Jay || {};
  11. Jay.VariableMix = Jay.VariableMix || {};
  12.  
  13. //=============================================================================
  14.  /*:
  15.  * @plugindesc Allows for variable mixes to be played.
  16.  *
  17.  * @author Jason R. Godding
  18.  *
  19.  * @help To keep playing the music from the same position as the current
  20.  * track, call the plugin command some time BEFORE you need it:
  21.  *
  22.  * LoadVariableMixBGM nameOfTrack [ratio] [volume] [pan]
  23.  *
  24.  * "ratio" should be the ratio between the tempo of the two tracks. The ratio
  25.  * should be current track to the replacement track. Fractions are allowed!
  26.  * So if the current track is half the speed of the replacement, set this
  27.  * to 1/2 or 0.5. If the replacement is half as fast, go with 2. If they
  28.  * are the same speed, either put 1, or just don't put any extra parameters at
  29.  * all (if you don't need to define volume or pan.)
  30.  *
  31.  * If volume and pan are not defined, they'll use the same values as the
  32.  * track that is currently playing. For technical reasons, pitch is not used; you
  33.  * should always use Pitch 100 when using variable mixes.
  34.  *
  35.  * And then, later, to actually play the track, call
  36.  *
  37.  * PlayVariableMixBGM
  38.  *
  39.  * You should call LoadVariableMixBGM in advance, so the replacement track has
  40.  * some time to load.
  41.  *
  42.  * ====================================
  43.  *
  44.  * Version 1.0.2 - Small robustness fix most people won't even notice.
  45.  *
  46.  * Version 1.0.1 - Fixed a game-crashing bug if you saved after loading a variable
  47.  * track, but before playing it.
  48.  *
  49.  * Version 1.0 - First version.
  50.  *
  51.  * This plugin is free for non-commercial and commercial use, but please credit
  52.  * Jason R. Godding if you use it. Thank you.
  53.  *
  54.  */
  55.  
  56. Jay.VariableMix.pluginCommand = Game_Interpreter.prototype.pluginCommand;
  57. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  58.     if (command === 'LoadVariableMixBGM') {
  59.         this.loadVariableMixBGM(args);
  60.     }
  61.     if (command === 'PlayVariableMixBGM') {
  62.         AudioManager.playVariableBgm();
  63.     }
  64.     Jay.VariableMix.pluginCommand.call(this, command, args);
  65. }
  66.  
  67. Game_Interpreter.prototype.loadVariableMixBGM = function(args) {
  68.     var oldBgm = AudioManager._currentBgm;
  69.     var bgm = {
  70.         name: args[0],
  71.         volume: Number(args[2]) || oldBgm.volume,
  72.         pitch: oldBgm.pitch,
  73.         pan: Number(args[3]) || oldBgm.pan,
  74.         tempoRatio: args[1] || 1.0
  75.     }
  76.     AudioManager.loadVariableBgm(bgm);
  77. }
  78.  
  79. AudioManager.loadVariableBgm = function(bgm) {
  80.     this._variableBuffer = this.createBuffer('bgm', bgm.name);
  81.     $gameParty._saveVariableBgm = bgm;
  82.     this._variableTrack = bgm;
  83. }
  84.  
  85. AudioManager.playVariableBgm = function() {
  86.     var bgm = this._variableTrack;
  87.     if(!this._variableBuffer) {
  88.         return;
  89.     }
  90.     var bgmPos = AudioManager.saveBgm();
  91.     this._bgmBuffer.fadeOut(1);
  92.     if(this._spareBuffer) {
  93.         this._spareBuffer.stop();
  94.     }
  95.     this._spareBuffer = this._bgmBuffer;
  96.     var currentPos = bgmPos.pos * eval(bgm.tempoRatio);
  97.     var loops = Math.floor((currentPos - this._variableBuffer._loopStart) / this._variableBuffer._loopLength);
  98.     if(this._variableBuffer._loopStart + this._variableBuffer._loopLength < currentPos) {
  99.         currentPos -= this._variableBuffer._loopLength * loops;
  100.     }
  101.     if (!this._meBuffer) {
  102.         this._variableBuffer.play(true, currentPos);
  103.     }
  104.     this._bgmBuffer = this._variableBuffer;
  105.     this._variableBuffer = null;
  106.     this.updateBgmParameters(bgm);
  107.     this._bgmBuffer.fadeIn(1);
  108.     this.updateCurrentBgm(bgm, currentPos);
  109. }
  110.  
  111. Jay.VariableMix.load = DataManager.loadGame;
  112. DataManager.loadGame = function(savefileId) {
  113.     var returnValue = Jay.VariableMix.load.call(this, savefileId);
  114.     if($gameParty._saveVariableBgm) {
  115.         AudioManager.loadVariableBgm($gameParty._saveVariableBgm);
  116.     }
  117.     return returnValue;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement