virtual int getSamples(int16_t *target, int noSamples) override { static int16_t temp[8192]; int sr; int samples_written = 0; while(samples_written < noSamples) { auto free_count = resampler_get_free_count(resampler[0]); if(free_count > 0) { const char *err = usf_render(usf_state->emu_state, temp, free_count, &sr); if(err) { LOGD("ERROR %s", err); return 0; } } if(sr != sample_rate) { resampler_set_rate(resampler[0], 44100.0 / (float)sample_rate); resampler_set_rate(resampler[1], 44100.0 / (float)sample_rate); sample_rate = sr; LOGD("######### NEW RATE %d", sample_rate); } uint32_t avg = 0; for(int i = 0; i<free_count; i++) { resampler_write_sample(resampler[0], temp[i*2]); resampler_write_sample(resampler[1], temp[i*2+1]); avg += (std::abs(temp[i*2]) + std::abs(temp[i*2+1])); } while(samples_written < noSamples && resampler_get_sample_count(resampler[0]) > 0) { auto s0 = resampler_get_sample(resampler[0]); resampler_remove_sample(resampler[0]); auto s1 = resampler_get_sample(resampler[1]); resampler_remove_sample(resampler[1]); target[samples_written++] = s0; target[samples_written++] = s1; } } return samples_written; }
const char * usf_render_resampled(void * state, int16_t * buffer, size_t count, int32_t sample_rate) { if ( !buffer ) { unsigned long samples_buffered = resampler_get_sample_count( USF_STATE->resampler ); resampler_clear(USF_STATE->resampler); if (samples_buffered) { unsigned long samples_to_remove = samples_buffered; if (samples_to_remove > count) samples_to_remove = count; while (samples_to_remove--) resampler_remove_sample(USF_STATE->resampler); if (!count) return 0; } count = (size_t)((uint64_t)count * USF_STATE->SampleRate / sample_rate); if (count > USF_STATE->samples_in_buffer_2) { count -= USF_STATE->samples_in_buffer_2; USF_STATE->samples_in_buffer_2 = 0; } else if (count) { USF_STATE->samples_in_buffer_2 -= count; memmove(USF_STATE->samplebuf2, USF_STATE->samplebuf2 + 8192 - USF_STATE->samples_in_buffer_2 * 2, USF_STATE->samples_in_buffer_2 * sizeof(short) * 2); return 0; } return usf_render(state, buffer, count, NULL); } while ( count ) { const char * err; while ( USF_STATE->samples_in_buffer_2 && resampler_get_free_count(USF_STATE->resampler) ) { int i = 0, j = resampler_get_free_count(USF_STATE->resampler); if (j > USF_STATE->samples_in_buffer_2) j = (int)USF_STATE->samples_in_buffer_2; for (i = 0; i < j; ++i) { resampler_write_sample(USF_STATE->resampler, USF_STATE->samplebuf2[i*2], USF_STATE->samplebuf2[i*2+1]); } memmove(USF_STATE->samplebuf2, USF_STATE->samplebuf2 + i * 2, (USF_STATE->samples_in_buffer_2 - i) * sizeof(short) * 2); USF_STATE->samples_in_buffer_2 -= i; } while ( count && resampler_get_sample_count(USF_STATE->resampler) ) { resampler_get_sample(USF_STATE->resampler, buffer, buffer + 1); resampler_remove_sample(USF_STATE->resampler); buffer += 2; --count; } if (!count) break; if (USF_STATE->samples_in_buffer_2) continue; err = usf_render(state, USF_STATE->samplebuf2, 4096, 0); if (err) return err; USF_STATE->samples_in_buffer_2 = 4096; resampler_set_rate(USF_STATE->resampler, (float)USF_STATE->SampleRate / (float)sample_rate); } return 0; }