Permalink
Browse files

audio: code cleanup

  • Loading branch information...
fzurita committed Feb 25, 2016
1 parent 66573de commit ea7dfe04795098e26ed1b07fdce3fdc868d474d8
Showing with 2 additions and 227 deletions.
  1. +2 −227 jni/mupen64plus-audio-sles/main.cpp
@@ -53,8 +53,6 @@
typedef struct threadLock_
{
- pthread_mutex_t mutex;
- pthread_cond_t cond;
volatile int value;
volatile int limit;
} threadLock;
@@ -86,16 +84,6 @@ static void *l_DebugCallContext = NULL;
static int l_PluginInit = 0;
static m64p_handle l_ConfigAudio;
-enum resampler_type {
- RESAMPLER_TRIVIAL,
-#ifdef USE_SRC
- RESAMPLER_SRC,
-#endif
-#ifdef USE_SPEEX
- RESAMPLER_SPEEX,
-#endif
-};
-
/* Read header for type definition */
static AUDIO_INFO AudioInfo;
/* Pointer to the primary audio buffer */
@@ -122,10 +110,6 @@ static int GameFreq = DEFAULT_FREQUENCY;
static unsigned int speed_factor = 100;
/* If this is true then left and right channels are swapped */
static int SwapChannels = 0;
-/* Resample type */
-static enum resampler_type Resample = RESAMPLER_TRIVIAL;
-/* Resampler specific quality */
-static int ResampleQuality = 3;
/* Output Audio frequency */
static int OutputFreq;
/* Indicate that the audio plugin failed to initialize, so the emulator can keep running without sound */
@@ -142,27 +126,12 @@ static void* audioConsumer(void*);
static pthread_t audioConsumerThread;
static struct threadqueue audioConsumerQueue;
-static volatile int shutdown = 1;
-static volatile int matchGameToAudio = 1;
+static volatile bool shutdown = true;
+static volatile bool matchGameToAudio = true;
using namespace soundtouch;
static SoundTouch soundTouch;
-/* Samplerate*/
-#ifdef USE_SRC
-static float *_src = NULL;
-static unsigned int _src_len = 0;
-static float *_dest = NULL;
-static unsigned int _dest_len = 0;
-static int error;
-static SRC_STATE *src_state;
-static SRC_DATA src_data;
-#endif
-#ifdef USE_SPEEX
-SpeexResamplerState* spx_state = NULL;
-static int error;
-#endif
-
/* Thread Lock */
threadLock lock;
@@ -284,12 +253,6 @@ static void CloseAudio(void)
engineObject = NULL;
engineEngine = NULL;
}
-
- /* Destroy thread Locks */
- pthread_cond_signal(&(lock.cond));
- pthread_mutex_unlock(&(lock.mutex));
- pthread_cond_destroy(&(lock.cond));
- pthread_mutex_destroy(&(lock.mutex));
}
static int CreatePrimaryBuffer(void)
@@ -408,17 +371,6 @@ static void InitializeAudio(int freq)
return;
}
- /* Create thread Locks to ensure synchronization between callback and processing code */
- if (pthread_mutex_init(&(lock.mutex), (pthread_mutexattr_t*) NULL) != 0)
- {
- OnInitFailure();
- return;
- }
- if (pthread_cond_init(&(lock.cond), (pthread_condattr_t*) NULL) != 0)
- {
- OnInitFailure();
- return;
- }
lock.value = lock.limit = SecondaryBufferNbr;
/* Engine object */
@@ -527,190 +479,14 @@ static void InitializeAudio(int freq)
return;
}
-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)
- {
- if(spx_state == NULL)
- {
- spx_state = speex_resampler_init(2, oldsamplerate, newsamplerate, ResampleQuality, &error);
- if(spx_state == NULL)
- {
- 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);
- return input_avail; // number of bytes consumed
- }
- return in_len * 4;
- }
-#endif
-#ifdef USE_SRC
- if(Resample == RESAMPLER_SRC)
- {
- // the high quality resampler needs more input than the samplerate ratio would indicate to work properly
- if (input_avail > output_needed * 3 / 2)
- 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 = (float*)malloc(_src_len);
- }
- if (_dest_len < output_needed*2 && output_needed > 0)
- {
- if(_dest) free(_dest);
- _dest_len = output_needed*2;
- _dest = (float*)malloc(_dest_len);
- }
- memset(_src,0,_src_len);
- memset(_dest,0,_dest_len);
- if(src_state == NULL)
- {
- src_state = src_new (ResampleQuality, 2, &error);
- if(src_state == NULL)
- {
- 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);
- 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;
- }
- 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];
- }
- return j * 4; //number of bytes consumed
-}
-
static void ReadConfig(void)
{
const char *resampler_id;
/* read the configuration values into our static variables */
GameFreq = ConfigGetParamInt(l_ConfigAudio, "DEFAULT_FREQUENCY");
SwapChannels = ConfigGetParamBool(l_ConfigAudio, "SWAP_CHANNELS");
- //PrimaryBufferSize = ConfigGetParamInt(l_ConfigAudio, "PRIMARY_BUFFER_SIZE");
- //econdaryBufferSize = ConfigGetParamInt(l_ConfigAudio, "SECONDARY_BUFFER_SIZE");
- //SecondaryBufferNbr = ConfigGetParamInt(l_ConfigAudio, "SECONDARY_BUFFER_NBR");
resampler_id = ConfigGetParamString(l_ConfigAudio, "RESAMPLE");
-
- if (!resampler_id) {
- Resample = RESAMPLER_TRIVIAL;
- DebugMessage(M64MSG_WARNING, "Could not find RESAMPLE configuration; use trivial resampler");
- return;
- }
- if (strcmp(resampler_id, "trivial") == 0) {
- Resample = RESAMPLER_TRIVIAL;
- return;
- }
-#ifdef USE_SPEEX
- if (strncmp(resampler_id, "speex-fixed-", strlen("speex-fixed-")) == 0) {
- int i;
- static const char *speex_quality[] = {
- "speex-fixed-0",
- "speex-fixed-1",
- "speex-fixed-2",
- "speex-fixed-3",
- "speex-fixed-4",
- "speex-fixed-5",
- "speex-fixed-6",
- "speex-fixed-7",
- "speex-fixed-8",
- "speex-fixed-9",
- "speex-fixed-10",
- };
- Resample = RESAMPLER_SPEEX;
- for (i = 0; i < sizeof(speex_quality) / sizeof(*speex_quality); i++) {
- if (strcmp(speex_quality[i], resampler_id) == 0) {
- ResampleQuality = i;
- return;
- }
- }
- DebugMessage(M64MSG_WARNING, "Unknown RESAMPLE configuration %s; use speex-fixed-4 resampler", resampler_id);
- ResampleQuality = 4;
- return;
- }
-#endif
-#ifdef USE_SRC
- if (strncmp(resampler_id, "src-", strlen("src-")) == 0) {
- Resample = RESAMPLER_SRC;
- if (strcmp(resampler_id, "src-sinc-best-quality") == 0) {
- ResampleQuality = SRC_SINC_BEST_QUALITY;
- return;
- }
- if (strcmp(resampler_id, "src-sinc-medium-quality") == 0) {
- ResampleQuality = SRC_SINC_MEDIUM_QUALITY;
- return;
- }
- if (strcmp(resampler_id, "src-sinc-fastest") == 0) {
- ResampleQuality = SRC_SINC_FASTEST;
- return;
- }
- if (strcmp(resampler_id, "src-zero-order-hold") == 0) {
- ResampleQuality = SRC_ZERO_ORDER_HOLD;
- return;
- }
- if (strcmp(resampler_id, "src-linear") == 0) {
- ResampleQuality = SRC_LINEAR;
- return;
- }
- DebugMessage(M64MSG_WARNING, "Unknown RESAMPLE configuration %s; use src-sinc-medium-quality resampler", resampler_id);
- ResampleQuality = SRC_SINC_MEDIUM_QUALITY;
- return;
- }
-#endif
- DebugMessage(M64MSG_WARNING, "Unknown RESAMPLE configuration %s; use trivial resampler", resampler_id);
- Resample = RESAMPLER_TRIVIAL;
}
/* Mupen64Plus plugin functions */
@@ -1243,4 +1019,3 @@ EXPORT const char * CALL VolumeGetString(void)
return "100%";
}
-

0 comments on commit ea7dfe0

Please sign in to comment.