RetroArch/audio/resamplers/resampler.c

215 lines
5.9 KiB
C
Raw Normal View History

2013-02-08 11:50:45 +01:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 18:17:42 +01:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2013-02-08 11:50:45 +01:00
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "resampler.h"
#ifdef RARCH_INTERNAL
#include "../../performance.h"
#endif
2014-10-22 03:35:04 +02:00
#include <file/config_file_userdata.h>
2013-02-08 11:50:45 +01:00
#include <string.h>
2014-08-15 18:17:44 +02:00
static const rarch_resampler_t *resampler_drivers[] = {
&sinc_resampler,
&CC_resampler,
2014-09-13 00:10:15 +02:00
&nearest_resampler,
NULL,
2014-02-25 02:55:05 +01:00
};
static const struct resampler_config resampler_config = {
config_userdata_get_float,
config_userdata_get_int,
config_userdata_get_float_array,
config_userdata_get_int_array,
config_userdata_get_string,
config_userdata_free,
};
2015-01-09 23:04:05 +01:00
/**
* find_resampler_driver_index:
* @ident : Identifier of resampler driver to find.
*
* Finds resampler driver index by @ident name.
*
* Returns: resampler driver index if resampler driver was found, otherwise
* -1.
**/
static int find_resampler_driver_index(const char *ident)
{
unsigned i;
2015-01-09 23:04:05 +01:00
2014-08-15 18:17:44 +02:00
for (i = 0; resampler_drivers[i]; i++)
2015-01-09 23:04:05 +01:00
if (strcasecmp(ident, resampler_drivers[i]->ident) == 0)
return i;
return -1;
}
#if !defined(RESAMPLER_TEST) && defined(RARCH_INTERNAL)
#include <string/string_list.h>
#include "../../general.h"
2015-01-09 23:04:05 +01:00
/**
* find_prev_resampler_driver:
*
2015-01-09 23:32:32 +01:00
* Finds previous driver in resampler driver array.
2015-01-09 23:04:05 +01:00
**/
void find_prev_resampler_driver(void)
{
int i = find_resampler_driver_index(g_settings.audio.resampler);
2015-01-09 23:04:05 +01:00
if (i > 0)
2014-09-09 05:56:12 +02:00
strlcpy(g_settings.audio.resampler, resampler_drivers[i - 1]->ident,
sizeof(g_settings.audio.resampler));
else
2014-09-09 05:56:12 +02:00
RARCH_WARN("Couldn't find any previous resampler driver (current one: \"%s\").\n",
driver.resampler->ident);
}
2015-01-09 23:04:05 +01:00
/**
* find_next_resampler_driver:
*
2015-01-09 23:32:32 +01:00
* Finds next driver in resampler driver array.
2015-01-09 23:04:05 +01:00
**/
void find_next_resampler_driver(void)
{
int i = find_resampler_driver_index(g_settings.audio.resampler);
2015-01-09 23:04:05 +01:00
2014-08-15 18:17:44 +02:00
if (i >= 0 && resampler_drivers[i + 1])
2014-09-09 05:56:12 +02:00
strlcpy(g_settings.audio.resampler, resampler_drivers[i + 1]->ident,
sizeof(g_settings.audio.resampler));
else
2014-09-09 05:56:12 +02:00
RARCH_WARN("Couldn't find any next resampler driver (current one: \"%s\").\n",
driver.resampler->ident);
}
2015-01-09 23:04:05 +01:00
/**
* config_get_audio_resampler_driver_options:
*
* Get an enumerated list of all resampler driver names, separated by '|'.
*
* Returns: string listing of all resampler driver names, separated by '|'.
**/
const char* config_get_audio_resampler_driver_options(void)
{
union string_list_elem_attr attr;
2015-01-12 17:16:14 +01:00
unsigned i;
char *options = NULL;
int options_len = 0;
struct string_list *options_l = string_list_new();
attr.i = 0;
2015-01-12 17:16:14 +01:00
for (i = 0; resampler_drivers[i]; i++)
{
2015-01-12 17:16:14 +01:00
const char *opt = resampler_drivers[i]->ident;
options_len += strlen(opt) + 1;
string_list_append(options_l, opt, attr);
}
options = (char*)calloc(options_len, sizeof(char));
string_list_join_concat(options, options_len, options_l, "|");
string_list_free(options_l);
options_l = NULL;
return options;
}
2014-03-23 14:27:31 +01:00
#endif
2015-01-09 23:04:05 +01:00
/**
* find_resampler_driver:
* @ident : Identifier of resampler driver to find.
*
* Finds resampler by @ident name.
*
* Returns: resampler driver if resampler driver was found, otherwise
* NULL.
**/
static const rarch_resampler_t *find_resampler_driver(const char *ident)
{
2015-01-09 23:04:05 +01:00
unsigned d;
int i = find_resampler_driver_index(ident);
2015-01-09 23:04:05 +01:00
if (i >= 0)
return resampler_drivers[i];
2015-01-09 23:04:05 +01:00
#ifdef RARCH_INTERNAL
2015-01-09 23:04:05 +01:00
RARCH_ERR("Couldn't find any resampler driver named \"%s\"\n", ident);
RARCH_LOG_OUTPUT("Available resampler drivers are:\n");
for (d = 0; resampler_drivers[d]; d++)
RARCH_LOG_OUTPUT("\t%s\n", resampler_drivers[d]->ident);
2015-01-09 23:04:05 +01:00
RARCH_WARN("Going to default to first resampler driver ...\n");
#endif
2015-01-09 23:04:05 +01:00
return resampler_drivers[0];
}
2015-01-09 23:04:05 +01:00
/**
* resampler_append_plugs:
* @re : Resampler handle
* @backend : Resampler backend that is about to be set.
* @bw_ratio : Bandwidth ratio.
*
* Initializes resampler driver based on queried CPU features.
*
* Returns: true (1) if successfully initialized, otherwise false (0).
**/
static bool resampler_append_plugs(void **re,
const rarch_resampler_t **backend,
double bw_ratio)
{
resampler_simd_mask_t mask = rarch_get_cpu_features();
*re = (*backend)->init(&resampler_config, bw_ratio, mask);
if (!*re)
return false;
return true;
}
2015-01-09 23:04:05 +01:00
/**
* rarch_resampler_realloc:
* @re : Resampler handle
* @backend : Resampler backend that is about to be set.
* @ident : Identifier name for resampler we want.
* @bw_ratio : Bandwidth ratio.
*
* Reallocates resampler. Will free previous handle before
* allocating a new one. If ident is NULL, first resampler will be used.
*
* Returns: true (1) if successful, otherwise false (0).
**/
2014-09-09 05:56:12 +02:00
bool rarch_resampler_realloc(void **re, const rarch_resampler_t **backend,
const char *ident, double bw_ratio)
{
if (*re && *backend)
(*backend)->free(*re);
*re = NULL;
2014-02-25 09:56:39 +01:00
*backend = find_resampler_driver(ident);
if (!resampler_append_plugs(re, backend, bw_ratio))
goto error;
return true;
error:
if (!*re)
*backend = NULL;
return false;
}