2017-01-22 13:58:20 +01:00
|
|
|
/* Copyright (C) 2010-2017 The RetroArch team
|
2013-02-08 11:50:45 +01:00
|
|
|
*
|
2016-12-12 13:02:29 +01:00
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
* The following license statement only applies to this file (audio_resampler.c).
|
|
|
|
* ---------------------------------------------------------------------------------------
|
2013-02-08 11:50:45 +01:00
|
|
|
*
|
2016-12-12 13:02:29 +01:00
|
|
|
* Permission is hereby granted, free of charge,
|
|
|
|
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2013-02-08 11:50:45 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-20 17:34:19 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <string/stdstring.h>
|
2016-05-10 20:31:27 +02:00
|
|
|
#include <features/features_cpu.h>
|
2016-12-12 12:36:46 +01:00
|
|
|
#include <file/config_file_userdata.h>
|
2016-01-20 17:34:19 +01:00
|
|
|
|
2016-12-12 13:02:29 +01:00
|
|
|
#include <audio/audio_resampler.h>
|
2013-02-08 11:50:45 +01:00
|
|
|
|
2017-01-09 12:45:51 +01:00
|
|
|
static const retro_resampler_t *resampler_drivers[] = {
|
2014-04-20 06:26:48 +02:00
|
|
|
&sinc_resampler,
|
2016-12-12 12:44:29 +01:00
|
|
|
#ifdef HAVE_CC_RESAMPLER
|
2014-03-14 16:53:04 +01:00
|
|
|
&CC_resampler,
|
2016-12-12 12:44:29 +01:00
|
|
|
#endif
|
2014-09-13 00:10:15 +02:00
|
|
|
&nearest_resampler,
|
2016-03-14 15:42:12 +01:00
|
|
|
&null_resampler,
|
2014-02-25 09:39:48 +01:00
|
|
|
NULL,
|
2014-02-25 02:55:05 +01:00
|
|
|
};
|
|
|
|
|
2014-09-26 17:05:24 +02: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)
|
2014-02-25 03:23:50 +01:00
|
|
|
{
|
|
|
|
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++)
|
2016-01-20 17:34:19 +01:00
|
|
|
if (string_is_equal_noncase(ident, resampler_drivers[i]->ident))
|
2014-02-25 03:23:50 +01:00
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-09 23:04:05 +01:00
|
|
|
/**
|
2015-01-12 21:12:48 +01:00
|
|
|
* audio_resampler_driver_find_handle:
|
2015-01-15 21:10:36 +01:00
|
|
|
* @idx : index of driver to get handle to.
|
2015-01-09 23:04:05 +01:00
|
|
|
*
|
2015-01-12 21:12:48 +01:00
|
|
|
* Returns: handle to audio resampler driver at index. Can be NULL
|
|
|
|
* if nothing found.
|
2015-01-09 23:04:05 +01:00
|
|
|
**/
|
2015-01-15 21:10:36 +01:00
|
|
|
const void *audio_resampler_driver_find_handle(int idx)
|
2014-02-25 03:23:50 +01:00
|
|
|
{
|
2015-01-15 21:10:36 +01:00
|
|
|
const void *drv = resampler_drivers[idx];
|
2015-01-12 21:12:48 +01:00
|
|
|
if (!drv)
|
|
|
|
return NULL;
|
|
|
|
return drv;
|
2014-02-25 03:23:50 +01:00
|
|
|
}
|
|
|
|
|
2015-01-09 23:04:05 +01:00
|
|
|
/**
|
2015-01-12 21:12:48 +01:00
|
|
|
* audio_resampler_driver_find_ident:
|
2015-01-15 21:10:36 +01:00
|
|
|
* @idx : index of driver to get handle to.
|
2015-01-09 23:04:05 +01:00
|
|
|
*
|
2015-01-12 21:12:48 +01:00
|
|
|
* Returns: Human-readable identifier of audio resampler driver at index.
|
|
|
|
* Can be NULL if nothing found.
|
2015-01-09 23:04:05 +01:00
|
|
|
**/
|
2015-01-15 21:10:36 +01:00
|
|
|
const char *audio_resampler_driver_find_ident(int idx)
|
2014-02-25 03:23:50 +01:00
|
|
|
{
|
2017-01-09 12:45:51 +01:00
|
|
|
const retro_resampler_t *drv = resampler_drivers[idx];
|
2015-01-12 21:12:48 +01:00
|
|
|
if (!drv)
|
|
|
|
return NULL;
|
|
|
|
return drv->ident;
|
2014-02-25 03:23:50 +01:00
|
|
|
}
|
2014-12-31 20:17:53 +01:00
|
|
|
|
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.
|
|
|
|
**/
|
2017-01-09 12:45:51 +01:00
|
|
|
static const retro_resampler_t *find_resampler_driver(const char *ident)
|
2014-09-23 07:20:10 +02:00
|
|
|
{
|
|
|
|
int i = find_resampler_driver_index(ident);
|
2015-01-09 23:04:05 +01:00
|
|
|
|
2014-09-23 07:20:10 +02:00
|
|
|
if (i >= 0)
|
|
|
|
return resampler_drivers[i];
|
2015-01-09 23:04:05 +01:00
|
|
|
|
|
|
|
return resampler_drivers[0];
|
2014-09-23 07:20:10 +02:00
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
**/
|
2014-09-26 16:13:10 +02:00
|
|
|
static bool resampler_append_plugs(void **re,
|
2017-01-09 12:45:51 +01:00
|
|
|
const retro_resampler_t **backend,
|
2014-09-26 16:13:10 +02:00
|
|
|
double bw_ratio)
|
|
|
|
{
|
2017-02-26 21:46:19 +01:00
|
|
|
resampler_simd_mask_t mask = (resampler_simd_mask_t)cpu_features_get();
|
2014-09-26 16:13:10 +02:00
|
|
|
|
2014-09-26 17:05:24 +02:00
|
|
|
*re = (*backend)->init(&resampler_config, bw_ratio, mask);
|
2014-09-26 16:13:10 +02:00
|
|
|
|
|
|
|
if (!*re)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-09 23:04:05 +01:00
|
|
|
/**
|
2017-01-09 12:45:51 +01:00
|
|
|
* retro_resampler_realloc:
|
2015-01-09 23:04:05 +01:00
|
|
|
* @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).
|
|
|
|
**/
|
2017-01-09 12:45:51 +01:00
|
|
|
bool retro_resampler_realloc(void **re, const retro_resampler_t **backend,
|
2014-09-09 05:56:12 +02:00
|
|
|
const char *ident, double bw_ratio)
|
2014-02-25 09:39:48 +01:00
|
|
|
{
|
|
|
|
if (*re && *backend)
|
|
|
|
(*backend)->free(*re);
|
|
|
|
|
|
|
|
*re = NULL;
|
2014-02-25 09:56:39 +01:00
|
|
|
*backend = find_resampler_driver(ident);
|
2014-07-18 19:11:53 +02:00
|
|
|
|
2014-09-26 16:13:10 +02:00
|
|
|
if (!resampler_append_plugs(re, backend, bw_ratio))
|
2016-12-12 14:09:58 +01:00
|
|
|
{
|
|
|
|
if (!*re)
|
|
|
|
*backend = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-25 09:51:24 +01:00
|
|
|
|
2014-09-26 16:13:10 +02:00
|
|
|
return true;
|
2014-02-25 09:39:48 +01:00
|
|
|
}
|