Skip to content

Commit b1ca498

Browse files
committed
Disallow invalid values
They would otherwise throw an exception which crashes Godot. Some parameters clamp, while others return with an error.
1 parent 67a7c39 commit b1ca498

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/audio_stream_mpt.cpp

+30-9
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,31 @@ void AudioStreamPlaybackMPT::set_current_row(int32_t row) const {
107107

108108
float AudioStreamPlaybackMPT::get_current_channel_vu_left(int32_t channel) const {
109109
CHECK_MOD_LOADED_RET(0.0f);
110+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), 0.0f);
110111
return this->mpt_module->get_current_channel_vu_left(channel);
111112
}
112113

113114
float AudioStreamPlaybackMPT::get_current_channel_vu_mono(int32_t channel) const {
114115
CHECK_MOD_LOADED_RET(0.0f);
116+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), 0.0f);
115117
return this->mpt_module->get_current_channel_vu_mono(channel);
116118
}
117119

118120
float AudioStreamPlaybackMPT::get_current_channel_vu_rear_left(int32_t channel) const {
119121
CHECK_MOD_LOADED_RET(0.0f);
122+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), 0.0f);
120123
return this->mpt_module->get_current_channel_vu_rear_left(channel);
121124
}
122125

123126
float AudioStreamPlaybackMPT::get_current_channel_vu_rear_right(int32_t channel) const {
124127
CHECK_MOD_LOADED_RET(0.0f);
128+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), 0.0f);
125129
return this->mpt_module->get_current_channel_vu_rear_right(channel);
126130
}
127131

128132
float AudioStreamPlaybackMPT::get_current_channel_vu_right(int32_t channel) const {
129133
CHECK_MOD_LOADED_RET(0.0f);
134+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), 0.0f);
130135
return this->mpt_module->get_current_channel_vu_right(channel);
131136
}
132137

