Skip to content

Commit ff28e34

Browse files
committed
[sgu] Use resampler only when needed
1 parent f82a291 commit ff28e34

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/x65.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ static void* labels = NULL;
111111
// audio-streaming callback
112112
static void push_audio(const float* samples, int num_samples, void* user_data) {
113113
(void)user_data;
114-
spx_uint32_t in_len = num_samples / X65_AUDIO_CHANNELS;
115-
spx_uint32_t out_len = X65_MAX_AUDIO_SAMPLES;
114+
spx_uint32_t in_frames = num_samples / X65_AUDIO_CHANNELS;
115+
if (!state.resampler) {
116+
saudio_push(samples, in_frames);
117+
return;
118+
}
119+
spx_uint32_t out_frames = X65_MAX_AUDIO_SAMPLES / X65_AUDIO_CHANNELS;
116120
static float buf[X65_MAX_AUDIO_SAMPLES];
117-
speex_resampler_process_interleaved_float(state.resampler, samples, &in_len, buf, &out_len);
118-
saudio_push(buf, out_len);
121+
speex_resampler_process_interleaved_float(state.resampler, samples, &in_frames, buf, &out_frames);
122+
saudio_push(buf, out_frames);
119123
}
120124

121125
// get x65_desc_t struct based on joystick type
@@ -159,7 +163,9 @@ void app_init(void) {
159163
.num_channels = X65_AUDIO_CHANNELS,
160164
.logger.func = slog_func,
161165
});
162-
state.resampler = speex_resampler_init(SGU1_AUDIO_CHANNELS, SGU_CHIP_CLOCK, saudio_sample_rate(), 4, 0);
166+
state.resampler = SGU_CHIP_CLOCK == saudio_sample_rate()
167+
? NULL
168+
: speex_resampler_init(SGU1_AUDIO_CHANNELS, SGU_CHIP_CLOCK, saudio_sample_rate(), 4, 0);
163169
x65_joystick_type_t joy_type = arguments.joy ? X65_JOYSTICKTYPE_DIGITAL_1 : X65_JOYSTICKTYPE_NONE;
164170
if (sargs_exists("joystick")) {
165171
if (sargs_equals("joystick", "digital_1")) {
@@ -362,7 +368,7 @@ void app_cleanup(void) {
362368
ui_discard();
363369
#endif
364370
saudio_shutdown();
365-
speex_resampler_destroy(state.resampler);
371+
if (state.resampler) speex_resampler_destroy(state.resampler);
366372
gfx_shutdown();
367373
sargs_shutdown();
368374
hid_shutdown();

0 commit comments

Comments
 (0)