Refactor audio driver code - add new_rate variable

This commit is contained in:
twinaphex 2017-01-09 13:09:14 +01:00
parent 8fa011d836
commit 83e8aec3d4
25 changed files with 72 additions and 62 deletions

View File

@ -304,6 +304,7 @@ static bool audio_driver_init_resampler(void)
static bool audio_driver_init_internal(bool audio_cb_inited)
{
unsigned new_rate = 0;
float *aud_inp_data = NULL;
float *samples_buf = NULL;
int16_t *conv_buf = NULL;
@ -358,7 +359,8 @@ static bool audio_driver_init_internal(bool audio_cb_inited)
&current_audio,
&audio_driver_context_audio_data,
*settings->audio.device ? settings->audio.device : NULL,
settings->audio.out_rate, settings->audio.latency,
settings->audio.out_rate, &new_rate,
settings->audio.latency,
current_audio))
{
RARCH_ERR("Cannot open threaded audio driver ... Exiting ...\n");
@ -371,9 +373,13 @@ static bool audio_driver_init_internal(bool audio_cb_inited)
audio_driver_context_audio_data =
current_audio->init(*settings->audio.device ?
settings->audio.device : NULL,
settings->audio.out_rate, settings->audio.latency);
settings->audio.out_rate, settings->audio.latency,
&new_rate);
}
if (new_rate != 0)
settings->audio.out_rate = new_rate;
if (!audio_driver_context_audio_data)
{
RARCH_ERR("Failed to initialize audio driver. Will continue without audio.\n");

View File

@ -39,7 +39,8 @@ typedef struct audio_driver
*
* Returns: audio driver handle on success, otherwise NULL.
**/
void *(*init)(const char *device, unsigned rate, unsigned latency);
void *(*init)(const char *device, unsigned rate,
unsigned latency, unsigned *new_rate);
/*
* @data : Pointer to audio data handle.

View File

@ -41,6 +41,7 @@ typedef struct audio_thread
/* Initialization options. */
const char *device;
unsigned *new_rate;
unsigned out_rate;
unsigned latency;
} audio_thread_t;
@ -53,7 +54,7 @@ static void audio_thread_loop(void *data)
return;
RARCH_LOG("[Audio Thread]: Initializing audio driver.\n");
thr->driver_data = thr->driver->init(thr->device, thr->out_rate, thr->latency);
thr->driver_data = thr->driver->init(thr->device, thr->out_rate, thr->latency, thr->new_rate);
slock_lock(thr->lock);
thr->inited = thr->driver_data ? 1 : -1;
if (thr->inited > 0 && thr->driver->use_float)
@ -276,7 +277,7 @@ static const audio_driver_t audio_thread = {
**/
bool audio_init_thread(const audio_driver_t **out_driver,
void **out_data, const char *device, unsigned audio_out_rate,
unsigned latency, const audio_driver_t *drv)
unsigned *new_rate, unsigned latency, const audio_driver_t *drv)
{
audio_thread_t *thr = (audio_thread_t*)calloc(1, sizeof(*thr));
if (!thr)
@ -285,6 +286,7 @@ bool audio_init_thread(const audio_driver_t **out_driver,
thr->driver = (const audio_driver_t*)drv;
thr->device = device;
thr->out_rate = audio_out_rate;
thr->new_rate = new_rate;
thr->latency = latency;
if (!(thr->cond = scond_new()))

View File

@ -27,6 +27,7 @@
* @out_data : output audio data
* @device : audio device (optional)
* @out_rate : output audio rate
* @new_rate : new output audio rate
* @latency : audio latency
* @driver : audio driver
*
@ -37,7 +38,7 @@
* Returns: true (1) if successful, otherwise false (0).
**/
bool audio_init_thread(const audio_driver_t **out_driver, void **out_data,
const char *device, unsigned out_rate, unsigned latency,
const char *device, unsigned out_rate, unsigned *new_rate, unsigned latency,
const audio_driver_t *driver);
#endif

View File

@ -53,7 +53,8 @@ static bool find_float_format(snd_pcm_t *pcm, void *data)
return false;
}
static void *alsa_init(const char *device, unsigned rate, unsigned latency)
static void *alsa_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
snd_pcm_format_t format;
snd_pcm_uframes_t buffer_size;

View File

@ -46,7 +46,7 @@ typedef struct alsa
typedef long snd_pcm_sframes_t;
static void *alsa_qsa_init(const char *device,
unsigned rate, unsigned latency)
unsigned rate, unsigned latency, unsigned *new_rate)
{
int err, card, dev, i;
snd_pcm_channel_info_t pi;

View File

@ -155,7 +155,7 @@ static void alsa_thread_free(void *data)
}
static void *alsa_thread_init(const char *device,
unsigned rate, unsigned latency)
unsigned rate, unsigned latency, unsigned *new_rate)
{
snd_pcm_uframes_t buffer_size;
snd_pcm_format_t format;

View File

@ -33,7 +33,6 @@
#include <string/stdstring.h>
#include "../audio_driver.h"
#include "../../configuration.h"
#include "../../verbosity.h"
typedef struct coreaudio
@ -181,7 +180,7 @@ done:
#endif
static void *coreaudio_init(const char *device,
unsigned rate, unsigned latency)
unsigned rate, unsigned latency, unsigned *new_rate)
{
size_t fifo_size;
UInt32 i_size;
@ -203,7 +202,6 @@ static void *coreaudio_init(const char *device,
#else
AudioComponentDescription desc = {0};
#endif
settings_t *settings = config_get_ptr();
coreaudio_t *dev = (coreaudio_t*)
calloc(1, sizeof(*dev));
if (!dev)
@ -290,7 +288,7 @@ static void *coreaudio_init(const char *device,
RARCH_LOG("[CoreAudio]: Using output sample rate of %.1f Hz\n",
(float)real_desc.mSampleRate);
settings->audio.out_rate = real_desc.mSampleRate;
*new_rate = real_desc.mSampleRate;
/* Set channel layout (fails on iOS). */
#ifndef TARGET_OS_IPHONE
@ -311,7 +309,7 @@ static void *coreaudio_init(const char *device,
if (AudioUnitInitialize(dev->dev) != noErr)
goto error;
fifo_size = (latency * settings->audio.out_rate) / 1000;
fifo_size = (latency * (*new_rate)) / 1000;
fifo_size *= 2 * sizeof(float);
dev->buffer_size = fifo_size;

View File

@ -103,10 +103,10 @@ Result csndPlaySound_custom(int chn, u32 flags, float vol, float pan,
return 0;
}
static void *ctr_csnd_audio_init(const char *device, unsigned rate, unsigned latency)
static void *ctr_csnd_audio_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
ctr_csnd_audio_t *ctr = (ctr_csnd_audio_t*)calloc(1, sizeof(ctr_csnd_audio_t));
settings_t *settings = config_get_ptr();
if (!ctr)
return NULL;
@ -115,7 +115,7 @@ static void *ctr_csnd_audio_init(const char *device, unsigned rate, unsigned lat
(void)rate;
(void)latency;
settings->audio.out_rate = CTR_CSND_AUDIO_RATE;
*new_rate = CTR_CSND_AUDIO_RATE;
ctr->l = linearAlloc(CTR_CSND_AUDIO_SIZE);
ctr->r = linearAlloc(CTR_CSND_AUDIO_SIZE);

View File

@ -18,7 +18,6 @@
#include <malloc.h>
#include "../audio_driver.h"
#include "../../configuration.h"
#include "../../performance_counters.h"
#include "../../runloop.h"
#include "../../ctr/ctr_debug.h"
@ -38,10 +37,10 @@ typedef struct
#define CTR_DSP_AUDIO_SIZE (CTR_DSP_AUDIO_COUNT * sizeof(int16_t) * 2)
#define CTR_DSP_AUDIO_SIZE_MASK (CTR_DSP_AUDIO_SIZE - 1u)
static void *ctr_dsp_audio_init(const char *device, unsigned rate, unsigned latency)
static void *ctr_dsp_audio_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
ctr_dsp_audio_t *ctr = NULL;
settings_t *settings = config_get_ptr();
(void)device;
(void)rate;
@ -55,7 +54,7 @@ static void *ctr_dsp_audio_init(const char *device, unsigned rate, unsigned late
if (!ctr)
return NULL;
settings->audio.out_rate = 32730;
*new_rate = 32730;
ctr->channel = 0;

View File

@ -303,7 +303,8 @@ static BOOL CALLBACK enumerate_cb(LPGUID guid, LPCSTR desc, LPCSTR module, LPVOI
return TRUE;
}
static void *dsound_init(const char *device, unsigned rate, unsigned latency)
static void *dsound_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
WAVEFORMATEX wfx = {0};
DSBUFFERDESC bufdesc = {0};

View File

@ -28,7 +28,6 @@
#include <retro_inline.h>
#include "../audio_driver.h"
#include "../../configuration.h"
#include "../../defines/gx_defines.h"
typedef struct
@ -69,9 +68,8 @@ static void dma_callback(void)
}
static void *gx_audio_init(const char *device,
unsigned rate, unsigned latency)
unsigned rate, unsigned latency, unsigned *new_rate)
{
settings_t *settings = config_get_ptr();
gx_audio_t *wa = (gx_audio_t*)memalign(32, sizeof(*wa));
if (!wa)
return NULL;
@ -86,12 +84,12 @@ static void *gx_audio_init(const char *device,
if (rate < 33000)
{
AISetDSPSampleRate(AI_SAMPLERATE_32KHZ);
settings->audio.out_rate = 32000;
*new_rate = 32000;
}
else
{
AISetDSPSampleRate(AI_SAMPLERATE_48KHZ);
settings->audio.out_rate = 48000;
*new_rate = 48000;
}
OSInitThreadQueue(&wa->cond);

View File

@ -122,13 +122,12 @@ static int parse_ports(char **dest_ports, const char **jports)
return 2;
}
static size_t find_buffersize(jack_t *jd, int latency)
static size_t find_buffersize(jack_t *jd, int latency, unsigned out_rate)
{
jack_latency_range_t range;
int i, buffer_frames, min_buffer_frames;
int jack_latency = 0;
settings_t *settings = config_get_ptr();
int frames = latency * settings->audio.out_rate / 1000;
int frames = latency * out_rate / 1000;
for (i = 0; i < 2; i++)
{
@ -150,14 +149,14 @@ static size_t find_buffersize(jack_t *jd, int latency)
return buffer_frames * sizeof(jack_default_audio_sample_t);
}
static void *ja_init(const char *device, unsigned rate, unsigned latency)
static void *ja_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
int i;
char *dest_ports[2];
const char **jports = NULL;
size_t bufsize = 0;
int parsed = 0;
settings_t *settings = config_get_ptr();
jack_t *jd = (jack_t*)calloc(1, sizeof(jack_t));
if (!jd)
@ -172,7 +171,7 @@ static void *ja_init(const char *device, unsigned rate, unsigned latency)
if (jd->client == NULL)
goto error;
settings->audio.out_rate = jack_get_sample_rate(jd->client);
*new_rate = jack_get_sample_rate(jd->client);
jack_set_process_callback(jd->client, process_cb, jd);
jack_on_shutdown(jd->client, shutdown_cb, jd);
@ -192,7 +191,7 @@ static void *ja_init(const char *device, unsigned rate, unsigned latency)
goto error;
}
bufsize = find_buffersize(jd, latency);
bufsize = find_buffersize(jd, latency, *new_rate);
jd->buffer_size = bufsize;
RARCH_LOG("JACK: Internal buffer size: %d frames.\n", (int)(bufsize / sizeof(jack_default_audio_sample_t)));

View File

@ -16,13 +16,15 @@
#include "../audio_driver.h"
#include "../../verbosity.h"
static void *null_audio_init(const char *device, unsigned rate, unsigned latency)
static void *null_audio_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
RARCH_ERR("Using the null audio driver. RetroArch will be silent.");
(void)device;
(void)rate;
(void)latency;
(void)new_rate;
return (void*)-1;
}

View File

@ -29,8 +29,9 @@
#include <windows.h>
#endif
#include <retro_miscellaneous.h>
#include "../audio_driver.h"
#include "../../configuration.h"
#include "../../verbosity.h"
#define BUFSIZE 1024
@ -79,7 +80,8 @@ static void al_free(void *data)
free(al);
}
static void *al_init(const char *device, unsigned rate, unsigned latency)
static void *al_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
al_t *al;

View File

@ -98,7 +98,8 @@ static void sl_free(void *data)
free(sl);
}
static void *sl_init(const char *device, unsigned rate, unsigned latency)
static void *sl_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
unsigned i;
SLDataFormat_PCM fmt_pcm = {0};

View File

@ -43,11 +43,11 @@
static bool oss_is_paused = false;
static void *oss_init(const char *device, unsigned rate, unsigned latency)
static void *oss_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_out_rate)
{
int frags, frag, channels, format, new_rate;
int *fd = (int*)calloc(1, sizeof(int));
settings_t *settings = config_get_ptr();
const char *oss_device = device ? device : DEFAULT_OSS_DEV;
if (!fd)
@ -83,7 +83,7 @@ static void *oss_init(const char *device, unsigned rate, unsigned latency)
if (new_rate != (int)rate)
{
RARCH_WARN("Requested sample rate not supported. Adjusting output rate to %d Hz.\n", new_rate);
settings->audio.out_rate = new_rate;
*new_out_rate = new_rate;
}
return fd;

View File

@ -78,7 +78,7 @@ static void event_loop(uint64_t data)
}
static void *ps3_audio_init(const char *device,
unsigned rate, unsigned latency)
unsigned rate, unsigned latency, unsigned *new_rate)
{
CellAudioPortParam params;
ps3_audio_t *data = calloc(1, sizeof(*data));

View File

@ -119,7 +119,7 @@ static int audioMainLoop(SceSize args, void* argp)
}
static void *psp_audio_init(const char *device,
unsigned rate, unsigned latency)
unsigned rate, unsigned latency, unsigned *new_rate)
{
psp_audio_t *psp = (psp_audio_t*)calloc(1, sizeof(psp_audio_t));

View File

@ -55,15 +55,14 @@ static void err_cb(void *userdata)
scond_signal(rsd->cond);
}
static void *rs_init(const char *device, unsigned rate, unsigned latency)
static void *rs_init(const char *device, unsigned rate, unsigned latency, unsigned *new_rate)
{
int channels, format;
rsd_t *rsd = (rsd_t*)calloc(1, sizeof(rsd_t));
rsound_t *rd = NULL;
rsd_t *rsd = (rsd_t*)calloc(1, sizeof(rsd_t));
if (!rsd)
return NULL;
rsound_t *rd;
if (rsd_init(&rd) < 0)
{
free(rsd);

View File

@ -37,16 +37,16 @@ static void rwebaudio_free(void *data)
RWebAudioFree();
}
static void *rwebaudio_init(const char *device, unsigned rate, unsigned latency)
static void *rwebaudio_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
settings_t *settings = config_get_ptr();
void *data = RWebAudioInit(latency);
(void)device;
(void)rate;
if (data)
settings->audio.out_rate = RWebAudioSampleRate();
*new_rate = RWebAudioSampleRate();
return data;
}

