-
Notifications
You must be signed in to change notification settings - Fork 4
/
RS_WaveSupport.js
217 lines (199 loc) · 6.82 KB
/
RS_WaveSupport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//================================================================
// RS_WaveSupport.js
// ---------------------------------------------------------------
// The MIT License
// Copyright (c) 2015 biud436
// ---------------------------------------------------------------
// Free for commercial and non commercial use.
//================================================================
/*:
* RS_WaveSupport.js
* @target MZ
* @author biud436
* @url https://github.com/biud436
* @plugindesc This can be used to control playback of .wav files.(AudioManager supports addtional playing the wav file)
*
* @param Wave Volume Text
* @desc
* @default Wave Volume
*
* @help
*
* You can play the wav files by using the following code.
*
* - Start playing the sound.
* The string parameter specifies a WAV file name. (Download the .wav file and place it in your Audio/wav folder)
* AudioManager.playWav('file_name');
*
* - Stop a previously played sound.
* AudioManager.stopWav();
*
* - Change Log
* 2015.12.25 (v1.0.0) - First Release Date.
* 2022.01.29 (v2.0.0) - RPG Maker MZ version is released.
*/
/*:ko
* @target MZ
* @plugindesc This can be used to control playback of .wav files.
* (AudioManager supports addtional playing the wav file)
* @author biud436
* @url https://github.com/biud436
*
* @param Wave Volume Text
* @text 웨이브 볼륨 텍스트
* @desc 옵션 창에 볼륨 조절 기능이 추가됩니다. 버튼명을 수정하세요.
* @default 웨이브 볼륨
*
* @help
* ===============================================================
* 소개
* ===============================================================
* audio/wav 폴더에 웨이브 파일을 넣고 스크립트 코드로 웨이브 파일을 재생할 수 있습니다.
*
* AudioManager.playWav('file_name');
* AudioManager.stopWav();
*
* 게임 배포 시, 웨이브 파일을 암호화하려면 RS_WavFileEncrypter (웨이브 파일 인크립터)가 필요합니다.
*
* ===============================================================
* 변동 사항
* ===============================================================
* 2015.12.25 (v1.0.0) - First Release Date.
* 2022.01.29 (v2.0.0) - RPG Maker MZ version is released.
*/
(() => {
const parameters = PluginManager.parameters("RS_WaveSupport");
TextManager.wavText = parameters["Wave Volume Text"] || "Wave Volume Text";
Utils.canPlayWav = function () {
if (!Utils._audioElement) {
Utils._audioElement = document.createElement("audio");
}
return !!(
Utils._audioElement &&
Utils._audioElement.canPlayType('audio/wav; codecs="1"')
);
};
const alias_WebAudio_startLoading = WebAudio.prototype._startLoading;
WebAudio.prototype._startLoading = function () {
alias_WebAudio_startLoading.call(this);
const url = this._url;
// detroy vorbis decoder if the file is a wav file
if (url.match(/\.wav$/i)) {
this._destroyDecoder();
}
};
AudioManager._wavVolume = 100;
AudioManager._wavBuffers = [];
Object.defineProperty(AudioManager, "wavVolume", {
get: function () {
return this._wavVolume;
},
set: function (value) {
this._wavVolume = value;
},
configurable: true,
});
/**
*
* @param {String} folder
* @param {String} name
* @param {String} extension
* @returns
*/
AudioManager.createBuffer = function (folder, name, extension) {
const ext = extension || this.audioFileExt();
const url = this._path + folder + "/" + Utils.encodeURI(name) + ext;
const buffer = new WebAudio(url);
buffer.name = name;
buffer.frameCount = Graphics.frameCount;
return buffer;
};
const alias_stopAll = AudioManager.stopAll;
AudioManager.stopAll = function () {
alias_stopAll.call(this);
this.stopWav();
};
const alias_checkErrors = AudioManager.checkErrors;
AudioManager.checkErrors = function () {
alias_checkErrors.call(this);
this._wavBuffers.forEach((buffer) => {
if (buffer && buffer.isError()) {
this.throwLoadError(buffer);
}
});
};
AudioManager.playWav = function (wavName, vol) {
var wav = {
name: wavName,
pan: 0,
pitch: 100,
volume: vol || ConfigManager.wavVolume,
};
if (wav.name) {
const latestBuffers = this._wavBuffers.filter(
(buffer) => buffer.frameCount === Graphics.frameCount
);
if (latestBuffers.find((buffer) => buffer.name === se.name)) {
return;
}
const buffer = this.createBuffer("/wav", wav.name, ".wav");
this.updateSeParameters(buffer, wav);
buffer.play(false);
this._wavBuffers.push(buffer);
this.cleanupWav();
}
};
AudioManager.cleanupWav = function () {
for (const buffer of this._wavBuffers) {
if (!buffer.isPlaying()) {
buffer.destroy();
}
}
this._wavBuffers = this._wavBuffers.filter((buffer) =>
buffer.isPlaying()
);
};
AudioManager.stopWav = function () {
this._wavBuffers.forEach(function (buffer) {
buffer.stop();
});
this._wavBuffers = [];
};
AudioManager.updateWavParameters = function (buffer, wav) {
this.updateBufferParameters(buffer, this._wavVolume, wav);
};
Object.defineProperty(ConfigManager, "wavVolume", {
get: function () {
return AudioManager.wavVolume;
},
set: function (value) {
AudioManager.wavVolume = value;
},
configurable: true,
});
const alias_makeData = ConfigManager.makeData;
ConfigManager.makeData = function () {
const config = alias_makeData.call(this);
config.wavVolume = this.wavVolume;
return config;
};
const alias_applyData = ConfigManager.applyData;
ConfigManager.applyData = function (config) {
alias_applyData.call(this, config);
this.wavVolume = this.readVolume(config, "wavVolume");
};
const alias_addVolumeOptions = Window_Options.prototype.addVolumeOptions;
Window_Options.prototype.addVolumeOptions = function () {
alias_addVolumeOptions.call(this);
this.addCommand(TextManager.wavText, "wavVolume");
};
const alias_Window_Options_isVolumeSymbol =
Window_Options.prototype.isVolumeSymbol;
Window_Options.prototype.isVolumeSymbol = function (symbol) {
const isOriginalVolumeSymbol = alias_Window_Options_isVolumeSymbol.call(
this,
symbol
);
return symbol === TextManager.wavText || isOriginalVolumeSymbol;
};
})();