2015-01-12 17:06:38 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2016-01-10 03:06:50 +00:00
|
|
|
* Copyright (C) 2011-2016 - Daniel De Matteis
|
2015-01-12 17:06:38 +00: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 <string.h>
|
2015-09-05 18:34:22 +00:00
|
|
|
|
2015-12-05 12:22:50 +00:00
|
|
|
#include <retro_assert.h>
|
|
|
|
|
2016-04-26 16:08:25 +00:00
|
|
|
#include <lists/string_list.h>
|
2016-05-16 09:11:02 +00:00
|
|
|
#include <conversion/float_to_s16.h>
|
2016-05-16 09:33:49 +00:00
|
|
|
#include <conversion/s16_to_float.h>
|
2016-04-26 16:08:25 +00:00
|
|
|
|
2016-09-06 21:48:27 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../config.h"
|
|
|
|
#endif
|
|
|
|
|
2015-09-06 12:46:04 +00:00
|
|
|
#include "audio_driver.h"
|
2016-09-06 21:28:45 +00:00
|
|
|
#include "audio_dsp_filter.h"
|
2015-11-23 20:58:39 +00:00
|
|
|
#include "audio_resampler_driver.h"
|
2015-12-05 15:12:29 +00:00
|
|
|
#include "../record/record_driver.h"
|
2015-01-12 17:09:31 +00:00
|
|
|
#include "audio_thread_wrapper.h"
|
2015-10-26 07:52:25 +00:00
|
|
|
|
2016-10-03 02:13:34 +00:00
|
|
|
#ifdef HAVE_NETWORKING
|
2016-10-03 04:25:30 +00:00
|
|
|
#include "../network/netplay/netplay.h"
|
2016-10-03 02:13:34 +00:00
|
|
|
#endif
|
|
|
|
|
2016-05-09 18:30:47 +00:00
|
|
|
#include "../command.h"
|
2016-09-05 15:29:19 +00:00
|
|
|
#include "../driver.h"
|
2015-12-05 12:22:50 +00:00
|
|
|
#include "../configuration.h"
|
2016-03-22 01:45:25 +00:00
|
|
|
#include "../retroarch.h"
|
|
|
|
#include "../runloop.h"
|
2016-05-10 06:53:14 +00:00
|
|
|
#include "../performance_counters.h"
|
2015-11-23 11:03:38 +00:00
|
|
|
#include "../verbosity.h"
|
2016-03-20 16:28:24 +00:00
|
|
|
#include "../list_special.h"
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2015-05-20 03:27:53 +00:00
|
|
|
#define AUDIO_BUFFER_FREE_SAMPLES_COUNT (8 * 1024)
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
struct audio_rate_data
|
2015-05-19 19:18:07 +00:00
|
|
|
{
|
2016-10-01 03:36:36 +00:00
|
|
|
float input;
|
|
|
|
bool control;
|
2016-03-04 20:38:18 +00:00
|
|
|
struct
|
|
|
|
{
|
2016-10-01 03:36:36 +00:00
|
|
|
double original;
|
|
|
|
double current;
|
|
|
|
} source_ratio;
|
2016-05-16 10:08:43 +00:00
|
|
|
};
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2015-01-12 17:06:38 +00:00
|
|
|
static const audio_driver_t *audio_drivers[] = {
|
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
&audio_alsa,
|
2016-05-11 17:02:43 +00:00
|
|
|
#if !defined(__QNX__) && defined(HAVE_THREADS)
|
2015-01-12 17:06:38 +00:00
|
|
|
&audio_alsathread,
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_OSS) || defined(HAVE_OSS_BSD)
|
|
|
|
&audio_oss,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_RSOUND
|
|
|
|
&audio_rsound,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_COREAUDIO
|
|
|
|
&audio_coreaudio,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_AL
|
|
|
|
&audio_openal,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SL
|
|
|
|
&audio_opensl,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ROAR
|
|
|
|
&audio_roar,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_JACK
|
|
|
|
&audio_jack,
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_SDL) || defined(HAVE_SDL2)
|
|
|
|
&audio_sdl,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_XAUDIO
|
|
|
|
&audio_xa,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_DSOUND
|
|
|
|
&audio_dsound,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PULSE
|
|
|
|
&audio_pulse,
|
|
|
|
#endif
|
|
|
|
#ifdef __CELLOS_LV2__
|
|
|
|
&audio_ps3,
|
|
|
|
#endif
|
|
|
|
#ifdef XENON
|
|
|
|
&audio_xenon360,
|
|
|
|
#endif
|
|
|
|
#ifdef GEKKO
|
|
|
|
&audio_gx,
|
|
|
|
#endif
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
&audio_rwebaudio,
|
|
|
|
#endif
|
2015-08-12 10:18:45 +00:00
|
|
|
#if defined(PSP) || defined(VITA)
|
|
|
|
&audio_psp,
|
2015-01-12 17:06:38 +00:00
|
|
|
#endif
|
2015-04-01 21:14:13 +00:00
|
|
|
#ifdef _3DS
|
2015-10-27 00:06:34 +00:00
|
|
|
&audio_ctr_csnd,
|
|
|
|
&audio_ctr_dsp,
|
2015-04-01 21:14:13 +00:00
|
|
|
#endif
|
2015-01-12 17:06:38 +00:00
|
|
|
&audio_null,
|
|
|
|
NULL,
|
|
|
|
};
|
2015-11-30 06:33:15 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
static size_t audio_driver_chunk_size = 0;
|
|
|
|
static size_t audio_driver_chunk_nonblock_size = 0;
|
|
|
|
static size_t audio_driver_chunk_block_size = 0;
|
|
|
|
|
|
|
|
static size_t audio_driver_rewind_ptr = 0;
|
|
|
|
static size_t audio_driver_rewind_size = 0;
|
|
|
|
static int16_t *audio_driver_rewind_buf = NULL;
|
|
|
|
|
|
|
|
static float *audio_driver_input_data = NULL;
|
2016-10-01 02:56:53 +00:00
|
|
|
|
|
|
|
static unsigned audio_driver_free_samples_buf[AUDIO_BUFFER_FREE_SAMPLES_COUNT];
|
2016-10-01 03:36:36 +00:00
|
|
|
static uint64_t audio_driver_free_samples_count = 0;
|
2016-10-01 02:56:53 +00:00
|
|
|
|
|
|
|
static float *audio_driver_output_samples_buf = NULL;
|
|
|
|
static int16_t *audio_driver_output_samples_conv_buf = NULL;
|
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
static float audio_driver_volume_gain = 0.0f;
|
|
|
|
|
|
|
|
static size_t audio_driver_buffer_size = 0;
|
|
|
|
static size_t audio_driver_data_ptr = 0;
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
static struct audio_rate_data audio_driver_data = {0};
|
2016-10-01 02:48:48 +00:00
|
|
|
static struct retro_audio_callback audio_callback = {0};
|
|
|
|
|
|
|
|
static rarch_dsp_filter_t *audio_driver_dsp = NULL;
|
|
|
|
static struct string_list *audio_driver_devices_list = NULL;
|
|
|
|
static const rarch_resampler_t *audio_driver_resampler = NULL;
|
|
|
|
static void *audio_driver_resampler_data = NULL;
|
|
|
|
static const audio_driver_t *current_audio = NULL;
|
|
|
|
static void *audio_driver_context_audio_data = NULL;
|
|
|
|
|
|
|
|
static struct retro_perf_counter resampler_proc = {0};
|
|
|
|
static bool audio_driver_use_float = false;
|
|
|
|
static bool audio_driver_active = false;
|
|
|
|
static bool audio_driver_data_own = false;
|
2015-11-30 06:33:15 +00:00
|
|
|
|
2015-01-12 17:06:38 +00:00
|
|
|
/**
|
|
|
|
* compute_audio_buffer_statistics:
|
|
|
|
*
|
|
|
|
* Computes audio buffer statistics.
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
static void compute_audio_buffer_statistics(void)
|
|
|
|
{
|
|
|
|
unsigned i, low_water_size, high_water_size, avg, stddev;
|
|
|
|
float avg_filled, deviation;
|
2016-03-01 23:07:31 +00:00
|
|
|
uint64_t accum = 0;
|
|
|
|
uint64_t accum_var = 0;
|
|
|
|
unsigned low_water_count = 0;
|
|
|
|
unsigned high_water_count = 0;
|
|
|
|
unsigned samples = MIN(
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_free_samples_count,
|
2015-01-12 17:06:38 +00:00
|
|
|
AUDIO_BUFFER_FREE_SAMPLES_COUNT);
|
|
|
|
|
|
|
|
if (samples < 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 1; i < samples; i++)
|
2016-10-01 02:56:53 +00:00
|
|
|
accum += audio_driver_free_samples_buf[i];
|
2015-01-12 17:06:38 +00:00
|
|
|
|
|
|
|
avg = accum / (samples - 1);
|
|
|
|
|
|
|
|
for (i = 1; i < samples; i++)
|
|
|
|
{
|
2016-10-01 02:56:53 +00:00
|
|
|
int diff = avg - audio_driver_free_samples_buf[i];
|
2015-01-12 17:06:38 +00:00
|
|
|
accum_var += diff * diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
stddev = (unsigned)sqrt((double)accum_var / (samples - 2));
|
2016-10-01 02:48:48 +00:00
|
|
|
avg_filled = 1.0f - (float)avg / audio_driver_buffer_size;
|
|
|
|
deviation = (float)stddev / audio_driver_buffer_size;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
low_water_size = audio_driver_buffer_size * 3 / 4;
|
|
|
|
high_water_size = audio_driver_buffer_size / 4;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
|
|
|
for (i = 1; i < samples; i++)
|
|
|
|
{
|
2016-10-01 02:56:53 +00:00
|
|
|
if (audio_driver_free_samples_buf[i] >= low_water_size)
|
2015-01-12 17:06:38 +00:00
|
|
|
low_water_count++;
|
2016-10-01 02:56:53 +00:00
|
|
|
else if (audio_driver_free_samples_buf[i] <= high_water_size)
|
2015-01-12 17:06:38 +00:00
|
|
|
high_water_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
RARCH_LOG("Average audio buffer saturation: %.2f %%, standard deviation (percentage points): %.2f %%.\n",
|
|
|
|
avg_filled * 100.0, deviation * 100.0);
|
|
|
|
RARCH_LOG("Amount of time spent close to underrun: %.2f %%. Close to blocking: %.2f %%.\n",
|
|
|
|
(100.0 * low_water_count) / (samples - 1),
|
|
|
|
(100.0 * high_water_count) / (samples - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audio_driver_find_handle:
|
2015-01-15 20:10:36 +00:00
|
|
|
* @idx : index of driver to get handle to.
|
2015-01-12 17:06:38 +00:00
|
|
|
*
|
|
|
|
* Returns: handle to audio driver at index. Can be NULL
|
|
|
|
* if nothing found.
|
|
|
|
**/
|
2015-01-15 20:10:36 +00:00
|
|
|
const void *audio_driver_find_handle(int idx)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2015-01-15 20:10:36 +00:00
|
|
|
const void *drv = audio_drivers[idx];
|
2015-01-12 17:06:38 +00:00
|
|
|
if (!drv)
|
|
|
|
return NULL;
|
|
|
|
return drv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audio_driver_find_ident:
|
2015-01-15 20:10:36 +00:00
|
|
|
* @idx : index of driver to get handle to.
|
2015-01-12 17:06:38 +00:00
|
|
|
*
|
|
|
|
* Returns: Human-readable identifier of audio driver at index. Can be NULL
|
|
|
|
* if nothing found.
|
|
|
|
**/
|
2015-01-15 20:10:36 +00:00
|
|
|
const char *audio_driver_find_ident(int idx)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2015-01-15 20:10:36 +00:00
|
|
|
const audio_driver_t *drv = audio_drivers[idx];
|
2015-01-12 17:06:38 +00:00
|
|
|
if (!drv)
|
|
|
|
return NULL;
|
|
|
|
return drv->ident;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* config_get_audio_driver_options:
|
|
|
|
*
|
|
|
|
* Get an enumerated list of all audio driver names, separated by '|'.
|
|
|
|
*
|
|
|
|
* Returns: string listing of all audio driver names, separated by '|'.
|
|
|
|
**/
|
2015-10-26 07:52:25 +00:00
|
|
|
const char *config_get_audio_driver_options(void)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2015-10-26 18:41:20 +00:00
|
|
|
return char_list_new_special(STRING_LIST_AUDIO_DRIVERS, NULL);
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-22 13:28:00 +00:00
|
|
|
static bool uninit_audio(void)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2015-03-20 18:48:23 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2015-03-18 18:40:00 +00:00
|
|
|
|
2015-12-09 17:04:48 +00:00
|
|
|
if (current_audio && current_audio->free)
|
|
|
|
{
|
|
|
|
if (audio_driver_context_audio_data)
|
|
|
|
current_audio->free(audio_driver_context_audio_data);
|
|
|
|
audio_driver_context_audio_data = NULL;
|
|
|
|
}
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
if (audio_driver_output_samples_conv_buf)
|
|
|
|
free(audio_driver_output_samples_conv_buf);
|
|
|
|
audio_driver_output_samples_conv_buf = NULL;
|
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_data_ptr = 0;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
if (audio_driver_rewind_buf)
|
|
|
|
free(audio_driver_rewind_buf);
|
|
|
|
audio_driver_rewind_buf = NULL;
|
|
|
|
|
|
|
|
audio_driver_rewind_size = 0;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2015-03-20 18:48:23 +00:00
|
|
|
if (!settings->audio.enable)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2016-05-08 16:24:25 +00:00
|
|
|
audio_driver_unset_active();
|
2015-11-22 13:28:00 +00:00
|
|
|
return false;
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 13:53:12 +00:00
|
|
|
audio_driver_deinit_resampler();
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
if (audio_driver_input_data)
|
|
|
|
free(audio_driver_input_data);
|
|
|
|
audio_driver_input_data = NULL;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
if (audio_driver_output_samples_buf)
|
|
|
|
free(audio_driver_output_samples_buf);
|
|
|
|
audio_driver_output_samples_buf = NULL;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-05-09 18:51:53 +00:00
|
|
|
command_event(CMD_EVENT_DSP_FILTER_DEINIT, NULL);
|
2015-01-12 17:06:38 +00:00
|
|
|
|
|
|
|
compute_audio_buffer_statistics();
|
2015-11-22 13:28:00 +00:00
|
|
|
|
|
|
|
return true;
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 14:02:46 +00:00
|
|
|
static bool audio_driver_init_internal(bool audio_cb_inited)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
|
|
|
size_t outsamples_max, max_bufsamples = AUDIO_CHUNK_SIZE_NONBLOCKING * 2;
|
2015-03-20 18:48:23 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-05-17 17:00:16 +00:00
|
|
|
convert_s16_to_float_init_simd();
|
|
|
|
convert_float_to_s16_init_simd();
|
2015-01-12 17:06:38 +00:00
|
|
|
|
|
|
|
/* Accomodate rewind since at some point we might have two full buffers. */
|
|
|
|
outsamples_max = max_bufsamples * AUDIO_MAX_RATIO *
|
2016-07-25 10:30:41 +00:00
|
|
|
settings->slowmotion_ratio;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_output_samples_conv_buf =
|
2016-07-12 21:08:00 +00:00
|
|
|
(int16_t*)malloc(outsamples_max * sizeof(int16_t));
|
2015-01-12 17:06:38 +00:00
|
|
|
/* Used for recording even if audio isn't enabled. */
|
2016-10-01 02:56:53 +00:00
|
|
|
retro_assert(audio_driver_output_samples_conv_buf != NULL);
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
if (!audio_driver_output_samples_conv_buf)
|
2015-03-21 03:43:18 +00:00
|
|
|
goto error;
|
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_chunk_block_size = AUDIO_CHUNK_SIZE_BLOCKING;
|
|
|
|
audio_driver_chunk_nonblock_size = AUDIO_CHUNK_SIZE_NONBLOCKING;
|
|
|
|
audio_driver_chunk_size = audio_driver_chunk_block_size;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
|
|
|
/* Needs to be able to hold full content of a full max_bufsamples
|
|
|
|
* in addition to its own. */
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_rewind_buf = (int16_t*)malloc
|
2016-07-12 21:08:00 +00:00
|
|
|
(max_bufsamples * sizeof(int16_t));
|
2016-10-01 03:36:36 +00:00
|
|
|
retro_assert(audio_driver_rewind_buf != NULL);
|
2015-03-21 03:43:18 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
if (!audio_driver_rewind_buf)
|
2015-03-21 03:43:18 +00:00
|
|
|
goto error;
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_rewind_size = max_bufsamples;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2015-03-20 18:48:23 +00:00
|
|
|
if (!settings->audio.enable)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2016-05-08 16:24:25 +00:00
|
|
|
audio_driver_unset_active();
|
2015-11-22 13:28:00 +00:00
|
|
|
return false;
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 14:50:23 +00:00
|
|
|
audio_driver_find_driver();
|
2015-01-12 17:06:38 +00:00
|
|
|
#ifdef HAVE_THREADS
|
2016-03-04 21:35:39 +00:00
|
|
|
if (audio_cb_inited)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
|
|
|
RARCH_LOG("Starting threaded audio driver ...\n");
|
2016-05-10 01:14:23 +00:00
|
|
|
if (!audio_init_thread(
|
2016-02-11 00:40:43 +00:00
|
|
|
¤t_audio,
|
|
|
|
&audio_driver_context_audio_data,
|
2015-03-20 18:48:23 +00:00
|
|
|
*settings->audio.device ? settings->audio.device : NULL,
|
|
|
|
settings->audio.out_rate, settings->audio.latency,
|
2015-11-23 18:30:47 +00:00
|
|
|
current_audio))
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Cannot open threaded audio driver ... Exiting ...\n");
|
2016-05-09 05:09:26 +00:00
|
|
|
retroarch_fail(1, "audio_driver_init_internal()");
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-02-11 00:40:43 +00:00
|
|
|
audio_driver_context_audio_data =
|
|
|
|
current_audio->init(*settings->audio.device ?
|
|
|
|
settings->audio.device : NULL,
|
|
|
|
settings->audio.out_rate, settings->audio.latency);
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 06:33:15 +00:00
|
|
|
if (!audio_driver_context_audio_data)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to initialize audio driver. Will continue without audio.\n");
|
2016-05-08 16:24:25 +00:00
|
|
|
audio_driver_unset_active();
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_use_float = false;
|
2016-05-08 16:24:25 +00:00
|
|
|
if ( audio_driver_is_active()
|
2016-02-11 00:40:43 +00:00
|
|
|
&& current_audio->use_float(audio_driver_context_audio_data))
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_use_float = true;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-05-08 16:24:25 +00:00
|
|
|
if (!settings->audio.sync && audio_driver_is_active())
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2016-05-09 18:51:53 +00:00
|
|
|
command_event(CMD_EVENT_AUDIO_SET_NONBLOCKING_STATE, NULL);
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_chunk_size = audio_driver_chunk_nonblock_size;
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
if (audio_driver_data.input <= 0.0f)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
|
|
|
/* Should never happen. */
|
|
|
|
RARCH_WARN("Input rate is invalid (%.3f Hz). Using output rate (%u Hz).\n",
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.input, settings->audio.out_rate);
|
|
|
|
audio_driver_data.input = settings->audio.out_rate;
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.source_ratio.original =
|
|
|
|
audio_driver_data.source_ratio.current =
|
|
|
|
(double)settings->audio.out_rate / audio_driver_data.input;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-05-08 14:09:08 +00:00
|
|
|
if (!audio_driver_init_resampler())
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to initialize resampler \"%s\".\n",
|
2015-03-20 18:48:23 +00:00
|
|
|
settings->audio.resampler);
|
2016-05-08 16:24:25 +00:00
|
|
|
audio_driver_unset_active();
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_input_data = (float*)
|
2016-07-12 21:08:00 +00:00
|
|
|
malloc(max_bufsamples * sizeof(float));
|
2016-10-01 03:36:36 +00:00
|
|
|
retro_assert(audio_driver_input_data != NULL);
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
if (!audio_driver_input_data)
|
2015-03-21 03:43:18 +00:00
|
|
|
goto error;
|
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_data_ptr = 0;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2015-10-26 02:18:13 +00:00
|
|
|
retro_assert(settings->audio.out_rate <
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.input * AUDIO_MAX_RATIO);
|
2016-07-12 21:08:00 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_output_samples_buf = (float*)
|
2016-07-12 21:08:00 +00:00
|
|
|
malloc(outsamples_max * sizeof(float));
|
2016-10-01 02:56:53 +00:00
|
|
|
retro_assert(audio_driver_output_samples_buf != NULL);
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
if (!audio_driver_output_samples_buf)
|
2015-03-21 03:43:18 +00:00
|
|
|
goto error;
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.control = false;
|
2015-12-04 01:16:17 +00:00
|
|
|
if (
|
2016-03-04 21:35:39 +00:00
|
|
|
!audio_cb_inited
|
2016-05-08 16:24:25 +00:00
|
|
|
&& audio_driver_is_active()
|
2015-12-04 01:16:17 +00:00
|
|
|
&& settings->audio.rate_control
|
|
|
|
)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2015-04-11 04:56:02 +00:00
|
|
|
/* Audio rate control requires write_avail
|
|
|
|
* and buffer_size to be implemented. */
|
2015-11-23 18:30:47 +00:00
|
|
|
if (current_audio->buffer_size)
|
2015-01-12 17:06:38 +00:00
|
|
|
{
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_buffer_size =
|
2015-11-30 06:33:15 +00:00
|
|
|
current_audio->buffer_size(audio_driver_context_audio_data);
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.control = true;
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
RARCH_WARN("Audio rate control was desired, but driver does not support needed features.\n");
|
|
|
|
}
|
|
|
|
|
2016-05-09 18:51:53 +00:00
|
|
|
command_event(CMD_EVENT_DSP_FILTER_INIT, NULL);
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_free_samples_count = 0;
|
2015-01-12 17:06:38 +00:00
|
|
|
|
2015-11-22 13:23:29 +00:00
|
|
|
/* Threaded driver is initially stopped. */
|
2015-12-04 01:16:17 +00:00
|
|
|
if (
|
2016-05-08 16:24:25 +00:00
|
|
|
audio_driver_is_active()
|
2015-12-04 01:16:17 +00:00
|
|
|
&& !settings->audio.mute_enable
|
2016-03-04 21:35:39 +00:00
|
|
|
&& audio_cb_inited
|
2015-12-04 01:16:17 +00:00
|
|
|
)
|
2016-05-08 16:00:32 +00:00
|
|
|
audio_driver_start();
|
2015-03-21 03:43:18 +00:00
|
|
|
|
2015-11-22 13:28:00 +00:00
|
|
|
return true;
|
2015-03-21 03:43:18 +00:00
|
|
|
|
|
|
|
error:
|
2016-05-08 14:45:58 +00:00
|
|
|
return audio_driver_deinit();
|
2015-01-12 17:06:38 +00:00
|
|
|
}
|
2015-02-10 20:16:26 +00:00
|
|
|
|
2015-02-11 16:36:31 +00:00
|
|
|
/*
|
|
|
|
* audio_driver_readjust_input_rate:
|
|
|
|
*
|
|
|
|
* Readjust the audio input rate.
|
|
|
|
*/
|
2015-11-22 14:10:14 +00:00
|
|
|
static void audio_driver_readjust_input_rate(void)
|
2015-02-11 16:36:31 +00:00
|
|
|
{
|
2015-03-20 18:48:23 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2016-10-01 02:56:53 +00:00
|
|
|
unsigned write_idx = audio_driver_free_samples_count++ &
|
2015-03-23 01:31:16 +00:00
|
|
|
(AUDIO_BUFFER_FREE_SAMPLES_COUNT - 1);
|
2016-10-01 02:48:48 +00:00
|
|
|
int half_size = audio_driver_buffer_size / 2;
|
2016-02-11 00:40:43 +00:00
|
|
|
int avail =
|
|
|
|
current_audio->write_avail(audio_driver_context_audio_data);
|
2015-03-23 01:31:16 +00:00
|
|
|
int delta_mid = avail - half_size;
|
|
|
|
double direction = (double)delta_mid / half_size;
|
|
|
|
double adjust = 1.0 + settings->audio.rate_control_delta * direction;
|
2015-02-11 16:36:31 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
RARCH_LOG_OUTPUT("Audio buffer is %u%% full\n",
|
2016-10-01 02:48:48 +00:00
|
|
|
(unsigned)(100 - (avail * 100) / audio_driver_buffer_size));
|
2015-02-11 16:36:31 +00:00
|
|
|
#endif
|
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_free_samples_buf[write_idx] = avail;
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.source_ratio.current =
|
|
|
|
audio_driver_data.source_ratio.original * adjust;
|
2015-02-11 16:36:31 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
RARCH_LOG_OUTPUT("New rate: %lf, Orig rate: %lf\n",
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.source_ratio.current,
|
|
|
|
audio_driver_data.source_ratio.original);
|
2015-02-11 16:36:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
2015-03-23 01:36:13 +00:00
|
|
|
|
2015-05-19 19:18:07 +00:00
|
|
|
void audio_driver_set_nonblocking_state(bool enable)
|
|
|
|
{
|
|
|
|
settings_t *settings = config_get_ptr();
|
2015-12-04 01:16:17 +00:00
|
|
|
if (
|
2016-05-08 16:24:25 +00:00
|
|
|
audio_driver_is_active()
|
2015-12-04 01:16:17 +00:00
|
|
|
&& audio_driver_context_audio_data
|
|
|
|
)
|
2015-11-30 06:33:15 +00:00
|
|
|
current_audio->set_nonblock_state(audio_driver_context_audio_data,
|
2015-11-22 14:16:03 +00:00
|
|
|
settings->audio.sync ? enable : true);
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_chunk_size = enable ?
|
|
|
|
audio_driver_chunk_nonblock_size :
|
|
|
|
audio_driver_chunk_block_size;
|
2015-05-19 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audio_driver_flush:
|
|
|
|
* @data : pointer to audio buffer.
|
|
|
|
* @right : amount of samples to write.
|
|
|
|
*
|
|
|
|
* Writes audio samples to audio driver. Will first
|
|
|
|
* perform DSP processing (if enabled) and resampling.
|
|
|
|
*
|
|
|
|
* Returns: true (1) if audio samples were written to the audio
|
|
|
|
* driver, false (0) in case of an error.
|
|
|
|
**/
|
2015-11-23 12:30:19 +00:00
|
|
|
static bool audio_driver_flush(const int16_t *data, size_t samples)
|
2015-05-19 19:18:07 +00:00
|
|
|
{
|
2016-10-19 01:48:34 +00:00
|
|
|
static struct retro_perf_counter audio_convert_s16 = {0};
|
2015-09-20 08:02:47 +00:00
|
|
|
static struct retro_perf_counter audio_convert_float = {0};
|
2016-10-19 01:48:34 +00:00
|
|
|
static struct retro_perf_counter audio_dsp = {0};
|
|
|
|
struct resampler_data src_data;
|
|
|
|
struct rarch_dsp_data dsp_data;
|
2015-09-20 08:02:47 +00:00
|
|
|
const void *output_data = NULL;
|
|
|
|
unsigned output_frames = 0;
|
|
|
|
size_t output_size = sizeof(float);
|
|
|
|
settings_t *settings = config_get_ptr();
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-10-19 01:48:34 +00:00
|
|
|
src_data.data_in = NULL;
|
|
|
|
src_data.data_out = NULL;
|
|
|
|
src_data.input_frames = 0;
|
|
|
|
src_data.output_frames = 0;
|
|
|
|
src_data.ratio = 0.0f;
|
|
|
|
|
|
|
|
dsp_data.input = NULL;
|
|
|
|
dsp_data.input_frames = 0;
|
|
|
|
dsp_data.output = NULL;
|
|
|
|
dsp_data.output_frames = 0;
|
|
|
|
|
2015-11-30 00:49:17 +00:00
|
|
|
recording_push_audio(data, samples);
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2015-11-30 21:25:13 +00:00
|
|
|
if (runloop_ctl(RUNLOOP_CTL_IS_PAUSED, NULL) || settings->audio.mute_enable)
|
2015-05-19 19:18:07 +00:00
|
|
|
return true;
|
2016-10-19 01:48:34 +00:00
|
|
|
if (!audio_driver_is_active() || !audio_driver_input_data)
|
2015-05-19 19:18:07 +00:00
|
|
|
return false;
|
|
|
|
|
2016-05-16 07:58:45 +00:00
|
|
|
performance_counter_init(&audio_convert_s16, "audio_convert_s16");
|
2016-05-16 07:53:54 +00:00
|
|
|
performance_counter_start(&audio_convert_s16);
|
2016-10-01 03:36:36 +00:00
|
|
|
convert_s16_to_float(audio_driver_input_data, data, samples,
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_volume_gain);
|
2016-05-16 07:48:14 +00:00
|
|
|
performance_counter_stop(&audio_convert_s16);
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
src_data.data_in = audio_driver_input_data;
|
2015-05-19 19:18:07 +00:00
|
|
|
src_data.input_frames = samples >> 1;
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
dsp_data.input = audio_driver_input_data;
|
2015-05-19 19:18:07 +00:00
|
|
|
dsp_data.input_frames = samples >> 1;
|
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
if (audio_driver_dsp)
|
2015-05-19 19:18:07 +00:00
|
|
|
{
|
2016-05-16 07:58:45 +00:00
|
|
|
performance_counter_init(&audio_dsp, "audio_dsp");
|
2016-05-16 07:53:54 +00:00
|
|
|
performance_counter_start(&audio_dsp);
|
2016-10-01 02:48:48 +00:00
|
|
|
rarch_dsp_filter_process(audio_driver_dsp, &dsp_data);
|
2016-05-16 07:48:14 +00:00
|
|
|
performance_counter_stop(&audio_dsp);
|
2015-05-19 19:18:07 +00:00
|
|
|
|
|
|
|
if (dsp_data.output)
|
|
|
|
{
|
|
|
|
src_data.data_in = dsp_data.output;
|
|
|
|
src_data.input_frames = dsp_data.output_frames;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
src_data.data_out = audio_driver_output_samples_buf;
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
if (audio_driver_data.control)
|
2015-05-19 19:18:07 +00:00
|
|
|
audio_driver_readjust_input_rate();
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
src_data.ratio = audio_driver_data.source_ratio.current;
|
2015-09-26 10:57:52 +00:00
|
|
|
|
2016-02-29 00:33:14 +00:00
|
|
|
if (runloop_ctl(RUNLOOP_CTL_IS_SLOWMOTION, NULL))
|
2016-07-25 10:30:41 +00:00
|
|
|
src_data.ratio *= settings->slowmotion_ratio;
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-05-08 14:09:08 +00:00
|
|
|
audio_driver_process_resampler(&src_data);
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
output_data = audio_driver_output_samples_buf;
|
2015-05-19 19:18:07 +00:00
|
|
|
output_frames = src_data.output_frames;
|
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
if (!audio_driver_use_float)
|
2015-05-19 19:18:07 +00:00
|
|
|
{
|
2016-05-16 07:58:45 +00:00
|
|
|
performance_counter_init(&audio_convert_float, "audio_convert_float");
|
2016-05-16 07:53:54 +00:00
|
|
|
performance_counter_start(&audio_convert_float);
|
2016-10-01 02:56:53 +00:00
|
|
|
convert_float_to_s16(audio_driver_output_samples_conv_buf,
|
2015-05-19 19:18:07 +00:00
|
|
|
(const float*)output_data, output_frames * 2);
|
2016-05-16 07:48:14 +00:00
|
|
|
performance_counter_stop(&audio_convert_float);
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
output_data = audio_driver_output_samples_conv_buf;
|
2015-05-19 19:18:07 +00:00
|
|
|
output_size = sizeof(int16_t);
|
|
|
|
}
|
|
|
|
|
2015-11-30 06:33:15 +00:00
|
|
|
if (current_audio->write(audio_driver_context_audio_data,
|
|
|
|
output_data, output_frames * output_size * 2) < 0)
|
2015-05-19 19:18:07 +00:00
|
|
|
{
|
2016-05-08 16:24:25 +00:00
|
|
|
audio_driver_unset_active();
|
2015-05-19 19:18:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audio_driver_sample:
|
|
|
|
* @left : value of the left audio channel.
|
|
|
|
* @right : value of the right audio channel.
|
|
|
|
*
|
|
|
|
* Audio sample render callback function.
|
|
|
|
**/
|
|
|
|
void audio_driver_sample(int16_t left, int16_t right)
|
|
|
|
{
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_output_samples_conv_buf[audio_driver_data_ptr++] = left;
|
|
|
|
audio_driver_output_samples_conv_buf[audio_driver_data_ptr++] = right;
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
if (audio_driver_data_ptr < audio_driver_chunk_size)
|
2015-05-19 19:18:07 +00:00
|
|
|
return;
|
|
|
|
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_flush(audio_driver_output_samples_conv_buf,
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_data_ptr);
|
2015-05-19 19:18:07 +00:00
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_data_ptr = 0;
|
2015-05-19 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audio_driver_sample_batch:
|
|
|
|
* @data : pointer to audio buffer.
|
|
|
|
* @frames : amount of audio frames to push.
|
|
|
|
*
|
|
|
|
* Batched audio sample render callback function.
|
|
|
|
*
|
|
|
|
* Returns: amount of frames sampled. Will be equal to @frames
|
|
|
|
* unless @frames exceeds (AUDIO_CHUNK_SIZE_NONBLOCKING / 2).
|
|
|
|
**/
|
|
|
|
size_t audio_driver_sample_batch(const int16_t *data, size_t frames)
|
|
|
|
{
|
|
|
|
if (frames > (AUDIO_CHUNK_SIZE_NONBLOCKING >> 1))
|
|
|
|
frames = AUDIO_CHUNK_SIZE_NONBLOCKING >> 1;
|
|
|
|
|
|
|
|
audio_driver_flush(data, frames << 1);
|
|
|
|
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audio_driver_sample_rewind:
|
|
|
|
* @left : value of the left audio channel.
|
|
|
|
* @right : value of the right audio channel.
|
|
|
|
*
|
2016-02-11 00:40:43 +00:00
|
|
|
* Audio sample render callback function (rewind version).
|
|
|
|
* This callback function will be used instead of
|
|
|
|
* audio_driver_sample when rewinding is activated.
|
2015-05-19 19:18:07 +00:00
|
|
|
**/
|
|
|
|
void audio_driver_sample_rewind(int16_t left, int16_t right)
|
|
|
|
{
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_rewind_buf[--audio_driver_rewind_ptr] = right;
|
|
|
|
audio_driver_rewind_buf[--audio_driver_rewind_ptr] = left;
|
2015-05-19 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audio_driver_sample_batch_rewind:
|
|
|
|
* @data : pointer to audio buffer.
|
|
|
|
* @frames : amount of audio frames to push.
|
|
|
|
*
|
2016-02-11 00:40:43 +00:00
|
|
|
* Batched audio sample render callback function (rewind version).
|
|
|
|
*
|
|
|
|
* This callback function will be used instead of
|
|
|
|
* audio_driver_sample_batch when rewinding is activated.
|
2015-05-19 19:18:07 +00:00
|
|
|
*
|
|
|
|
* Returns: amount of frames sampled. Will be equal to @frames
|
|
|
|
* unless @frames exceeds (AUDIO_CHUNK_SIZE_NONBLOCKING / 2).
|
|
|
|
**/
|
|
|
|
size_t audio_driver_sample_batch_rewind(const int16_t *data, size_t frames)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t samples = frames << 1;
|
|
|
|
|
|
|
|
for (i = 0; i < samples; i++)
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_rewind_buf[--audio_driver_rewind_ptr] = data[i];
|
2015-05-19 19:18:07 +00:00
|
|
|
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_set_volume_gain(float gain)
|
|
|
|
{
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_volume_gain = gain;
|
2015-05-19 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_dsp_filter_free(void)
|
|
|
|
{
|
2016-10-01 02:48:48 +00:00
|
|
|
if (audio_driver_dsp)
|
|
|
|
rarch_dsp_filter_free(audio_driver_dsp);
|
|
|
|
audio_driver_dsp = NULL;
|
2015-05-19 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_dsp_filter_init(const char *device)
|
|
|
|
{
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_dsp = rarch_dsp_filter_new(
|
2016-10-01 03:36:36 +00:00
|
|
|
device, audio_driver_data.input);
|
2016-02-11 00:40:43 +00:00
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
if (!audio_driver_dsp)
|
2015-05-19 19:18:07 +00:00
|
|
|
RARCH_ERR("[DSP]: Failed to initialize DSP filter \"%s\".\n", device);
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_set_buffer_size(size_t bufsize)
|
|
|
|
{
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_buffer_size = bufsize;
|
2015-05-19 19:18:07 +00:00
|
|
|
}
|
2015-05-19 19:35:40 +00:00
|
|
|
|
2016-05-08 15:26:17 +00:00
|
|
|
void audio_driver_monitor_adjust_system_rates(void)
|
2015-11-22 14:07:11 +00:00
|
|
|
{
|
|
|
|
float timing_skew;
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
const struct retro_system_timing *info = NULL;
|
|
|
|
struct retro_system_av_info *av_info = video_viewport_get_system_av_info();
|
|
|
|
|
|
|
|
if (av_info)
|
|
|
|
info = (const struct retro_system_timing*)&av_info->timing;
|
|
|
|
|
|
|
|
if (!info || info->sample_rate <= 0.0)
|
|
|
|
return;
|
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
timing_skew = fabs(1.0f - info->fps / settings->video.refresh_rate);
|
|
|
|
audio_driver_data.input = info->sample_rate;
|
2015-11-22 14:07:11 +00:00
|
|
|
|
|
|
|
if (timing_skew <= settings->audio.max_timing_skew)
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.input *= (settings->video.refresh_rate / info->fps);
|
2015-11-22 14:07:11 +00:00
|
|
|
|
|
|
|
RARCH_LOG("Set audio input rate to: %.2f Hz.\n",
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.input);
|
2015-11-22 14:07:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 14:28:24 +00:00
|
|
|
void audio_driver_setup_rewind(void)
|
2015-11-22 14:25:43 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/* Push audio ready to be played. */
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_rewind_ptr = audio_driver_rewind_size;
|
2015-11-22 14:25:43 +00:00
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
for (i = 0; i < audio_driver_data_ptr; i += 2)
|
2015-11-22 14:25:43 +00:00
|
|
|
{
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_rewind_buf[--audio_driver_rewind_ptr] =
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_output_samples_conv_buf[i + 1];
|
2015-11-22 14:25:43 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_rewind_buf[--audio_driver_rewind_ptr] =
|
2016-10-01 02:56:53 +00:00
|
|
|
audio_driver_output_samples_conv_buf[i + 0];
|
2015-11-22 14:25:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 02:48:48 +00:00
|
|
|
audio_driver_data_ptr = 0;
|
2015-11-22 14:25:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 14:50:23 +00:00
|
|
|
bool audio_driver_find_driver(void)
|
2015-11-23 09:13:27 +00:00
|
|
|
{
|
2016-02-01 12:15:53 +00:00
|
|
|
int i;
|
|
|
|
driver_ctx_info_t drv;
|
2015-11-23 09:13:27 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
|
2016-02-01 12:15:53 +00:00
|
|
|
drv.label = "audio_driver";
|
|
|
|
drv.s = settings->audio.driver;
|
|
|
|
|
|
|
|
driver_ctl(RARCH_DRIVER_CTL_FIND_INDEX, &drv);
|
|
|
|
|
|
|
|
i = drv.len;
|
2015-11-23 09:13:27 +00:00
|
|
|
|
|
|
|
if (i >= 0)
|
2015-11-23 18:30:47 +00:00
|
|
|
current_audio = (const audio_driver_t*)audio_driver_find_handle(i);
|
2015-11-23 09:13:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned d;
|
|
|
|
RARCH_ERR("Couldn't find any audio driver named \"%s\"\n",
|
|
|
|
settings->audio.driver);
|
|
|
|
RARCH_LOG_OUTPUT("Available audio drivers are:\n");
|
|
|
|
for (d = 0; audio_driver_find_handle(d); d++)
|
|
|
|
RARCH_LOG_OUTPUT("\t%s\n", audio_driver_find_ident(d));
|
|
|
|
RARCH_WARN("Going to default to first audio driver...\n");
|
|
|
|
|
2015-11-23 18:30:47 +00:00
|
|
|
current_audio = (const audio_driver_t*)audio_driver_find_handle(0);
|
2015-11-23 09:13:27 +00:00
|
|
|
|
2015-11-23 18:30:47 +00:00
|
|
|
if (!current_audio)
|
2016-05-09 05:09:26 +00:00
|
|
|
retroarch_fail(1, "audio_driver_find()");
|
2015-11-23 09:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-08 13:53:12 +00:00
|
|
|
void audio_driver_deinit_resampler(void)
|
|
|
|
{
|
2016-09-13 09:41:45 +00:00
|
|
|
if (audio_driver_resampler && audio_driver_resampler_data)
|
|
|
|
audio_driver_resampler->free(audio_driver_resampler_data);
|
|
|
|
audio_driver_resampler = NULL;
|
|
|
|
audio_driver_resampler_data = NULL;
|
2016-05-08 13:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_free_devices_list(void)
|
|
|
|
{
|
|
|
|
if (!current_audio || !current_audio->device_list_free
|
|
|
|
|| !audio_driver_context_audio_data)
|
|
|
|
return false;
|
|
|
|
current_audio->device_list_free(audio_driver_context_audio_data,
|
|
|
|
audio_driver_devices_list);
|
|
|
|
audio_driver_devices_list = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_new_devices_list(void)
|
|
|
|
{
|
|
|
|
if (!current_audio || !current_audio->device_list_new
|
|
|
|
|| !audio_driver_context_audio_data)
|
|
|
|
return false;
|
|
|
|
audio_driver_devices_list = (struct string_list*)
|
|
|
|
current_audio->device_list_new(audio_driver_context_audio_data);
|
|
|
|
if (!audio_driver_devices_list)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-08 14:02:46 +00:00
|
|
|
bool audio_driver_init(void)
|
|
|
|
{
|
|
|
|
return audio_driver_init_internal(audio_callback.callback != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_get_devices_list(void **data)
|
|
|
|
{
|
|
|
|
struct string_list**ptr = (struct string_list**)data;
|
|
|
|
if (!ptr)
|
|
|
|
return false;
|
|
|
|
*ptr = audio_driver_devices_list;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-08 14:09:08 +00:00
|
|
|
bool audio_driver_init_resampler(void)
|
|
|
|
{
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
return rarch_resampler_realloc(
|
|
|
|
&audio_driver_resampler_data,
|
|
|
|
&audio_driver_resampler,
|
|
|
|
settings->audio.resampler,
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.source_ratio.original);
|
2016-05-08 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 21:28:45 +00:00
|
|
|
void audio_driver_process_resampler(void *data)
|
2016-05-08 14:09:08 +00:00
|
|
|
{
|
2016-09-06 21:28:45 +00:00
|
|
|
struct resampler_data *resampler = (struct resampler_data*)data;
|
2016-05-16 07:58:45 +00:00
|
|
|
performance_counter_init(&resampler_proc, "resampler_proc");
|
2016-05-16 07:53:54 +00:00
|
|
|
performance_counter_start(&resampler_proc);
|
2016-09-13 09:41:45 +00:00
|
|
|
|
|
|
|
audio_driver_resampler->process(audio_driver_resampler_data, resampler);
|
2016-05-16 07:48:14 +00:00
|
|
|
performance_counter_stop(&resampler_proc);
|
2016-05-08 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 14:45:58 +00:00
|
|
|
bool audio_driver_deinit(void)
|
|
|
|
{
|
|
|
|
audio_driver_free_devices_list();
|
|
|
|
if (!uninit_audio())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_set_callback(const void *data)
|
|
|
|
{
|
|
|
|
const struct retro_audio_callback *cb = (const struct retro_audio_callback*)data;
|
2016-09-29 19:07:10 +00:00
|
|
|
#ifdef HAVE_NETWORKING
|
2016-10-03 02:13:34 +00:00
|
|
|
if (netplay_driver_ctl(RARCH_NETPLAY_CTL_IS_ENABLED, NULL))
|
2016-09-29 05:18:02 +00:00
|
|
|
return false;
|
2016-05-08 14:45:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (recording_driver_get_data_ptr()) /* A/V sync is a must. */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (cb)
|
|
|
|
audio_callback = *cb;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-08 14:50:23 +00:00
|
|
|
bool audio_driver_enable_callback(void)
|
|
|
|
{
|
2016-05-08 15:03:51 +00:00
|
|
|
if (!audio_driver_has_callback())
|
2016-05-08 14:50:23 +00:00
|
|
|
return false;
|
|
|
|
if (audio_callback.set_state)
|
|
|
|
audio_callback.set_state(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_disable_callback(void)
|
|
|
|
{
|
2016-05-08 15:03:51 +00:00
|
|
|
if (!audio_driver_has_callback())
|
2016-05-08 14:50:23 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (audio_callback.set_state)
|
|
|
|
audio_callback.set_state(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-08 14:54:15 +00:00
|
|
|
/* Sets audio monitor rate to new value. */
|
|
|
|
void audio_driver_monitor_set_rate(void)
|
|
|
|
{
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
double new_src_ratio = (double)settings->audio.out_rate /
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.input;
|
2016-05-08 14:54:15 +00:00
|
|
|
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_data.source_ratio.original = new_src_ratio;
|
|
|
|
audio_driver_data.source_ratio.current = new_src_ratio;
|
2016-05-08 14:54:15 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 15:03:51 +00:00
|
|
|
bool audio_driver_callback(void)
|
|
|
|
{
|
|
|
|
if (!audio_driver_has_callback())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (audio_callback.callback)
|
|
|
|
audio_callback.callback();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_has_callback(void)
|
|
|
|
{
|
|
|
|
return audio_callback.callback;
|
|
|
|
}
|
|
|
|
|
2016-05-08 15:42:20 +00:00
|
|
|
bool audio_driver_toggle_mute(void)
|
|
|
|
{
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
if (!audio_driver_context_audio_data)
|
|
|
|
return false;
|
2016-05-08 16:24:25 +00:00
|
|
|
if (!audio_driver_is_active())
|
2016-05-08 15:42:20 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
settings->audio.mute_enable = !settings->audio.mute_enable;
|
|
|
|
|
|
|
|
if (settings->audio.mute_enable)
|
2016-05-09 18:51:53 +00:00
|
|
|
command_event(CMD_EVENT_AUDIO_STOP, NULL);
|
|
|
|
else if (!command_event(CMD_EVENT_AUDIO_START, NULL))
|
2016-05-08 15:42:20 +00:00
|
|
|
{
|
2016-05-08 16:24:25 +00:00
|
|
|
audio_driver_unset_active();
|
2016-05-08 15:42:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-08 16:00:32 +00:00
|
|
|
bool audio_driver_start(void)
|
|
|
|
{
|
|
|
|
if (!current_audio || !current_audio->start
|
|
|
|
|| !audio_driver_context_audio_data)
|
|
|
|
return false;
|
|
|
|
return current_audio->start(audio_driver_context_audio_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_stop(void)
|
|
|
|
{
|
|
|
|
if (!current_audio || !current_audio->stop
|
|
|
|
|| !audio_driver_context_audio_data)
|
|
|
|
return false;
|
|
|
|
return current_audio->stop(audio_driver_context_audio_data);
|
|
|
|
}
|
|
|
|
|
2016-05-08 16:03:09 +00:00
|
|
|
void audio_driver_unset_callback(void)
|
|
|
|
{
|
|
|
|
audio_callback.callback = NULL;
|
|
|
|
audio_callback.set_state = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_alive(void)
|
|
|
|
{
|
|
|
|
if (!current_audio || !current_audio->alive
|
|
|
|
|| !audio_driver_context_audio_data)
|
|
|
|
return false;
|
|
|
|
return current_audio->alive(audio_driver_context_audio_data);
|
|
|
|
}
|
|
|
|
|
2016-05-08 16:24:25 +00:00
|
|
|
void audio_driver_frame_is_reverse(void)
|
2015-11-22 13:17:46 +00:00
|
|
|
{
|
2016-05-08 16:24:25 +00:00
|
|
|
/* We just rewound. Flush rewind audio buffer. */
|
|
|
|
audio_driver_flush(
|
2016-10-01 03:36:36 +00:00
|
|
|
audio_driver_rewind_buf + audio_driver_rewind_ptr,
|
|
|
|
audio_driver_rewind_size - audio_driver_rewind_ptr);
|
2016-05-08 16:24:25 +00:00
|
|
|
}
|
2015-11-22 13:17:46 +00:00
|
|
|
|
2016-05-08 16:24:25 +00:00
|
|
|
void audio_driver_destroy_data(void)
|
|
|
|
{
|
|
|
|
audio_driver_context_audio_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_set_own_driver(void)
|
|
|
|
{
|
|
|
|
audio_driver_data_own = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_unset_own_driver(void)
|
|
|
|
{
|
|
|
|
audio_driver_data_own = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_owns_driver(void)
|
|
|
|
{
|
|
|
|
return audio_driver_data_own;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_set_active(void)
|
|
|
|
{
|
|
|
|
audio_driver_active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_unset_active(void)
|
|
|
|
{
|
|
|
|
audio_driver_active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool audio_driver_is_active(void)
|
|
|
|
{
|
|
|
|
return audio_driver_active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_driver_destroy(void)
|
|
|
|
{
|
|
|
|
audio_driver_active = false;
|
|
|
|
audio_driver_data_own = false;
|
|
|
|
current_audio = NULL;
|
2015-11-22 13:17:46 +00:00
|
|
|
}
|