3636#include " data.h"
3737#include " configure.h"
3838#include " gdm2s3m.h"
39+ #include " fsafeopen.h"
3940
4041struct _ModPlugFile
4142{
@@ -72,8 +73,8 @@ void audio_callback(void *userdata, Uint8 *stream, int len)
7273 // Reset position
7374 audio.current_mod ->mSoundFile .SetCurrentPos (0 );
7475 // Read anew remaining bytes
75- ModPlug_Read (audio.current_mod , audio.mod_buffer + read_len,
76- len - read_len);
76+ ModPlug_Read (audio.current_mod , audio.mod_buffer + read_len,
77+ len - read_len);
7778 }
7879 }
7980 else
@@ -232,10 +233,42 @@ void init_audio(config_info *conf)
232233 NULL
233234 };
234235
236+ if (conf->oversampling_on )
237+ {
238+ audio.mod_settings .mFlags = MODPLUG_ENABLE_OVERSAMPLING ;
239+ }
240+
235241 audio.mod_settings .mFrequency = 44100 ;
236242 audio.mod_settings .mChannels = 2 ;
237243 audio.mod_settings .mBits = 16 ;
238- audio.mod_settings .mResamplingMode = MODPLUG_RESAMPLE_LINEAR ;
244+
245+ switch (conf->resampling_mode )
246+ {
247+ case 0 :
248+ {
249+ audio.mod_settings .mResamplingMode = MODPLUG_RESAMPLE_NEAREST ;
250+ break ;
251+ }
252+
253+ case 1 :
254+ {
255+ audio.mod_settings .mResamplingMode = MODPLUG_RESAMPLE_LINEAR ;
256+ break ;
257+ }
258+
259+ case 2 :
260+ {
261+ audio.mod_settings .mResamplingMode = MODPLUG_RESAMPLE_SPLINE ;
262+ break ;
263+ }
264+
265+ case 3 :
266+ {
267+ audio.mod_settings .mResamplingMode = MODPLUG_RESAMPLE_FIR ;
268+ break ;
269+ }
270+ }
271+
239272 audio.mod_settings .mLoopCount = -1 ;
240273
241274 ModPlug_SetSettings (&audio.mod_settings );
@@ -252,20 +285,23 @@ void load_mod(char *filename)
252285 char *input_buffer;
253286 int file_size;
254287 int extension_pos = strlen (filename) - 4 ;
255- char new_file[256 ];
288+ char new_file[MAX_PATH ];
256289
257290 if (extension_pos && !strcasecmp (filename + extension_pos, " .gdm" ))
258291 {
259- struct stat file_info;
292+ char translated_filename_src[MAX_PATH ];
293+ char translated_filename_dest[MAX_PATH ];
260294
261295 // GDM -> S3M
262296 strcpy (new_file, filename);
263297 memcpy (new_file + extension_pos, " .s3m" , 4 );
264298
265- if (stat (new_file, &file_info))
299+ fsafetranslate (filename, translated_filename_src);
300+
301+ if (fsafetranslate (new_file, translated_filename_dest) < 0 )
266302 {
267303 // If it doesn't exist, create it by converting.
268- convert_gdm_s3m (filename , new_file);
304+ convert_gdm_s3m (translated_filename_src , new_file);
269305 }
270306
271307 filename = new_file;
@@ -274,7 +310,7 @@ void load_mod(char *filename)
274310 if (audio.mod_playing )
275311 end_mod ();
276312
277- input_file = fopen (filename, " rb" );
313+ input_file = fsafeopen (filename, " rb" );
278314
279315 if (input_file)
280316 {
@@ -304,7 +340,6 @@ void end_mod(void)
304340
305341void play_sample (int freq, char *filename)
306342{
307- struct stat file_info;
308343 FILE *input_file;
309344 char *input_buffer;
310345 int file_size;
@@ -313,23 +348,29 @@ void play_sample(int freq, char *filename)
313348
314349 // FIXME - destroy least recently used?
315350 if (audio.num_samples_playing >= MAX_SAMS )
316- return ;
351+ return ;
317352
318- if (( extension_pos >= 0 ) && !strcasecmp (filename + extension_pos, " .sam" ))
353+ if (extension_pos && !strcasecmp (filename + extension_pos, " .sam" ))
319354 {
320- // SAM -> WAV
355+ char translated_filename_src[MAX_PATH ];
356+ char translated_filename_dest[MAX_PATH ];
357+
358+ // GDM -> S3M
321359 strcpy (new_file, filename);
322360 memcpy (new_file + extension_pos, " .wav" , 4 );
323361
324- if (stat (new_file, &file_info))
362+ fsafetranslate (filename, translated_filename_src);
363+
364+ if (fsafetranslate (new_file, translated_filename_dest) < 0 )
325365 {
326- // Create it
327- convert_sam_to_wav (filename , new_file);
366+ // If it doesn't exist, create it by converting.
367+ convert_sam_to_wav (translated_filename_src , new_file);
328368 }
369+
329370 filename = new_file;
330371 }
331372
332- input_file = fopen (filename, " rb" );
373+ input_file = fsafeopen (filename, " rb" );
333374
334375 if (input_file)
335376 {
@@ -344,31 +385,14 @@ void play_sample(int freq, char *filename)
344385
345386 if (sample_loaded)
346387 {
347- int sample_length = sample_loaded->mSoundFile .Ins [1 ].nLength ;
348-
349388 // A little hack to modify the pitch
350389 sample_loaded->mSoundFile .Ins [1 ].nC4Speed = (freq_conversion / freq) / 2 ;
351390 sample_loaded->mSoundFile .Ins [2 ].nC4Speed = (freq_conversion / freq) / 2 ;
352391 sample_loaded->mSoundFile .Ins [1 ].nVolume = (256 * sound_gvol) / 8 ;
353392 sample_loaded->mSoundFile .Ins [2 ].nVolume = (256 * sound_gvol) / 8 ;
354393
355- // This number is pretty much obtained via experimentation. It can probably
356- // stand to be a little higher.
357- sample_length /= 110 ;
358-
359- if (sample_length < 256 )
360- {
361- sample_loaded->mSoundFile .Patterns [0 ][0 ].param = sample_length;
362- sample_loaded->mSoundFile .Patterns [0 ][2 ].command = CMD_PATTERNBREAK ;
363- }
364- else
365- {
366- int row_duration = sample_length / 255 ;
367- sample_loaded->mSoundFile .Patterns [0 ][0 ].param = 255 ;
368- if (row_duration < 62 )
369- sample_loaded->mSoundFile .Patterns [0 ][2 + row_duration].command =
370- CMD_PATTERNBREAK ;
371- }
394+ // This number is pretty much obtained via experimentation. It can probably
395+ // stand to be a little higher.
372396
373397 // Find a free position to put it
374398 for (i = 0 ; i < 16 ; i++)
@@ -384,7 +408,7 @@ void play_sample(int freq, char *filename)
384408 }
385409
386410 free (input_buffer);
387- fclose (input_file);
411+ fclose (input_file);
388412 }
389413}
390414
@@ -567,4 +591,3 @@ void convert_sam_to_wav(char *source_name, char *dest_name)
567591 fclose (source);
568592 fclose (dest);
569593}
570-
0 commit comments