project64/Source/Project64-audio/Driver/OpenSLES.cpp

670 lines
22 KiB
C++
Raw Normal View History

2021-03-02 02:13:17 +00:00
// Project64 - A Nintendo 64 emulator
// https://www.pj64-emu.com/
2021-03-02 02:13:17 +00:00
// Copyright(C) 2001-2021 Project64.
// Copyright(C) 2015 Gilles Siberlin
// Copyright(C) 2007 - 2009 Richard Goedeken
// Copyright(C) 2007 - 2008 Ebenblues
// Copyright(C) 2003 JttL
// Copyright(C) 2002 Hacktarux
// GNU/GPLv2 licensed: https://gnu.org/licenses/gpl-2.0.html
2021-04-14 06:22:34 +00:00
#include "OpenSLES.h"
#include <Project64-audio/trace.h>
#include <Project64-audio/SettingsID.h>
#include <Project64-audio/AudioMain.h>
#ifdef ANDROID
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#endif
#ifdef ANDROID
typedef struct threadLock_
{
pthread_mutex_t mutex;
pthread_cond_t cond;
volatile unsigned char value;
volatile unsigned char limit;
} threadLock;
#endif
2021-04-14 06:22:34 +00:00
// Default start-time size of primary buffer (in equivalent output samples)
// This is the buffer where audio is loaded after it's extracted from N64's memory
2017-09-14 08:07:12 +00:00
enum { PRIMARY_BUFFER_SIZE = 16384 };
2021-04-14 06:22:34 +00:00
// Size of a single secondary buffer, in output samples. This is the requested size of OpenSLES's
// hardware buffer, this should be a power of two.
2017-09-14 08:07:12 +00:00
enum { SECONDARY_BUFFER_SIZE = 1024 };
2021-04-14 06:22:34 +00:00
// This is the requested number of OpenSLES's hardware buffers
2017-09-14 08:07:12 +00:00
enum { SECONDARY_BUFFER_NBR = 2 };
2021-04-14 06:22:34 +00:00
// This sets default frequency what is used if ROM doesn't want to change it.
// Probably only game that needs this is Zelda: Ocarina Of Time Master Quest
// TODO: We should try to find out why Demos' frequencies are always wrong
// They tend to rely on a default frequency, but apparently never the same one
2021-04-14 06:22:34 +00:00
2017-09-14 08:07:12 +00:00
enum { DEFAULT_FREQUENCY = 33600 };
2021-04-14 06:22:34 +00:00
// Number of bytes per sample
enum
{
N64_SAMPLE_BYTES = 4,
SLES_SAMPLE_BYTES = 4,
};
2021-04-14 06:22:34 +00:00
// Pointer to the primary audio buffer
2021-04-12 11:35:39 +00:00
uint8_t * g_primaryBuffer = nullptr;
2021-04-14 06:22:34 +00:00
// Size of the primary buffer
uint32_t g_primaryBufferBytes = 0;
2021-04-14 06:22:34 +00:00
// Pointer to secondary buffers
2021-04-12 11:35:39 +00:00
uint8_t ** g_secondaryBuffers = nullptr;
2021-04-14 06:22:34 +00:00
// Size of a single secondary buffer
uint32_t g_secondaryBufferBytes = 0;
2021-04-14 06:22:34 +00:00
// Position in the primary buffer where next audio chunk should be placed
uint32_t g_primaryBufferPos = 0;
2021-04-14 06:22:34 +00:00
// Index of the next secondary buffer available
uint32_t g_secondaryBufferIndex = 0;
2021-04-14 06:22:34 +00:00
// Audio frequency, this is usually obtained from the game, but for compatibility we set default value
uint32_t g_GameFreq = DEFAULT_FREQUENCY;
2021-04-14 06:22:34 +00:00
// SpeedFactor is used to increase/decrease game playback speed
uint32_t g_speed_factor = 100;
2021-04-14 06:22:34 +00:00
// Output audio frequency
int g_OutputFreq = 44100;
2021-04-14 06:22:34 +00:00
// Indicate that the audio plugin failed to initialize, so the emulator can keep running without sound
bool g_critical_failure = false;
#ifdef ANDROID
// Thread lock
threadLock g_lock;
2021-04-14 06:22:34 +00:00
// Engine interfaces
2021-04-12 11:35:39 +00:00
SLObjectItf g_engineObject = nullptr;
SLEngineItf g_engineEngine = nullptr;
2021-04-14 06:22:34 +00:00
// Output mix interfaces
2021-04-12 11:35:39 +00:00
SLObjectItf g_outputMixObject = nullptr;
2021-04-14 06:22:34 +00:00
// Player interfaces
2021-04-12 11:35:39 +00:00
SLObjectItf g_playerObject = nullptr;
SLPlayItf g_playerPlay = nullptr;
2021-04-14 06:22:34 +00:00
// Buffer queue interfaces
2021-04-12 11:35:39 +00:00
SLAndroidSimpleBufferQueueItf g_bufferQueue = nullptr;
#endif
static bool CreatePrimaryBuffer(void)
{
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Start");
2017-09-14 08:07:12 +00:00
unsigned int primaryBytes = (unsigned int)(PRIMARY_BUFFER_SIZE * N64_SAMPLE_BYTES);
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Allocating memory for primary audio buffer: %i bytes.", primaryBytes);
g_primaryBuffer = new uint8_t[primaryBytes];
2021-04-12 11:35:39 +00:00
if (g_primaryBuffer == nullptr)
{
2021-04-12 11:35:39 +00:00
WriteTrace(TraceAudioInitShutdown, TraceError, "g_primaryBuffer == nullptr");
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done (res: false)");
return false;
}
memset(g_primaryBuffer, 0, primaryBytes);
g_primaryBufferBytes = primaryBytes;
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done (res: True)");
return true;
}
static void CloseAudio(void)
{
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Start");
g_primaryBufferPos = 0;
g_secondaryBufferIndex = 0;
2021-04-14 06:22:34 +00:00
// Delete primary buffer
2021-04-12 11:35:39 +00:00
if (g_primaryBuffer != nullptr)
{
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Delete g_primaryBuffer (%p)", g_primaryBuffer);
g_primaryBufferBytes = 0;
delete[] g_primaryBuffer;
2021-04-12 11:35:39 +00:00
g_primaryBuffer = nullptr;
}
2021-04-14 06:22:34 +00:00
// Delete secondary buffers
2021-04-12 11:35:39 +00:00
if (g_secondaryBuffers != nullptr)
{
2017-09-14 08:07:12 +00:00
for (uint32_t i = 0; i < SECONDARY_BUFFER_NBR; i++)
{
2021-04-12 11:35:39 +00:00
if (g_secondaryBuffers[i] != nullptr)
{
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Delete g_secondaryBuffers[%d] (%p)", i, g_secondaryBuffers[i]);
delete[] g_secondaryBuffers[i];
2021-04-12 11:35:39 +00:00
g_secondaryBuffers[i] = nullptr;
}
}
g_secondaryBufferBytes = 0;
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Delete g_secondaryBuffers (%p)", g_secondaryBuffers);
delete[] g_secondaryBuffers;
2021-04-12 11:35:39 +00:00
g_secondaryBuffers = nullptr;
}
#ifdef ANDROID
2021-04-14 06:22:34 +00:00
// Destroy buffer queue audio player object, and invalidate all associated interfaces
2021-04-12 11:35:39 +00:00
if (g_playerObject != nullptr)
{
SLuint32 state = SL_PLAYSTATE_PLAYING;
(*g_playerPlay)->SetPlayState(g_playerPlay, SL_PLAYSTATE_STOPPED);
while (state != SL_PLAYSTATE_STOPPED)
{
(*g_playerPlay)->GetPlayState(g_playerPlay, &state);
}
(*g_playerObject)->Destroy(g_playerObject);
2021-04-12 11:35:39 +00:00
g_playerObject = nullptr;
g_playerPlay = nullptr;
g_bufferQueue = nullptr;
}
2021-04-14 06:22:34 +00:00
// Destroy output mix object, and invalidate all associated interfaces
2021-04-12 11:35:39 +00:00
if (g_outputMixObject != nullptr)
{
(*g_outputMixObject)->Destroy(g_outputMixObject);
2021-04-12 11:35:39 +00:00
g_outputMixObject = nullptr;
}
2021-04-14 06:22:34 +00:00
// Destroy engine object, and invalidate all associated interfaces
2021-04-12 11:35:39 +00:00
if (g_engineObject != nullptr)
{
(*g_engineObject)->Destroy(g_engineObject);
2021-04-12 11:35:39 +00:00
g_engineObject = nullptr;
g_engineEngine = nullptr;
}
// Destroy thread locks
pthread_cond_signal(&(g_lock.cond));
pthread_mutex_unlock(&(g_lock.mutex));
pthread_cond_destroy(&(g_lock.cond));
pthread_mutex_destroy(&(g_lock.mutex));
#endif
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done");
}
static bool CreateSecondaryBuffers(void)
{
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Start");
bool status = true;
2017-09-14 08:07:12 +00:00
unsigned int secondaryBytes = (unsigned int)(SECONDARY_BUFFER_SIZE * SLES_SAMPLE_BYTES);
2017-09-14 08:07:12 +00:00
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Allocating memory for %d secondary audio buffers: %i bytes.", SECONDARY_BUFFER_NBR, secondaryBytes);
2021-04-14 06:22:34 +00:00
// Allocate number of secondary buffers
2017-09-14 08:07:12 +00:00
g_secondaryBuffers = new uint8_t *[SECONDARY_BUFFER_NBR];
2021-04-12 11:35:39 +00:00
if (g_secondaryBuffers == nullptr)
{
2021-04-12 11:35:39 +00:00
WriteTrace(TraceAudioInitShutdown, TraceError, "g_secondaryBuffers == nullptr");
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done (res: false)");
return false;
}
2021-04-14 06:22:34 +00:00
// Allocate size of each secondary buffers
2017-09-14 08:07:12 +00:00
for (uint32_t i = 0; i < SECONDARY_BUFFER_NBR; i++)
{
g_secondaryBuffers[i] = new uint8_t[secondaryBytes];
2021-04-12 11:35:39 +00:00
if (g_secondaryBuffers[i] == nullptr)
{
status = false;
break;
}
memset(g_secondaryBuffers[i], 0, secondaryBytes);
}
g_secondaryBufferBytes = secondaryBytes;
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done (res: %s)", status ? "True" : "False");
return status;
}
static int resample(unsigned char *input, int /*input_avail*/, int oldsamplerate, unsigned char *output, int output_needed, int newsamplerate)
{
int *psrc = (int*)input;
int *pdest = (int*)output;
int i = 0, j = 0;
#ifdef USE_SPEEX
spx_uint32_t in_len, out_len;
if (Resample == RESAMPLER_SPEEX)
{
2021-04-12 11:35:39 +00:00
if (spx_state == nullptr)
{
spx_state = speex_resampler_init(2, oldsamplerate, newsamplerate, ResampleQuality, &error);
2021-04-12 11:35:39 +00:00
if (spx_state == nullptr)
{
memset(output, 0, output_needed);
return 0;
}
}
speex_resampler_set_rate(spx_state, oldsamplerate, newsamplerate);
in_len = input_avail / 4;
out_len = output_needed / 4;
if ((error = speex_resampler_process_interleaved_int(spx_state, (const spx_int16_t *)input, &in_len, (spx_int16_t *)output, &out_len)))
{
memset(output, 0, output_needed);
2021-04-14 06:22:34 +00:00
return input_avail; // Number of bytes consumed
}
return in_len * 4;
}
#endif
#ifdef USE_SRC
if (Resample == RESAMPLER_SRC)
{
2021-04-14 06:22:34 +00:00
// The high quality resampler needs more input than the sample rate ratio would indicate to work properly
if (input_avail > output_needed * 3 / 2)
2021-04-14 06:22:34 +00:00
input_avail = output_needed * 3 / 2; // Just to avoid too much short-float-short conversion time
if (_src_len < input_avail * 2 && input_avail > 0)
{
if (_src) free(_src);
_src_len = input_avail * 2;
_src = malloc(_src_len);
}
if (_dest_len < output_needed * 2 && output_needed > 0)
{
if (_dest) free(_dest);
_dest_len = output_needed * 2;
_dest = malloc(_dest_len);
}
memset(_src, 0, _src_len);
memset(_dest, 0, _dest_len);
2021-04-12 11:35:39 +00:00
if (src_state == nullptr)
{
src_state = src_new(ResampleQuality, 2, &error);
2021-04-12 11:35:39 +00:00
if (src_state == nullptr)
{
memset(output, 0, output_needed);
return 0;
}
}
src_short_to_float_array((short *)input, _src, input_avail / 2);
src_data.end_of_input = 0;
src_data.data_in = _src;
src_data.input_frames = input_avail / 4;
src_data.src_ratio = (float)newsamplerate / oldsamplerate;
src_data.data_out = _dest;
src_data.output_frames = output_needed / 4;
if ((error = src_process(src_state, &src_data)))
{
memset(output, 0, output_needed);
2021-04-14 06:22:34 +00:00
return input_avail; // Number of bytes consumed
}
src_float_to_short_array(_dest, (short *)output, output_needed / 2);
return src_data.input_frames_used * 4;
}
#endif
// RESAMPLE == TRIVIAL
if (newsamplerate >= oldsamplerate)
{
int sldf = oldsamplerate;
int const2 = 2 * sldf;
int dldf = newsamplerate;
int const1 = const2 - 2 * dldf;
int criteria = const2 - dldf;
for (i = 0; i < output_needed / 4; i++)
{
pdest[i] = psrc[j];
if (criteria >= 0)
{
++j;
criteria += const1;
}
else criteria += const2;
}
2021-04-14 06:22:34 +00:00
return j * 4; // Number of bytes consumed
}
// newsamplerate < oldsamplerate, this only happens when speed_factor > 1
for (i = 0; i < output_needed / 4; i++)
{
j = i * oldsamplerate / newsamplerate;
pdest[i] = psrc[j];
}
2021-04-14 06:22:34 +00:00
return j * 4; // Number of bytes consumed
}
2021-04-14 06:22:34 +00:00
// This callback handler is called every time a buffer finishes playing
#ifdef ANDROID
void queueCallback(SLAndroidSimpleBufferQueueItf caller, void *context)
{
threadLock *plock = (threadLock *)context;
pthread_mutex_lock(&(plock->mutex));
if (plock->value < plock->limit)
plock->value++;
pthread_cond_signal(&(plock->cond));
pthread_mutex_unlock(&(plock->mutex));
}
#endif
void OpenSLESDriver::AI_SetFrequency(uint32_t freq, uint32_t BufferSize)
{
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Start (freq: %d)", freq);
if (freq < 4000)
{
WriteTrace(TraceAudioInitShutdown, TraceInfo, "Sometimes a bad frequency is requested so ignore it (freq: %d)", freq);
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done");
return;
}
2021-04-12 11:35:39 +00:00
if (g_GameFreq == freq && g_primaryBuffer != nullptr)
{
2021-04-14 06:22:34 +00:00
WriteTrace(TraceAudioInitShutdown, TraceInfo, "We are already using this frequency, so ignore it (freq: %d)", freq);
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done");
return;
}
if (g_critical_failure)
{
WriteTrace(TraceAudioInitShutdown, TraceInfo, "Critical failure in setting up plugin, ignoring initialization...");
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done");
return;
}
2021-04-14 06:22:34 +00:00
// This is important for the sync
g_GameFreq = freq;
#ifdef ANDROID
SLuint32 sample_rate;
if ((freq / 1000) <= 11)
{
g_OutputFreq = 11025;
sample_rate = SL_SAMPLINGRATE_11_025;
2017-09-14 08:07:12 +00:00
}
else if ((freq / 1000) <= 22)
{
g_OutputFreq = 22050;
sample_rate = SL_SAMPLINGRATE_22_05;
}
else if ((freq / 1000) <= 32)
{
g_OutputFreq = 32000;
sample_rate = SL_SAMPLINGRATE_32;
}
else
{
g_OutputFreq = 44100;
sample_rate = SL_SAMPLINGRATE_44_1;
}
#endif
WriteTrace(TraceAudioInitShutdown, TraceInfo, "Requesting frequency: %iHz.", g_OutputFreq);
2021-04-14 06:22:34 +00:00
// Close everything because InitializeAudio can be called more than once
CloseAudio();
2021-04-14 06:22:34 +00:00
// Create primary buffer
if (!CreatePrimaryBuffer())
{
WriteTrace(TraceAudioInitShutdown, TraceError, "CreatePrimaryBuffer failed");
CloseAudio();
g_critical_failure = true;
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done");
return;
}
2021-04-14 06:22:34 +00:00
// Create secondary buffers
if (!CreateSecondaryBuffers())
{
WriteTrace(TraceAudioInitShutdown, TraceError, "CreateSecondaryBuffers failed");
CloseAudio();
g_critical_failure = true;
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done");
return;
}
#ifdef ANDROID
// Create thread locks to ensure synchronization between callback and processing code
2021-04-12 11:35:39 +00:00
if (pthread_mutex_init(&(g_lock.mutex), (pthread_mutexattr_t*)nullptr) != 0)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "pthread_mutex_init failed");
CloseAudio();
g_critical_failure = true;
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done");
return;
}
2021-04-12 11:35:39 +00:00
if (pthread_cond_init(&(g_lock.cond), (pthread_condattr_t*)nullptr) != 0)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "pthread_cond_init failed");
CloseAudio();
g_critical_failure = true;
WriteTrace(TraceAudioInitShutdown, TraceDebug, "Done");
return;
}
pthread_mutex_lock(&(g_lock.mutex));
2017-09-14 08:07:12 +00:00
g_lock.value = g_lock.limit = SECONDARY_BUFFER_NBR;
pthread_mutex_unlock(&(g_lock.mutex));
2021-04-14 06:22:34 +00:00
// Engine object
2021-04-12 11:35:39 +00:00
SLresult result = slCreateEngine(&g_engineObject, 0, nullptr, 0, nullptr, nullptr);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "slCreateEngine failed (result: %d)", result);
}
if (result == SL_RESULT_SUCCESS)
{
result = (*g_engineObject)->Realize(g_engineObject, SL_BOOLEAN_FALSE);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "slCreateEngine->Realize failed (result: %d)", result);
}
}
if (result == SL_RESULT_SUCCESS)
{
result = (*g_engineObject)->GetInterface(g_engineObject, SL_IID_ENGINE, &g_engineEngine);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "slCreateEngine->GetInterface failed (result: %d)", result);
}
}
if (result == SL_RESULT_SUCCESS)
{
2021-04-14 06:22:34 +00:00
// Output mix object
2021-04-12 11:35:39 +00:00
result = (*g_engineEngine)->CreateOutputMix(g_engineEngine, &g_outputMixObject, 0, nullptr, nullptr);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "slCreateEngine->CreateOutputMix failed (result: %d)", result);
}
}
if (result == SL_RESULT_SUCCESS)
{
result = (*g_outputMixObject)->Realize(g_outputMixObject, SL_BOOLEAN_FALSE);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "g_outputMixObject->Realize failed (result: %d)", result);
}
}
if (result == SL_RESULT_SUCCESS)
{
2017-09-14 08:07:12 +00:00
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, SECONDARY_BUFFER_NBR };
SLDataFormat_PCM format_pcm = { SL_DATAFORMAT_PCM,2, sample_rate, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
(SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT), SL_BYTEORDER_LITTLEENDIAN };
SLDataSource audioSrc = { &loc_bufq, &format_pcm };
2021-04-14 06:22:34 +00:00
// Configure audio sink
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, g_outputMixObject };
2021-04-12 11:35:39 +00:00
SLDataSink audioSnk = { &loc_outmix, nullptr };
2021-04-14 06:22:34 +00:00
// Create audio player
const SLInterfaceID ids1[] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
const SLboolean req1[] = { SL_BOOLEAN_TRUE };
result = (*g_engineEngine)->CreateAudioPlayer(g_engineEngine, &(g_playerObject), &audioSrc, &audioSnk, 1, ids1, req1);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "g_engineEngine->CreateAudioPlayer failed (result: %d)", result);
}
}
2021-04-14 06:22:34 +00:00
// Realize the player
if (result == SL_RESULT_SUCCESS)
{
result = (*g_playerObject)->Realize(g_playerObject, SL_BOOLEAN_FALSE);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "g_playerObject->Realize failed (result: %d)", result);
}
}
2021-04-14 06:22:34 +00:00
// Get the play interface
if (result == SL_RESULT_SUCCESS)
{
result = (*g_playerObject)->GetInterface(g_playerObject, SL_IID_PLAY, &(g_playerPlay));
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "g_playerObject->GetInterface(SL_IID_PLAY) failed (result: %d)", result);
}
}
2021-04-14 06:22:34 +00:00
// Get the buffer queue interface
if (result == SL_RESULT_SUCCESS)
{
result = (*g_playerObject)->GetInterface(g_playerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &(g_bufferQueue));
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "g_playerObject->GetInterface(SL_IID_ANDROIDSIMPLEBUFFERQUEUE) failed (result: %d)", result);
}
}
2021-04-14 06:22:34 +00:00
// Register callback on the buffer queue
if (result == SL_RESULT_SUCCESS)
{
result = (*g_bufferQueue)->RegisterCallback(g_bufferQueue, queueCallback, &g_lock);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "bufferQueue->RegisterCallback() failed (result: %d)", result);
}
}
2021-04-14 06:22:34 +00:00
// Set the player's state to playing
if (result == SL_RESULT_SUCCESS)
{
result = (*g_playerPlay)->SetPlayState(g_playerPlay, SL_PLAYSTATE_PLAYING);
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceError, "g_playerPlay->SetPlayState(SL_PLAYSTATE_PLAYING) failed (result: %d)", result);
}
}
if (result != SL_RESULT_SUCCESS)
{
WriteTrace(TraceAudioInitShutdown, TraceNotice, "Couldn't open OpenSLES audio");
CloseAudio();
g_critical_failure = true;
}
#endif
WriteTrace(TraceAudioInitShutdown, TraceNotice, "Done");
2017-09-14 08:07:12 +00:00
}
void OpenSLESDriver::AI_Startup(void)
{
AI_SetFrequency(DEFAULT_FREQUENCY, 0);
2017-09-14 08:07:12 +00:00
}
void OpenSLESDriver::AI_Shutdown(void)
{
CloseAudio();
}
void OpenSLESDriver::AI_LenChanged(uint8_t *start, uint32_t length)
{
WriteTrace(TraceAudioInterface, TraceDebug, "Start");
WriteTrace(TraceAudioInterface, TraceDebug, "g_primaryBufferPos = 0x%X length = 0x%X g_primaryBufferBytes = %X", g_primaryBufferPos, length, g_primaryBufferBytes);
if (g_primaryBufferPos + length < g_primaryBufferBytes)
{
unsigned int i;
for (i = 0; i < length; i += 4)
{
2021-04-14 06:22:34 +00:00
// Left channel
2017-09-14 08:07:12 +00:00
g_primaryBuffer[g_primaryBufferPos + i] = start[i + 2];
g_primaryBuffer[g_primaryBufferPos + i + 1] = start[i + 3];
2021-04-14 06:22:34 +00:00
// Right channel
2017-09-14 08:07:12 +00:00
g_primaryBuffer[g_primaryBufferPos + i + 2] = start[i];
g_primaryBuffer[g_primaryBufferPos + i + 3] = start[i + 1];
}
g_primaryBufferPos += i;
}
else
{
WriteTrace(TraceAudioInterface, TraceDebug, "Audio primary buffer overflow. (g_primaryBufferPos: %d LenReg: %d g_primaryBufferBytes: %d)", g_primaryBufferPos, length, g_primaryBufferBytes);
}
uint32_t newsamplerate = g_OutputFreq * 100 / g_speed_factor;
uint32_t oldsamplerate = g_GameFreq != 0 ? g_GameFreq : DEFAULT_FREQUENCY;
while (g_primaryBufferPos >= ((g_secondaryBufferBytes * oldsamplerate) / newsamplerate))
{
WriteTrace(TraceAudioInterface, TraceDebug, "g_secondaryBufferBytes = %d", g_secondaryBufferBytes);
WriteTrace(TraceAudioInterface, TraceDebug, "oldsamplerate = %d", oldsamplerate);
WriteTrace(TraceAudioInterface, TraceDebug, "newsamplerate = %d", newsamplerate);
WriteTrace(TraceAudioInterface, TraceDebug, "((g_secondaryBufferBytes * oldsamplerate) / newsamplerate) = %d", ((g_secondaryBufferBytes * oldsamplerate) / newsamplerate));
WriteTrace(TraceAudioInterface, TraceDebug, "g_primaryBufferPos= %d", g_primaryBufferPos);
#ifdef ANDROID
pthread_mutex_lock(&(g_lock.mutex));
2021-04-14 06:22:34 +00:00
// Wait for the next callback if no more output buffers available
while (g_lock.value == 0)
{
pthread_cond_wait(&(g_lock.cond), &(g_lock.mutex));
}
g_lock.value--;
pthread_mutex_unlock(&(g_lock.mutex));
#endif
WriteTrace(TraceAudioInterface, TraceDebug, "Finished with lock");
2021-04-14 06:22:34 +00:00
// TODO: Don't resample if speed_factor = 100 and newsamplerate ~= oldsamplerate
int input_used = resample(g_primaryBuffer, g_primaryBufferPos, oldsamplerate, g_secondaryBuffers[g_secondaryBufferIndex], g_secondaryBufferBytes, newsamplerate);
#ifdef ANDROID
(*g_bufferQueue)->Enqueue(g_bufferQueue, g_secondaryBuffers[g_secondaryBufferIndex], g_secondaryBufferBytes);
#endif
memmove(g_primaryBuffer, &g_primaryBuffer[input_used], g_primaryBufferPos - input_used);
g_primaryBufferPos -= input_used;
g_secondaryBufferIndex++;
2017-09-14 08:07:12 +00:00
if (g_secondaryBufferIndex > (SECONDARY_BUFFER_NBR - 1))
{
g_secondaryBufferIndex = 0;
}
}
WriteTrace(TraceAudioInterface, TraceDebug, "Done");
2017-09-20 07:24:54 +00:00
}
void OpenSLESDriver::AI_Update(bool Wait)
{
m_AiUpdateEvent.IsTriggered(Wait ? SyncEvent::INFINITE_TIMEOUT : 0);
}