(task_audio_mixer.c) Optimize further

This commit is contained in:
twinaphex 2020-05-22 22:00:25 +02:00
parent eaba575c9e
commit 421bcb07d3

View File

@ -457,6 +457,15 @@ bool task_push_audio_mixer_load_and_play(
struct audio_mixer_handle *mixer = NULL;
retro_task_t *t = task_init();
struct audio_mixer_userdata *user = (struct audio_mixer_userdata*)calloc(1, sizeof(*user));
/* We are comparing against a fixed list of file
* extensions, the longest (jpeg) being 4 characters
* in length. We therefore only need to extract the first
* 5 characters from the extension of the input path
* to correctly validate a match */
const char *ext = NULL;
char ext_lower[6];
ext_lower[0] = '\0';
if (!t || !user)
goto error;
@ -479,34 +488,44 @@ bool task_push_audio_mixer_load_and_play(
nbio->type = NBIO_TYPE_NONE;
mixer->type = AUDIO_MIXER_TYPE_NONE;
if (string_ends_with(fullpath, ".wav"))
/* Get file extension */
ext = strrchr(fullpath, '.');
if (!ext || (*(++ext) == '\0'))
goto error;
/* Copy and convert to lower case */
strlcpy(ext_lower, ext, sizeof(ext_lower));
string_to_lower(ext_lower);
if (string_is_equal(ext_lower, "wav"))
{
mixer->type = AUDIO_MIXER_TYPE_WAV;
nbio->type = NBIO_TYPE_WAV;
t->callback = task_audio_mixer_handle_upload_wav_and_play;
}
else if (string_ends_with(fullpath, ".ogg"))
else if (string_is_equal(ext_lower, "ogg"))
{
mixer->type = AUDIO_MIXER_TYPE_OGG;
nbio->type = NBIO_TYPE_OGG;
t->callback = task_audio_mixer_handle_upload_ogg_and_play;
}
else if (string_ends_with(fullpath, ".mp3"))
else if (string_is_equal(ext_lower, "mp3"))
{
mixer->type = AUDIO_MIXER_TYPE_MP3;
nbio->type = NBIO_TYPE_MP3;
t->callback = task_audio_mixer_handle_upload_mp3_and_play;
}
else if (string_ends_with(fullpath, ".flac"))
else if (string_is_equal(ext_lower, "flac"))
{
mixer->type = AUDIO_MIXER_TYPE_FLAC;
nbio->type = NBIO_TYPE_FLAC;
t->callback = task_audio_mixer_handle_upload_flac_and_play;
}
else if (
string_ends_with(fullpath, ".mod") ||
string_ends_with(fullpath, ".s3m") ||
string_ends_with(fullpath, ".xm"))
string_is_equal(ext_lower, "mod") ||
string_is_equal(ext_lower, "s3m") ||
string_is_equal(ext_lower, "xm"))
{
mixer->type = AUDIO_MIXER_TYPE_MOD;
nbio->type = NBIO_TYPE_MOD;
@ -566,6 +585,15 @@ bool task_push_audio_mixer_load(
struct audio_mixer_handle *mixer = NULL;
retro_task_t *t = task_init();
struct audio_mixer_userdata *user = (struct audio_mixer_userdata*)calloc(1, sizeof(*user));
/* We are comparing against a fixed list of file
* extensions, the longest (jpeg) being 4 characters
* in length. We therefore only need to extract the first
* 5 characters from the extension of the input path
* to correctly validate a match */
const char *ext = NULL;
char ext_lower[6];
ext_lower[0] = '\0';
if (!t || !user)
goto error;
@ -589,34 +617,44 @@ bool task_push_audio_mixer_load(
nbio->type = NBIO_TYPE_NONE;
mixer->type = AUDIO_MIXER_TYPE_NONE;
if (string_ends_with(fullpath, ".wav"))
/* Get file extension */
ext = strrchr(fullpath, '.');
if (!ext || (*(++ext) == '\0'))
goto error;
/* Copy and convert to lower case */
strlcpy(ext_lower, ext, sizeof(ext_lower));
string_to_lower(ext_lower);
if (string_is_equal(ext_lower, "wav"))
{
mixer->type = AUDIO_MIXER_TYPE_WAV;
nbio->type = NBIO_TYPE_WAV;
t->callback = task_audio_mixer_handle_upload_wav;
}
else if (string_ends_with(fullpath, ".ogg"))
else if (string_is_equal(ext_lower, "ogg"))
{
mixer->type = AUDIO_MIXER_TYPE_OGG;
nbio->type = NBIO_TYPE_OGG;
t->callback = task_audio_mixer_handle_upload_ogg;
}
else if (string_ends_with(fullpath, ".mp3"))
else if (string_is_equal(ext_lower, "mp3"))
{
mixer->type = AUDIO_MIXER_TYPE_MP3;
nbio->type = NBIO_TYPE_MP3;
t->callback = task_audio_mixer_handle_upload_mp3;
}
else if (string_ends_with(fullpath, ".flac"))
else if (string_is_equal(ext_lower, "flac"))
{
mixer->type = AUDIO_MIXER_TYPE_FLAC;
nbio->type = NBIO_TYPE_FLAC;
t->callback = task_audio_mixer_handle_upload_flac;
}
else if (
string_ends_with(fullpath, ".mod") ||
string_ends_with(fullpath, ".s3m") ||
string_ends_with(fullpath, ".xm"))
string_is_equal(ext_lower, "mod") ||
string_is_equal(ext_lower, "s3m") ||
string_is_equal(ext_lower, "xm"))
{
mixer->type = AUDIO_MIXER_TYPE_MOD;
nbio->type = NBIO_TYPE_MOD;