@@ -137,6 +142,7 @@ double AudioStreamPlaybackMPT::get_current_estimated_bpm() const {
137142

138143
void AudioStreamPlaybackMPT::select_subsong(int32_t subsong) {
139144
CHECK_MOD_LOADED_RETV();
145+
ERR_FAIL_COND(subsong < -1 || subsong >= this->mpt_module->get_num_subsongs());
140146
this->mpt_module->select_subsong(subsong);
141147
}
142148

@@ -147,7 +153,7 @@ int32_t AudioStreamPlaybackMPT::get_selected_subsong() const {
147153

148154
void AudioStreamPlaybackMPT::set_current_speed(int32_t speed) {
149155
CHECK_INT_LOADED_RETV();
150-
this->mpt_interactive->set_current_speed(speed);
156+
this->mpt_interactive->set_current_speed(Math::clamp(speed, 1, 65535));
151157
}
152158

153159
int32_t AudioStreamPlaybackMPT::get_current_speed() const {
@@ -157,7 +163,7 @@ int32_t AudioStreamPlaybackMPT::get_current_speed() const {
157163

158164
void AudioStreamPlaybackMPT::set_current_tempo(double tempo) {
159165
CHECK_INT3_LOADED_RETV();
160-
return this->mpt_interactive3->set_current_tempo2(tempo);
166+
return this->mpt_interactive3->set_current_tempo2(Math::clamp(tempo, 32.0, 512.0));
161167
}
162168

163169
double AudioStreamPlaybackMPT::get_current_tempo() const {
@@ -167,7 +173,7 @@ double AudioStreamPlaybackMPT::get_current_tempo() const {
167173

168174
void AudioStreamPlaybackMPT::set_tempo_factor(double factor) {
169175
CHECK_INT_LOADED_RETV();
170-
this->mpt_interactive->set_tempo_factor(factor);
176+
this->mpt_interactive->set_tempo_factor(Math::clamp(factor, 0.00001, 4.0));
171177
}
172178

173179
double AudioStreamPlaybackMPT::get_tempo_factor() const {
@@ -177,7 +183,7 @@ double AudioStreamPlaybackMPT::get_tempo_factor() const {
177183

178184
void AudioStreamPlaybackMPT::set_pitch_factor(double factor) {
179185
CHECK_INT_LOADED_RETV();
180-
this->mpt_interactive->set_pitch_factor(factor);
186+
this->mpt_interactive->set_pitch_factor(Math::clamp(factor, 0.00001, 4.0));
181187
}
182188

183189
double AudioStreamPlaybackMPT::get_pitch_factor() const {
@@ -187,7 +193,7 @@ double AudioStreamPlaybackMPT::get_pitch_factor() const {
187193

188194
void AudioStreamPlaybackMPT::set_global_volume(double volume) {
189195
CHECK_INT_LOADED_RETV();
190-
this->mpt_interactive->set_global_volume(volume);
196+
this->mpt_interactive->set_global_volume(Math::clamp(volume, 0.0, 1.0));
191197
}
192198

193199
double AudioStreamPlaybackMPT::get_global_volume() const {
@@ -197,71 +203,85 @@ double AudioStreamPlaybackMPT::get_global_volume() const {
197203

198204
void AudioStreamPlaybackMPT::set_channel_volume(int32_t channel, double volume) {
199205
CHECK_INT_LOADED_RETV();
200-
this->mpt_interactive->set_channel_volume(channel, volume);
206+
ERR_FAIL_INDEX(channel, this->mpt_module->get_num_channels());
207+
this->mpt_interactive->set_channel_volume(channel, Math::clamp(volume, 0.0, 1.0));
201208
}
202209

203210
double AudioStreamPlaybackMPT::get_channel_volume(int32_t channel) const {
204211
CHECK_INT_LOADED_RET(0.0);
212+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), 0.0);
205213
return this->mpt_interactive->get_channel_volume(channel);
206214
}
207215

208216
void AudioStreamPlaybackMPT::set_channel_mute_status(int32_t channel, bool mute) {
209217
CHECK_INT_LOADED_RETV();
218+
ERR_FAIL_INDEX(channel, this->mpt_module->get_num_channels());
210219
this->mpt_interactive->set_channel_mute_status(channel, mute);
211220
}
212221

213222
bool AudioStreamPlaybackMPT::get_channel_mute_status(int32_t channel) const {
214223
CHECK_INT_LOADED_RET(false);
224+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), false);
215225
return this->mpt_interactive->get_channel_mute_status(channel);
216226
}
217227

218228
void AudioStreamPlaybackMPT::set_instrument_mute_status(int32_t instrument, bool mute) {
219229
CHECK_INT_LOADED_RETV();
230+
ERR_FAIL_INDEX(instrument, this->mpt_module->get_num_instruments());
220231
this->mpt_interactive->set_instrument_mute_status(instrument, mute);
221232
}
222233

223234
bool AudioStreamPlaybackMPT::get_instrument_mute_status(int32_t instrument) const {
224235
CHECK_INT_LOADED_RET(false);
236+
ERR_FAIL_INDEX_V(instrument, this->mpt_module->get_num_instruments(), false);
225237
return this->mpt_interactive->get_instrument_mute_status(instrument);
226238
}
227239

228240
int32_t AudioStreamPlaybackMPT::play_note(int32_t instrument, int32_t note, double volume, double panning) {
229241
CHECK_INT_LOADED_RET(-1);
230-
return this->mpt_interactive->play_note(instrument, note, volume, panning);
242+
ERR_FAIL_INDEX_V(instrument, this->mpt_module->get_num_instruments(), -1);
243+
return this->mpt_interactive->play_note(instrument, Math::clamp(note, 0, 119), Math::clamp(volume, 0.0, 1.0), Math::clamp(panning, -1.0, 1.0));
231244
}
232245

233246
void AudioStreamPlaybackMPT::stop_note(int32_t channel) {
234247
CHECK_INT_LOADED_RETV();
248+
ERR_FAIL_INDEX(channel, this->mpt_module->get_num_channels());
235249
this->mpt_interactive->stop_note(channel);
236250
}
237251

238252
void AudioStreamPlaybackMPT::note_off(int32_t channel) {
239253
CHECK_INT2_LOADED_RETV();
254+
ERR_FAIL_INDEX(channel, this->mpt_module->get_num_channels());
240255
this->mpt_interactive2->note_off(channel);
241256
}
242257

243258
void AudioStreamPlaybackMPT::note_fade(int32_t channel) {
244259
CHECK_INT2_LOADED_RETV();
260+
ERR_FAIL_INDEX(channel, this->mpt_module->get_num_channels());
245261
this->mpt_interactive2->note_fade(channel);
246262
}
247263

248264
void AudioStreamPlaybackMPT::set_channel_panning(int32_t channel, double panning) {
249265
CHECK_INT2_LOADED_RETV();
250-
this->mpt_interactive2->set_channel_panning(channel, panning);
266+
ERR_FAIL_INDEX(channel, this->mpt_module->get_num_channels());
267+
this->mpt_interactive2->set_channel_panning(channel, Math::clamp(panning, -1.0, 1.0));
251268
}
252269

253270
double AudioStreamPlaybackMPT::get_channel_panning(int32_t channel) const {
254271
CHECK_INT2_LOADED_RET(0.0);
272+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), 0.0);
255273
return this->mpt_interactive2->get_channel_panning(channel);
256274
}
257275

258276
void AudioStreamPlaybackMPT::set_note_finetune(int32_t channel, double finetune) {
259277
CHECK_INT2_LOADED_RETV();
260-
this->mpt_interactive2->set_note_finetune(channel, finetune);
278+
ERR_FAIL_INDEX(channel, this->mpt_module->get_num_channels());
279+
this->mpt_interactive2->set_note_finetune(channel, Math::clamp(finetune, -1.0, 1.0));
261280
}
262281

263282
double AudioStreamPlaybackMPT::get_note_finetune(int32_t channel) const {
264283
CHECK_INT2_LOADED_RET(0.0);
284+
ERR_FAIL_INDEX_V(channel, this->mpt_module->get_num_channels(), 0.0);
265285
return this->mpt_interactive2->get_note_finetune(channel);
266286
}
267287

@@ -459,6 +479,7 @@ const PackedByteArray& AudioStreamMPT::get_data() const {
459479

460480
void AudioStreamMPT::select_subsong(int32_t subsong) {
461481
CHECK_MOD_LOADED_RETV();
482+
ERR_FAIL_COND(subsong < -1 || subsong >= this->mpt_module->get_num_subsongs());
462483
this->mpt_module->select_subsong(subsong);
463484
}
464485

0 commit comments

Comments
 (0)