//=============================================================================
// RPG Maker MZ - AudioAdjust
//
// Copyright 2024 Panzer-IV. All rights reserved.
// This source code or any portion thereof must not be
// reproduced or used without licensed in any manner whatsoever.
//=============================================================================
/*:
 * @target MZ
 * @plugindesc 音声調節プラグイン
 * @author 四号戦車
 * @base PluginCommonBase
 * @base DebugSwitch
 * @base TextResource
 * @base PluginUtils
 * @orderAfter PluginCommonBase
 * @orderAfter DebugSwitch
 * @orderAfter TextResource
 * @orderAfter PluginUtils
 *
 * @help AudioAdjust.js
 *
 * Version: 0.0.3
 *
 * ME/BGS/BGMをパラレルに鳴らしたり、全体音量調節をするプラグイン
 *
 */
(() => {
	"use strict";

    //========================================================================
    // プラグイン定義
    //========================================================================
    const pluginName = "AudioAdjust";
    const script = document.currentScript;
    const param  = PluginManagerEx.createParameter(script);

    //========================================================================
    // コアスクリプト変更部 (AudioManager)
    //========================================================================
    AudioManager._bgmVolume = 20;
    AudioManager._bgsVolume = 60;
    AudioManager._meVolume = 100;
    AudioManager._seVolume = 60;

    const _AudioManager_playSe = AudioManager.playSe;
    AudioManager.playSe = function(audio) {
        const adjust = 1.0;
        if (!!audio) {
            const adjusted = {
                name: audio.name,
                volume: (audio.volume * adjust),
                pitch: audio.pitch,
                pan: audio.pan,
                pos: audio.pos,
            };
            _AudioManager_playSe.apply(this, [ adjusted ]);
        } else {
            _AudioManager_playSe.apply(this, arguments);
        }
    };

    const _AudioManager_playMe = AudioManager.playMe;
    AudioManager.playMe = function(audio) {
        const adjust = 1.0;
        if (!!audio) {
            const adjusted = {
                name: audio.name,
                volume: (audio.volume * adjust),
                pitch: audio.pitch,
                pan: audio.pan,
                pos: audio.pos,
            };

            this.stopMe();

            this._meBuffer = this.createBuffer("me/", audio.name);
            this.updateMeParameters(adjusted);
            this._meBuffer.play(false);
            this._meBuffer.addStopListener(this.stopMe.bind(this));
        } else {
            this.stopMe();
        }
    };

    const _AudioManager_playBgs = AudioManager.playBgs;
    AudioManager.playBgs = function(audio, pos) {
        const adjust = 1.0;
        if (!!audio && $gameSystem._bgsOnSave) {
            const adjusted = {
                name: audio.name,
                volume: (audio.volume * adjust),
                pitch: audio.pitch,
                pan: audio.pan,
                pos: audio.pos,
            };
            _AudioManager_playBgs.apply(this, [ adjusted, pos ]);
        } else {
            _AudioManager_playBgs.apply(this, arguments);
        }
    };

    const _AudioManager_playBgm = AudioManager.playBgm;
    AudioManager.playBgm = function(audio, pos) {
        if (!!audio && audio !== $gameSystem._savedBgm && audio !== $gameSystem._bgmOnSave) {
            const adjust = 1.0;
            const adjusted = {
                name: audio.name,
                volume: (audio.volume * adjust),
                pitch: audio.pitch,
                pan: audio.pan,
                pos: audio.pos,
            };
            _AudioManager_playBgm.apply(this, [ adjusted, pos ]);
        } else {
            _AudioManager_playBgm.apply(this, arguments);
        }
    };

    //========================================================================
    // 定数定義 ()
    //========================================================================

    //========================================================================
    // クラス定義 ()
    //========================================================================

    //========================================================================
    // プラグインコマンド
    //========================================================================
})();
