int main(int argc, char *argv[]) { int16_t input_i[1024]; int16_t output_i[1024 * 8]; float input_f[1024]; float output_f[1024 * 8]; if (argc != 3) { fprintf(stderr, "Usage: %s <in-rate> <out-rate> (max ratio: 8.0)\n", argv[0]); return 1; } double in_rate = strtod(argv[1], NULL); double out_rate = strtod(argv[2], NULL); double ratio = out_rate / in_rate; if (ratio >= 7.99) { fprintf(stderr, "Ratio is too high.\n"); return 1; } const rarch_resampler_t *resampler = NULL; void *re = NULL; if (!rarch_resampler_realloc(&re, &resampler, NULL)) { fprintf(stderr, "Failed to allocate resampler ...\n"); return 1; } for (;;) { if (fread(input_i, sizeof(int16_t), 1024, stdin) != 1024) break; audio_convert_s16_to_float(input_f, input_i, 1024, 1.0f); struct resampler_data data = { .data_in = input_f, .data_out = output_f, .input_frames = sizeof(input_f) / (2 * sizeof(float)), .ratio = ratio, }; rarch_resampler_process(resampler, re, &data); size_t output_samples = data.output_frames * 2; audio_convert_float_to_s16(output_i, output_f, output_samples); if (fwrite(output_i, sizeof(int16_t), output_samples, stdout) != output_samples) break; } rarch_resampler_freep(&resampler, &re); }
static void ffmpeg_audio_resample(ffmpeg_t *handle, struct ffemu_audio_data *data) { if (!handle->audio.use_float && !handle->audio.resampler) return; if (data->frames > handle->audio.float_conv_frames) { handle->audio.float_conv = (float*)av_realloc(handle->audio.float_conv, data->frames * handle->params.channels * sizeof(float)); if (!handle->audio.float_conv) return; handle->audio.float_conv_frames = data->frames; // To make sure we don't accidentially overflow. handle->audio.resample_out_frames = data->frames * handle->audio.ratio + 16; handle->audio.resample_out = (float*)av_realloc(handle->audio.resample_out, handle->audio.resample_out_frames * handle->params.channels * sizeof(float)); if (!handle->audio.resample_out) return; handle->audio.fixed_conv_frames = max(handle->audio.resample_out_frames, handle->audio.float_conv_frames); handle->audio.fixed_conv = (int16_t*)av_realloc(handle->audio.fixed_conv, handle->audio.fixed_conv_frames * handle->params.channels * sizeof(int16_t)); if (!handle->audio.fixed_conv) return; } if (handle->audio.use_float || handle->audio.resampler) { audio_convert_s16_to_float(handle->audio.float_conv, (const int16_t*)data->data, data->frames * handle->params.channels, 1.0); data->data = handle->audio.float_conv; } if (handle->audio.resampler) { // It's always two channels ... struct resampler_data info = {0}; info.data_in = (const float*)data->data; info.data_out = handle->audio.resample_out; info.input_frames = data->frames; info.ratio = handle->audio.ratio; rarch_resampler_process(handle->audio.resampler, handle->audio.resampler_data, &info); data->data = handle->audio.resample_out; data->frames = info.output_frames; if (!handle->audio.use_float) { audio_convert_float_to_s16(handle->audio.fixed_conv, handle->audio.resample_out, data->frames * handle->params.channels); data->data = handle->audio.fixed_conv; } } }
int main(int argc, char *argv[]) { srand(time(NULL)); int16_t input_i[1024]; int16_t output_i[1024 * 8]; float input_f[1024]; float output_f[1024 * 8]; double ratio_max_deviation = 0.0; if (argc < 3 || argc > 4) { fprintf(stderr, "Usage: %s <in-rate> <out-rate> [ratio deviation] (max ratio: 8.0)\n", argv[0]); return 1; } else if (argc == 4) { ratio_max_deviation = fabs(strtod(argv[3], NULL)); fprintf(stderr, "Ratio deviation: %.4f.\n", ratio_max_deviation); } double in_rate = strtod(argv[1], NULL); double out_rate = strtod(argv[2], NULL); double ratio = out_rate / in_rate; if (ratio >= 7.99) { fprintf(stderr, "Ratio is too high.\n"); return 1; } const rarch_resampler_t *resampler = NULL; void *re = NULL; if (!rarch_resampler_realloc(&re, &resampler, RESAMPLER_IDENT, out_rate / in_rate)) { fprintf(stderr, "Failed to allocate resampler ...\n"); return 1; } for (;;) { if (fread(input_i, sizeof(int16_t), 1024, stdin) != 1024) break; double uniform = (2.0 * rand()) / RAND_MAX - 1.0; double rate_mod = 1.0 + ratio_max_deviation * uniform; audio_convert_s16_to_float(input_f, input_i, 1024, 1.0f); struct resampler_data data = { .data_in = input_f, .data_out = output_f, .input_frames = sizeof(input_f) / (2 * sizeof(float)), .ratio = ratio * rate_mod, }; rarch_resampler_process(resampler, re, &data); size_t output_samples = data.output_frames * 2; audio_convert_float_to_s16(output_i, output_f, output_samples); if (fwrite(output_i, sizeof(int16_t), output_samples, stdout) != output_samples) break; } rarch_resampler_freep(&resampler, &re); }