View File

@ -27,7 +27,6 @@
#include <retro_inline.h>
#include "../audio_driver.h"
#include "../../configuration.h"
#include "../../verbosity.h"
typedef struct sdl_audio
@ -67,14 +66,13 @@ static INLINE int find_num_frames(int rate, int latency)
}
static void *sdl_audio_init(const char *device,
unsigned rate, unsigned latency)
unsigned rate, unsigned latency, unsigned *new_rate)
{
int frames;
size_t bufsize;
SDL_AudioSpec out;
SDL_AudioSpec spec = {0};
void *tmp = NULL;
settings_t *settings = config_get_ptr();
sdl_audio_t *sdl = NULL;
(void)device;
@ -111,7 +109,7 @@ static void *sdl_audio_init(const char *device,
return 0;
}
settings->audio.out_rate = out.freq;
*new_rate = out.freq;
#ifdef HAVE_THREADS
sdl->lock = slock_new();
@ -119,7 +117,7 @@ static void *sdl_audio_init(const char *device,
#endif
RARCH_LOG("SDL audio: Requested %u ms latency, got %d ms\n",
latency, (int)(out.samples * 4 * 1000 / settings->audio.out_rate));
latency, (int)(out.samples * 4 * 1000 / (*new_rate)));
/* Create a buffer twice as big as needed and prefill the buffer. */
bufsize = out.samples * 4 * sizeof(int16_t);

View File

@ -83,7 +83,9 @@ void wiiu_ax_callback(void)
}
extern void AXRegisterFrameCallback(void *cb);
static void* ax_audio_init(const char* device, unsigned rate, unsigned latency)
static void* ax_audio_init(const char* device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
ax_audio_t* ax = (ax_audio_t*)calloc(1, sizeof(ax_audio_t));

View File

@ -234,11 +234,12 @@ static size_t xaudio2_write(xaudio2_t *handle, const void *buf, size_t bytes_)
return bytes_;
}
static void *xa_init(const char *device, unsigned rate, unsigned latency)
static void *xa_init(const char *device, unsigned rate, unsigned latency,
unsigned *new_rate)
{
size_t bufsize;
unsigned device_index = 0;
xa_t *xa = (xa_t*)calloc(1, sizeof(*xa));
xa_t *xa = (xa_t*)calloc(1, sizeof(*xa));
if (!xa)
return NULL;

View File

@ -22,7 +22,6 @@
#include <retro_inline.h>
#include "../audio_driver.h"
#include "../../configuration.h"
#define SOUND_FREQUENCY 48000
#define MAX_BUFFER 2048
@ -35,10 +34,9 @@ typedef struct
} xenon_audio_t;
static void *xenon360_audio_init(const char *device,
unsigned rate, unsigned latency)
unsigned rate, unsigned latency, unsigned *new_rate)
{
static bool inited = false;
settings_t *settings = config_get_ptr();
if (!inited)
{
@ -46,7 +44,8 @@ static void *xenon360_audio_init(const char *device,
inited = true;
}
settings->audio.out_rate = SOUND_FREQUENCY;
*new_rate = SOUND_FREQUENCY;
return calloc(1, sizeof(xenon_audio_t));
}